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())
Example #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())
Example #4
0
class BowlingGameTest(unittest.TestCase):
    """Main Class."""
    def setUp(self):
        """Create the Instance of the Game class."""
        self.game = BowlingGame()

    def test_gutter_game(self):
        """Test all missed."""
        self._roll_many(0, 20)
        self.assertEqual(0, self.game.total_score())
        self.game.scoresheet()
        self.game.scoresheet()

    def test_all_ones(self):
        """Test 20 one in a row."""
        self._roll_many(1, 20)
        self.assertEqual(20, self.game.total_score())
        self.game.scoresheet()

    def test_one_spare(self):
        """Test only one Spare."""
        self._roll_spare()
        self.game.roll(3)
        self._roll_many(0, 17)
        self.assertEqual(16, self.game.total_score())
        self.game.scoresheet()

    def test_one_strike(self):
        """Test Only one Strike."""
        self.game.roll(10)
        self.game.roll(3)
        self.game.roll(4)
        self._roll_many(0, 16)
        self.assertEqual(24, self.game.total_score())
        self.game.scoresheet()

    def test_perfect_game(self):
        """Test 12 strike in a row."""
        self._roll_many(10, 12)
        self.assertEqual(300, self.game.total_score())
        self.game.scoresheet()

    def test_simple_game(self):
        """Test Random Game with Stike, Spare."""
        for pins in [1, 4, 4, 5, 6, 4, 5, 5, 10, 0, 1, 7, 3, 6, 4, 10, 2, 7]:
            self.game.roll(pins)
        self.assertEqual(125, self.game.total_score())

    def _roll_many(self, pins, num):
        for i in range(num):
            self.game.roll(pins)

    def _roll_spare(self):
        self.game.roll(5)
        self.game.roll(5)
Example #5
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)