Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
226 lines (170 loc) · 5.36 KB

File metadata and controls

executable file
·
226 lines (170 loc) · 5.36 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
"""tests for tictactoe.py"""
from subprocess import getstatusoutput, getoutput
import os
import random
import re
import string
prg = './tictactoe.py'
# --------------------------------------------------
def test_exists():
"""exists"""
assert os.path.isfile(prg)
# --------------------------------------------------
def test_usage():
"""usage"""
for flag in ['-h', '--help']:
rv, out = getstatusoutput(f'{prg} {flag}')
assert rv == 0
assert out.lower().startswith('usage')
# --------------------------------------------------
def test_no_input():
"""makes board on no input"""
board = """
-------------
| 1 | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------
No winner.
""".strip()
rv, out = getstatusoutput(prg)
assert rv == 0
assert out.strip() == board
# --------------------------------------------------
def test_bad_board():
"""dies on bad board"""
expected = '--board "{}" must be 9 characters of ., X, O'
for bad in ['ABC', '...XXX', 'XXXOOOXX']:
rv, out = getstatusoutput(f'{prg} --board {bad}')
assert rv != 0
assert re.search(expected.format(bad), out)
# --------------------------------------------------
def test_bad_player():
"""dies on bad player"""
bad = random.choice([c for c in string.ascii_uppercase if c not in 'XO'])
rv, out = getstatusoutput(f'{prg} -p {bad}')
assert rv != 0
expected = f"-p/--player: invalid choice: '{bad}'"
assert re.search(expected, out)
# --------------------------------------------------
def test_bad_cell_int():
"""dies on bad cell"""
for bad in [0, 10]:
rv, out = getstatusoutput(f'{prg} --cell {bad}')
assert rv != 0
assert re.search(f'-c/--cell: invalid choice: {bad}', out)
# --------------------------------------------------
def test_bad_cell_str():
"""dies on bad cell string value"""
bad = random.choice(string.ascii_letters)
rv, out = getstatusoutput(f'{prg} --cell {bad}')
assert rv != 0
assert re.search(f"-c/--cell: invalid int value: '{bad}'", out, re.I)
# --------------------------------------------------
def test_both_player_and_cell():
"""test for both --player and --cell"""
player = random.choice('XO')
rv, out = getstatusoutput(f'{prg} --player {player}')
assert rv != 0
assert re.search('Must provide both --player and --cell', out)
# --------------------------------------------------
def test_good_board_01():
"""makes board on good input"""
board = """
-------------
| 1 | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------
No winner.
""".strip()
rv, out = getstatusoutput(f'{prg} -b .........')
assert rv == 0
assert out.strip() == board
# --------------------------------------------------
def test_good_board_02():
"""makes board on good input"""
board = """
-------------
| 1 | 2 | 3 |
-------------
| O | X | X |
-------------
| 7 | 8 | 9 |
-------------
No winner.
""".strip()
rv, out = getstatusoutput(f'{prg} --board ...OXX...')
assert rv == 0
assert out.strip() == board
# --------------------------------------------------
def test_mutate_board_01():
"""mutates board on good input"""
board = """
-------------
| X | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------
No winner.
""".strip()
rv, out = getstatusoutput(f'{prg} -b ......... --player X -c 1')
assert rv == 0
assert out.strip() == board
# --------------------------------------------------
def test_mutate_board_02():
"""mutates board on good input"""
board = """
-------------
| X | X | O |
-------------
| 4 | O | 6 |
-------------
| O | O | X |
-------------
O has won!
""".strip()
rv, out = getstatusoutput(f'{prg} --board XXO...OOX --p O -c 5')
assert rv == 0
assert out.strip() == board
# --------------------------------------------------
def test_mutate_cell_taken():
"""test for a cell already taken"""
rv1, out1 = getstatusoutput(f'{prg} -b XXO...OOX --player X --cell 9')
assert rv1 != 0
assert re.search('--cell "9" already taken', out1)
rv2, out2 = getstatusoutput(f'{prg} --board XXO...OOX --p O -c 1')
assert rv2 != 0
assert re.search('--cell "1" already taken', out2)
# --------------------------------------------------
def test_winning():
"""test winning boards"""
wins = [('PPP......'), ('...PPP...'), ('......PPP'), ('P..P..P..'),
('.P..P..P.'), ('..P..P..P'), ('P...P...P'), ('..P.P.P..')]
for player in 'XO':
other_player = 'O' if player == 'X' else 'X'
for board in wins:
board = board.replace('P', player)
dots = [i for i in range(len(board)) if board[i] == '.']
mut = random.sample(dots, k=2)
test_board = ''.join([
other_player if i in mut else board[i]
for i in range(len(board))
])
out = getoutput(f'{prg} -b {test_board}').splitlines()
assert out[-1].strip() == f'{player} has won!'
# --------------------------------------------------
def test_losing():
"""test losing boards"""
losing_board = list('XXOO.....')
for i in range(10):
random.shuffle(losing_board)
out = getoutput(f'{prg} -b {"".join(losing_board)}').splitlines()
assert out[-1].strip() == 'No winner.'
Morty Proxy This is a proxified and sanitized view of the page, visit original site.