-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.py
executable file
·129 lines (94 loc) · 2.66 KB
/
test.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
#!/usr/bin/env python
import glob
import sgf
try:
from StringIO import StringIO # pragma: no cover
except ImportError: # pragma: no cover
from io import StringIO # pragma: no cover
for filename in glob.glob("examples/*.sgf"):
with open(filename) as f:
sgf.parse(f.read())
example = "(;FF[4]GM[1]SZ[19];B[aa];W[bb];B[cc];W[dd];B[ad];W[bd])"
collection = sgf.parse(example)
for game in collection:
for node in game:
pass
out = StringIO()
collection[0].nodes[1].output(out)
assert out.getvalue() == ";B[aa]"
out.close()
out = StringIO()
collection.output(out)
assert out.getvalue() == example
out.close()
example2 = "(;FF[4]GM[1]SZ[19];B[aa];W[bb](;B[cc];W[dd];B[ad];W[bd])" \
"(;B[hh];W[hg]))"
collection = sgf.parse(example2)
out = StringIO()
collection.output(out)
assert out.getvalue() == example2
out.close()
example3 = "(;C[foo\\]\\\\])"
collection = sgf.parse(example3)
assert collection[0].nodes[0].properties["C"] == ["foo]\\"]
out = StringIO()
collection.output(out)
assert out.getvalue() == example3
out.close()
sgf.parse("foo(;)") # junk before first ( is supported
sgf.parse("( ;)") # whitespace after ( is allowed
sgf.parse("(;;)") # a node after an empty node is allowed
sgf.parse("(;(;))") # a gametree after an empty node is allowed
# errors
try:
sgf.parse("()") # games must have a node
assert False # pragma: no cover
except sgf.ParseException:
pass
try:
sgf.parse("(W[tt])") # a property has to be in a node
assert False # pragma: no cover
except sgf.ParseException:
pass
try:
sgf.parse("(;)W[tt]") # a property has to be in a game
assert False # pragma: no cover
except sgf.ParseException:
pass
try:
sgf.parse("(;1)") # property names can't start with numbers
assert False # pragma: no cover
except sgf.ParseException:
pass
try:
sgf.parse("(;A5[])") # property names can't have numbers at all
assert False # pragma: no cover
except sgf.ParseException:
pass
try:
sgf.parse("(;FOO[bar]5)") # bad character after a property value
assert False # pragma: no cover
except sgf.ParseException:
pass
try:
sgf.parse("(;") # finished mid-gametree
assert False # pragma: no cover
except sgf.ParseException:
pass
# new features for 0.5
with open("examples/ff4_ex.sgf") as f:
ff4_ex = sgf.parse(f.read())
assert len(ff4_ex) == 2
game1 = ff4_ex[0]
assert game1.root.properties["SZ"] == ["19"]
count = 0
for node in game1.rest:
count += 1
assert count == 13
collection = sgf.parse(example2)
count = 0
for node in collection[0].rest:
count += 1
assert count == 6
# test game.rest if only one node
assert sgf.parse("(;)")[0].rest is None