class BowlingGameTest(unittest.TestCase): def setUp(self): self.g = Game() def testGutterGame(self): self.__rollMany(0,0) def testAllOnes(self): self.__rollMany(1,20) def testAddFrame(self): self.g.add_frame(Frame()) self.assertEquals(1, len(self.g.frames)) def testBuildFramesWhileRolling(self): self.g.roll(10) self.assertEqual([[10]], self.g.frames_to_list()) self.g.roll(2) self.assertEqual([[10], [2]], self.g.frames_to_list()) self.g.roll(4) self.assertEqual([[10], [2,4]], self.g.frames_to_list()) self.g.roll(10) self.assertEqual([[10], [2,4], [10]], self.g.frames_to_list()) def testSpare(self): self.g.roll(4) self.g.roll(6) self.g.roll(4) self.assertEqual(14+4, self.g.get_score()) def testStrike(self): self.g.roll(10) self.g.roll(2) self.g.roll(4) self.assertEqual(10+6+6, self.g.get_score()) def testShouldNotAddMoreThenTenFrames(self): for i in range(10): self.g.add_frame(Frame()) self.assertFalse(self.g.add_frame(Frame())) self.assertEqual(10, len(self.g.frames)) def testPerfectGame(self): for i in range(12): self.g.roll(10) self.assertEqual(300, self.g.get_score()) def __rollMany(self, pins, expected_score): for i in range(20): self.g.roll(pins) self.assertEqual(expected_score, self.g.get_score())
class BowlingGameTest(unittest.TestCase): def setUp(self): self.g = Game() def testGutterGame(self): self.__rollMany(0, 0) def testAllOnes(self): self.__rollMany(1, 20) def testAddFrame(self): self.g.add_frame(Frame()) self.assertEquals(1, len(self.g.frames)) def testBuildFramesWhileRolling(self): self.g.roll(10) self.assertEqual([[10]], self.g.frames_to_list()) self.g.roll(2) self.assertEqual([[10], [2]], self.g.frames_to_list()) self.g.roll(4) self.assertEqual([[10], [2, 4]], self.g.frames_to_list()) self.g.roll(10) self.assertEqual([[10], [2, 4], [10]], self.g.frames_to_list()) def testSpare(self): self.g.roll(4) self.g.roll(6) self.g.roll(4) self.assertEqual(14 + 4, self.g.get_score()) def testStrike(self): self.g.roll(10) self.g.roll(2) self.g.roll(4) self.assertEqual(10 + 6 + 6, self.g.get_score()) def testShouldNotAddMoreThenTenFrames(self): for i in range(10): self.g.add_frame(Frame()) self.assertFalse(self.g.add_frame(Frame())) self.assertEqual(10, len(self.g.frames)) def testPerfectGame(self): for i in range(12): self.g.roll(10) self.assertEqual(300, self.g.get_score()) def __rollMany(self, pins, expected_score): for i in range(20): self.g.roll(pins) self.assertEqual(expected_score, self.g.get_score())
class TestGame(TestCase): def setUp(self): """ Pretty much every single test is going to require a game, so we set one up by default.""" self.game = Game() def test_perfect_game(self): """If all 10 frames in a game are strike, we should have a perfect game and should score 300.""" for _ in range(9): self.game.add_frame(Frame(10)) self.game.add_frame(Frame(10, 10, 10)) self.assertTrue(self.game.is_perfect_game) self.assertEqual(self.game.total_score, 300) self.assertFalse(self.game.is_gutter_game) def test_gutter_game(self): """If no pins were down in the entire game, we should have a gutter game.""" for _ in range(10): self.game.add_frame(Frame(0, 0)) self.assertTrue(self.game.is_gutter_game) self.assertFalse(self.game.is_perfect_game) def test_fill_allowed_if_strike(self): """Ensure fill frames are allowed is a strike is rolled on the last frame.""" for _ in range(9): self.game.add_frame(Frame(0, 0)) self.game.add_frame( (Frame(10, 10, 10))) # Last frame has extra rolls because it's a strike def test_fill_allowed_if_spare(self): """Ensure fill frames are allowed is a spare is rolled on the last frame.""" for _ in range(9): self.game.add_frame(Frame(0, 0)) self.game.add_frame((Frame(7, 3, 5))) def test_fill_not_allowed_if_not_all_down(self): """Fill frames are only allowed if all 10 pins come down in the first 2 rolls.""" for _ in range(9): self.game.add_frame(Frame(0, 0)) with self.assertRaises(AttributeError): self.game.add_frame(Frame(2, 3, 9)) def test_fill_not_allowed_if_not_last(self): """Test that a fill frame is only allowed if it's the last roll.""" self.game.add_frame(Frame(5, 3)) with self.assertRaises(AttributeError): self.game.add_frame(Frame(10, 10, 10)) def test_addiing_11_frames(self): """Only 11 frames are allowed in any one game of 10-pin bowling""" for _ in range(10): self.game.add_frame(Frame(4, 3)) # Adding the 11th frame should raise an exception with self.assertRaises(IndexError): self.game.add_frame(Frame(10)) def test_score_with_strike(self): """Basic test to ensure strikes are calculated correctly. This is test by rolling a strike, followed by a (5, 3) and a (6, 3). We should have (10 + 5 + 3) + 8 + 9 points == 35""" self.game.add_frame(Frame(10)) # Strike self.game.add_frame(Frame(5, 3)) self.game.add_frame(Frame(6, 3)) self.assertEqual(self.game.total_score, 35) def test_consecutive_strikes(self): """ Ensure score is calculated correctly if we roll several strikes in a row. The score should be (10 + 10 + 10) + (10 + 10 + 5) + (10 + 5 + 3) + 8 + 9 == 90. """ self.game.add_frame(Frame(10)) # Strike self.game.add_frame(Frame(10)) # Strike self.game.add_frame(Frame(10)) # Strike self.game.add_frame(Frame(5, 3)) self.game.add_frame(Frame(6, 3)) self.assertEqual(self.game.total_score, 90) def test_score_with_spare(self): """Basic test to ensure spares are calculated correctly. This is done by rolling a spare (10 points) + 8 pins. We should end up with (10 + 5) + 8 points. """ self.game.add_frame(Frame(6, 4)) # Spare self.game.add_frame(Frame(5, 3)) self.assertEqual(self.game.total_score, 23) def test_score_with_consecutive_spares(self): """ Ensure score is calculated correctly if we roll several spares in a row. The score should be (10 + 2) + (10 + 7) + (10 + 5) + 8 == 66. """ self.game.add_frame(Frame(6, 4)) # Spare self.game.add_frame(Frame(2, 8)) # Spare self.game.add_frame(Frame(7, 3)) # Spare self.game.add_frame(Frame(5, 3)) self.assertEqual(self.game.total_score, 52) def test_score_with_strike_spare_combo(self): """ Ensure score is calculated correctly if we mix spares and strikes in consecutive frames. The score should be (10 + 10) + (10 + 7 + 3) + (10 + 10) (10 + 5 + 5) + 10 == 100. """ self.game.add_frame(Frame(6, 4)) # Spare self.game.add_frame(Frame(10)) # Strike self.game.add_frame(Frame(7, 3)) # Spare self.game.add_frame(Frame(10)) # Strike self.game.add_frame(Frame(5, 5)) # Spare self.assertEqual(self.game.total_score, 90) def test_score_with_regular_simple_scores(self): """ This is to test the scoring works with some regular casual rolls. Game and score was created using https://www.bowlinggenius.com/. """ self.game.add_frame(Frame(8, 2)) self.game.add_frame(Frame(6, 4)) self.game.add_frame(Frame(3, 2)) self.game.add_frame(Frame(6, 2)) self.game.add_frame(Frame(1, 3)) self.game.add_frame(Frame(4, 5)) self.game.add_frame(Frame(4, 3)) self.game.add_frame(Frame(9, 1)) self.game.add_frame(Frame(10)) self.game.add_frame(Frame(0, 2)) self.assertEqual(self.game.total_score, 96)