コード例 #1
0
 def test_returns_advantage_player1_when_score_5_4(self):
     # arrange
     tennis_game = TennisGame().with_players(
             Player().called("Rafael").with_points(5),
             Player().called("Roger").with_points(4)
     )
     # act
     actual_score = tennis_game.score
     # assert
     self.assertEqual("advantage Rafael", actual_score)
コード例 #2
0
 def test_returns_fifteen_love_when_score_1_0(self):
     # arrange
     tennis_game = TennisGame().with_players(
             Player().called("Rafael").with_points(1),
             Player().called("Roger").with_points(0)
     )
     # act
     actual_score = tennis_game.score
     # assert
     self.assertEqual("fifteen-love", actual_score)
コード例 #3
0
 def test_returns_deuce_when_score_3_3(self):
     # arrange
     tennis_game = TennisGame().with_players(
             Player().called("Rafael").with_points(3),
             Player().called("Roger").with_points(3)
     )
     # act
     actual_score = tennis_game.score
     # assert
     self.assertEqual("deuce", actual_score)
コード例 #4
0
 def test_returns_player2_wins_when_score_9_11(self):
     # arrange
     tennis_game = TennisGame().with_players(
             Player().called("Rafael").with_points(9),
             Player().called("Roger").with_points(11)
     )
     # act
     actual_score = tennis_game.score
     # assert
     self.assertEqual("Roger wins", actual_score)
コード例 #5
0
 def test_returns_thirty_forty_when_score_2_3(self):
     # arrange
     tennis_game = TennisGame().with_players(
             Player().called("Rafael").with_points(2),
             Player().called("Roger").with_points(3)
     )
     # act
     actual_score = tennis_game.score
     # assert
     self.assertEqual("thirty-forty", actual_score)
コード例 #6
0
def play_game(p1_points, p2_points):
    game = TennisGame("player1", "player2")
    for i in range(max(p1_points, p2_points)):
        if i < p1_points:
            game.won_point("player1")
        if i < p2_points:
            game.won_point("player2")
    return game
コード例 #7
0
def play_out_game(player1_score, player2_score):
    player1_name = "Federer"
    player2_name = "Nadal"
    game = TennisGame(player1_name, player2_name)

    for point in range(max(player1_score, player2_score)):
        if point < player1_score:
            game.won_point(player1_name)
        if point < player2_score:
            game.won_point(player2_name)

    return game
コード例 #8
0
    def test_won_point_returns_next_point_for_zero_points(self):
        tennis_game = TennisGame()
        next_point = tennis_game.wonPoint('player1')

        self.assertTrue(isinstance(next_point, OnePoint))
コード例 #9
0
    def test_get_score_player1_won_point(self):
        score = "Player 1 15, Player 2 Love"
        tennis_game = TennisGame()
        next_point = tennis_game.wonPoint('player1')

        self.assertEqual(score, tennis_game.getScore())
コード例 #10
0
def main():
    game = TennisGame("player1", "player2")

    print(game.get_score())

    game.won_point("player1")
    print(game.get_score())

    game.won_point("player1")
    print(game.get_score())

    game.won_point("player2")
    print(game.get_score())

    game.won_point("player1")
    print(game.get_score())

    game.won_point("player1")
    print(game.get_score())
コード例 #11
0
 def setUp(self):
     self.game = TennisGame()
コード例 #12
0
class TestTennisGame(unittest.TestCase):
    """
    setup before every test
    """
    def setUp(self):
        self.game = TennisGame()

    def test_something(self):
        initial_score = self.game.score
        self.assertEqual("love all", initial_score)

    def test_score_is_15_0_when_p1_scored_once(self):
        self._create_score(1, 0)
        self.assertEqual("fifteen love", self.game.score)

    def test_score_is_0_15_when_p2_scored_once(self):
        self._create_score(0, 1)
        self.assertEqual("love fifteen", self.game.score)

    def test_score_is_15_15_when_p2_scored_once(self):
        self._create_score(1, 1)
        self.assertEqual("fifteen all", self.game.score)

    def test_score_is_30_0_when_p1_scored_twice(self):
        self._create_score(2, 0)
        self.assertEqual("thirty love", self.game.score)

    def test_score_is_0_30_when_p2_scored_twice(self):
        self._create_score(0, 2)
        self.assertEqual("love thirty", self.game.score)

    def test_score_is_0_40_when_p2_scored_three(self):
        self._create_score(0, 3)
        self.assertEqual("love fourty", self.game.score)

    def test_score_is_40_0_when_p1_scored_three(self):
        self._create_score(3, 0)
        self.assertEqual("fourty love", self.game.score)

    def test_score_is_30_15_when_p1_scored_twice_and_p2_scored_once(self):
        self._create_score(2, 1)
        self.assertEqual("thirty fifteen", self.game.score)

    def test_game_for_p1_when_scored_four_times(self):
        self._create_score(4, 0)
        self.assertEqual("Game for P1", self.game.score)

    def test_game_for_p2_when_scored_four_times(self):
        self._create_score(0, 4)
        self.assertEqual("Game for P2", self.game.score)

    def test_deuce(self):
        self._create_score(4, 4)
        self.assertEqual("Deuce", self.game.score)

    def test_deuce_when_both_scored_seven_times(self):
        self._create_score(7, 7)
        self.assertEqual("Deuce", self.game.score)

    def test_advantage_p1_when_score_5_4(self):
        self._create_score(5, 4)
        self.assertEqual("Advantage P1", self.game.score)

    def test_advantage_p2_when_score_4_5(self):
        self._create_score(4, 5)
        self.assertEqual("Advantage P2", self.game.score)

    def test_advantage_p1_when_score_12_11(self):
        self._create_score(12, 11)
        self.assertEqual("Advantage P1", self.game.score)

    def test_game_for_p1_after_deuce(self):
        self._create_score(6, 4)
        self.assertEqual("Game for P1", self.game.score)

    def test_game_for_p2_after_deuce(self):
        self._create_score(4, 6)
        self.assertEqual("Game for P2", self.game.score)

    def _create_score(self, player_one_score, player_two_score):
        for _ in range(player_one_score):
            self.game.player_one_scored()
        for _ in range(player_two_score):
            self.game.player_two_scored()
コード例 #13
0
class TestTennisGame(unittest.TestCase):

    def setUp(self):
        self.game = TennisGame()

    def test_initial_score(self):
        initial_score = self.game.score
        self.assertEqual("Love all", initial_score)

    def test_score_is_15_0_when_p1_scored_once(self):
        self._create_score(1, 0)
        self.assertEqual("Fifteen Love", self.game.score)

    def test_score_is_15_15_when_both_scored_once(self):
        self._create_score(1, 1)
        self.assertEqual("Fifteen all", self.game.score)

    def test_score_is_30_0_when_p1_scored_twice(self):
        self._create_score(2, 0)
        self.assertEqual("Thirty Love", self.game.score)

    def test_score_is_30_15_when_p1_scored_twice_and_p2_scored_once(self):
        self._create_score(2, 1)
        self.assertEqual("Thirty Fifteen", self.game.score)

    def test_score_is_40_15_when_p1_scored_three_times_and_p2_scored_once(self):
        self._create_score(3, 1)
        self.assertEqual("Forty Fifteen", self.game.score)

    def test_score_is_15_30_when_p1_scored_once_and_p2_scored_twice(self):
        self._create_score(1, 2)
        self.assertEqual("Fifteen Thirty", self.game.score)

    def test_score_is_0_15_when_p2_scored_once(self):
        self._create_score(0, 1)
        self.assertEqual("Love Fifteen", self.game.score)

    def test_score_is_0_30_when_p2_scored_twice(self):
        self._create_score(0, 2)
        self.assertEqual("Love Thirty", self.game.score)

    def test_score_is_0_40_when_p2_scored_three_times(self):
        self._create_score(0, 3)
        self.assertEqual("Love Forty", self.game.score)

    def test_game_for_p1_when_scored_four_times(self):
        self._create_score(4, 0)
        self.assertEqual("Game for P1", self.game.score)

    def test_game_for_p2_when_scored_four_times(self):
        self._create_score(0, 4)
        self.assertEqual("Game for P2", self.game.score)

    def test_deuce(self):
        self._create_score(4, 4)
        self.assertEqual("Deuce", self.game.score)

    def test_deuce_when_both_scored_seven_times(self):
        self._create_score(7, 7)
        self.assertEqual("Deuce", self.game.score)

    def test_advantage_p1_when_score_5_4(self):
        self._create_score(5, 4)
        self.assertEqual("Advantage P1", self.game.score)

    def test_advantage_p1_when_score_12_11(self):
        self._create_score(12, 11)
        self.assertEqual("Advantage P1", self.game.score)

    def test_advantage_p2_when_score_4_5(self):
        self._create_score(4, 5)
        self.assertEqual("Advantage P2", self.game.score)

    def test_advantage_p1_when_score_11_12(self):
        self._create_score(11, 12)
        self.assertEqual("Advantage P2", self.game.score)

    def test_game_for_p1_after_deuce(self):
        self._create_score(6, 4)
        self.assertEqual("Game for P1", self.game.score)

    def test_game_for_p2_after_deuce(self):
        self._create_score(4, 6)
        self.assertEqual("Game for P2", self.game.score)

    def test_game_for_p1_when_score_24_22(self):
        self._create_score(24, 22)
        self.assertEqual("Game for P1", self.game.score)

    def _create_score(self, player_one_score, player_two_score):
        for _ in range(player_one_score):
            self.game.player_one_scored()
        for _ in range(player_two_score):
            self.game.player_two_scored()