-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sgflib.py
executable file
·417 lines (373 loc) · 14.1 KB
/
test_sgflib.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import io
import pytest
import sgflib
class TestNode:
@staticmethod
def node1():
n = sgflib.Node()
n['B'] = 'aa'
n['BL'] = '123.45'
n['C'] = 'text /\\[]()'
n['LB'] = ['bb:A', 'cc:B']
return n
node1_str = r';B[aa]BL[123.45]C[text /\\[\]()]LB[bb:A][cc:B]'
node1_repr = r"Node(B='aa', BL='123.45', C='text /\\[]()', LB=['bb:A', 'cc:B'])"
@staticmethod
def node2():
n = sgflib.Node()
n.B = 'aa'
n.BL = '123.45'
n.C = 'text /\\[]()'
n.LB = ['bb:A', 'cc:B']
return n
@staticmethod
def node3():
n = sgflib.Node()
n.black = 'aa'
n.black_time_left = '123.45'
n.comment = 'text /\\[]()'
n.label = ['bb:A', 'cc:B']
return n
def test_node(self):
n1 = self.node1()
assert str(n1) == self.node1_str
assert repr(n1) == self.node1_repr
n2 = self.node2()
assert str(n2) == self.node1_str
assert repr(n2) == self.node1_repr
n3 = self.node3()
assert str(n3) == self.node1_str
assert repr(n3) == self.node1_repr
def test_escape_text(self):
n1 = self.node1()
assert n1.escape_text(r'abc\def]ghi') == r'abc\\def\]ghi'
class TestGameTree:
def test_game_tree(self):
g = sgflib.GameTree()
assert str(g) == '(\n)'
n = TestNode.node1()
g = sgflib.GameTree(n)
assert str(g) == '(\n{}\n)'.format(TestNode.node1_str)
class TestParser:
sgfdata1 = rb""" (;GM [1]FF[4]CA[UTF-8]US[someone]CP[\
Permission to reproduce this game is given.]GN[a-b]EV[None]RE[B+Resign]
PW[a]WR[2k*]PB[b]BR[4k*]PC[somewhere]DT[2000-01-16]SZ[19]TM[300]KM[4.5]
HA[3]AB[pd][dp][dd];W[pp];B[nq];W[oq]C[ x started observation.
](;B[qc]C[ [b\]: \\ hi x! ;-) \\];W[kc])(;B[hc];W[oe])) """
sgfdata1_str = r"""(
;GM[1]FF[4]CA[UTF-8]US[someone]CP[ Permission to reproduce this game is given.]GN[a-b]EV[None]RE[B+Resign]PW[a]WR[2k*]PB[b]BR[4k*]PC[somewhere]DT[2000-01-16]SZ[19]TM[300]KM[4.5]HA[3]AB[pd][dp][dd]
;W[pp]
;B[nq]
;W[oq]C[ x started observation.
]
(
;B[qc]C[ [b\]: \\ hi x! ;-) \\]
;W[kc]
)
(
;B[hc]
;W[oe]
)
)"""
sgfdata1_repr = r"Collection(GameTree(nodelist=[Node(GM='1', FF='4', CA='UTF-8', US='someone', CP=' Permission to reproduce this game is given.', GN='a-b', EV='None', RE='B+Resign', PW='a', WR='2k*', PB='b', BR='4k*', PC='somewhere', DT='2000-01-16', SZ='19', TM='300', KM='4.5', HA='3', AB=['pd', 'dp', 'dd']), ...], branches=[GameTree(nodelist=[Node(B='qc', C=' [b]: \\ hi x! ;-) \\'), ...], ), ...]), ...)"
sgfdata1_mainline_str = r"""(
;GM[1]FF[4]CA[UTF-8]US[someone]CP[ Permission to reproduce this game is given.]GN[a-b]EV[None]RE[B+Resign]PW[a]WR[2k*]PB[b]BR[4k*]PC[somewhere]DT[2000-01-16]SZ[19]TM[300]KM[4.5]HA[3]AB[pd][dp][dd]
;W[pp]
;B[nq]
;W[oq]C[ x started observation.
]
;B[qc]C[ [b\]: \\ hi x! ;-) \\]
;W[kc]
)"""
def test_sgfdata1(self):
parser = sgflib.Parser(self.sgfdata1)
assert parser.encoding == 'latin-1'
collection = parser.parse()
assert len(collection) == 1
assert parser.encoding == 'UTF-8'
assert repr(collection) == self.sgfdata1_repr
assert str(collection) == self.sgfdata1_str
assert bytes(collection) == self.sgfdata1_str.encode('UTF-8')
mainline = collection[0].trunk()
assert str(mainline) == self.sgfdata1_mainline_str
parser2 = sgflib.Parser(bytes(collection))
collection2 = parser2.parse()
assert str(collection2) == str(collection)
sgfdata2_unicode = r"""(;GM[1]FF[4]CA[UTF-8]US[高橋]CP[©2021 Üñìṿé₹šâł ℂøđəδ ÇħÅŗ∀ćτεя §∊⊤…]GN[面白いゲーム]RE[B+Resign]PW[しろちゃん]WR[2k*]PB[くろくん]BR[4k*]PC[インターネット]DT[2021-01-01]SZ[19]TM[300]KM[4.5]HA[3]AB[pd][dp][dd];W[pp];B[nq];W[oq]C[誰が勝っている?](;B[qc]C[[しろちゃん\]: いい手だね];W[kc])(;B[hc];W[oe]))"""
sgfdata2 = sgfdata2_unicode.encode('utf-8')
sgfdata2_str = r"""(
;GM[1]FF[4]CA[UTF-8]US[高橋]CP[©2021 Üñìṿé₹šâł ℂøđəδ ÇħÅŗ∀ćτεя §∊⊤…]GN[面白いゲーム]RE[B+Resign]PW[しろちゃん]WR[2k*]PB[くろくん]BR[4k*]PC[インターネット]DT[2021-01-01]SZ[19]TM[300]KM[4.5]HA[3]AB[pd][dp][dd]
;W[pp]
;B[nq]
;W[oq]C[誰が勝っている?]
(
;B[qc]C[[しろちゃん\]: いい手だね]
;W[kc]
)
(
;B[hc]
;W[oe]
)
)"""
sgfdata2_repr = "Collection(GameTree(nodelist=[Node(GM='1', FF='4', CA='UTF-8', US='高橋', CP='©2021 Üñìṿé₹šâł ℂøđəδ ÇħÅŗ∀ćτεя §∊⊤…', GN='面白いゲーム', RE='B+Resign', PW='しろちゃん', WR='2k*', PB='くろくん', BR='4k*', PC='インターネット', DT='2021-01-01', SZ='19', TM='300', KM='4.5', HA='3', AB=['pd', 'dp', 'dd']), ...], branches=[GameTree(nodelist=[Node(B='qc', C='[しろちゃん]: いい手だね'), ...], ), ...]), ...)"
sgfdata2_mainline_str = r"""(
;GM[1]FF[4]CA[UTF-8]US[高橋]CP[©2021 Üñìṿé₹šâł ℂøđəδ ÇħÅŗ∀ćτεя §∊⊤…]GN[面白いゲーム]RE[B+Resign]PW[しろちゃん]WR[2k*]PB[くろくん]BR[4k*]PC[インターネット]DT[2021-01-01]SZ[19]TM[300]KM[4.5]HA[3]AB[pd][dp][dd]
;W[pp]
;B[nq]
;W[oq]C[誰が勝っている?]
;B[qc]C[[しろちゃん\]: いい手だね]
;W[kc]
)"""
def test_sgfdata2(self):
parser = sgflib.Parser(self.sgfdata2)
assert parser.encoding == 'latin-1'
collection = parser.parse()
assert len(collection) == 1
assert parser.encoding == 'UTF-8'
assert repr(collection) == self.sgfdata2_repr
assert str(collection) == self.sgfdata2_str
assert bytes(collection) == self.sgfdata2_str.encode('UTF-8')
mainline = collection[0].trunk()
assert str(mainline) == self.sgfdata2_mainline_str
parser2 = sgflib.Parser(bytes(collection))
collection2 = parser2.parse()
assert str(collection2) == str(collection)
class TestMerge:
sgfdata_singles = [
rb"""
(
;GM[1]FF[4]CA[UTF-8]
;W[pp]
;B[nq]
;W[oq]C[comment 1]
(
;B[qc]C[comment 2]
;W[kc]
)
(
;B[hc]
;W[oe]
)
)""",
# rb""" (;GM [1]FF[4]CA[UTF-8]US[someone]CP[\
# Permission to reproduce this game is given.]GN[a-b]EV[None]RE[B+Resign]
# PW[a]WR[2k*]PB[b]BR[4k*]PC[somewhere]DT[2000-01-16]SZ[19]TM[300]KM[4.5]
# HA[3]AB[pd][dp][dd];W[pp];B[nq];W[oq]C[ x started observation.
# ](;B[qc]C[ [b\]: \\ hi x! ;-) \\];W[kc])(;B[hc];W[oe])) """
]
expected_single_with_one_comment = [
rb"""
(
;GM[1]FF[4]CA[UTF-8]
;W[pp]
;B[nq]
;W[oq]C[comment 1]
(
;B[qc]C[comment 2]
;W[kc]
)
(
;B[hc]
;W[oe]
)
)""",
]
expected_single_with_two_comments = [
rb"""
(
;GM[1]FF[4]CA[UTF-8]C[[[first\]\]]
;W[pp]
;B[nq]
;W[oq]C[comment 1]
(
;B[qc]C[comment 2]
;W[kc]
)
(
;B[hc]
;W[oe]
)
)""",
]
def test_merge_once(self):
"""Merge single game once."""
for sgfdata in self.sgfdata_singles:
result = sgflib.Collection([sgflib.GameTree()])
parser = sgflib.Parser(sgfdata)
game = parser.parse()
game.normalize()
game_before = game.pretty()
result.merge(game)
game_after = game.pretty()
assert game_before == game_after
assert result.pretty() == game.pretty()
assert result == game
def test_merge_twice(self):
"""Merge single game twice."""
for sgfdata in self.sgfdata_singles:
result = sgflib.Collection([sgflib.GameTree()])
parser = sgflib.Parser(sgfdata)
game = parser.parse()
game.normalize()
game_before = game.pretty()
result.merge(game)
result.merge(game)
game_after = game.pretty()
assert game_before == game_after
assert result.pretty() == game.pretty()
assert result == game
def test_merge_self(self):
"""Merge single game with itself."""
for sgfdata in self.sgfdata_singles:
result = sgflib.Collection([sgflib.GameTree()])
parser = sgflib.Parser(sgfdata)
game = parser.parse()
game.normalize()
result.merge(game)
game.merge(game)
assert result.pretty() == game.pretty()
assert result == game
def test_merge_with_one_comment(self):
"""Merge single game twice, with a comment on the second merge."""
for (i, sgfdata) in enumerate(self.sgfdata_singles):
result = sgflib.Collection([sgflib.GameTree()])
parser = sgflib.Parser(sgfdata)
game = parser.parse()
game.normalize()
game_before = game.pretty()
result.merge(game)
result.merge(game, '[[second]]')
game_after = game.pretty()
assert game_before == game_after
expected = sgflib.Parser(
self.expected_single_with_one_comment[i]).parse()
expected.normalize()
assert result.pretty() == expected.pretty()
assert result == expected
def test_merge_with_two_comments(self):
for (i, sgfdata) in enumerate(self.sgfdata_singles):
result = sgflib.Collection([sgflib.GameTree()])
parser = sgflib.Parser(sgfdata)
game = parser.parse()
game.normalize()
game_before = game.pretty()
result.merge(game, '[[first]]')
result.merge(game, '[[second]]')
game_after = game.pretty()
assert game_before == game_after
expected = sgflib.Parser(
self.expected_single_with_two_comments[i]).parse()
expected.normalize()
assert result.pretty() == expected.pretty()
assert result == expected
merger_CLI_configs = (
# ((argv,), path_to_expected_output, load_expected_as_game),
(('--no-filename-comments',
'test_data/game1.sgf',
'test_data/game1copy.sgf'),
'test_data/game1.sgf',
True),
(('--filename-comments',
'test_data/game1.sgf',
'test_data/game1copy.sgf'),
'test_data/expected_self_merge_filename_comments.sgf',
True),
(('test_data/game1.sgf',
'test_data/game1copy.sgf',
'[[secondary 1]]'),
'test_data/expected_self_merge_secondary_comment.sgf',
True),
(('test_data/game2.sgf',
'test_data/game2a.sgf',
'[[secondary 2]]'),
'test_data/expected_merge_game_2_2a_with_comments.sgf',
True),
(('--pretty-format',
'test_data/game2.sgf',
'[[primary]]',
'test_data/game3.sgf',
'[[secondary 3]]'),
'test_data/expected_merge_game_2_3_with_comments.sgf',
False),
(('test_data/game4a.sgf',
'test_data/game4b.sgf'),
'test_data/expected_merge_game_4a_4b.sgf',
True),
# test for a completed game & its scratchpad with a prefix comment:
(('--comments-on-branches-only',
'test_data/game5_server.sgf',
'test_data/game5_scratchpad.sgf',
'[[scratchpad]]'),
'test_data/expected_game_5_merged.sgf',
True),
)
@pytest.mark.parametrize(
"argv, expected_path, load_expected_as_game", merger_CLI_configs)
def test_MergerCLI(self, argv, expected_path, load_expected_as_game):
cli = sgflib.MergerCLI(argv=argv)
with io.BytesIO() as cli.settings.output:
cli.run()
merged = cli.settings.output.getvalue()
with open(expected_path, 'rb') as expected_file:
expected = expected_file.read()
if load_expected_as_game:
game = sgflib.Collection.load(data=expected)
game.normalize()
expected = bytes(game)
assert merged == expected
## old version:
# def test_MergerCLI(self):
# for (i, config) in enumerate(self.merger_CLI_configs):
# argv, expected_path, normalize = config
# print(i, (argv, expected_path, normalize))
# cli = sgflib.MergerCLI(argv=argv)
# with io.BytesIO() as cli.settings.output:
# cli.run()
# merged = cli.settings.output.getvalue()
# if normalize:
# expected = sgflib.Collection.load(expected_path)
# expected.normalize()
# else:
# expected = open(expected_path, encoding='utf-8').read()
# assert merged.decode('utf-8') == str(expected)
# print(i, 'OK')
def self_test_1(onConsole=0):
"""Canned data test case"""
sgfdata = r""" (;GM [1]US[someone]CP[\
Permission to reproduce this game is given.]GN[a-b]EV[None]RE[B+Resign]
PW[a]WR[2k*]PB[b]BR[4k*]PC[somewhere]DT[2000-01-16]SZ[19]TM[300]KM[4.5]
HA[3]AB[pd][dp][dd];W[pp];B[nq];W[oq]C[ x started observation.
](;B[qc]C[ [b\]: \\ hi x! ;-) \\];W[kc])(;B[hc];W[oe])) """
print("\n\n********** Self-Test 1 **********\n")
print("Input data:\n")
print(sgfdata)
print("\n\nParsed data: ")
collection = sgflib.Parser(sgfdata).parse()
print("done\n")
cstr = str(collection)
print(cstr, "\n")
print("Trunk:\n")
m = collection[0].trunk()
print(m, "\n")
##print("as GameTree:\n")
##print(GameTree(m), "\n")
print("Tree traversal (forward):\n")
cursor = collection.cursor()
while 1:
print("nodenum: %s; index: %s; children: %s; node: %s" % (cursor.nodenum, cursor.index, len(cursor.children), cursor.node))
if cursor.at_end: break
cursor.next()
print("\nTree traversal (backward):\n")
while 1:
print("nodenum: %s; index: %s; children: %s; node: %s" % (cursor.nodenum, cursor.index, len(cursor.children), cursor.node))
if cursor.at_start: break
cursor.previous()
print("\nSearch for property 'B':")
print(collection[0].property_search("B", 1))
print("\nSearch for property 'C':")
print(collection[0].property_search("C", 1))
if __name__ == '__main__':
sys.exit(pytest.main())