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())
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): """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.bowling_game = BowlingGame() def test_gutter_game(self): #Arrange expected_score = 0 #Act self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) self.bowling_game.roll(0) #Assert self.assertEqual(self.bowling_game.score, expected_score)
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)
def _roll_many(self, rolls): game = BowlingGame() for pins in rolls: game.roll(pins) return game
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)
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 roll_new_game(self, rolls): game = BowlingGame() for roll in rolls: game.roll(roll) return game
class BowlingGameScoringTestCase(TestCase): def setUp(self): self.target = BowlingGame() def test_all_gutter_rolls_result_in_zero_score(self): for i in range(20): self.target.roll(0) self.assertEqual(self.target.score, 0) def test_all_1_rolls_result_in_20_score(self): for i in range(20): self.target.roll(1) self.assertEqual(self.target.score, 20) def test_open_frame_score_is_number_of_pins_knocked_down(self): self.target.roll(3) self.target.roll(3) # end of first frame. open frame. self.target.roll(3) self.assertEqual(self.target.score, 9) def test_spare_frame_score_is_own_score_plus_next_roll(self): self.target.roll(7) self.target.roll(3) # end of first frame. spare. self.target.roll(7) self.target.roll(2) # end of second frame. self.assertEqual(self.target.score, 26) def test_spare_then_3_then_all_misses_score_16(self): self.target.roll(2) self.target.roll(8) self.target.roll(3) for i in range(17): self.target.roll(0) self.assertTrue(self.target.game_state, GameStates.Finished) self.assertEqual(self.target.score, 16) def test_strike_frame_score_is_own_score_plus_next_two_rolls(self): self.target.roll(10) self.assertEqual(self.target.score, 10) self.target.roll(3) self.target.roll(7) self.assertEqual(self.target.score, 20) def test_strike_then_three_then_four_then_all_misses_score_24(self): self.target.roll(10) self.target.roll(3) self.target.roll(4) for i in range(16): self.target.roll(0) self.assertTrue(self.target.game_state, GameStates.Finished) self.assertEqual(self.target.score, 24) def test_perfect_game_scores_300(self): for i in range(12): self.target.roll(10) self.assertEqual(self.target.score, 300) def test_spare_frame_score_not_counted_until_next_throw(self): self.target.roll(7) self.target.roll(3) self.assertEqual(self.target.score, 0) self.target.roll(2) self.assertEqual(self.target.score, 14) def test_strike_in_the_last_frame_gives_two_more_rolls(self): for i in range(18): self.target.roll(0) #self.assertEqual(self.target.score, 0) self.assertEqual(self.target.current_frame_number, 10) self.target.roll(10) #self.assertEqual(self.target.score, 10) self.assertEqual(self.target.game_state, GameStates.InProgress) self.target.roll(10) self.assertEqual(self.target.game_state, GameStates.InProgress) self.assertEqual(self.target.score, 20) self.target.roll(10) self.assertEqual(self.target.game_state, GameStates.Finished) self.assertEqual(self.target.score, 30)
class BowlingGameTestCase(BowlingGameTestCaseBase): def setUp(self): self.target = BowlingGame() def test_game_starts_at_frame_1(self): self.assertEqual(self.target.current_frame_number, 1) def test_game_starts_at_in_progress_state(self): self.assertEqual(self.target.game_state, GameStates.InProgress) def test_game_finishes_after_10_frames(self): for i in range(20): self.target.roll(1) self.assertEqual(self.target.current_frame_number, 10) self.assertEqual(self.target.game_state, GameStates.Finished) def test_cannot_roll_after_game_is_finished(self): for i in range(20): self.target.roll(1) self.assertEqual(self.target.game_state, GameStates.Finished) with self.assertRaises(BowlingGameFinishedException): self.target.roll() def test_game_remembers_last_knocked_down_pin_count(self): self.target.roll(1) self.assertEqual(self.target.last_throw_count, 1) def test_roll_without_specified_score_knocks_down_random_pins(self): self.target.roll() self.assertBetween(self.target.last_throw_count, 0, 10) def test_knock_down_less_than_10_pins_frame_does_not_change(self): self.target.roll(5) self.assertBetween(self.target.current_frame_number, 0, 10) def test_frame_advances_after_two_rolls(self): self.target.roll(5) self.target.roll(2) self.assertEqual(self.target.current_frame_number, 2) def test_strike_in_first_roll_of_frame_ends_frame(self): self.target.roll(10) self.assertEqual(self.target.current_frame_number, 2) def test_bowling_game_str(self): self.assertEqual(str(self.target), '<BowlingGame frm: {0}>'.format(1)) self.target.roll(1) self.target.roll(1) self.assertEqual(str(self.target), '<BowlingGame frm: {0}>'.format(2))