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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
"""test_app.py - Testaa pelin suoritusta"""
# pylint: disable = missing-class-docstring, too-few-public-methods
from io import StringIO
import unittest
from unittest.mock import patch
from app import App
from tui import Action
class KbdTest:
# pylint: disable = unused-argument, missing-function-docstring
def __init__(self, actions):
self.actions = actions
def read_action(self):
if self.actions:
action, _, _ = self.actions.pop(0)
else:
action = Action.NOOP
return action
def read_matrix_action(self, w, h, x, y):
return self.actions.pop(0) if self.actions else (Action.NOOP,x,y)
class TestAppClass(unittest.TestCase):
""" Testit itse appille """
class DefaultArgs:
autoplay = 2
intermediate = None
expert = None
board = None
mines = None
size = None
bot = None
quiet = None
delay = None
sure_win_board = [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]
]
sure_lose_board = [
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,0,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1]
]
dssp_win_board = [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,1,1,0,1,1,0,1,0]
]
mini_board = [
[0,0],
[0,1]
]
def test_run(self):
""" Testataan että edes pyörähtää """
app = App(self.DefaultArgs)
app.run()
del app
def test_quit(self):
""" Testataan Quittaamista """
app = App()
app.ui.kbd=KbdTest([
(Action.QUIT,0,0),
(Action.OPEN,0,0)
])
app.run()
del app
def test_many_games(self):
""" Varman voiton lauta palauttaa true """
class Args(self.DefaultArgs):
quiet = True
for _ in range(50):
app = App(Args)
app.run()
del app
Args.intermediate = True
for _ in range(20):
app = App(Args)
app.run()
del app
Args.expert = True
for _ in range(10):
app = App(Args)
app.run()
del app
def test_sure_win(self):
""" Varman voiton lauta palauttaa true """
class Args(self.DefaultArgs):
board = self.sure_win_board
quiet = True
app = App(Args)
self.assertTrue(app.run())
del app
def test_dssp_win(self):
""" Varman voiton lauta palauttaa true """
class Args(self.DefaultArgs):
board = self.dssp_win_board
app = App(Args)
self.assertTrue(app.run())
del app
def test_no_dssp_win_with_simple(self):
""" Varman voiton lauta palauttaa true """
class Args(self.DefaultArgs):
board = self.dssp_win_board
quiet = True
bot = 1
while True:
app = App(Args)
if not app.run():
break
del app
def test_sure_lose(self):
""" Varman häviön lauta palauttaa false """
class Args(self.DefaultArgs):
board = self.sure_lose_board
app = App(Args)
self.assertFalse(app.run())
del app
def test_custom_size(self):
""" Varman häviön lauta palauttaa false """
class Args(self.DefaultArgs):
size = (4, 4)
with patch('sys.stdout', new = StringIO()) as captured:
app = App(Args)
app.run()
self.assertIn("Mukautettu (4x4", captured.getvalue())
del app
def test_sure_win_with_actions(self):
""" Varman voiton lauta palauttaa true """
class Args(self.DefaultArgs):
board = self.sure_win_board
autoplay = 0
bot = 0
app = App(Args)
app.ui.kbd=KbdTest([
(Action.SAFE,0,0),
(Action.OPEN,0,0)
])
self.assertTrue(app.run())
del app
def test_sure_lose_with_actions(self):
""" Varman voiton lauta palauttaa true """
class Args(self.DefaultArgs):
board = self.sure_lose_board
autoplay = 0
app = App(Args)
app.ui.kbd=KbdTest([
(Action.FLAG,0,0),
(Action.MINE,0,0),
(Action.OPEN,0,0)
])
self.assertFalse(app.run())
del app
def test_auto_play_hints(self):
""" Vihjeiden automaattipelaaminen toimii """
class Args(self.DefaultArgs):
board = self.dssp_win_board
autoplay = 1
app = App(Args)
app.ui.kbd=KbdTest([
(Action.OPEN,0,0),
(Action.HINT,0,0),
])
self.assertTrue(app.run())
del app
def test_delay(self):
""" Hidastus toimii """
class Args(self.DefaultArgs):
board = self.dssp_win_board
delay = 5
app = App(Args)
with patch('time.sleep') as patched_sleep:
self.assertTrue(app.run())
del app
patched_sleep.assert_called()
def test_delay_can_be_off(self):
""" Hidastus ei ole aina päälle """
class Args(self.DefaultArgs):
board = self.dssp_win_board
app = App(Args)
with patch('time.sleep') as patched_sleep:
self.assertTrue(app.run())
del app
patched_sleep.assert_not_called()
def test_botless_play(self):
""" Hidastus toimii """
class Args(self.DefaultArgs):
board = self.mini_board
autoplay = 0
delay = 50000
app = App(Args)
app.ui.kbd=KbdTest([
(Action.OPEN,0,0),
(Action.HINT,0,0),
(Action.OPEN,1,0),
(Action.HINT,0,0),
(Action.OPEN,0,1)
])
self.assertTrue(app.run())
del app
|