class HandleAngryDiceTest(unittest.TestCase): """Test the functionality of the AngryDice class handle_angry_dice function.""" # Mocks being sys.stdout and will stor whet's print()'d into # mock_stdout @patch('sys.stdout', new_callable=StringIO) def test_handle_angry_dice(self, mock_stdout): """Set values of die_a and die_b to "ANGRY" and game_stage to 2. Call tested function. Check that the game reset to stage 1, and that the expected string was output.""" self.new_angry_dice = AngryDice() self.new_angry_dice.die_a.current_value = "ANGRY" self.new_angry_dice.die_b.current_value = "ANGRY" self.new_angry_dice.game_stage = 2 expected_output = "WOW, you're ANGRY!\nTime to go back to Stage 1!\n" self.new_angry_dice.handle_angry_dice() self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(1, self.new_angry_dice.game_stage) del self.new_angry_dice
def test_display_current_dice(self, mock_stdout): """Set values of die_a and die_b to some values. Call tested function, and check that the expected string was output.""" self.new_angry_dice = AngryDice() self.new_angry_dice.die_a.current_value = "2" self.new_angry_dice.die_b.current_value = "5" expected_output = "You rolled:\n a = [ 2 ]\n b = [ 5 ]\n\n" self.new_angry_dice.display_current_dice() self.assertEqual(expected_output, mock_stdout.getvalue()) del self.new_angry_dice
class IsDieHeldInWrongStageTest(unittest.TestCase): """Test the functionality of the AngryDice class is_die_held_in_wrong_stage function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice def test_is_die_held_in_wrong_stage(self): """Generate all input and correct output value combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = [(die_value, stage, ok_output),...] test_input_cases = [ ("1", 1, False), ("2", 1, False), ("ANGRY", 1, True), ("4", 1, True), ("5", 1, True), ("6", 1, True), ("1", 2, True), ("2", 2, True), ("ANGRY", 2, False), ("4", 2, False), ("5", 2, True), ("6", 2, True), ("1", 3, True), ("2", 3, True), ("ANGRY", 3, True), ("4", 3, True), ("5", 3, False), ("6", 3, False), ] for test_io in test_input_cases: self._test_is_die_held_in_wrong_stage(*test_io) def _test_is_die_held_in_wrong_stage(self, die_value, stage, ok_output): """Set die_a's value to die_value, game_stage to stage. Check that the output of tested function matches ok_output.""" self.new_angry_dice.die_a.current_value = die_value self.new_angry_dice.game_stage = stage actual_out = self.new_angry_dice.is_die_held_in_wrong_stage( self.new_angry_dice.die_a) self.assertEqual( actual_out, ok_output, "Incorrect output for holding a {} in stage {}.".format( die_value, stage)) def test_is_die_held_in_wrong_stage_bad_argument(self): """Call tested function with a float argument. Check that a TypeError exception is generated.""" self.assertRaises(TypeError, self.new_angry_dice.is_die_held_in_wrong_stage, 54.8)
class IsAdvancingToNextStageTest(unittest.TestCase): """Test the functionality of the AngryDice class is_advancing_to_next_stage function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice def test_is_advancing_to_next_stage_yes(self): """Generate advancing die and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, stage, ok_output),] test_input_cases = [ ("1", "2", 1, True), ("2", "1", 1, True), ("ANGRY", "4", 2, True), ("4", "ANGRY", 2, True), ] for test_io in test_input_cases: self._test_is_game_over(*test_io) def test_is_advancing_to_next_stage_no(self): """Generate some non-advancing die and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, stage, ok_output),] test_input_cases = [ ("1", "2", 2, False), ("2", "1", 3, False), ("1", "1", 1, False), ("1", "1", 2, False), ("1", "1", 3, False), ("ANGRY", "1", 1, False), ("ANGRY", "1", 2, False), ] for test_io in test_input_cases: self._test_is_game_over(*test_io) def _test_is_game_over(self, die_a_value, die_b_value, stage, ok_output): """Set both die's value and game_stage. Check that the output of tested function matches ok_output.""" self.new_angry_dice.die_a.current_value = die_a_value self.new_angry_dice.die_b.current_value = die_b_value self.new_angry_dice.game_stage = stage self.assertEqual(self.new_angry_dice.is_advancing_to_next_stage(), ok_output, "Incorrect output for die_a:{}, " "die_b:{} in stage:{}." .format(die_a_value, die_b_value, stage))
class IsDieHeldInWrongStageTest(unittest.TestCase): """Test the functionality of the AngryDice class is_die_held_in_wrong_stage function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice def test_is_die_held_in_wrong_stage(self): """Generate all input and correct output value combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = [(die_value, stage, ok_output),...] test_input_cases = [ ("1", 1, False), ("2", 1, False), ("ANGRY", 1, True), ("4", 1, True), ("5", 1, True), ("6", 1, True), ("1", 2, True), ("2", 2, True), ("ANGRY", 2, False), ("4", 2, False), ("5", 2, True), ("6", 2, True), ("1", 3, True), ("2", 3, True), ("ANGRY", 3, True), ("4", 3, True), ("5", 3, False), ("6", 3, False), ] for test_io in test_input_cases: self._test_is_die_held_in_wrong_stage(*test_io) def _test_is_die_held_in_wrong_stage(self, die_value, stage, ok_output): """Set die_a's value to die_value, game_stage to stage. Check that the output of tested function matches ok_output.""" self.new_angry_dice.die_a.current_value = die_value self.new_angry_dice.game_stage = stage actual_out = self.new_angry_dice.is_die_held_in_wrong_stage( self.new_angry_dice.die_a) self.assertEqual(actual_out, ok_output, "Incorrect output for holding a {} in stage {}." .format(die_value, stage)) def test_is_die_held_in_wrong_stage_bad_argument(self): """Call tested function with a float argument. Check that a TypeError exception is generated.""" self.assertRaises(TypeError, self.new_angry_dice.is_die_held_in_wrong_stage, 54.8)
def test_handle_angry_dice(self, mock_stdout): """Set values of die_a and die_b to "ANGRY" and game_stage to 2. Call tested function. Check that the game reset to stage 1, and that the expected string was output.""" self.new_angry_dice = AngryDice() self.new_angry_dice.die_a.current_value = "ANGRY" self.new_angry_dice.die_b.current_value = "ANGRY" self.new_angry_dice.game_stage = 2 expected_output = "WOW, you're ANGRY!\nTime to go back to Stage 1!\n" self.new_angry_dice.handle_angry_dice() self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(1, self.new_angry_dice.game_stage) del self.new_angry_dice
class DisplayCurrentDiceTest(unittest.TestCase): """Test the functionality of the AngryDice class display_current_dice function.""" # Mocks being sys.stdout and will stor whet's print()'d into # mock_stdout @patch('sys.stdout', new_callable=StringIO) def test_display_current_dice(self, mock_stdout): """Set values of die_a and die_b to some values. Call tested function, and check that the expected string was output.""" self.new_angry_dice = AngryDice() self.new_angry_dice.die_a.current_value = "2" self.new_angry_dice.die_b.current_value = "5" expected_output = "You rolled:\n a = [ 2 ]\n b = [ 5 ]\n\n" self.new_angry_dice.display_current_dice() self.assertEqual(expected_output, mock_stdout.getvalue()) del self.new_angry_dice
class RegisterPlayerCheatingTest(unittest.TestCase): """Test the functionality of the AngryDice class register_player_cheating function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice def test_register_player_cheating_die_a_not_6_hold(self): """Set die_a's value to "1", just_cheated_a to False. Call function: ("a", []). Check that function returns False.""" self.new_angry_dice.die_a.current_value = "1" self.new_angry_dice.register_player_cheating("a", []) self.assertEqual(self.new_angry_dice.just_cheated_a, False, "Die 'a' unexpectedly registered cheated.") def test_register_player_cheating_die_a_is_6_hold(self): """Set die_a's value to "6", just_cheated_a to False. Call function: ("a", []). Check that just_cheated_a is True.""" self.new_angry_dice.die_a.current_value = "6" self.new_angry_dice.just_cheated_a = False self.new_angry_dice.register_player_cheating("a", []) self.assertEqual(self.new_angry_dice.just_cheated_a, True, "Die 'a' failed to change to cheated.") def test_register_player_cheating_die_a_not_6_nohold(self): """Set die_a's value to "1", just_cheated_a to False. Call function: ("a", ["a"]). Check that just_cheated_a is unchanged.""" self.new_angry_dice.die_a.current_value = "1" self.new_angry_dice.just_cheated_a = False self.new_angry_dice.register_player_cheating("a", ["a"]) self.assertEqual(self.new_angry_dice.just_cheated_a, False, "Die 'a' unexpectedly registered cheated.") def test_register_player_cheating_die_a_is_6_nohold(self): """Set die_a's value to "6", just_cheated_a to False. Call function: ("a", ["a"]). Check that just_cheated_a is unchanged.""" self.new_angry_dice.die_a.current_value = "6" self.new_angry_dice.just_cheated_a = False self.new_angry_dice.register_player_cheating("a", ["a"]) self.assertEqual(self.new_angry_dice.just_cheated_a, False, "Die 'a' unexpectedly registered cheated.") def test_register_player_cheating_die_b_not_6_hold(self): """Set die_b's value to "1", just_cheated_b to False. Call function: ("b", []). Check that just_cheated_b is unchanged.""" self.new_angry_dice.die_b.current_value = "1" self.new_angry_dice.just_cheated_b = False self.new_angry_dice.register_player_cheating("b", []) self.assertEqual(self.new_angry_dice.just_cheated_b, False, "Die 'b' unexpectedly registered cheated.") def test_register_player_cheating_die_b_is_6_hold(self): """Set die_b's value to "6", just_cheated_b to False. Call function: ("b", []). Check that just_cheated_b is True.""" self.new_angry_dice.die_b.current_value = "6" self.new_angry_dice.just_cheated_b = False self.new_angry_dice.register_player_cheating("b", []) self.assertEqual(self.new_angry_dice.just_cheated_b, True, "Die 'b' failed to change to cheated.") def test_register_player_cheating_die_b_not_6_nohold(self): """Set die_b's value to "1", just_cheated_b to False. Call function: ("b", ["b"]). Check that just_cheated_b is unchanged.""" self.new_angry_dice.die_b.current_value = "1" self.new_angry_dice.just_cheated_b = False self.new_angry_dice.register_player_cheating("b", ["b"]) self.assertEqual(self.new_angry_dice.just_cheated_b, False, "Die 'b' unexpectedly registered cheated.") def test_register_player_cheating_die_b_is_6_nohold(self): """Set die_b's value to "6", just_cheated_b to False. Call function: ("b", ["b"]). Check that just_cheated_b is unchanged.""" self.new_angry_dice.die_b.current_value = "6" self.new_angry_dice.just_cheated_b = False self.new_angry_dice.register_player_cheating("b", ["b"]) self.assertEqual(self.new_angry_dice.just_cheated_b, False, "Die 'b' unexpectedly registered cheated.") def test_register_player_cheating_wrong_die_hold_ab(self): """Set both die's value to "6", just_cheated_a, just_cheated_b to False. Call function: ("c34", []). Check that just_cheated_a and just_cheated_b are unchanged.""" self.new_angry_dice.die_a.current_value = "6" self.new_angry_dice.die_b.current_value = "6" self.new_angry_dice.just_cheated_a = False self.new_angry_dice.just_cheated_b = False self.new_angry_dice.register_player_cheating("c34", []) self.assertEqual(self.new_angry_dice.just_cheated_a, False, "Die 'a' unexpectedly registered cheated.") self.assertEqual(self.new_angry_dice.just_cheated_b, False, "Die 'b' unexpectedly registered cheated.")
def setUp(self): self.new_angry_dice = AngryDice()
class IsGameOverTest(unittest.TestCase): """Test the functionality of the AngryDice class is_game_over function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice def test_is_game_over_player_won(self): """Generate winning die, cheated and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output),] test_input_cases = [ ("6", "5", False, False, 3, True), ("5", "6", False, False, 3, True), ] for test_io in test_input_cases: self._test_is_game_over(*test_io) def test_is_game_over_cheating(self): """Generate representative die, cheated and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output),] test_input_cases = [ ("6", "5", True, False, 3, False), ("5", "6", False, True, 3, False), ("6", "6", True, True, 3, False), ("6", "6", True, False, 3, False), ("6", "6", False, True, 3, False), ] def test_is_game_over_stage_1_2(self): """Generate representative die, cheated and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output),] test_input_cases = [ ("6", "5", False, False, 2, False), ("5", "6", False, False, 1, False), ("ANGRY", "ANGRY", False, False, 1, False), ("4", "1", False, False, 2, False), ("2", "2", False, False, 1, False), ] for test_io in test_input_cases: self._test_is_game_over(*test_io) def _test_is_game_over(self, die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output): """Set both die's value, both cheated attribute and game_stage. Check that the output of tested function matches ok_output.""" self.new_angry_dice.die_a.current_value = die_a_value self.new_angry_dice.die_b.current_value = die_b_value self.new_angry_dice.just_cheated_a = cheated_a self.new_angry_dice.just_cheated_b = cheated_b self.new_angry_dice.game_stage = stage self.assertEqual( self.new_angry_dice.is_game_over(), ok_output, "Incorrect output for die_a:{}, cheated_a:{}, " "die_b:{}, cheated_b:{} in stage:{}.".format( die_a_value, cheated_a, die_b_value, cheated_b, stage))
class IsGameOverTest(unittest.TestCase): """Test the functionality of the AngryDice class is_game_over function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice def test_is_game_over_player_won(self): """Generate winning die, cheated and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output),] test_input_cases = [ ("6", "5", False, False, 3, True), ("5", "6", False, False, 3, True), ] for test_io in test_input_cases: self._test_is_game_over(*test_io) def test_is_game_over_cheating(self): """Generate representative die, cheated and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output),] test_input_cases = [ ("6", "5", True, False, 3, False), ("5", "6", False, True, 3, False), ("6", "6", True, True, 3, False), ("6", "6", True, False, 3, False), ("6", "6", False, True, 3, False), ] def test_is_game_over_stage_1_2(self): """Generate representative die, cheated and stage combinations. Call _test_is_die_held_in_wrong_stage() with all input combinations.""" # test_input_cases = # [(die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output),] test_input_cases = [ ("6", "5", False, False, 2, False), ("5", "6", False, False, 1, False), ("ANGRY", "ANGRY", False, False, 1, False), ("4", "1", False, False, 2, False), ("2", "2", False, False, 1, False), ] for test_io in test_input_cases: self._test_is_game_over(*test_io) def _test_is_game_over(self, die_a_value, die_b_value, cheated_a, cheated_b, stage, ok_output): """Set both die's value, both cheated attribute and game_stage. Check that the output of tested function matches ok_output.""" self.new_angry_dice.die_a.current_value = die_a_value self.new_angry_dice.die_b.current_value = die_b_value self.new_angry_dice.just_cheated_a = cheated_a self.new_angry_dice.just_cheated_b = cheated_b self.new_angry_dice.game_stage = stage self.assertEqual(self.new_angry_dice.is_game_over(), ok_output, "Incorrect output for die_a:{}, cheated_a:{}, " "die_b:{}, cheated_b:{} in stage:{}." .format(die_a_value, cheated_a, die_b_value, cheated_b, stage))
class ClearJustCheatedTest(unittest.TestCase): """Test the functionality of the AngryDice class clear_just_cheated function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice def test_clear_just_cheated_none(self): """Set values of just_cheated_a and just_cheated_b to True. Call tested function with empty list. Check that neither just_cheated_x value changed.""" self.new_angry_dice.just_cheated_a = True self.new_angry_dice.just_cheated_b = True self.new_angry_dice.clear_just_cheated([]) self.assertEqual(self.new_angry_dice.just_cheated_a, True, "The just_cheated_a attribute was cleared.") self.assertEqual(self.new_angry_dice.just_cheated_b, True, "The just_cheated_b attribute was cleared.") def test_clear_just_cheated_die_a(self): """Set values of just_cheated_a and just_cheated_b to True. Call tested function with ["a"] argument. Check that just_cheated_a value has changed to False, but just_cheated_b has not changed.""" self.new_angry_dice.just_cheated_a = True self.new_angry_dice.just_cheated_b = True self.new_angry_dice.clear_just_cheated(["a"]) self.assertEqual(self.new_angry_dice.just_cheated_a, False, "The just_cheated_a attribute failed to clear.") self.assertEqual(self.new_angry_dice.just_cheated_b, True, "The just_cheated_b attribute was cleared.") def test_clear_just_cheated_die_b(self): """Set values of just_cheated_a and just_cheated_b to True. Call tested function with ["b"] argument. Check that just_cheated_b value has changed to False, but just_cheated_a has not changed.""" self.new_angry_dice.just_cheated_a = True self.new_angry_dice.just_cheated_b = True self.new_angry_dice.clear_just_cheated(["b"]) self.assertEqual(self.new_angry_dice.just_cheated_b, False, "The just_cheated_b attribute failed to clear.") self.assertEqual(self.new_angry_dice.just_cheated_a, True, "The just_cheated_a attribute was cleared.") def test_clear_just_cheated_bad_argument(self): """Generate a list of improper argument types. Call tested function with those arguments. Check that an TypeError exception is raised.""" bad_args = [23.7, "barf", ['b', 3]] for bad_arg in bad_args: self._test_clear_just_cheated_bad_argument(bad_arg) def _test_clear_just_cheated_bad_argument(self, bad_arg): """Call tested function with "argonaut" argument. Check that a TypeError exception is raised.""" self.assertRaises(TypeError, self.new_angry_dice.clear_just_cheated, bad_arg)
class ProcessUserInputTest(unittest.TestCase): """Test the functionality of the AngryDice class process_user_input function.""" def setUp(self): self.new_angry_dice = AngryDice() def tearDown(self): del self.new_angry_dice # Will mock the input for 1 input prompt @patch('builtins.input', return_value='') def test_process_user_input_empty(self, input_value): """Test if an empty list is generated as output if the user doesn't input anything.""" dice_to_roll = self.new_angry_dice.process_user_input() self.assertEqual([], dice_to_roll, "Output list is not empty.") @patch('builtins.input', return_value='a') def test_process_user_input_only_a(self, input_value): """Test if the correct list is generated as output if the user inputs 'a'.""" dice_to_roll = self.new_angry_dice.process_user_input() self.assertEqual(['a'], dice_to_roll, "Output list does not equal ['a'].") @patch('builtins.input', return_value='b') def test_process_user_input_only_b(self, input_value): """Test if the correct list is generated as output if the user inputs 'b'.""" dice_to_roll = self.new_angry_dice.process_user_input() self.assertEqual(['b'], dice_to_roll, "Output list does not equal ['b'].") @patch('builtins.input', return_value='ab') def test_process_user_input_a_and_b(self, input_value): """Test if the correct list is generated as output if the user inputs 'ab'.""" dice_to_roll = self.new_angry_dice.process_user_input() self.assertIn('a', dice_to_roll, "'a' is missing from output list.") self.assertIn('b', dice_to_roll, "'b' is missing from output list.") @patch('builtins.input', return_value='l1a! a') def test_process_user_input_junk_and_a(self, input_value): """Test if the correct list is generated as output if the user inputs a string that contains at least one 'a' but no 'b'.""" dice_to_roll = self.new_angry_dice.process_user_input() self.assertEqual(['a'], dice_to_roll, "Output list does not equal ['a'].") @patch('builtins.input', return_value='bln!bb 7') def test_process_user_input_junk_and_b(self, input_value): """Test if the correct list is generated as output if the user inputs a string that contains at least one 'b' but no 'a'.""" dice_to_roll = self.new_angry_dice.process_user_input() self.assertEqual(['b'], dice_to_roll, "Output list does not equal ['b'].")