7
7
import re
8
8
import string
9
9
10
- prg = ' ./tictactoe.py'
10
+ PRG = " ./tictactoe.py"
11
11
12
12
13
13
# --------------------------------------------------
14
14
def test_exists ():
15
15
"""exists"""
16
16
17
- assert os .path .isfile (prg )
17
+ assert os .path .isfile (PRG )
18
18
19
19
20
20
# --------------------------------------------------
21
21
def test_usage ():
22
22
"""usage"""
23
23
24
- for flag in ['-h' , ' --help' ]:
25
- rv , out = getstatusoutput (f' { prg } { flag } ' )
24
+ for flag in ["-h" , " --help" ]:
25
+ rv , out = getstatusoutput (f"python { PRG } { flag } " )
26
26
assert rv == 0
27
- assert out .lower ().startswith (' usage' )
27
+ assert out .lower ().startswith (" usage" )
28
28
29
29
30
30
# --------------------------------------------------
@@ -42,7 +42,7 @@ def test_no_input():
42
42
No winner.
43
43
""" .strip ()
44
44
45
- rv , out = getstatusoutput (prg )
45
+ rv , out = getstatusoutput (f"python { PRG } " )
46
46
assert rv == 0
47
47
assert out .strip () == board
48
48
@@ -53,8 +53,8 @@ def test_bad_board():
53
53
54
54
expected = '--board "{}" must be 9 characters of ., X, O'
55
55
56
- for bad in [' ABC' , ' ...XXX' , ' XXXOOOXX' ]:
57
- rv , out = getstatusoutput (f' { prg } --board { bad } ' )
56
+ for bad in [" ABC" , " ...XXX" , " XXXOOOXX" ]:
57
+ rv , out = getstatusoutput (f"python { PRG } --board { bad } " )
58
58
assert rv != 0
59
59
assert re .search (expected .format (bad ), out )
60
60
@@ -63,8 +63,8 @@ def test_bad_board():
63
63
def test_bad_player ():
64
64
"""dies on bad player"""
65
65
66
- bad = random .choice ([c for c in string .ascii_uppercase if c not in 'XO' ])
67
- rv , out = getstatusoutput (f' { prg } -p { bad } ' )
66
+ bad = random .choice ([c for c in string .ascii_uppercase if c not in "XO" ])
67
+ rv , out = getstatusoutput (f"python { PRG } -p { bad } " )
68
68
assert rv != 0
69
69
expected = f"-p/--player: invalid choice: '{ bad } '"
70
70
assert re .search (expected , out )
@@ -75,17 +75,17 @@ def test_bad_cell_int():
75
75
"""dies on bad cell"""
76
76
77
77
for bad in [0 , 10 ]:
78
- rv , out = getstatusoutput (f' { prg } --cell { bad } ' )
78
+ rv , out = getstatusoutput (f"python { PRG } --cell { bad } " )
79
79
assert rv != 0
80
- assert re .search (f' -c/--cell: invalid choice: { bad } ' , out )
80
+ assert re .search (f" -c/--cell: invalid choice: { bad } " , out )
81
81
82
82
83
83
# --------------------------------------------------
84
84
def test_bad_cell_str ():
85
85
"""dies on bad cell string value"""
86
86
87
87
bad = random .choice (string .ascii_letters )
88
- rv , out = getstatusoutput (f' { prg } --cell { bad } ' )
88
+ rv , out = getstatusoutput (f"python { PRG } --cell { bad } " )
89
89
assert rv != 0
90
90
assert re .search (f"-c/--cell: invalid int value: '{ bad } '" , out , re .I )
91
91
@@ -94,10 +94,10 @@ def test_bad_cell_str():
94
94
def test_both_player_and_cell ():
95
95
"""test for both --player and --cell"""
96
96
97
- player = random .choice ('XO' )
98
- rv , out = getstatusoutput (f' { prg } --player { player } ' )
97
+ player = random .choice ("XO" )
98
+ rv , out = getstatusoutput (f"python { PRG } --player { player } " )
99
99
assert rv != 0
100
- assert re .search (' Must provide both --player and --cell' , out )
100
+ assert re .search (" Must provide both --player and --cell" , out )
101
101
102
102
103
103
# --------------------------------------------------
@@ -115,7 +115,7 @@ def test_good_board_01():
115
115
No winner.
116
116
""" .strip ()
117
117
118
- rv , out = getstatusoutput (f' { prg } -b .........' )
118
+ rv , out = getstatusoutput (f"python { PRG } -b ........." )
119
119
assert rv == 0
120
120
assert out .strip () == board
121
121
@@ -135,7 +135,7 @@ def test_good_board_02():
135
135
No winner.
136
136
""" .strip ()
137
137
138
- rv , out = getstatusoutput (f' { prg } --board ...OXX...' )
138
+ rv , out = getstatusoutput (f"python { PRG } --board ...OXX..." )
139
139
assert rv == 0
140
140
assert out .strip () == board
141
141
@@ -155,7 +155,7 @@ def test_mutate_board_01():
155
155
No winner.
156
156
""" .strip ()
157
157
158
- rv , out = getstatusoutput (f' { prg } -b ......... --player X -c 1' )
158
+ rv , out = getstatusoutput (f"python { PRG } -b ......... --player X -c 1" )
159
159
assert rv == 0
160
160
assert out .strip () == board
161
161
@@ -175,7 +175,7 @@ def test_mutate_board_02():
175
175
O has won!
176
176
""" .strip ()
177
177
178
- rv , out = getstatusoutput (f' { prg } --board XXO...OOX --p O -c 5' )
178
+ rv , out = getstatusoutput (f"python { PRG } --board XXO...OOX --p O -c 5" )
179
179
assert rv == 0
180
180
assert out .strip () == board
181
181
@@ -184,11 +184,11 @@ def test_mutate_board_02():
184
184
def test_mutate_cell_taken ():
185
185
"""test for a cell already taken"""
186
186
187
- rv1 , out1 = getstatusoutput (f' { prg } -b XXO...OOX --player X --cell 9' )
187
+ rv1 , out1 = getstatusoutput (f"python { PRG } -b XXO...OOX --player X --cell 9" )
188
188
assert rv1 != 0
189
189
assert re .search ('--cell "9" already taken' , out1 )
190
190
191
- rv2 , out2 = getstatusoutput (f' { prg } --board XXO...OOX --p O -c 1' )
191
+ rv2 , out2 = getstatusoutput (f"python { PRG } --board XXO...OOX --p O -c 1" )
192
192
assert rv2 != 0
193
193
assert re .search ('--cell "1" already taken' , out2 )
194
194
@@ -197,30 +197,37 @@ def test_mutate_cell_taken():
197
197
def test_winning ():
198
198
"""test winning boards"""
199
199
200
- wins = [('PPP......' ), ('...PPP...' ), ('......PPP' ), ('P..P..P..' ),
201
- ('.P..P..P.' ), ('..P..P..P' ), ('P...P...P' ), ('..P.P.P..' )]
200
+ wins = [
201
+ ("PPP......" ),
202
+ ("...PPP..." ),
203
+ ("......PPP" ),
204
+ ("P..P..P.." ),
205
+ (".P..P..P." ),
206
+ ("..P..P..P" ),
207
+ ("P...P...P" ),
208
+ ("..P.P.P.." ),
209
+ ]
202
210
203
- for player in 'XO' :
204
- other_player = 'O' if player == 'X' else 'X'
211
+ for player in "XO" :
212
+ other_player = "O" if player == "X" else "X"
205
213
206
214
for board in wins :
207
- board = board .replace ('P' , player )
208
- dots = [i for i in range (len (board )) if board [i ] == '.' ]
215
+ board = board .replace ("P" , player )
216
+ dots = [i for i in range (len (board )) if board [i ] == "." ]
209
217
mut = random .sample (dots , k = 2 )
210
- test_board = '' .join ([
211
- other_player if i in mut else board [i ]
212
- for i in range (len (board ))
213
- ])
214
- out = getoutput (f'{ prg } -b { test_board } ' ).splitlines ()
215
- assert out [- 1 ].strip () == f'{ player } has won!'
218
+ test_board = "" .join (
219
+ [other_player if i in mut else board [i ] for i in range (len (board ))]
220
+ )
221
+ out = getoutput (f"python { PRG } -b { test_board } " ).splitlines ()
222
+ assert out [- 1 ].strip () == f"{ player } has won!"
216
223
217
224
218
225
# --------------------------------------------------
219
226
def test_losing ():
220
227
"""test losing boards"""
221
228
222
- losing_board = list (' XXOO.....' )
229
+ losing_board = list (" XXOO....." )
223
230
for i in range (10 ):
224
231
random .shuffle (losing_board )
225
- out = getoutput (f'{ prg } -b { "" .join (losing_board )} ' ).splitlines ()
226
- assert out [- 1 ].strip () == ' No winner.'
232
+ out = getoutput (f'python { PRG } -b { "" .join (losing_board )} ' ).splitlines ()
233
+ assert out [- 1 ].strip () == " No winner."
0 commit comments