class AngryDiceDetermineRollTest(unittest.TestCase): def setUp(self): self.angry_game = AngryDiceGame() def tearDown(self): del self.angry_game @patch('builtins.input', return_value='ab') def test_input_a_and_b(self, inputted_value): dice_to_roll = self.angry_game.determine_roll() self.assertIn(self.angry_game.die_a, dice_to_roll) self.assertIn(self.angry_game.die_b, dice_to_roll) @patch('builtins.input', return_value='b') def test_input_just_b(self, inputted_value): dice_to_roll = self.angry_game.determine_roll() self.assertNotIn(self.angry_game.die_a, dice_to_roll) self.assertIn(self.angry_game.die_b, dice_to_roll) @patch('builtins.input', return_value='a') def test_input_just_a(self, inputted_value): dice_to_roll = self.angry_game.determine_roll() self.assertNotIn(self.angry_game.die_b, dice_to_roll) self.assertIn(self.angry_game.die_a, dice_to_roll) @patch('builtins.input', return_value='') def test_input_just_a(self, inputted_value): dice_to_roll = self.angry_game.determine_roll() self.assertNotIn(self.angry_game.die_b, dice_to_roll) self.assertNotIn(self.angry_game.die_a, dice_to_roll)
def test_roll_invalid_input(self): angry_game = AngryDiceGame() try: angry_game.roll_the_dice(2) except TypeError: self.assertTrue(False, "Invalid type passed and not handled.")
def test_main_run(self, input_mock, mock_game_cheating): game = AngryDiceGame() game.current_stage = 3 game.die_b.setDieFaceValue("5") game.die_a.setDieFaceValue("6") game.main() mock_game_cheating.assert_called_with(["Die B"])
def test_roll_one_die(self): """Roll one die using the roll_the_dice function.""" angry_game = AngryDiceGame() single_die = Die([1,2,3,"Dog"]) single_die.currentValue = 7 angry_game.roll_the_dice([single_die]) self.assertNotEqual(single_die.currentValue, 7, "Die was not rolled")
def test_roll_one_die(self): """Roll one die using the roll_the_dice function.""" angry_game = AngryDiceGame() single_die = Die([1, 2, 3, "Dog"]) single_die.currentValue = 7 angry_game.roll_the_dice([single_die]) self.assertNotEqual(single_die.currentValue, 7, "Die was not rolled")
def test_roll_empty_list(self): angry_game = AngryDiceGame() exception = False try: angry_game.roll_the_dice([]) except: exception = True self.assertFalse(exception)
def test_roll_five_dice(self): """Roll five dice using the roll_the_dice function.""" angry_game = AngryDiceGame() dice = [] for i in range(5): dice.append(Die([1, 2, 3, "Dog"])) dice[i].currentValue = 7 angry_game.roll_the_dice(dice) for die in dice: self.assertNotEqual(die.currentValue, 7, "Die {} of 5 was not rolled".format(i + 1))
class AngryDiceCheckAngryTest(unittest.TestCase): def setUp(self): self.angry_game = AngryDiceGame() def tearDown(self): del self.angry_game @patch('sys.stdout', new_callable=StringIO) def test_both_die_angry(self, mock_stdout): """Set both dice to ANGRY and check that the game resets their game.""" self.angry_game.die_a.setDieFaceValue("ANGRY") self.angry_game.die_b.setDieFaceValue("ANGRY") angry_text = "WOW, you're ANGRY!\nTime to go back to Stage 1!\n" self.angry_game.check_angry() self.assertEqual(mock_stdout.getvalue(), angry_text) self.assertEqual(self.angry_game.current_stage, 1) @patch('sys.stdout', new_callable=StringIO) def test_die_a_angry(self, mock_stdout): """Set both dice to ANGRY and check that the game resets their game.""" self.angry_game.die_a.setDieFaceValue("ANGRY") self.angry_game.die_b.setDieFaceValue("5") angry_text = "WOW, you're ANGRY!\nTime to go back to Stage 1!\n" self.angry_game.current_stage = 2 self.angry_game.check_angry() self.assertNotEqual(mock_stdout.getvalue(), angry_text) self.assertEqual(self.angry_game.current_stage, 2) @patch('sys.stdout', new_callable=StringIO) def test_die_b_angry(self, mock_stdout): """Set both dice to ANGRY and check that the game resets their game.""" self.angry_game.die_b.setDieFaceValue("ANGRY") self.angry_game.die_a.setDieFaceValue("5") angry_text = "WOW, you're ANGRY!\nTime to go back to Stage 1!\n" self.angry_game.current_stage = 2 self.angry_game.check_angry() self.assertNotEqual(mock_stdout.getvalue(), angry_text) self.assertEqual(self.angry_game.current_stage, 2) @patch('sys.stdout', new_callable=StringIO) def test_neither_die_angry(self, mock_stdout): """Set both dice to ANGRY and check that the game resets their game.""" self.angry_game.die_b.setDieFaceValue("2") self.angry_game.die_a.setDieFaceValue("5") angry_text = "WOW, you're ANGRY!\nTime to go back to Stage 1!\n" self.angry_game.current_stage = 2 self.angry_game.check_angry() self.assertNotEqual(mock_stdout.getvalue(), angry_text) self.assertEqual(self.angry_game.current_stage, 2)
def test_roll_five_dice(self): """Roll five dice using the roll_the_dice function.""" angry_game = AngryDiceGame() dice = [] for i in range(5): dice.append(Die([1,2,3,"Dog"])) dice[i].currentValue = 7 angry_game.roll_the_dice(dice) for die in dice: self.assertNotEqual(die.currentValue, 7, "Die {} of 5 was not rolled" .format(i+1))
class AngryDicePrintDiceTest(unittest.TestCase): def setUp(self): self.angry_game = AngryDiceGame() def tearDown(self): del self.angry_game @patch('sys.stdout', new_callable=StringIO) def test_valid_die_values(self, mock_stdout): """Set both dice to ANGRY and check that the game resets their game.""" self.angry_game.die_a.setDieFaceValue("ANGRY") self.angry_game.die_b.setDieFaceValue("4") printed_die_string = "You rolled:\n a = [ {} ]\n b = [ {} ]" \ "\n\nYou are in Stage {}\n".format("ANGRY", "4", 1) self.angry_game.print_dice() self.assertEqual(mock_stdout.getvalue(), printed_die_string)
def setUp(self): self.angry_game = AngryDiceGame()
class AngryDiceCheckCheatingTest(unittest.TestCase): def setUp(self): self.angry_game = AngryDiceGame() def tearDown(self): del self.angry_game @patch('sys.stdout', new_callable=StringIO) def test_stage_three_holding_six(self, mock_stdout): """Hold a 6 in Stage 3 and confirm user is found cheating.""" self.angry_game.die_a.setDieFaceValue("5") self.angry_game.die_b.setDieFaceValue("6") self.angry_game.current_stage = 3 self.angry_game.check_cheating([self.angry_game.die_a]) self.assertEqual( mock_stdout.getvalue(), "You're cheating! You cannot " "lock a 6! You cannot win " "until you reroll it!\n") self.assertTrue(self.angry_game.cheating) @patch('sys.stdout', new_callable=StringIO) def test_stage_three_holding_five(self, mock_stdout): """Hold a 5 in Stage 3 and confirm user is not found cheating.""" self.angry_game.die_a.setDieFaceValue("6") self.angry_game.die_b.setDieFaceValue("5") self.angry_game.current_stage = 3 self.angry_game.check_cheating([self.angry_game.die_a]) self.assertEqual(mock_stdout.getvalue(), "") self.assertFalse(self.angry_game.cheating) @patch('sys.stdout', new_callable=StringIO) def test_stage_three_not_holding_anything(self, mock_stdout): """Roll both dice in Stage 3 and confirm user is not found cheating.""" self.angry_game.die_a.setDieFaceValue("6") self.angry_game.die_b.setDieFaceValue("5") self.angry_game.current_stage = 3 self.angry_game.check_cheating( [self.angry_game.die_a, self.angry_game.die_b]) self.assertEqual(mock_stdout.getvalue(), "") self.assertFalse(self.angry_game.cheating) @patch('sys.stdout', new_callable=StringIO) def test_stage_three_holding_six_and_five(self, mock_stdout): """Hold a 6 & 5 in Stage 3 and confirm user is found cheating.""" self.angry_game.die_a.setDieFaceValue("6") self.angry_game.die_b.setDieFaceValue("5") self.angry_game.current_stage = 3 self.angry_game.check_cheating([]) self.assertEqual( mock_stdout.getvalue(), "You're cheating! You cannot " "lock a 6! You cannot win " "until you reroll it!\n") self.assertTrue(self.angry_game.cheating)
class AngryDiceStageCheckTest(unittest.TestCase): def setUp(self): self.angry_game = AngryDiceGame() def tearDown(self): del self.angry_game def test_stage_one_to_stage_two_valid_inputs(self): self.angry_game.die_a.setDieFaceValue("1") self.angry_game.die_b.setDieFaceValue("2") self.angry_game.cheating = False self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 2) def test_stage_one_to_stage_two_invalid_inputs(self): self.angry_game.die_a.setDieFaceValue("ANGRY") self.angry_game.die_b.setDieFaceValue("2") self.angry_game.cheating = False self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 1) def test_stage_one_to_stage_two_valid_cheating(self): self.angry_game.die_a.setDieFaceValue("1") self.angry_game.die_b.setDieFaceValue("2") self.angry_game.cheating = True self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 1) def test_stage_one_to_stage_two_cheating(self): self.angry_game.die_a.setDieFaceValue("ANGRY") self.angry_game.die_b.setDieFaceValue("3") self.angry_game.cheating = True self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 1) def test_stage_two_to_stage_three_valid_inputs(self): self.angry_game.die_a.setDieFaceValue("ANGRY") self.angry_game.die_b.setDieFaceValue("4") self.angry_game.cheating = False self.angry_game.current_stage = 2 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 3) def test_stage_two_to_stage_three_invalid_inputs(self): self.angry_game.die_a.setDieFaceValue("5") self.angry_game.die_b.setDieFaceValue("4") self.angry_game.cheating = False self.angry_game.current_stage = 2 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 2) def test_stage_two_to_stage_three_valid_cheating(self): self.angry_game.die_a.setDieFaceValue("ANGRY") self.angry_game.die_b.setDieFaceValue("4") self.angry_game.cheating = True self.angry_game.current_stage = 2 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 2) def test_stage_two_to_stage_three_cheating(self): self.angry_game.die_a.setDieFaceValue("1") self.angry_game.die_b.setDieFaceValue("4") self.angry_game.cheating = True self.angry_game.current_stage = 2 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 2) def test_stage_three_to_stage_four_valid_inputs(self): self.angry_game.die_a.setDieFaceValue("5") self.angry_game.die_b.setDieFaceValue("6") self.angry_game.cheating = False self.angry_game.current_stage = 3 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 4) def test_stage_three_to_stage_four_invalid_inputs(self): self.angry_game.die_a.setDieFaceValue("1") self.angry_game.die_b.setDieFaceValue("3") self.angry_game.cheating = False self.angry_game.current_stage = 3 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 3) def test_stage_three_to_stage_four_valid_cheating(self): self.angry_game.die_a.setDieFaceValue("5") self.angry_game.die_b.setDieFaceValue("6") self.angry_game.cheating = True self.angry_game.current_stage = 3 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 3) def test_stage_three_to_stage_four_cheating(self): self.angry_game.die_a.setDieFaceValue("1") self.angry_game.die_b.setDieFaceValue("4") self.angry_game.cheating = True self.angry_game.current_stage = 3 self.angry_game.check_stage() self.assertEqual(self.angry_game.current_stage, 3)
class AngryDiceCheckCheatingTest(unittest.TestCase): def setUp(self): self.angry_game = AngryDiceGame() def tearDown(self): del self.angry_game @patch("sys.stdout", new_callable=StringIO) def test_stage_three_holding_six(self, mock_stdout): """Hold a 6 in Stage 3 and confirm user is found cheating.""" self.angry_game.die_a.setDieFaceValue("5") self.angry_game.die_b.setDieFaceValue("6") self.angry_game.current_stage = 3 self.angry_game.check_cheating([self.angry_game.die_a]) self.assertEqual( mock_stdout.getvalue(), "You're cheating! You cannot " "lock a 6! You cannot win " "until you reroll it!\n" ) self.assertTrue(self.angry_game.cheating) @patch("sys.stdout", new_callable=StringIO) def test_stage_three_holding_five(self, mock_stdout): """Hold a 5 in Stage 3 and confirm user is not found cheating.""" self.angry_game.die_a.setDieFaceValue("6") self.angry_game.die_b.setDieFaceValue("5") self.angry_game.current_stage = 3 self.angry_game.check_cheating([self.angry_game.die_a]) self.assertEqual(mock_stdout.getvalue(), "") self.assertFalse(self.angry_game.cheating) @patch("sys.stdout", new_callable=StringIO) def test_stage_three_not_holding_anything(self, mock_stdout): """Roll both dice in Stage 3 and confirm user is not found cheating.""" self.angry_game.die_a.setDieFaceValue("6") self.angry_game.die_b.setDieFaceValue("5") self.angry_game.current_stage = 3 self.angry_game.check_cheating([self.angry_game.die_a, self.angry_game.die_b]) self.assertEqual(mock_stdout.getvalue(), "") self.assertFalse(self.angry_game.cheating) @patch("sys.stdout", new_callable=StringIO) def test_stage_three_holding_six_and_five(self, mock_stdout): """Hold a 6 & 5 in Stage 3 and confirm user is found cheating.""" self.angry_game.die_a.setDieFaceValue("6") self.angry_game.die_b.setDieFaceValue("5") self.angry_game.current_stage = 3 self.angry_game.check_cheating([]) self.assertEqual( mock_stdout.getvalue(), "You're cheating! You cannot " "lock a 6! You cannot win " "until you reroll it!\n" ) self.assertTrue(self.angry_game.cheating)