class Tests(unittest.TestCase): def setUp(self): self.game = BowlingGame() def do_rolls(self, n): for i in rolls[:n]: self.game.roll(i) def test_no_rolls(self): self.assertEqual(0, self.game.score()) def test_one_roll(self): self.do_rolls(1) self.assertEqual(1, self.game.score()) def test_two_rolls(self): self.do_rolls(2) self.assertEqual(5, self.game.score()) def test_four_rolls(self): self.do_rolls(4) self.assertEqual(14, self.game.score()) def test_rolls_including_spare(self): self.do_rolls(7) self.assertEqual(34, self.game.score()) def test_full_game(self): self.do_rolls(len(rolls)) self.assertEqual(133, self.game.score()) def test_perfect_game(self): for i in range(22): self.game.roll(10) self.assertEqual(300, self.game.score())
def test_score_only_misses(self): """ Own Test 1: Test only misses. """ print("Own Test 1: Test only misses.") test_game = BowlingGame("- - - - - - - - - -") self.assertEqual(test_game.score(), 0)
def test_score_only_splits(self): """ Example Test 3: Test splits. """ print("Example Test 3: Test spare.") test_game = BowlingGame("5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5") self.assertEqual(test_game.score(), 150)
def test_score_following_misses(self): """ Example Test 2: Test nines. """ print("Example Test 2: Test nines.") test_game = BowlingGame("9- 9- 9- 9- 9- 9- 9- 9- 9- 9-") self.assertEqual(test_game.score(), 90)
def test_score_only_strikes(self): """ Example Test 1: Test strikes. """ print("Example Test 1: Test strikes.") test_game = BowlingGame("X X X X X X X X X X X X") self.assertEqual(test_game.score(), 300)
def test_score_preceding_misses(self): """ Own Test 7: Test misses preceding hits. """ print("Own Test 7: Test misses preceding hits.") # 10*2 = 20 test_game = BowlingGame("-2 -2 -2 -2 -2 -2 -2 -2 -2 -2") self.assertEqual(test_game.score(), 20)
def test_score_single_miss(self): """ Own Test 6: Test a single miss. """ print("Own Test 6: Test a single miss.") # 0 + 9*3 = 27 test_game = BowlingGame("- 12 12 12 12 12 12 12 12 12") self.assertEqual(test_game.score(), 27)
def test_score_second_to_last_strike(self): """ Own Test 8: Test with a strike second to last. """ print("Own Test 8: Test with a strike second to last.") # 8*3 + 10 + 2*3 = 40 test_game = BowlingGame("12 12 12 12 12 12 12 12 X 12") self.assertEqual(test_game.score(), 40)
def test_score_single_spare(self): """ Own Test 4: Test a single spare. """ print("Own Test 4: Test a single spare.") # (10 + 1) + 9*3 = 38 test_game = BowlingGame("1/ 12 12 12 12 12 12 12 12 12") self.assertEqual(test_game.score(), 38)
def test_score_no_special_cases(self): """ Own Test 3: Test no special cases like strikes, spares, misses. """ print("Own Test 3: Test no special cases like strikes, spares, misses.") # 10*3 = 30 test_game = BowlingGame("12 12 12 12 12 12 12 12 12 12") self.assertEqual(test_game.score(), 30)
def test_score_mixed_cases(self): """ Own Test 2: Test mixed cases, i.e. a mix of spares, strikes and misses. """ print("Own Test 2: Test mixed cases, i.e. a mix of spares, strikes and misses.") # 0 + (10+10) + (10+0+0) + 0 + 7 + (10+6) + (10+10) + (10 + 0+0) + (10+0) = 93 test_game = BowlingGame("- 2/ X - 52 4/ 6/ X - 8/ -") self.assertEqual(test_game.score(), 93)
def test_score_single_strike(self): """ Own Test 5: Test a single strike. """ print("Own Test 5: Test a single strike.") # (10 + 3) + 9*3 = 40 test_game = BowlingGame("X 12 12 12 12 12 12 12 12 12") self.assertEqual(test_game.score(), 40)
class BowlingTests(unittest.TestCase): def setUp(self): """setup test""" self.game = BowlingGame() def roll(self, pins): """rolls once""" self.rollMany(1, pins) def rollMany(self, times, pins): """rolls many""" for i in range(times): self.game.roll(pins) def testGameExists(self): """can create a game""" game = BowlingGame() def testGutterGame(self): """score 0 for gutter game""" self.rollMany(20, 0) assert self.game.score() == 0 def testAllOnes(self): """score 20 for all ones game""" self.rollMany(20, 1) assert self.game.score() == 20 def testOneSpare(self): """score 16 for one spare""" self.roll(3) self.roll(7) self.roll(3) self.rollMany(17, 0) assert self.game.score() == 16 def testOneStrike(self): """score 24 for one strike""" self.roll(10) self.roll(3) self.roll(4) self.rollMany(16, 0) assert self.game.score() == 24
class BowlingGameTests(unittest.TestCase): def setUp(self): self.game = BowlingGame() def roll_many_times(self, pins, rolls): for _ in range(rolls): self.game.roll(pins) def roll_spare(self): self.game.roll(5) self.game.roll(5) def roll_strike(self): self.game.roll(10) def test_can_create_BowlingGame(self): pass def test_when_all_throws_in_gutter_then_score_should_be_zero(self): self.roll_many_times(pins=0, rolls=20) self.assertEqual(self.game.score(), 0) def test_when_no_bonus_points_then_score_should_be_sum_of_rolls(self): self.roll_many_times(pins=1, rolls=20) self.assertEqual(self.game.score(), 20) def test_when_spare_then_bonus_for_next_roll(self): self.roll_spare() self.roll_many_times(pins=1, rolls=18) self.assertEqual(self.game.score(), 11 + 18) def test_when_strike_then_bonus_for_two_next_rolls(self): self.roll_strike() self.roll_many_times(pins=1, rolls=18) self.assertEqual(self.game.score(), 12 + 18) def test_when_perfect_game_then_score_should_be_300(self): self.roll_many_times(pins=10, rolls=12) self.assertEqual(self.game.score(), 300)
class TestBowlingGame(unittest.TestCase): def setUp(self): self.game = BowlingGame() def test_gutter_game(self): self.roll_many(0, 20) self.assertEqual(self.game.score(), 0) def test_all_ones(self): self.roll_many(1, 20) self.assertEqual(self.game.score(), 20) def test_one_spare(self): self.game.roll(5) self.game.roll(5) self.game.roll(3) self.roll_many(0, 17) self.assertEqual(self.game.score(), 16) def test_one_strike(self): self.game.roll(10) self.game.roll(4) self.game.roll(3) self.roll_many(0, 16) self.assertEqual(self.game.score(), 16) def test_perfect_game(self): self.roll_many(10, 12) self.assertEqual(self.game.score(), 300) def test_all_spares(self): self.roll_many(5, 21) self.assertEqual(self.game.score(), 150) def roll_many(self, pins, rolls): for i in range(rolls): self.game.roll(pins)
class BowlingTests(unittest.TestCase): def setUp(self): self.game = BowlingGame() def roll(self, rolls): [self.game.roll(roll) for roll in rolls] def roll_and_score(self, rolls): self.roll(rolls) return self.game.score() def test_should_be_able_to_score_a_game_with_all_zeros(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 0) def test_should_be_able_to_score_a_game_with_no_strikes_or_spares(self): rolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6] score = self.roll_and_score(rolls) self.assertEqual(score, 90) def test_a_spare_follow_by_zeros_is_worth_ten_points(self): rolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 10) def test_points_scored_in_the_roll_after_a_spare_are_counted_twice(self): rolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 16) def test_consecutive_spares_each_get_a_one_roll_bonus(self): rolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 31) def test_last_frame_spare_gets_bonus_roll_that_is_counted_twice(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7] score = self.roll_and_score(rolls) self.assertEqual(score, 17) def test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll(self): rolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 10) def test_two_rolls_points_after_strike_are_counted_twice(self): rolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 26) def test_consecutive_stikes_each_get_the_two_roll_bonus(self): rolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 81) def test_strike_in_last_frame_gets_two_roll_bonus_counted_once(self): rolls = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1 ] score = self.roll_and_score(rolls) self.assertEqual(score, 18) def test_rolling_spare_with_bonus_roll_does_not_get_bonus(self): rolls = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3 ] score = self.roll_and_score(rolls) self.assertEqual(score, 20) def test_strikes_with_the_two_bonus_rolls_do_not_get_bonus_rolls(self): rolls = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10 ] score = self.roll_and_score(rolls) self.assertEqual(score, 30) def test_strike_with_bonus_after_spare_in_last_frame_gets_no_bonus(self): rolls = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10 ] score = self.roll_and_score(rolls) self.assertEqual(score, 20) def test_all_strikes_is_a_perfect_game(self): rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] score = self.roll_and_score(rolls) self.assertEqual(score, 300) def test_rolls_cannot_score_negative_points(self): self.assertRaises(ValueError, self.game.roll, -11) def test_a_roll_cannot_score_more_than_10_points(self): self.assertRaises(ValueError, self.game.roll, 11) def test_two_rolls_in_a_frame_cannot_score_more_than_10_points(self): self.game.roll(5) self.assertRaises(ValueError, self.game.roll, 6) def test_bonus_after_strike_in_last_frame_cannot_score_more_than_10(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10] self.roll(rolls) self.assertRaises(ValueError, self.game.roll, 11) def test_bonus_aft_last_frame_strk_can_be_more_than_10_if_1_is_strk(self): rolls = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6 ] score = self.roll_and_score(rolls) self.assertEqual(score, 26) def test_bonus_aft_last_frame_strk_cnt_be_strk_if_first_is_not_strk(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6] self.roll(rolls) self.assertRaises(ValueError, self.game.roll, 10) def test_an_incomplete_game_cannot_be_scored(self): rolls = [0, 0] self.roll(rolls) self.assertRaises(IndexError, self.game.score) def test_cannot_roll_if_there_are_already_ten_frames(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] self.roll(rolls) self.assertRaises(IndexError, self.game.roll, 0) def test_bonus_rolls_for_strike_must_be_rolled_before_score_is_calc(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10] self.roll(rolls) self.assertRaises(IndexError, self.game.score) def test_both_bonuses_for_strike_must_be_rolled_before_score(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10] self.roll(rolls) self.assertRaises(IndexError, self.game.score) def test_bonus_rolls_for_spare_must_be_rolled_before_score_is_calc(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3] self.roll(rolls) self.assertRaises(IndexError, self.game.score)
def test_score_output_type(self): """ Own Test 9: Test output type. """ test_game = BowlingGame("12 12 12 12 12 12 12 12 X 12") self.assertIsInstance(test_game.score(), int)
class BowlingTests(unittest.TestCase): def setUp(self): self.game = BowlingGame() def roll(self, rolls): [self.game.roll(roll) for roll in rolls] def roll_and_score(self, rolls): self.roll(rolls) return self.game.score() def test_should_be_able_to_score_a_game_with_all_zeros(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 0) def test_should_be_able_to_score_a_game_with_no_strikes_or_spares(self): rolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6] score = self.roll_and_score(rolls) self.assertEqual(score, 90) def test_a_spare_follow_by_zeros_is_worth_ten_points(self): rolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 10) def test_points_scored_in_the_roll_after_a_spare_are_counted_twice(self): rolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 16) def test_consecutive_spares_each_get_a_one_roll_bonus(self): rolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 31) def test_last_frame_spare_gets_bonus_roll_that_is_counted_twice(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7] score = self.roll_and_score(rolls) self.assertEqual(score, 17) def test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll(self): rolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 10) def test_two_rolls_points_after_strike_are_counted_twice(self): rolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 26) def test_consecutive_stikes_each_get_the_two_roll_bonus(self): rolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score = self.roll_and_score(rolls) self.assertEqual(score, 81) def test_strike_in_last_frame_gets_two_roll_bonus_counted_once(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1] score = self.roll_and_score(rolls) self.assertEqual(score, 18) def test_rolling_spare_with_bonus_roll_does_not_get_bonus(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3] score = self.roll_and_score(rolls) self.assertEqual(score, 20) def test_strikes_with_the_two_bonus_rolls_do_not_get_bonus_rolls(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10] score = self.roll_and_score(rolls) self.assertEqual(score, 30) def test_strike_with_bonus_after_spare_in_last_frame_gets_no_bonus(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10] score = self.roll_and_score(rolls) self.assertEqual(score, 20) def test_all_strikes_is_a_perfect_game(self): rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] score = self.roll_and_score(rolls) self.assertEqual(score, 300) def test_rolls_cannot_score_negative_points(self): self.assertRaises(ValueError, self.game.roll, -11) def test_a_roll_cannot_score_more_than_10_points(self): self.assertRaises(ValueError, self.game.roll, 11) def test_two_rolls_in_a_frame_cannot_score_more_than_10_points(self): self.game.roll(5) self.assertRaises(ValueError, self.game.roll, 6) def test_bonus_after_strike_in_last_frame_cannot_score_more_than_10(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10] self.roll(rolls) self.assertRaises(ValueError, self.game.roll, 11) def test_bonus_aft_last_frame_strk_can_be_more_than_10_if_1_is_strk(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6] score = self.roll_and_score(rolls) self.assertEqual(score, 26) def test_bonus_aft_last_frame_strk_cnt_be_strk_if_first_is_not_strk(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6] self.roll(rolls) self.assertRaises(ValueError, self.game.roll, 10) def test_an_incomplete_game_cannot_be_scored(self): rolls = [0, 0] self.roll(rolls) self.assertRaises(IndexError, self.game.score) def test_cannot_roll_if_there_are_already_ten_frames(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] self.roll(rolls) self.assertRaises(IndexError, self.game.roll, 0) def test_bonus_rolls_for_strike_must_be_rolled_before_score_is_calc(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10] self.roll(rolls) self.assertRaises(IndexError, self.game.score) def test_both_bonuses_for_strike_must_be_rolled_before_score(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10] self.roll(rolls) self.assertRaises(IndexError, self.game.score) def test_bonus_rolls_for_spare_must_be_rolled_before_score_is_calc(self): rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3] self.roll(rolls) self.assertRaises(IndexError, self.game.score)
def main(): new_bowling_game = BowlingGame("1- 2- 3- 4- 5- 6- 7- 8- 9- 9-") print(new_bowling_game.score())