-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
392 lines (359 loc) · 8.72 KB
/
parser_test.go
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
package pgn
import "testing"
func TestTagPairs(t *testing.T) {
tests := []struct {
input string
expectedName string
expectedValue string
}{
{`[Event "F/S Return Match"]`, "Event", "F/S Return Match"},
{`[Site "Belgrade, Serbia JUG"]`, "Site", "Belgrade, Serbia JUG"},
{`[Date "1992.11.04"]`, "Date", "1992.11.04"},
{`[Round "29"]`, "Round", "29"},
{`[White "Fischer, Robert J."]`, "White", "Fischer, Robert J."},
{`[Black "Spassky, Boris V."]`, "Black", "Spassky, Boris V."},
{`[Result "1/2-1/2"]`, "Result", "1/2-1/2"},
}
for _, tt := range tests {
l := newLexer(tt.input)
p := newParser(l)
game, _ := p.ParsePGN()
checkParserErrors(t, p)
if value, exists := game.TagPairs()[tt.expectedName]; !exists {
t.Errorf("tag %q not found in game tags", tt.expectedName)
} else if value != tt.expectedValue {
t.Errorf("tag %q has wrong value. got=%q, want=%q",
tt.expectedName, value, tt.expectedValue)
}
}
}
func TestGetTag(t *testing.T) {
tests := []struct {
input string
expectedName string
expectedValue string
}{
{`[Event "F/S Return Match"]`, "Event", "F/S Return Match"},
{`[Site "Belgrade, Serbia JUG"]`, "Site", "Belgrade, Serbia JUG"},
{`[Date "1992.11.04"]`, "Date", "1992.11.04"},
{`[Round "29"]`, "Round", "29"},
{`[White "Fischer, Robert J."]`, "White", "Fischer, Robert J."},
{`[Black "Spassky, Boris V."]`, "Black", "Spassky, Boris V."},
{`[Result "1/2-1/2"]`, "Result", "1/2-1/2"},
}
for _, tt := range tests {
l := newLexer(tt.input)
p := newParser(l)
game, _ := p.ParsePGN()
checkParserErrors(t, p)
if got := game.GetTag(tt.expectedName); got != tt.expectedValue {
t.Errorf("GetTag(%q) wrong value. got=%q, want=%q",
tt.expectedName, got, tt.expectedValue)
}
}
}
func TestMoves(t *testing.T) {
tests := []struct {
input string
expectedMoveNumber int
expectedMoveWhite string
expectedMoveBlack string
}{
{"1. e4 $69 e5", 1, "e4", "e5"},
{"2. Nf3 Nc6", 2, "Nf3", "Nc6"},
{"3. Bb5 a6", 3, "Bb5", "a6"},
{"4. Ba4 Nf6", 4, "Ba4", "Nf6"},
{"12. cxb5 axb5", 12, "cxb5", "axb5"},
{"24. Bxf7+ Rxf7", 24, "Bxf7+", "Rxf7"},
}
for _, tt := range tests {
l := newLexer(tt.input)
p := newParser(l)
game, _ := p.ParsePGN()
checkParserErrors(t, p)
if got := game.GetMove(tt.expectedMoveNumber).White(); got != tt.expectedMoveWhite {
t.Errorf("GetMove(%q) wrong value. got=%q, want=%q",
tt.expectedMoveNumber, got, tt.expectedMoveWhite)
}
}
}
func checkParserErrors(t *testing.T, p *parser) {
errors := p.Errors()
if len(errors) == 0 {
return
}
t.Errorf("Parser has %d errors", len(errors))
for _, msg := range errors {
t.Errorf("parser error: %q", msg)
}
t.FailNow()
}
func TestMovesWithNoPeriods(t *testing.T) {
tests := []struct {
input string
expectedMoveNumber int
expectedMoveWhite string
expectedMoveBlack string
}{
{"1 e4 e5", 1, "e4", "e5"},
{"2 Nf3 Nc6", 2, "Nf3", "Nc6"},
{"3 Bb5 a6", 3, "Bb5", "a6"},
{"4 Ba4 Nf6", 4, "Ba4", "Nf6"},
{"12 cxb5 axb5", 12, "cxb5", "axb5"},
{"24 Bxf7+ Rxf7", 24, "Bxf7+", "Rxf7"},
}
for _, tt := range tests {
l := newLexer(tt.input)
p := newParser(l)
game, _ := p.ParsePGN()
checkParserErrors(t, p)
if got := game.GetMove(tt.expectedMoveNumber).White(); got != tt.expectedMoveWhite {
t.Errorf("GetMove(%q) wrong value. got=%q, want=%q",
tt.expectedMoveNumber, got, tt.expectedMoveWhite)
}
}
}
func TestMovesWithThreePeriods(t *testing.T) {
tests := []struct {
input string
expectedMoveNumber int
expectedMoveWhite string
expectedMoveBlack string
}{
{"1... e4 e5", 1, "e4", "e5"},
{"2... Nf3 Nc6", 2, "Nf3", "Nc6"},
{"3... Bb5 a6", 3, "Bb5", "a6"},
{"4... Ba4 Nf6", 4, "Ba4", "Nf6"},
{"12... cxb5 axb5", 12, "cxb5", "axb5"},
{"24... Bxf7+ Rxf7", 24, "Bxf7+", "Rxf7"},
}
for _, tt := range tests {
l := newLexer(tt.input)
p := newParser(l)
game, _ := p.ParsePGN()
checkParserErrors(t, p)
if got := game.GetMove(tt.expectedMoveNumber).White(); got != tt.expectedMoveWhite {
t.Errorf("GetMove(%q) wrong value. got=%q, want=%q",
tt.expectedMoveNumber, got, tt.expectedMoveWhite)
}
}
}
func TestCompletePGN(t *testing.T) {
input := `
[Event "Live Chess"]
[Site "Chess.com"]
[Date "2024.11.03"]
[Round "?"]
[White "shbhtngpl"]
[Black "Michal_Chmara_2002"]
[Result "1-0"]
[TimeControl "180+2"]
[WhiteElo "1616"]
[BlackElo "1674"]
[Termination "shbhtngpl won by resignation"]
[Link "https://www.chess.com/game/live/124369775413"]
1. d4 Nf6 2. c4 g6 3. Nf3 Bg7 4. g3 O-O 5. Bg2 d6 6. O-O c5 7. e3 cxd4 8. exd4
Bg4 9. Nbd2 Qc7 10. h3 Bd7 11. b3 Nc6 12. Bb2 Rac8 13. Re1 Rfe8 14. d5 Nb4 15.
Ne4 Nxe4 16. Bxg7 Nxg3 17. Bc3 Qb6 18. Bxb4 Qxb4 19. fxg3 Qc5+ 20. Qd4 Qc7 21.
Qh4 e6 22. Ng5 h5 23. Qf4 Rf8 24. Qf6 exd5 25. Bxd5 Bc6 26. Rac1 Bxd5 27. cxd5
Qb6+ 28. Kh2 Rxc1 29. Rxc1 Qe3 30. Rf1 Qe2+ 31. Rf2 Qa6 32. Ne6 1-0
`
expectedTags := map[string]string{
"Event": "Live Chess",
"Site": "Chess.com",
"Date": "2024.11.03",
"Round": "?",
"White": "shbhtngpl",
"Black": "Michal_Chmara_2002",
"Result": "1-0",
"TimeControl": "180+2",
"WhiteElo": "1616",
"BlackElo": "1674",
"Termination": "shbhtngpl won by resignation",
"Link": "https://www.chess.com/game/live/124369775413",
}
expectedMoves := map[int]*Move{
1: &Move{
MoveNumber: 1,
MoveWhite: "d4",
MoveBlack: "Nf6",
},
2: &Move{
MoveNumber: 2,
MoveWhite: "c4",
MoveBlack: "g6",
},
3: &Move{
MoveNumber: 3,
MoveWhite: "Nf3",
MoveBlack: "Bg7",
},
4: &Move{
MoveNumber: 4,
MoveWhite: "g3",
MoveBlack: "O-O",
},
5: &Move{
MoveNumber: 5,
MoveWhite: "Bg2",
MoveBlack: "d6",
},
6: &Move{
MoveNumber: 6,
MoveWhite: "O-O",
MoveBlack: "c5",
},
7: &Move{
MoveNumber: 7,
MoveWhite: "e3",
MoveBlack: "cxd4",
},
8: &Move{
MoveNumber: 8,
MoveWhite: "exd4",
MoveBlack: "Bg4",
},
9: &Move{
MoveNumber: 9,
MoveWhite: "Nbd2",
MoveBlack: "Qc7",
},
10: &Move{
MoveNumber: 10,
MoveWhite: "h3",
MoveBlack: "Bd7",
},
11: &Move{
MoveNumber: 11,
MoveWhite: "b3",
MoveBlack: "Nc6",
},
12: &Move{
MoveNumber: 12,
MoveWhite: "Bb2",
MoveBlack: "Rac8",
},
13: &Move{
MoveNumber: 13,
MoveWhite: "Re1",
MoveBlack: "Rfe8",
},
14: &Move{
MoveNumber: 14,
MoveWhite: "d5",
MoveBlack: "Nb4",
},
15: &Move{
MoveNumber: 15,
MoveWhite: "Ne4",
MoveBlack: "Nxe4",
},
16: &Move{
MoveNumber: 16,
MoveWhite: "Bxg7",
MoveBlack: "Nxg3",
},
17: &Move{
MoveNumber: 17,
MoveWhite: "Bc3",
MoveBlack: "Qb6",
},
18: &Move{
MoveNumber: 18,
MoveWhite: "Bxb4",
MoveBlack: "Qxb4",
},
19: &Move{
MoveNumber: 19,
MoveWhite: "fxg3",
MoveBlack: "Qc5+",
},
20: &Move{
MoveNumber: 20,
MoveWhite: "Qd4",
MoveBlack: "Qc7",
},
21: &Move{
MoveNumber: 21,
MoveWhite: "Qh4",
MoveBlack: "e6",
},
22: &Move{
MoveNumber: 22,
MoveWhite: "Ng5",
MoveBlack: "h5",
},
23: &Move{
MoveNumber: 23,
MoveWhite: "Qf4",
MoveBlack: "Rf8",
},
24: &Move{
MoveNumber: 24,
MoveWhite: "Qf6",
MoveBlack: "exd5",
},
25: &Move{
MoveNumber: 25,
MoveWhite: "Bxd5",
MoveBlack: "Bc6",
},
26: &Move{
MoveNumber: 26,
MoveWhite: "Rac1",
MoveBlack: "Bxd5",
},
27: &Move{
MoveNumber: 27,
MoveWhite: "cxd5",
MoveBlack: "Qb6+",
},
28: &Move{
MoveNumber: 28,
MoveWhite: "Kh2",
MoveBlack: "Rxc1",
},
29: &Move{
MoveNumber: 29,
MoveWhite: "Rxc1",
MoveBlack: "Qe3",
},
30: &Move{
MoveNumber: 30,
MoveWhite: "Rf1",
MoveBlack: "Qe2+",
},
31: &Move{
MoveNumber: 31,
MoveWhite: "Rf2",
MoveBlack: "Qa6",
},
32: &Move{
MoveNumber: 32,
MoveWhite: "Ne6",
MoveBlack: "",
},
}
l := newLexer(input)
p := newParser(l)
game, _ := p.ParsePGN()
checkParserErrors(t, p)
for expectedKey, expectedValue := range expectedTags {
if value, exists := game.TagPairs()[expectedKey]; !exists {
t.Errorf("tag %q not found in game tags", expectedKey)
} else if value != expectedValue {
t.Errorf("tag %q has wrong value. got=%q, want=%q",
expectedKey, value, expectedValue)
}
}
for expectedMoveNumber, expectedValue := range expectedMoves {
if got := game.GetMove(expectedMoveNumber).White(); got != expectedValue.White() {
t.Errorf("GetMove(%q) wrong value. got=%q, want=%q",
expectedMoveNumber, got, expectedValue.White())
}
if got := game.GetMove(expectedMoveNumber).Black(); got != expectedValue.Black() {
t.Errorf("GetMove(%q) wrong value. got=%q, want=%q",
expectedMoveNumber, got, expectedValue.Black())
}
}
}