class TestBowling(unittest.TestCase): def setUp(self): self.bowling = Bowling() def test_gutter(self): self.bowling.roll(0) self.assertEqual(self.bowling.score(), 0) def test_one_roll(self): self.bowling.roll(7) self.assertEqual(self.bowling.score(), 7) def test_two_rolls(self): self.bowling.roll(2) self.bowling.roll(3) self.assertEqual(self.bowling.score(), 5) def test_two_rolls_with_spare(self): self.bowling.roll(3) self.bowling.roll(7) self.assertEqual(self.bowling.score(), 10) self.bowling.roll(1) self.assertEqual(self.bowling.score(), 12) def test_two_rolls_with_strike(self): self.bowling.roll(10) self.assertEqual(self.bowling.score(), 10) self.bowling.roll(1) self.bowling.roll(3) self.assertEqual(self.bowling.score(), 18) def test_two_rolls_with_2_strike(self): self.bowling.roll(10) self.assertEqual(self.bowling.score(), 10) self.bowling.roll(10) self.assertEqual(self.bowling.score(), 30) self.bowling.roll(1) self.bowling.roll(3) self.assertEqual(self.bowling.score(), 38) def test_frame(self): self.bowling.roll(1) self.bowling.roll(2) self.assertEqual(self.bowling.frames, [[1,2]])
class BowlingTestSuite(unittest.TestCase): def setUp(self): self.uut = Bowling() def test_roll_gutter_game(self): self.roll([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) self.assertEqual(self.uut.score(), 0) def test_roll_all_ones(self): self.roll([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) self.assertEqual(self.uut.score(), 20) def test_roll_spare_followed_by_five(self): """ First frame is worth 15 points, second frame adds 5 for 20 total """ self.roll([7, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) self.assertEqual(20, self.uut.score()) def test_roll_strike_followed_by_two_and_three(self): """ As the first ball of frame one is a strike, the total for the frame should be the sum of the strike plus the next two rolls. Frame one is worth 15 points, frame 2 is worth 5 points, for 20 total Returns: """ self.roll([10, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) self.assertEqual(20, self.uut.score()) def test_played_perfect_game(self): self.roll([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]) self.assertEqual(300, self.uut.score()) def test_sample_game_scores(self): self.roll([1, 4, 4, 5, 6, 4, 5, 5, 10, 0, 1, 7, 3, 6, 4, 10, 2, 8, 6]) self.assertEqual(133, self.uut.score()) def roll(self, pins_down: list): for i in pins_down: self.uut.roll(i)
class TestBowling(unittest.TestCase): def setUp(self): self.game = Bowling() # Gutter game scores zero # When you roll all misses, you get a total score of zero. def test_gutter_game(self): for i in range(23): self.game.roll(0) self.assertEqual(self.game.score(), 0) # All ones scores 20 # When you knock down one pin with each ball, your total score is 20 def test_1_pin_each_roll(self): for i in range(23): self.game.roll(1) self.assertEqual(self.game.score(), 20) # A spare in the first frame, followed by three pins, # followed by all misses scores 16 def test_score_a_spare(self): self.game.roll(4) self.game.roll(6) self.game.roll(3) for i in range(20): self.game.roll(0) self.assertEqual(self.game.score(), 16) # A strike in the first frame, followed by three and then four pins, # followed by all misses, scores 24 def test_score_a_strike(self): self.game.roll(10) self.game.roll(3) self.game.roll(4) for i in range(20): self.game.roll(0) self.assertEqual(self.game.score(), 24) # 3 strikes followed by all misses, scores 60 def test_score_3_strike(self): self.game.roll(10) self.game.roll(10) self.game.roll(10) for i in range(18): self.game.roll(0) self.assertEqual(self.game.score(), 60) # 3 strikes followed by all misses, scores 60 def test_score_2_strike(self): self.game.roll(10) self.game.roll(10) self.game.roll(5) self.game.roll(5) for i in range(18): self.game.roll(0) self.assertEqual(self.game.score(), 55) # 1 spare, 1 strikes, 1 spare, 1 meh followed by all misses, scores 60 def test_score_mixed(self): self.game.roll(5) self.game.roll(5) self.game.roll(10) self.game.roll(5) self.game.roll(5) self.game.roll(5) self.game.roll(0) for i in range(16): self.game.roll(0) self.assertEqual(self.game.score(), 60) # A perfect game (12 strikes) scores 300 def test_score_a_perfect_game(self): for i in range(12): self.game.roll(10) self.assertEqual(self.game.score(), 300) # Almost perfect game (10 strikes) scores 270 def test_score_an_almost_perfect_game(self): for i in range(10): self.game.roll(10) self.game.roll(0) self.game.roll(0) self.assertEqual(self.game.score(), 270)