def test_play(self): """ Tests if the play build up correctly """ game = BowlingGame(['test']) game.play([2, 3, 6, 4, 8, 1, 10, 3, 0, 10, 10, 10, 10, 1, 9, 5], verbose=False) self.assertEqual(len(game.frames['test']), 10) self.assertEqual(BowlingGame.computeScoreOnFrames(game.frames['test']), 164)
def test_str(self): """ Tests if the string representation of the game is correct """ frame1 = BowlingFrame("test") frame2 = BowlingFrame("test") game = BowlingGame(['test']) game.frames = {"test": [frame1, frame2]} representation = 'test\n' + frame1.__str__() + ' ' + frame2.__str__() + ' \n' + \ frame1.__str__(True) + ' ' + frame2.__str__(True) + ' \n' self.assertEqual(str(game).strip(), representation.strip())
def test_compute_incomplete_score_on_frames(self): """ Tests if the score is computed correctly on an incomplete game """ frames = [] frame = BowlingFrame("test") frame.registerThrowing(2) frame.registerThrowing(3) frames.append(frame) frame = BowlingFrame("test") frame.registerThrowing(6) frame.registerThrowing(4) frames.append(frame) # Spare frame = BowlingFrame("test") frame.registerThrowing(8) frame.registerThrowing(1) frames.append(frame) frame = BowlingFrame("test") frame.registerThrowing(10) frames.append(frame) # Strike frame = BowlingFrame("test") frame.registerThrowing(3) frame.registerThrowing(0) frames.append(frame) self.assertEqual(BowlingGame.computeScoreOnFrames(frames), 48)
class BowlingTest(unittest.TestCase): bowling_game = BowlingGame() def setup(self): self.bowling_game = BowlingGame() def rollPinsMany(self, pins, howMany): for i in range(1, howMany + 1): self.bowling_game.roll(pins) def testScore0_WhenAllRollsAreMissed(self): self.rollPinsMany(0, 20) assert self.bowling_game.score() == 0 def testScore20_OnePinEachBall(self): self.rollPinsMany(1, 20) print(self.bowling_game.score()) assert self.bowling_game.score() == 20
def test_compute_max_score_on_frames(self): """ Tests if the maximum score is computed correctly """ frames = [] for _ in range(9): frame = BowlingFrame("test") frame.registerThrowing(10) frames.append(frame) frame = BowlingFrame("test", ending=True) # Three strikes are allowed in the ending frame frame.registerThrowing(10) frame.registerThrowing(10) frame.registerThrowing(10) frames.append(frame) self.assertEqual(BowlingGame.computeScoreOnFrames(frames), 300)
def test_compute_undefined_score_on_frames(self): """ Tests if the score is undefined on an incomplete game ending on a strike """ frames = [] frame = BowlingFrame("test") frame.registerThrowing(2) frame.registerThrowing(3) frames.append(frame) frame = BowlingFrame("test") frame.registerThrowing(6) frame.registerThrowing(4) frames.append(frame) # Spare frame = BowlingFrame("test") frame.registerThrowing(8) frame.registerThrowing(1) frames.append(frame) frame = BowlingFrame("test") frame.registerThrowing(10) frames.append(frame) # Strike self.assertEqual(BowlingGame.computeScoreOnFrames(frames), None)
def setUp(self): self.game = BowlingGame()
class BowlingTests(TestCase): def setUp(self): self.game = BowlingGame() def roll_many(self, pins, times): for _ in range(times): self.game.throw(pins) def test_starts_all_rolls_with_empty_list(self): self.assertEqual([], self.game.all_rolls) def test_starts_game_with_first_frame(self): self.assertEqual([self.game.active_frame], self.game.frames) def test_throw_adds_to_rolls(self): self.game.all_rolls = [10] self.game.throw(1) self.assertEqual([10, 1], self.game.all_rolls) def test_scores_zero_score_game(self): self.roll_many(0, 20) self.assertEqual(0, self.game.score_for_frame()) def test_scores_all_ones(self): self.roll_many(1, 20) self.assertEqual(20, self.game.score_for_frame()) def test_scores_spare_properly(self): self.roll_many(5, 3) self.roll_many(0, 17) self.assertEqual(20, self.game.score_for_frame()) def test_scores_perfect_game(self): self.roll_many(10, 12) self.assertEqual(300, self.game.score_for_frame()) def test_scores_strike_and_other(self): self.game.throw(10) self.roll_many(3, 2) self.roll_many(0, 16) self.assertEqual(22, self.game.score_for_frame()) def test_scores_all_strikes_and_spare_in_tenth(self): self.roll_many(10, 9) self.game.throw(8) self.game.throw(2) self.game.throw(10) self.assertEqual(278, self.game.score_for_frame()) def test_add_new_frame_adds_frame_to_frames_list(self): # note, instantiating the game starts the first frame self.game.add_new_frame() self.assertEqual(2, len(self.game.frames)) self.assertTrue(isinstance(self.game.frames[1], Frame)) def test_add_new_frame_sets_active_frame_to_new_frame(self): existing_frame = Frame(1) self.game.frames = [existing_frame] self.game.add_new_frame() new_frame = self.game.frames[-1] self.assertEqual(new_frame, self.game.active_frame) self.assertEqual(False, new_frame.is_tenth) self.assertNotEqual(existing_frame, self.game.active_frame) def test_add_new_frame_sends_frame_number_to_frame(self): self.assertEqual(1, self.game.active_frame.number) self.game.add_new_frame() self.assertEqual(2, self.game.active_frame.number) self.game.add_new_frame() self.assertEqual(3, self.game.active_frame.number) def test_adds_new_frame_on_throw_when_active_frame_is_complete(self): active_frame = Frame(1) active_frame.rolls = [1,2] self.game.frames = [active_frame] self.game.active_frame = active_frame self.game.throw(5) new_frame = self.game.frames[-1] self.assertEqual(2, len(self.game.frames)) self.assertEqual([5], new_frame.rolls) self.assertEqual(new_frame, self.game.active_frame) def test_throw_adds_pins_to_active_frame(self): active_frame = Frame(1) self.game.active_frame = active_frame self.game.throw(1) self.game.throw(5) self.assertEqual([1, 5], active_frame.rolls) def test_game_has_more_throws_when_active_frame_is_not_tenth(self): active_frame = Frame(1) self.game.active_frame = active_frame self.assertEqual(True, self.game.more_throws) def test_game_has_more_throws_when_tenth_frame_but_not_complete(self): active_frame = Frame(10) self.game.active_frame = active_frame self.assertEqual(True, self.game.more_throws) def test_game_does_not_have_more_throws_when_active_frame_is_tenth_and_is_complete(self): active_frame = Frame(10) active_frame.rolls = [10, 10, 10] self.game.active_frame = active_frame self.assertEqual(False, self.game.more_throws) def test_current_frame_returns_active_frame_number_when_not_complete(self): active_frame = Frame(1) active_frame.rolls = [5] self.game.active_frame = active_frame self.assertEqual(1, self.game.current_frame) def test_current_frame_returns_active_frame_plus_one_when_already_complete(self): active_frame = Frame(1) active_frame.rolls = [10] self.game.active_frame = active_frame self.assertEqual(2, self.game.current_frame) def test_pins_for_frame_returns_frame_rolls_for_corresponding_frame(self): frame_rolls = [1, 9] self.game.active_frame.rolls = frame_rolls self.assertEqual(frame_rolls, self.game.pins_for_frame(1)) def test_pins_for_frame_returns_frame_rolls_on_arbitrary_frame(self): self.game.frames = [Frame(x) for x in range(1,11)] frame_rolls = [3, 5] self.game.frames[5].rolls = frame_rolls self.assertEqual(frame_rolls, self.game.pins_for_frame(6))
import sys from bowling.game import BowlingGame if __name__ == '__main__': throwing = [] if len(sys.argv) > 1: throwing = [int(arg) for arg in sys.argv[1:]] print(BowlingGame.computeScore(throwing))
import sys from bowling.game import BowlingGame if __name__ == '__main__': players = ["Player1"] if len(sys.argv) > 1: players = sys.argv[1:] game = BowlingGame(players) game.play()
def setup(self): self.bowling_game = BowlingGame()
def test_score_empty_game(self): """ Tests if the score of an empty game is 0 """ self.assertEqual(BowlingGame.computeScoreOnFrames([]), 0) self.assertEqual(BowlingGame.computeScore([]), 0)
def test_compute_score(self): """ Tests if the score is computed correctly without frames on a typical game """ pins = [2, 3, 6, 4, 8, 1, 10, 3, 0, 10, 10, 10, 10, 1, 9, 5] self.assertEqual(BowlingGame.computeScore(pins), 164)
def test_compute_max_score(self): """ Tests if the maximum score is computed correctly without frames """ pins = [10 for _ in range(12)] self.assertEqual(BowlingGame.computeScore(pins), 300)
class BowlingGameTest(unittest.TestCase): def setUp(self): self.game = BowlingGame() def testGutterGame(self): self.rollMany(0, 20) assert self.game.score() == 0 def testAllOnes(self): self.rollMany(1, 20) assert self.game.score()== 20 def testOneSpare(self): self.game.roll(5) self.game.roll(5) self.game.roll(3) self.rollMany(0, 17) assert self.game.score() == 16 def testOneStrike(self): self.game.roll(10) self.game.roll(3) self.game.roll(4) self.rollMany(0, 16) assert self.game.score() == 24 def testPerfectGame(self): self.rollMany(10, 12) assert self.game.score() == 300 def testAllSpares(self): self.rollMany(5, 21) assert self.game.score() == 150 def rollMany(self, pins, rolls): for i in range(rolls): self.game.roll(pins)
def game(): yield BowlingGame()