-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoice_test.go
116 lines (107 loc) · 3.24 KB
/
choice_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
package cmb
import (
"fmt"
"testing"
)
func TestChoice(t *testing.T) {
parser := &Parser{}
sourceString := []byte("abcdefghijkl")
returnA := &ParseTreeNode{"a", []byte("abc"), 0, 3, sourceString, []*ParseTreeNode{}}
returnB := &ParseTreeNode{"b", []byte("def"), 3, 6, sourceString, []*ParseTreeNode{}}
returnC := &ParseTreeNode{"c", []byte("ghi"), 6, 9, sourceString, []*ParseTreeNode{}}
errorA := fmt.Errorf("a")
errorB := fmt.Errorf("b")
errorC := fmt.Errorf("c")
makeParselet := func(val *ParseTreeNode, err error) Parselet {
return func(s []byte, pos int, parser *Parser) (*ParseTreeNode, error) { return val, err }
}
parselet := Choice()
actualVal, actualErr := parselet(sourceString, 3, parser)
if actualVal != nil {
t.Errorf("Expected val to be nil, got %v", actualVal)
}
if actualErr == nil {
t.Errorf("Expected err to be non-nil")
}
parselet = Choice(
makeParselet(nil, errorA),
makeParselet(nil, errorB),
makeParselet(nil, errorC),
)
actualVal, actualErr = parselet(sourceString, 3, parser)
if actualVal != nil {
t.Errorf("Expected val to be nil, got %v", actualVal)
}
if actualErr == nil {
t.Errorf("Expected err to be non-nil")
}
parselet = Choice(
makeParselet(returnA, nil),
makeParselet(nil, errorA),
makeParselet(nil, errorB),
makeParselet(nil, errorC),
)
actualVal, actualErr = parselet(sourceString, 0, parser)
if actualErr != nil {
t.Errorf("Expected err to be nil, got %v", actualErr)
}
if string(actualVal.Text) != "abc" {
t.Errorf("val.Text: expected '%v', got '%v'", "abc", string(actualVal.Text))
}
parselet = Choice(
makeParselet(nil, errorA),
makeParselet(nil, errorB),
makeParselet(nil, errorC),
makeParselet(returnA, nil),
)
actualVal, actualErr = parselet(sourceString, 0, parser)
if actualErr != nil {
t.Errorf("Expected err to be nil, got %v", actualErr)
}
if string(actualVal.Text) != "abc" {
t.Errorf("val.Text: expected '%v', got '%v'", "abc", string(actualVal.Text))
}
parselet = Choice(
makeParselet(nil, errorA),
makeParselet(returnA, nil),
makeParselet(nil, errorB),
makeParselet(nil, errorC),
)
actualVal, actualErr = parselet(sourceString, 0, parser)
if actualErr != nil {
t.Errorf("Expected err to be nil, got %v", actualErr)
}
if string(actualVal.Text) != "abc" {
t.Errorf("val.Text: expected '%v', got '%v'", "abc", string(actualVal.Text))
}
parselet = Choice(
makeParselet(nil, errorA),
makeParselet(returnA, nil),
makeParselet(nil, errorB),
makeParselet(returnB, nil),
makeParselet(nil, errorC),
makeParselet(returnC, nil),
)
actualVal, actualErr = parselet(sourceString, 0, parser)
if actualErr != nil {
t.Errorf("Expected err to be nil, got %v", actualErr)
}
if string(actualVal.Text) != "abc" {
t.Errorf("val.Text: expected '%v', got '%v'", "abc", string(actualVal.Text))
}
parselet = Choice(
makeParselet(nil, errorA),
makeParselet(returnB, nil),
makeParselet(nil, errorB),
makeParselet(returnA, nil),
makeParselet(nil, errorC),
makeParselet(returnC, nil),
)
actualVal, actualErr = parselet(sourceString, 0, parser)
if actualErr != nil {
t.Errorf("Expected err to be nil, got %v", actualErr)
}
if string(actualVal.Text) != "def" {
t.Errorf("val.Text: expected '%v', got '%v'", "def", string(actualVal.Text))
}
}