-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_game.py
98 lines (81 loc) · 2.52 KB
/
test_game.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
import sys
import unittest
import ttt
class TestAI(unittest.TestCase):
def test_eval_we_win(self):
board = ttt.Board([
'x', '.', 'o',
'.', 'x', 'o',
'.', '.', 'x',
])
self.assertEqual(
sys.maxint,
ttt.AI.evaluate(board, 'x')
)
def test_eval_they_win(self):
board = ttt.Board([
'x', '.', 'o',
'.', 'x', 'o',
'.', '.', 'x',
])
self.assertEqual(
-sys.maxint,
ttt.AI.evaluate(board, 'o')
)
def test_next_move(self):
board = ttt.Board([
'x', 'o', 'o',
'o', 'x', '.',
'x', 'o', '.',
])
moves = ttt.all_moves(board, 'x')
self.assertEqual(
[
(2, 1),
(2, 2),
],
list(moves),
)
def _test_eval(self):
board0 = ttt.Board([
'.', '.', '.'
'.', 'x', '.',
'.', '.', '.',
])
board1 = ttt.Board([
'x', '.', '.',
'.', '.', '.',
'.', '.', '.',
])
self.assertTrue(
ttt.AI.evaluate(board0, 'x') > ttt.AI.evaluate(board1, 'x')
)
class TestGame(unittest.TestCase):
def setUp(self):
self.game = ttt.Game()
def test_invalid_row(self):
with self.assertRaises(Exception) as ctx:
self.game.move('x', -1, 0)
self.assertEqual("Invalid location", str(ctx.exception))
def test_invalid_row_big(self):
with self.assertRaises(Exception) as ctx:
self.game.move('x', 3, 0)
self.assertEqual("Invalid location", str(ctx.exception))
def test_invalid_col(self):
with self.assertRaises(Exception) as ctx:
self.game.move('x', 0, -1)
self.assertEqual("Invalid location", str(ctx.exception))
def test_invalid_col_big(self):
with self.assertRaises(Exception) as ctx:
self.game.move('x', 0, 3)
self.assertEqual("Invalid location", str(ctx.exception))
def test_x_moves_twice(self):
self.game.move('x', 0, 0)
with self.assertRaises(Exception) as ctx:
self.game.move('x', 0, 1)
self.assertEqual("Invalid move", str(ctx.exception))
def test_x_moves_twice_same_location(self):
self.game.move('x', 0, 0)
with self.assertRaises(Exception) as ctx:
self.game.move('x', 0, 0)
self.assertEqual("Invalid move", str(ctx.exception))