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)
def test_result_if_there_is_one_stpare(self): game = BowlingGame( [1, 4, 1, 2, 5, 5, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) res = game.result() self.assertEqual(res, 21)
def test_given_index_from_differen_frames_return_false(self): game = BowlingGame( [3, 1, 7, 3, 2, 3, 5, 1, 1, 0, 1, 2, 3, 1, 4, 3, 2, 1, 6, 1]) res = game._is_spare(2) self.assertEqual(res, False)
def test_return_true_if_it_is_not_spare(self): game = BowlingGame( [3, 1, 2, 3, 2, 3, 5, 1, 1, 0, 1, 2, 3, 1, 4, 3, 2, 1, 6, 1]) res = game._is_spare(3) self.assertEqual(res, False)
def test_return_true_if_it_is_strike(self): game = BowlingGame( [3, 1, 10, 3, 2, 3, 5, 1, 1, 0, 1, 2, 3, 1, 4, 3, 2, 1, 6]) res = game._is_strike(2) self.assertEqual(res, True)
def test_result_if_there_is_one_strike(self): game = BowlingGame( [1, 4, 10, 1, 2, 3, 5, 1, 1, 0, 1, 2, 3, 1, 4, 3, 2, 1, 6]) res = game.result() self.assertEqual(res, 54)
def test_result_if_there_are_many_stpares_in_a_row(self): game = BowlingGame( [6, 4, 3, 7, 3, 0, 5, 5, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) res = game.result() self.assertEqual(res, 42)
def test_result_if_there_is_a_spare_in_the_tenth_frame(self): game = BowlingGame( [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1]) res = game.result() self.assertEqual(res, 29)
def test_result_if_there_are_many_strikes(self): game = BowlingGame( [1, 4, 10, 1, 2, 10, 1, 1, 1, 0, 10, 1, 2, 1, 1, 1, 1]) res = game.result() self.assertEqual(res, 56)
def test_result_if_there_are_no_strikes_and_spares(self): game = BowlingGame( [1, 4, 4, 5, 6, 3, 5, 1, 1, 0, 1, 7, 3, 6, 4, 3, 2, 1, 6, 2]) res = game.result() self.assertEqual(res, 65)
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())
def test_with_empty_list_raise_error(self): exc = None try: BowlingGame.validate([]) except Exception as err: exc = err self.assertIsNotNone(str(exc)) self.assertEqual(str(exc), 'Invalid number of frames')
def test_with_invalid_numbers_of_frames_raise_error(self): exc = None try: BowlingGame.validate([5, 1, 1, 0, 1, 7, 3, 6, 4, 3, 2, 1, 6]) except Exception as err: exc = err self.assertIsNotNone(str(exc)) self.assertEqual(str(exc), 'Invalid number of frames')
def test_with_invalid_numbers_of_frames_with_strike_not_in_the_tenth_frame_raise_error( self): exc = None try: BowlingGame.validate( [5, 1, 10, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) except Exception as err: exc = err self.assertIsNotNone(str(exc)) self.assertEqual(str(exc), 'Invalid number of frames')
def test_bowling_game_str_representation_is_as_expected(self): test_argument = [1, 2, 3, 1, 4, 6, 1, 8, 2, 3] test_game = BowlingGame(test_argument) self.assertEqual( str(test_game), '[Open Frame] [Open Frame] [Spare] [Open Frame] [Open Frame]')
def test_if_number_of_elements_is_lower_than_11_raise_TypeError(self): entry = [1, 2, 0, 0, 0, 0, 0, 0, 0, 0] exc = None try: game = BowlingGame(entry) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Elements need to be between 11 and 21')
def test_if_entry_is_not_a_list_should_raise_TypeError(self): entry = 123 exc = None try: game = BowlingGame(entry) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Entry should be a list')
def test_if_elements_are_bigger_than_10_should_raise_ValueError(self): entry = [1, 2, 10, 12, 0, 0, 0, 0, 0, 0, 0, 0] exc = None try: game = BowlingGame(entry) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Elements need to be between 0 and 10')
def test_initialization_with_incorrect_values(self): input = [3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 7, 8] exc = None try: BowlingGame(input) except Exception as err: exc = err self.assertIsNotNone(exc)
def test_if_elements_in_entry_are_negative_integers_should_raise_ValueError( self): entry = [1, 2, -4, 0, 0, 0, 0, 0, 0, 0, 0] exc = None try: game = BowlingGame(entry) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Elements need to be positive integers')
def test_bowling_game_init_initializes_object_as_expected(self): test_argument = [1, 2, 3, 1, 4, 6, 1, 8, 2, 3] test_game = BowlingGame(test_argument) self.assertEqual( getattr(test_game, 'frames'), [Frame(1, 2), Frame(3, 1), Frame(4, 6), Frame(1, 8), Frame(2, 3)])
def test_initialization_with_correct_values(self): input = [ 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 10, 10, 10 ] wanted_result = [[3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [10, 0], [10, 0], [10, 0]] bowling_game = BowlingGame(input) result = bowling_game.frames self.assertEqual(wanted_result, result)
def test_bowling_game_validation_raises_excpetion_if_argument_is_not_list( self): test_argument = (1, 2, 3, 1, 4, 6, 1, 8, 2, 3) exc = None try: test_game = BowlingGame(test_argument) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Argument must be of "list" type.')
def test_bowling_game_validation_raises_exception_if_less_than_10_frames( self): test_argument = [1, 2, 3, 1, 4, 6, 1, 8, 2] exc = None try: test_game = BowlingGame(test_argument) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Cannot have less than 10 throws.')
def test_bowling_game_validation_raises_exception_if_elem_in_list_not_int( self): test_argument = [1, 2, 3, 1, 4, 6, 1, 8, 2, '3'] exc = None try: test_game = BowlingGame(test_argument) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Number of knocked pins must be of "int" type.')
def test_bowling_game_validation_raises_exception_if_more_than_20_frames( self): test_argument = [ 1, 4, 4, 5, 6, 3, 5, 1, 1, 0, 1, 7, 3, 6, 4, 3, 2, 1, 6, 2, 6 ] exc = None try: test_game = BowlingGame(test_argument) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Cannot have more than 20 throws.')
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())
def setUp(self): self.game = BowlingGame()
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())
def post(self): keys = list(games.keys()) game_id = 0 if len(keys) == 0 else max(keys) + 1 games[game_id] = BowlingGame() return {'game_id': game_id}, 201