Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bowling import Game
- class GameFixture:
- def __init__(self):
- self.game = Game()
- self.num_rolls = 0
- def roll(self, pins):
- self.num_rolls = self.num_rolls + 1
- self.game.roll(pins)
- def score(self):
- return self.game.score()
- def finish_game_with_zeroes(self):
- rolls_left = 20 - self.num_rolls
- for i in range(rolls_left):
- self.game.roll(0)
- def is_in_error_state(self):
- return self.game.score() == self.game.error
- def test_zero_game():
- game = GameFixture()
- game.finish_game_with_zeroes()
- assert game.score() == 0
- def test_all_ones():
- game = Game()
- for i in range(20):
- game.roll(1)
- assert game.score() == 20
- def test_one():
- game = GameFixture()
- game.roll(1)
- game.finish_game_with_zeroes()
- assert game.score() == 1
- def test_too_many_pins():
- game = GameFixture()
- for i in range(20):
- game.roll(50)
- assert game.is_in_error_state()
- # Error state persists after bad roll
- game = GameFixture()
- game.roll(11)
- game.finish_game_with_zeroes()
- assert game.is_in_error_state()
- # Error state persists even when subsequent rolls are good
- game = GameFixture()
- game.roll(11)
- game.roll(1)
- game.finish_game_with_zeroes()
- assert game.is_in_error_state()
- def test_too_few_rolls():
- game = GameFixture()
- # When one roll is made
- game.roll(0)
- # Then the game can't be scored
- assert game.is_in_error_state()
- # And when a second roll is made
- game.roll(0)
- # Then the game still can't be scored
- assert game.is_in_error_state()
- def test_too_many_rolls():
- # Given a game with more 21 rolls (and no expectation of a 21st roll)
- game = GameFixture()
- for i in range(21):
- game.roll(0)
- # Then the game can't be scored
- assert game.is_in_error_state()
- def test_that_frame_1_doesnt_exceed_10():
- # Given a game
- game = GameFixture()
- # When the first two rolls add up to more than 10
- game.roll(7)
- game.roll(7)
- game.finish_game_with_zeroes()
- # Then the game is in an error state
- assert game.is_in_error_state()
- def test_that_frame_2_doesnt_exceed_10():
- # Given a game with two rolls already made
- game = GameFixture()
- game.roll(0)
- game.roll(0)
- # When the next two rolls add up to more than 10
- game.roll(7)
- game.roll(7)
- game.finish_game_with_zeroes()
- # Then the game is in an error state
- assert game.is_in_error_state()
- def test_that_frame_3_doesnt_exceed_10():
- # Given a game with four rolls already made
- game = GameFixture()
- game.roll(0)
- game.roll(0)
- game.roll(0)
- game.roll(0)
- # When the next two rolls add up to more than 10
- game.roll(7)
- game.roll(7)
- game.finish_game_with_zeroes()
- # Then the game is in an error state
- assert game.is_in_error_state()
- def test_frame_2_validity_doesnt_count_frame_1():
- # Given a game with two valid frames totalling >10 pins
- game = GameFixture()
- game.roll(5)
- game.roll(5)
- game.roll(5)
- game.roll(5)
- # When the game finishes
- game.finish_game_with_zeroes()
- # Then the game is in an error state
- assert not game.is_in_error_state()
- def test_frame_3_validity_doesnt_count_frame_2():
- # Given a game with three valid frames where frame 2 and 3 total >10 pins
- game = GameFixture()
- game.roll(0)
- game.roll(0)
- game.roll(5)
- game.roll(5)
- game.roll(5)
- game.roll(5)
- # When the game finishes
- game.finish_game_with_zeroes()
- # Then the game is in an error state
- assert not game.is_in_error_state()
- def test_spares():
- test_next_pins = [1, 2]
- for next_pin in test_next_pins:
- # Given a game in which the first frame is a spare
- game = GameFixture()
- game.roll(6)
- game.roll(4)
- # When the next pin is n, then nothing else is scored
- game.roll(next_pin)
- game.finish_game_with_zeroes()
- # Then the final score is (10 + n + spare bonus of n)
- assert game.score() == 10 + 2 * next_pin
- def test_spare_bonus_only_counts_one_extra_roll():
- # Given a game in which the first frame is a spare
- game = GameFixture()
- game.roll(6)
- game.roll(4)
- # When the next two pins are 1s
- game.roll(1)
- game.roll(1)
- game.finish_game_with_zeroes()
- # Then only the third roll is doubled (i.e., the score is 13)
- assert game.score() == 13
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement