-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch_test.go
More file actions
89 lines (78 loc) · 1.76 KB
/
Copy pathbranch_test.go
File metadata and controls
89 lines (78 loc) · 1.76 KB
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
package gotak
import "testing"
func TestParseTurnLabel(t *testing.T) {
cases := []struct {
in string
num int64
branch string
ok bool
}{
{"1", 1, "", true},
{"12", 12, "", true},
{"1a", 1, "a", true},
{"42c", 42, "c", true},
{"", 0, "", false},
{"a1", 0, "", false},
{"1ab", 0, "", false},
{"1A", 0, "", false},
}
for _, c := range cases {
t.Run(c.in, func(t *testing.T) {
n, br, ok := parseTurnLabel(c.in)
if ok != c.ok || n != c.num || br != c.branch {
t.Errorf("parseTurnLabel(%q) = (%d,%q,%v), want (%d,%q,%v)", c.in, n, br, ok, c.num, c.branch, c.ok)
}
})
}
}
func TestParsePTN_branches(t *testing.T) {
ptn := []byte(`[Size "5"]
[Player1 "alice"]
[Player2 "bob"]
1. a1 e5
2. b2 d4
3. c3 c4
{Alternate continuation}
1a. a1 e5
2a. b2 d4
3a. e1 a5
`)
g, err := ParsePTN(ptn)
if err != nil {
t.Fatalf("ParsePTN: %v", err)
}
main := g.TurnsInBranch("")
if len(main) != 3 {
t.Errorf("main branch has %d turns, want 3", len(main))
}
a := g.TurnsInBranch("a")
if len(a) != 3 {
t.Errorf("branch 'a' has %d turns, want 3", len(a))
}
branches := g.Branches()
if len(branches) != 1 || branches[0] != "a" {
t.Errorf("Branches() = %v, want [a]", branches)
}
// Branch turns should keep their original numbering on parse.
for i, turn := range a {
if turn.Number != int64(i+1) {
t.Errorf("branch a turn %d has Number=%d, want %d", i, turn.Number, i+1)
}
if turn.Branch != "a" {
t.Errorf("branch a turn %d has Branch=%q, want \"a\"", i, turn.Branch)
}
}
}
func TestTurnText_branch(t *testing.T) {
turn := &Turn{
Number: 2,
Branch: "b",
First: &Move{Text: "a1"},
Second: &Move{Text: "e5"},
}
got := turn.Text()
want := "2b. a1 e5"
if got != want {
t.Errorf("Text() = %q, want %q", got, want)
}
}