-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tictac.py
206 lines (185 loc) · 6.12 KB
/
test_tictac.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
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
# -*- coding: utf-8 -*-
"""
Practice run at TDD as if you meant it workshop by
Keith Braithwaite.
"""
from tictac import TicTac, NotInTurnError, InvalidMoveError
import pytest
class TestsTicTacToe:
def test_a_game_starts_with_a_empty_board(self):
"""
When a new Tic Tac Toe game is created the board of the
game must be empty.
"""
game = TicTac()
assert game.board.is_empty()
def test_after_player_one_moves_board_is_not_empty(self):
"""
After the player one makes a move to the board,
then the board of the game cannot be considered empty.
"""
game = TicTac()
game.player_one.make_move(1, 1)
assert not game.board.is_empty()
def test_player_one_cannot_make_consecutive_moves(self):
"""
After player one has make a move it cannot attempt to
make another one.
"""
game = TicTac()
game.player_one.make_move(1, 1)
with pytest.raises(NotInTurnError) as exc_info:
game.player_one.make_move(1, 2)
def test_board_can_be_visualized_as_a_list_at_game_start(self):
"""
The game board can be visualized as a list of coordinates
at game start.
"""
game = TicTac()
row = [0,0,0]
expected = [row, row, row]
assert game.show_game_board() == expected
def test_board_reflects_the_movement_made_by_player_one(self):
"""
The board must reflect the movements made by each player
after is done.
"""
game = TicTac()
game.player_one.make_move(1, 1)
expected = [[0,0,0], [0, 'x', 0], [0, 0, 0]]
assert game.show_game_board() == expected
def test_board_does_not_let_a_move_into_occupied_space(self):
"""
If a player tries to make a move into a space already occupied by
another player, a error will be raised.
"""
game = TicTac()
game.player_one.make_move(1, 1)
with pytest.raises(InvalidMoveError):
game.player_two.make_move(1, 1)
def test_a_player_cannot_make_a_move_outside_the_board(self):
"""
If a player tries to make a move with the x coordinates
outside of the board, then a InvalidMove exception must
be raised.
"""
game = TicTac()
with pytest.raises(InvalidMoveError):
game.player_one.make_move(3, 1)
def test_a_player_cannot_make_a_move_outside_the_y_axis(self):
"""
If a player tries to make a move with the y coordinates
outside of the board, then a InvalidMove exception
must be raises.
"""
game = TicTac()
with pytest.raises(InvalidMoveError):
game.player_one.make_move(1, 3)
def test_a_player_cannot_introduce_negative_values_x_axis(self):
"""
If a player tries to make a negative move on the x coordinates
a InvalidMove exception must be raised.
"""
game = TicTac()
with pytest.raises(InvalidMoveError):
game.player_one.make_move(-1, 2)
def test_a_player_cannot_introduce_negative_value_y_axis(self):
"""
If a player tries to make a negative move on the y coordinates
a InvalidMove exception must be raised.
"""
game = TicTac()
with pytest.raises(InvalidMoveError):
game.player_one.make_move(1, -1)
def test_a_newly_started_game_cannot_be_over(self):
"""
A new game cannot be considered over.
"""
game = TicTac()
assert not game.is_over()
def test_a_game_with_empty_tiles_cannot_be_over(self):
"""
A game in which there are still empty tiles cannot be considered
over.
"""
game = TicTac()
game.player_one.make_move(1, 1)
assert not game.is_over()
def test_a_without_empty_tiles_can_be_considered_over(self):
"""
A game in which therer are no more empty tiles can
be considered over.
"""
game = TicTac()
game.board.body = [
['x', 'x', 'y'],
['y', 'x', 'x'],
['x', 'y', 'x'],
]
assert game.is_over()
def test_a_player_wons_by_having_any_row_with_its_mark(self):
"""
The first player can win by having any row complete
with its mark.
"""
game = TicTac()
game.board.body = [
['x', 'x', 'x'],
['x', 'y', 'y'],
['y', 'y', 'x'],
]
assert game.who_won() == game.player_one
def test_a_player_can_win_by_having_a_column_filled(self):
"""
The player can win by having a complete column with
its mark.
"""
game = TicTac()
game.board.body = [
['x', 'x', 'y'],
['y', 'x', 'y'],
['x', 'y', 'y'],
]
assert game.who_won() == game.player_two
def test_a_player_can_win_by_filling_a_diagonal(self):
"""
A player can win by filling one of the two possible
diagonals available.
"""
game = TicTac()
game.board.body = [
['x', 'x', 'y'],
['y', 'x', 'y'],
['y', 'y', 'x'],
]
assert game.who_won() == game.player_one
def test_a_player_can_win_by_filling_a_second_diagonal(self):
"""
A player can win by filling the second diagonal.
"""
game = TicTac()
game.board.body = [
['x', 'x', 'y'],
['x', 'y', 'x'],
['y', 'x', 'y'],
]
assert game.who_won() == game.player_two
def test_a_game_without_a_clear_victor_is_a_draw(self):
"""
A game without victor is a draw.
"""
game = TicTac()
game.board.body = [
['y', 'x', 'y'],
['y', 'x', 'y'],
['x', 'y', 'x'],
]
assert game.is_a_draw()
def test_a_game_can_be_over_once_one_player_has_won(self):
game = TicTac()
game.board.body = [
['y', 0, 0],
['y', 0, 0],
['y', 'x', 0],
]
assert game.is_over()