-
Notifications
You must be signed in to change notification settings - Fork 32
/
tests.py
executable file
·266 lines (226 loc) · 9.52 KB
/
tests.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import glob
import os
import sys
import tempfile
import unittest
import puz
def temp_filename(suffix='puz'):
# uses NamedTemporaryFile to create a temporary file but then exits the context
# so as to close the fd and unlink the file. These tests typically want a filename
# they can write to which doesn't work on every OS when the fd is open.
with tempfile.NamedTemporaryFile(suffix=suffix) as tmp:
return tmp.name
class PuzzleTests(unittest.TestCase):
def test_clue_numbering(self):
p = puz.read('testfiles/washpost.puz')
clues = p.clue_numbering()
self.assertEqual(len(p.clues), len(clues.across) + len(clues.down))
self.assertTrue(len(p.clues) > 0)
def test_diagramless_clue_numbering(self):
p = puz.read('testfiles/nyt_diagramless.puz')
clues = p.clue_numbering()
self.assertEqual(len(p.clues), len(clues.across) + len(clues.down))
self.assertTrue(len(p.clues) > 0)
def test_extensions(self):
p = puz.read('testfiles/nyt_rebus_with_notes_and_shape.puz')
# We don't use assertIn for compatibility with Python 2.6
self.assertTrue(puz.Extensions.Rebus in p.extensions)
self.assertTrue(puz.Extensions.RebusSolutions in p.extensions)
self.assertTrue(puz.Extensions.Markup in p.extensions)
def test_rebus(self):
p = puz.read('testfiles/nyt_rebus_with_notes_and_shape.puz')
self.assertTrue(p.has_rebus())
r = p.rebus()
self.assertTrue(r.has_rebus())
self.assertEqual(3, len(r.get_rebus_squares()))
self.assertTrue(all(r.is_rebus_square(i)
for i in r.get_rebus_squares()))
self.assertTrue(all('STAR' == r.get_rebus_solution(i)
for i in r.get_rebus_squares()))
self.assertEqual(None, r.get_rebus_solution(100))
# trigger save
p.tobytes()
def test_markup(self):
p = puz.read('testfiles/nyt_rebus_with_notes_and_shape.puz')
self.assertTrue(p.has_markup())
m = p.markup()
self.assertTrue(all(puz.GridMarkup.Circled == m.markup[i]
and m.is_markup_square(i)
for i in m.get_markup_squares()))
# trigger save
p.tobytes()
p = puz.read('testfiles/washpost.puz')
self.assertFalse(p.has_markup())
m = p.markup()
self.assertFalse(m.has_markup())
# trigger save
p.tobytes()
def test_puzzle_type(self):
self.assertNotEqual(
puz.read('testfiles/washpost.puz').puzzletype,
puz.PuzzleType.Diagramless)
self.assertNotEqual(
puz.read('testfiles/nyt_locked.puz').puzzletype,
puz.PuzzleType.Diagramless)
self.assertEqual(
puz.read('testfiles/nyt_diagramless.puz').puzzletype,
puz.PuzzleType.Diagramless)
def test_empty_puzzle(self):
p = puz.Puzzle()
self.assertRaises(puz.PuzzleFormatError, p.load, b'')
def test_junk_at_end_of_puzzle(self):
with open('testfiles/washpost.puz', 'rb') as fp:
data = fp.read() + b'\r\n\r\n'
p = puz.Puzzle()
p.load(data)
self.assertEqual(p.postscript, b'\r\n\r\n')
def test_v1_4(self):
p = puz.read('testfiles/nyt_v1_4.puz')
p.tobytes()
def test_v2_unicode(self):
p = puz.read('testfiles/unicode.puz')
# puzzle title contains emoji
self.assertEqual(p.title, u'\u2694\ufe0f')
self.assertEqual(p.encoding, 'UTF-8')
p.tobytes()
def test_v2_upgrade(self):
p = puz.read('testfiles/washpost.puz')
p.title = u'\u2694\ufe0f'
p.version = b'2.0'
p.fileversion = b'2.0\0'
p.encoding = puz.ENCODING_UTF8
data = p.tobytes()
p2 = puz.load(data)
self.assertEqual(p2.title, u'\u2694\ufe0f')
def test_save_empty_puzzle(self):
''' confirm an empty Puzzle() can be saved to a file '''
filename = temp_filename()
try:
p = puz.Puzzle()
p.save(filename)
p2 = puz.read(filename)
self.assertEqual(p.puzzletype, p2.puzzletype)
self.assertEqual(p.version, p2.version)
self.assertEqual(p.scrambled_cksum, p2.scrambled_cksum)
finally:
os.unlink(filename)
def test_save_small_puzzle(self):
''' an example of creating a small 3x3 puzzle from scratch and writing
to a file
'''
filename = temp_filename()
try:
p = puz.Puzzle()
p.title = 'Test Puzzle'
p.author = 'Alex'
p.height = 3
p.width = 3
p.solution = 'A' * 9
p.clues = ['clue'] * 6
p.fill = '-' * 9
p.save(filename)
p2 = puz.read(filename)
self.assertEqual(p.title, p2.title)
self.assertEqual(p.author, p2.author)
self.assertEqual(p.solution, p2.solution)
self.assertEqual(p.clues, p2.clues)
self.assertEqual(p.fill, p2.fill)
finally:
os.unlink(filename)
class LockTests(unittest.TestCase):
def test_scramble_functions(self):
''' tests some examples from the file format documentation wiki
'''
self.assertEqual('MLOOPKJ', puz.scramble_string('AEBFCDG', 1234))
self.assertEqual('MOP..KLOJ',
puz.scramble_solution('ABC..DEFG', 3, 3, 1234))
self.assertEqual('AEBFCDG', puz.unscramble_string('MLOOPKJ', 1234))
self.assertEqual('ABC..DEFG',
puz.unscramble_solution('MOP..KLOJ', 3, 3, 1234))
# rectangular example - tricky
a = 'ABCD.EFGH.KHIJKLM.NOPW.XYZ'
scrambled = puz.scramble_solution(a, 13, 2, 9721)
unscrambled = puz.unscramble_solution(scrambled, 13, 2, 9721)
self.assertEqual(a, unscrambled)
def test_locked_bit(self):
self.assertFalse(
puz.read('testfiles/washpost.puz').is_solution_locked())
self.assertTrue(
puz.read('testfiles/nyt_locked.puz').is_solution_locked())
def test_unlock(self):
p = puz.read('testfiles/nyt_locked.puz')
self.assertTrue(p.is_solution_locked())
self.assertFalse(p.unlock_solution(1234))
self.assertTrue(p.is_solution_locked()) # still locked
self.assertTrue(p.unlock_solution(7844))
self.assertFalse(p.is_solution_locked()) # unlocked!
# We don't use assertIn for compatibility with Python 2.6
self.assertTrue('LAKEONTARIO' in p.solution)
def test_unlock_relock(self):
with open('testfiles/nyt_locked.puz', 'rb') as fp:
orig = fp.read()
p = puz.read('testfiles/nyt_locked.puz')
self.assertTrue(p.is_solution_locked())
self.assertTrue(p.unlock_solution(7844))
p.lock_solution(7844)
new = p.tobytes()
self.assertEqual(orig, new, 'nyt_locked.puz did not round-trip')
def test_check_answers_locked(self):
'''Verify that we can check answers even when the solution is locked
'''
p1 = puz.read('testfiles/nyt_locked.puz')
p2 = puz.read('testfiles/nyt_locked.puz')
p1.unlock_solution(7844)
self.assertTrue(p2.is_solution_locked())
self.assertTrue(p2.check_answers(p1.solution))
def test_unlock_relock_diagramless(self):
with open('testfiles/nyt_diagramless.puz', 'rb') as fp:
orig = fp.read()
p = puz.read('testfiles/nyt_diagramless.puz')
self.assertTrue(p.is_solution_locked())
self.assertTrue(p.unlock_solution(3285))
self.assertFalse(p.is_solution_locked())
p.lock_solution(3285)
new = p.tobytes()
self.assertEqual(orig, new, 'nyt_diagramless.puz did not round-trip')
class RoundtripPuzfileTests(unittest.TestCase):
def __init__(self, filename):
unittest.TestCase.__init__(self)
self.filename = filename
def runTest(self):
try:
with open(self.filename, 'rb') as fp_filename:
orig = fp_filename.read()
p = puz.read(self.filename)
if (p.puzzletype == puz.PuzzleType.Normal):
clues = p.clue_numbering()
# smoke test the clue numbering while we're at it
self.assertEqual(
len(p.clues), len(clues.across) + len(clues.down),
'failed in %s' % self.filename)
# this is the roundtrip
new = p.tobytes()
self.assertEqual(orig, new,
'%s did not round-trip' % self.filename)
except puz.PuzzleFormatError:
args = (self.filename, sys.exc_info()[1].message)
self.assertTrue(False, '%s threw PuzzleFormatError: %s' % args)
def tests_in_dir(directory):
tests = []
for path, _, _ in os.walk(directory):
for filename in glob.glob(os.path.join(path, '*.puz')):
tests.append(RoundtripPuzfileTests(filename))
return tests
def suite():
# suite consists of any test* method defined in PuzzleTests,
# plus a round-trip test for each .puz file in ./testfiles/
suite = unittest.TestSuite()
loader = unittest.defaultTestLoader
suite.addTests(loader.loadTestsFromTestCase(PuzzleTests))
suite.addTests(loader.loadTestsFromTestCase(LockTests))
suite.addTests(tests_in_dir('testfiles'))
return suite
if __name__ == '__main__':
print(__file__)
result = unittest.TextTestRunner().run(suite())
sys.exit(not result.wasSuccessful())