class BowlingGameTests(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()

    def roll_many_times(self, rolls, pins):
        for i in range(rolls):
            self.game.roll(pins)

    def assert_score_is(self, expected):
        self.assertEqual(self.game.score(), expected)

    def test_twenty_misses_should_give_zero_score(self):
        self.roll_many_times(rolls=20, pins=0)
        self.assert_score_is(0)

    def test_score_should_be_sum_of_pins_when_no_bonus_points(self):
        self.roll_many_times(rolls=20, pins=1)
        self.assert_score_is(20)

    def test_when_spare_then_bonus_equals_next_roll(self):
        self.roll_many_times(rolls=2, pins=5)
        self.roll_many_times(rolls=18, pins=1)
        self.assert_score_is(29)

    def test_when_strike_then_bonus_equals_next_two_rolls(self):
        self.game.roll(10)
        self.roll_many_times(rolls=18, pins=1)
        self.assert_score_is(30)

    def test_perfect_game(self):
        self.roll_many_times(rolls=12, pins=10)
        self.assert_score_is(300)
class BowlingGameTest(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()

    def test_gutter_game(self):
        for x in range(1, 21):
            self.game.roll(0)
        self.assertEqual(0, self.game.score())

    def test_all_ones(self):
        for x in range(1, 21):
            self.game.roll(1)
        self.assertEqual(20, self.game.score())

    def test_one_spare(self):
        self.game.roll(9)
        self.game.roll(1)
        self.game.roll(5)
        for x in range(4, 21):
            self.game.roll(0)
        self.assertEqual(20, self.game.score())
Пример #3
0
class BowlingGameTestCase(unittest.TestCase):
    def roll_many(self, amount, pins):
        for i in range(0, amount):
            self.game.roll(pins)

    def test_gutter_game(self):
        self.game = BowlingGame()
        self.roll_many(20, 0)
        self.assertEqual(0, self.game.score())

    def test_all_ones(self):
        self.game = BowlingGame()
        self.roll_many(20, 1)
        self.assertEqual(20, self.game.score())

    def test_one_spare(self):
        self.game = BowlingGame()
        self.game.roll(5)
        self.game.roll(5)
        self.game.roll(3)
        self.roll_many(17, 0)
        self.assertEqual(16, self.game.score())
Пример #4
0
class TestBowlingGame(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()

    def test_score_before_any_rolls(self):
        self.assertEqual(self.game.score(), 0)

    @hypothesis.given(hypothesis.strategies.integers(0, 10))
    def test_single_roll(self, x):
        self.game = BowlingGame()
        self.assertEqual(self.game.score(), 0)
        self.game.roll(x)
        self.assertEqual(self.game.score(), x)

    def test_two_rolls(self):
        self.game.roll(3)
        self.game.roll(2)
        self.assertEqual(self.game.score(), 5)

    @hypothesis.given(hypothesis.strategies.integers(0, 10))
    def test_spare(self, x):
        self.game = BowlingGame()
        self.game.roll(4)
        self.game.roll(6)
        self.game.roll(x)
        self.assertEqual(self.game.score(), 10 + 2 * x)

    def test_fake_spare(self):
        self.game.roll(3)
        self.game.roll(5)
        self.game.roll(5)
        self.assertEqual(self.game.score(), 13)
        self.game.roll(3)
        self.assertEqual(self.game.score(), 16)

    def test_strike(self):
        self.game.roll(10)
        self.assertEqual(self.game.score(), 10)

    def test_strike_and_next_roll(self):
        self.game.roll(10)
        self.game.roll(5)
        self.assertEqual(self.game.score(), 20)