def test_play_hand_correct_handlen(self): student_deal_hand = ps3.deal_hand saved_stdout = sys.stdout #create place to save output out = StringIO() sys.stdout = out hand = {'d':2, 'a':1, 'o':1, 'u':1, 't':1, '@':1} error = "Expected %s, received %s as score for hand '%s' when user input is %s" #mock input for game try: inputs = [['dad','*END*']] results = [10] for i in range(len(results)): with mock.patch('builtins.input', side_effect=inputs[i]): result = ps3.play_hand(hand, word_list) self.assertEqual(result, results[i], error %(results[i], result, hand, inputs[i])) ps3.deal_hand = student_deal_hand except StopIteration as e: sys.stdout = saved_stdout ps3.deal_hand = student_deal_hand self.fail(STOP_ITER_MSG %(inputs[i], hand)) except NotImplementedError as e: sys.stdout = saved_stdout ps3.deal_hand = student_deal_hand self.fail(NOT_IMPLEMENTED) except: #restore saved output sys.stdout = saved_stdout ps3.deal_hand = student_deal_hand raise #restore saved output sys.stdout = saved_stdout
def test_play_hand_wildcard(self): #redirect output to silence print statements during running of tests saved_stdout = sys.stdout student_deal_hand = ps3.deal_hand #create place to save output out = StringIO() sys.stdout = out hand = {'c':1, 'o':1, 'w':1, 's':1, '@':1, 'z':1} error = "Expected %s, received %s as score for hand '%s' when user input is %s." #mock input for game try: inputs = [['cows', '*END*'], ['@ows', '*END*'], ['co@z', '*END*'], ['c@ws', '*END*']] results = [36, 24, 0,0] for i in range(len(results)): with mock.patch('builtins.input', side_effect=inputs[i]): result = ps3.play_hand(hand, word_list) self.assertEqual(result, results[i], error %(results[i], result, hand, inputs[i])) ps3.deal_hand = student_deal_hand except StopIteration as e: sys.stdout = saved_stdout ps3.deal_hand = student_deal_hand self.fail(STOP_ITER_MSG %(inputs[i], hand)) except NotImplementedError as e: sys.stdout = saved_stdout ps3.deal_hand = student_deal_hand self.fail(NOT_IMPLEMENTED) except: #restore saved output ps3.deal_hand = student_deal_hand sys.stdout = saved_stdout raise sys.stdout = saved_stdout
def test_play_hand_2(self): #create place to save output student_deal_hand = ps3.deal_hand saved_stdout = sys.stdout out = StringIO() sys.stdout = out hand = {'a':1, 'c':1, 'f':1, 'i':1, '@':1, 't':1, 'x':1 } error = "Expected %s, received %s as score for hand '%s' when user input is %s" user_input = ['fix', 'tc', 'a@'] e_result = 36 result = -1 try: with mock.patch('builtins.input', side_effect=user_input): result = ps3.play_hand(hand, word_list) self.assertEqual(result, e_result, error %(e_result, result, hand, user_input)) ps3.deal_hand = student_deal_hand except StopIteration as e: sys.stdout = saved_stdout ps3.deal_hand = student_deal_hand self.fail(STOP_ITER_MSG %(user_input, hand)) except NotImplementedError as e: sys.stdout = saved_stdout ps3.deal_hand = student_deal_hand self.fail(NOT_IMPLEMENTED) except: ps3.deal_hand = student_deal_hand sys.stdout = saved_stdout raise ps3.deal_hand = student_deal_hand sys.stdout = saved_stdout