-
Notifications
You must be signed in to change notification settings - Fork 0
/
tennis_unittest.py
98 lines (83 loc) · 3.68 KB
/
tennis_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
import unittest
from tennis import TennisGame1, TennisGame2, TennisGame3
test_cases = [
(0, 0, "Love-All", "player1", "player2"),
(1, 1, "Fifteen-All", "player1", "player2"),
(2, 2, "Thirty-All", "player1", "player2"),
(3, 3, "Deuce", "player1", "player2"),
(4, 4, "Deuce", "player1", "player2"),
(1, 0, "Fifteen-Love", "player1", "player2"),
(0, 1, "Love-Fifteen", "player1", "player2"),
(2, 0, "Thirty-Love", "player1", "player2"),
(0, 2, "Love-Thirty", "player1", "player2"),
(3, 0, "Forty-Love", "player1", "player2"),
(0, 3, "Love-Forty", "player1", "player2"),
(4, 0, "Win for player1", "player1", "player2"),
(0, 4, "Win for player2", "player1", "player2"),
(2, 1, "Thirty-Fifteen", "player1", "player2"),
(1, 2, "Fifteen-Thirty", "player1", "player2"),
(3, 1, "Forty-Fifteen", "player1", "player2"),
(1, 3, "Fifteen-Forty", "player1", "player2"),
(4, 1, "Win for player1", "player1", "player2"),
(1, 4, "Win for player2", "player1", "player2"),
(3, 2, "Forty-Thirty", "player1", "player2"),
(2, 3, "Thirty-Forty", "player1", "player2"),
(4, 2, "Win for player1", "player1", "player2"),
(2, 4, "Win for player2", "player1", "player2"),
(4, 3, "Advantage player1", "player1", "player2"),
(3, 4, "Advantage player2", "player1", "player2"),
(5, 4, "Advantage player1", "player1", "player2"),
(4, 5, "Advantage player2", "player1", "player2"),
(15, 14, "Advantage player1", "player1", "player2"),
(14, 15, "Advantage player2", "player1", "player2"),
(6, 4, "Win for player1", "player1", "player2"),
(4, 6, "Win for player2", "player1", "player2"),
(16, 14, "Win for player1", "player1", "player2"),
(14, 16, "Win for player2", "player1", "player2"),
(6, 4, "Win for One", "One", "player2"),
(4, 6, "Win for Two", "player1", "Two"),
(6, 5, "Advantage One", "One", "player2"),
(5, 6, "Advantage Two", "player1", "Two"),
]
def play_game(TennisGame, p1Points, p2Points, p1Name, p2Name):
game = TennisGame(p1Name, p2Name)
for i in range(max(p1Points, p2Points)):
if i < p1Points:
game.won_point(p1Name)
if i < p2Points:
game.won_point(p2Name)
return game
class TestTennis(unittest.TestCase):
def test_Score_Game1(self):
for testcase in test_cases:
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name)
self.assertEqual(score, game.score())
def test_Score_Game2(self):
for testcase in test_cases:
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame2, p1Points, p2Points, p1Name, p2Name)
self.assertEqual(score, game.score())
def test_Score_Game3(self):
for testcase in test_cases:
(p1Points, p2Points, score, p1Name, p2Name) = testcase
game = play_game(TennisGame3, p1Points, p2Points, p1Name, p2Name)
self.assertEqual(score, game.score())
def test_player_game1(self):
game = TennisGame1("Player 1", "Player 2")
game.won_point("Player 3")
self.assertEqual(0, game.p1points)
self.assertEqual(0, game.p2points)
def test_player_game2(self):
game = TennisGame2("Player 1", "Player 2")
game.won_point("Player 3")
self.assertEqual(0, game.p1points)
self.assertEqual(0, game.p2points)
def test_player_game3(self):
game = TennisGame3("Player 1", "Player 2")
game.won_point("Player 3")
self.assertEqual(0, game.p1)
self.assertEqual(0, game.p2)
if __name__ == "__main__":
unittest.main()