-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_tic_tac_toe.py
175 lines (112 loc) · 3.78 KB
/
test_tic_tac_toe.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
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
#!/usr/bin/env python
from tic_tac_toe import (print_board, get_player_token_choice,
who_goes_first, get_player_move, get_computer_move,
is_space_free, is_there_a_win, get_play_again,
is_board_full)
def test_print_board():
board = [None, None, None,
None, None, None,
None, None, None]
expected = (" | | \n"
" 1 | 2 | 3 \n"
" | | \n"
"-----------------\n"
" | | \n"
" 4 | 5 | 6 \n"
" | | \n"
"-----------------\n"
" | | \n"
" 7 | 8 | 9 \n"
" | | \n")
result = print_board(board)
assert result == expected
board = ['o', None, 'o',
'x', None, None,
'o', None, 'x']
expected = (" | | \n"
" O | 2 | O \n"
" | | \n"
"-----------------\n"
" | | \n"
" X | 5 | 6 \n"
" | | \n"
"-----------------\n"
" | | \n"
" O | 8 | X \n"
" | | \n")
result = print_board(board)
assert result == expected
def test_get_player_token_choice():
result = get_player_token_choice()
assert result == 'x' or result == 'o'
def test_who_goes_first():
result = who_goes_first()
assert result == 'x' or result == 'o'
def test_get_player_move():
board = ['o', None, 'o',
'x', None, None,
'o', None, 'x']
result = get_player_move(board, 'x')
assert result in [2, 5, 6, 8]
def test_get_computer_move():
board = ['o', None, 'o',
'x', None, None,
'o', None, 'x']
result = get_computer_move(board, 'o')
assert result in [2, 5, 6, 8]
def test_is_space_free():
board = ['o', 'o', 'x',
None, None, 'x',
'o', None, 'x']
assert is_space_free(board, 4) == True
assert is_space_free(board, 1) == False
def test_is_there_a_win():
board = ['o', None, 'o',
'x', None, None,
'o', None, 'x']
result = is_there_a_win(board)
assert result == 'no'
board = ['o', 'o', 'o',
'x', None, 'x',
'o', 'x', None]
result = is_there_a_win(board)
assert result == 'o'
board = ['o', 'o', 'x',
None, None, 'x',
'o', None, 'x']
result = is_there_a_win(board)
assert result == 'x'
def test_get_play_again():
result = get_play_again()
assert result is True or result is False
def test_is_board_full():
board = [None, None, None,
None, None, None,
None, None, None]
assert is_board_full(board) == False
board = ['o', 'o', 'x',
None, None, 'x',
'o', None, 'x']
assert is_board_full(board) == False
board = ['x', 'o', 'x',
'x', 'o', 'o',
'o', 'x', 'x']
assert is_board_full(board) == True
def run_all_tests():
tests = [test_print_board,
test_get_player_token_choice,
test_who_goes_first,
test_get_player_move,
test_get_computer_move,
test_is_space_free,
test_is_there_a_win,
test_get_play_again,
test_is_board_full]
for test in tests:
try:
test()
print('The {} passed.'.format(test.func_name))
except AssertionError:
print('The {} failed.'.format(test.func_name))
if __name__ == "__main__":
run_all_tests()