77import re
88import string
99
10- prg = ' ./tictactoe.py'
10+ PRG = " ./tictactoe.py"
1111
1212
1313# --------------------------------------------------
1414def test_exists ():
1515 """exists"""
1616
17- assert os .path .isfile (prg )
17+ assert os .path .isfile (PRG )
1818
1919
2020# --------------------------------------------------
2121def test_usage ():
2222 """usage"""
2323
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 } " )
2626 assert rv == 0
27- assert out .lower ().startswith (' usage' )
27+ assert out .lower ().startswith (" usage" )
2828
2929
3030# --------------------------------------------------
@@ -42,7 +42,7 @@ def test_no_input():
4242No winner.
4343""" .strip ()
4444
45- rv , out = getstatusoutput (prg )
45+ rv , out = getstatusoutput (f"python { PRG } " )
4646 assert rv == 0
4747 assert out .strip () == board
4848
@@ -53,8 +53,8 @@ def test_bad_board():
5353
5454 expected = '--board "{}" must be 9 characters of ., X, O'
5555
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 } " )
5858 assert rv != 0
5959 assert re .search (expected .format (bad ), out )
6060
@@ -63,8 +63,8 @@ def test_bad_board():
6363def test_bad_player ():
6464 """dies on bad player"""
6565
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 } " )
6868 assert rv != 0
6969 expected = f"-p/--player: invalid choice: '{ bad } '"
7070 assert re .search (expected , out )
@@ -75,17 +75,17 @@ def test_bad_cell_int():
7575 """dies on bad cell"""
7676
7777 for bad in [0 , 10 ]:
78- rv , out = getstatusoutput (f' { prg } --cell { bad } ' )
78+ rv , out = getstatusoutput (f"python { PRG } --cell { bad } " )
7979 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 )
8181
8282
8383# --------------------------------------------------
8484def test_bad_cell_str ():
8585 """dies on bad cell string value"""
8686
8787 bad = random .choice (string .ascii_letters )
88- rv , out = getstatusoutput (f' { prg } --cell { bad } ' )
88+ rv , out = getstatusoutput (f"python { PRG } --cell { bad } " )
8989 assert rv != 0
9090 assert re .search (f"-c/--cell: invalid int value: '{ bad } '" , out , re .I )
9191
@@ -94,10 +94,10 @@ def test_bad_cell_str():
9494def test_both_player_and_cell ():
9595 """test for both --player and --cell"""
9696
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 } " )
9999 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 )
101101
102102
103103# --------------------------------------------------
@@ -115,7 +115,7 @@ def test_good_board_01():
115115No winner.
116116""" .strip ()
117117
118- rv , out = getstatusoutput (f' { prg } -b .........' )
118+ rv , out = getstatusoutput (f"python { PRG } -b ........." )
119119 assert rv == 0
120120 assert out .strip () == board
121121
@@ -135,7 +135,7 @@ def test_good_board_02():
135135No winner.
136136""" .strip ()
137137
138- rv , out = getstatusoutput (f' { prg } --board ...OXX...' )
138+ rv , out = getstatusoutput (f"python { PRG } --board ...OXX..." )
139139 assert rv == 0
140140 assert out .strip () == board
141141
@@ -155,7 +155,7 @@ def test_mutate_board_01():
155155No winner.
156156""" .strip ()
157157
158- rv , out = getstatusoutput (f' { prg } -b ......... --player X -c 1' )
158+ rv , out = getstatusoutput (f"python { PRG } -b ......... --player X -c 1" )
159159 assert rv == 0
160160 assert out .strip () == board
161161
@@ -175,7 +175,7 @@ def test_mutate_board_02():
175175O has won!
176176""" .strip ()
177177
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" )
179179 assert rv == 0
180180 assert out .strip () == board
181181
@@ -184,11 +184,11 @@ def test_mutate_board_02():
184184def test_mutate_cell_taken ():
185185 """test for a cell already taken"""
186186
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" )
188188 assert rv1 != 0
189189 assert re .search ('--cell "9" already taken' , out1 )
190190
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" )
192192 assert rv2 != 0
193193 assert re .search ('--cell "1" already taken' , out2 )
194194
@@ -197,30 +197,37 @@ def test_mutate_cell_taken():
197197def test_winning ():
198198 """test winning boards"""
199199
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+ ]
202210
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"
205213
206214 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 ] == "." ]
209217 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!"
216223
217224
218225# --------------------------------------------------
219226def test_losing ():
220227 """test losing boards"""
221228
222- losing_board = list (' XXOO.....' )
229+ losing_board = list (" XXOO....." )
223230 for i in range (10 ):
224231 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