Advertisement
Archon

TDD demo: tests

Aug 25th, 2021
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  1. from bowling import Game
  2.  
  3. class GameFixture:
  4.     def __init__(self):
  5.         self.game = Game()
  6.         self.num_rolls = 0
  7.        
  8.     def roll(self, pins):
  9.         self.num_rolls = self.num_rolls + 1
  10.         self.game.roll(pins)
  11.        
  12.     def score(self):
  13.         return self.game.score()
  14.    
  15.     def finish_game_with_zeroes(self):
  16.         rolls_left = 20 - self.num_rolls
  17.         for i in range(rolls_left):
  18.             self.game.roll(0)
  19.            
  20.     def is_in_error_state(self):
  21.         return self.game.score() == self.game.error
  22.    
  23.  
  24. def test_zero_game():
  25.     game = GameFixture()
  26.     game.finish_game_with_zeroes()
  27.     assert game.score() == 0
  28.    
  29.    
  30. def test_all_ones():
  31.     game = Game()
  32.     for i in range(20):
  33.         game.roll(1)
  34.     assert game.score() == 20
  35.    
  36.    
  37. def test_one():
  38.     game = GameFixture()
  39.     game.roll(1)
  40.     game.finish_game_with_zeroes()
  41.     assert game.score() == 1
  42.    
  43.    
  44. def test_too_many_pins():
  45.     game = GameFixture()
  46.     for i in range(20):
  47.         game.roll(50)
  48.     assert game.is_in_error_state()
  49.    
  50.     # Error state persists after bad roll
  51.     game = GameFixture()
  52.     game.roll(11)
  53.     game.finish_game_with_zeroes()
  54.     assert game.is_in_error_state()
  55.    
  56.     # Error state persists even when subsequent rolls are good
  57.     game = GameFixture()
  58.     game.roll(11)
  59.     game.roll(1)
  60.     game.finish_game_with_zeroes()
  61.     assert game.is_in_error_state()
  62.    
  63.    
  64. def test_too_few_rolls():
  65.     game = GameFixture()
  66.    
  67.     # When one roll is made
  68.     game.roll(0)
  69.    
  70.     # Then the game can't be scored
  71.     assert game.is_in_error_state()
  72.    
  73.     # And when a second roll is made
  74.     game.roll(0)
  75.    
  76.     # Then the game still can't be scored
  77.     assert game.is_in_error_state()
  78.  
  79.    
  80. def test_too_many_rolls():
  81.     # Given a game with more 21 rolls (and no expectation of a 21st roll)
  82.     game = GameFixture()
  83.     for i in range(21):
  84.         game.roll(0)
  85.        
  86.     # Then the game can't be scored
  87.     assert game.is_in_error_state()
  88.    
  89.    
  90. def test_that_frame_1_doesnt_exceed_10():
  91.     # Given a game
  92.     game = GameFixture()
  93.    
  94.     # When the first two rolls add up to more than 10
  95.     game.roll(7)
  96.     game.roll(7)
  97.     game.finish_game_with_zeroes()
  98.    
  99.     # Then the game is in an error state
  100.     assert game.is_in_error_state()
  101.    
  102.    
  103. def test_that_frame_2_doesnt_exceed_10():
  104.     # Given a game with two rolls already made
  105.     game = GameFixture()
  106.     game.roll(0)
  107.     game.roll(0)
  108.    
  109.     # When the next two rolls add up to more than 10
  110.     game.roll(7)
  111.     game.roll(7)
  112.     game.finish_game_with_zeroes()
  113.    
  114.     # Then the game is in an error state
  115.     assert game.is_in_error_state()
  116.    
  117.    
  118. def test_that_frame_3_doesnt_exceed_10():
  119.     # Given a game with four rolls already made
  120.     game = GameFixture()
  121.     game.roll(0)
  122.     game.roll(0)
  123.     game.roll(0)
  124.     game.roll(0)
  125.    
  126.     # When the next two rolls add up to more than 10
  127.     game.roll(7)
  128.     game.roll(7)
  129.     game.finish_game_with_zeroes()
  130.    
  131.     # Then the game is in an error state
  132.     assert game.is_in_error_state()
  133.    
  134.    
  135. def test_frame_2_validity_doesnt_count_frame_1():
  136.     # Given a game with two valid frames totalling >10 pins
  137.     game = GameFixture()
  138.     game.roll(5)
  139.     game.roll(5)
  140.     game.roll(5)
  141.     game.roll(5)
  142.    
  143.     # When the game finishes
  144.     game.finish_game_with_zeroes()
  145.    
  146.     # Then the game is in an error state
  147.     assert not game.is_in_error_state()
  148.    
  149.    
  150. def test_frame_3_validity_doesnt_count_frame_2():
  151.     # Given a game with three valid frames where frame 2 and 3 total >10 pins
  152.     game = GameFixture()
  153.     game.roll(0)
  154.     game.roll(0)
  155.     game.roll(5)
  156.     game.roll(5)
  157.     game.roll(5)
  158.     game.roll(5)
  159.    
  160.     # When the game finishes
  161.     game.finish_game_with_zeroes()
  162.    
  163.     # Then the game is in an error state
  164.     assert not game.is_in_error_state()
  165.    
  166. def test_spares():
  167.     test_next_pins = [1, 2]
  168.     for next_pin in test_next_pins:
  169.    
  170.         # Given a game in which the first frame is a spare
  171.         game = GameFixture()
  172.         game.roll(6)
  173.         game.roll(4)
  174.  
  175.         # When the next pin is n, then nothing else is scored
  176.         game.roll(next_pin)
  177.         game.finish_game_with_zeroes()
  178.  
  179.         # Then the final score is (10 + n + spare bonus of n)
  180.         assert game.score() == 10 + 2 * next_pin
  181.    
  182. def test_spare_bonus_only_counts_one_extra_roll():
  183.     # Given a game in which the first frame is a spare
  184.     game = GameFixture()
  185.     game.roll(6)
  186.     game.roll(4)
  187.        
  188.     # When the next  two pins are 1s
  189.     game.roll(1)
  190.     game.roll(1)
  191.     game.finish_game_with_zeroes()
  192.    
  193.     # Then only the third roll is doubled (i.e., the score is 13)
  194.     assert game.score() == 13
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement