-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablebased_test.go
229 lines (204 loc) · 5.28 KB
/
tablebased_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
package peg
import (
"fmt"
"sort"
"strconv"
"strings"
"testing"
"unicode/utf8"
)
// Test pattern match results, errors, groups and captures.
type patternTestData struct {
text string
ok bool
n int
fail bool
groups string
caps string
pat Pattern
}
func runPatternTestData(t *testing.T, data patternTestData) {
r, err := Match(data.pat, data.text)
if err != nil {
if data.fail {
t.Logf("INFO: the expected failure `%s` occurs when match(%s, %q)", err, data.pat, data.text)
} else {
t.Errorf("UNEXPECTED ERROR `%s` occurs when match(%s, %q)", err, data.pat, data.text)
}
return
} else if data.fail {
t.Errorf("EXPECTED BUT NO ERROR occurs when match(%s, %q)", data.pat, data.text)
return
}
if r.Ok != data.ok {
t.Errorf("RESULT DISMATCH: match(%s, %q) => (%v != %v, %d)",
data.pat, data.text, r.Ok, data.ok, r.N)
return
}
if data.ok && r.N != data.n {
t.Errorf("RESULT DISMATCH: match(%s, %q) => (%v, %d != %d)",
data.pat, data.text, r.Ok, r.N, data.n)
return
}
grps, ngrps := parseGroupsString(data.groups)
grouping0 := formatGrouping(r.Groups, r.NamedGroups)
grouping1 := formatGrouping(grps, ngrps)
if grouping0 != grouping1 {
t.Errorf("GROUPS DISMATCH: match(%s, %q) => {%s} != {%s}",
data.pat, data.text, grouping0, grouping1)
return
}
caps := formatCapList(r.Captures)
if caps != data.caps {
t.Errorf("CAPTURES DISMATCH: match(%s, %q) => %q != %q",
data.pat, data.text, caps, data.caps)
return
}
}
func parseGroupsString(groups string) (grps []string, ngrps map[string]string) {
if len(groups) == 0 {
return
}
ngrps = make(map[string]string)
for _, item := range strings.Split(groups, ",") {
fields := strings.SplitN(item, "=", 2)
if len(fields) < 2 {
s, _ := strconv.Unquote(item)
grps = append(grps, s)
} else {
k, _ := strconv.Unquote(fields[0])
v, _ := strconv.Unquote(fields[1])
ngrps[k] = v
}
}
return grps, ngrps
}
func formatGrouping(grps []string, ngrps map[string]string) string {
gstrs := make([]string, 0, len(grps))
for _, s := range grps {
gstrs = append(gstrs, strconv.Quote(s))
}
nstrs := make([]string, 0, len(ngrps))
for k, v := range ngrps {
nstrs = append(nstrs, fmt.Sprintf("%q=%q", k, v))
}
sort.Strings(nstrs)
gstr := strings.Join(gstrs, ",")
nstr := strings.Join(nstrs, ",")
switch {
case len(gstr) == 0 && len(nstr) == 0:
return ""
case len(gstr) == 0 && len(nstr) != 0:
return nstr
case len(gstr) != 0 && len(nstr) == 0:
return gstr
default:
return nstr + "," + gstr
}
}
func formatCapList(caps []Capture) string {
strs := make([]string, len(caps))
for i := range caps {
strs[i] = formatCap(caps[i])
}
return strings.Join(strs, ", ")
}
func formatCap(cap Capture) string {
if cap.IsTerminal() {
if tok, ok := cap.(*Token); ok {
return fmt.Sprintf("<%d%q>", tok.Type, tok.Value)
}
return fmt.Sprintf("<%v>", cap)
} else if v, ok := cap.(*Variable); ok {
return fmt.Sprintf("%s(%s)", v.Name, formatCapList(v.Subs))
}
return fmt.Sprintf("%v", cap)
}
// Test pattern match side effects.
type (
sideEffectsTestContext struct {
states []string
}
sideEffectsTestData struct {
text string
ok bool
n int
fail bool
state string
metapat func(*sideEffectsTestContext) Pattern
}
)
func runSideEffectsTestData(t *testing.T, ctx *sideEffectsTestContext, data sideEffectsTestData) {
ctx.clear()
pat := data.metapat(ctx)
r, err := Match(pat, data.text)
if err != nil {
if data.fail {
t.Logf("INFO: the expected failure `%s` occurs when match(%s, %q)", err, pat, data.text)
} else {
t.Errorf("UNEXPECTED ERROR `%s` occurs when match(%s, %q)", err, pat, data.text)
}
return
} else if data.fail {
t.Errorf("EXPECTED BUT NO ERROR occurs when match(%s, %q)", pat, data.text)
return
}
if r.Ok != data.ok {
t.Errorf("RESULT DISMATCH: match(%s, %q) => (%v != %v, %d)",
pat, data.text, r.Ok, data.ok, r.N)
return
}
if data.ok && r.N != data.n {
t.Errorf("RESULT DISMATCH: match(%s, %q) => (%v, %d != %d)",
pat, data.text, r.Ok, r.N, data.n)
return
}
state := ctx.format()
if state != data.state {
t.Errorf("SIDE EFFECTS DISMATCH: match(%s, %q) => %q != %q",
pat, data.text, state, data.state)
return
}
}
func newSideEffectsTestContext() *sideEffectsTestContext {
return &sideEffectsTestContext{}
}
func (ctx *sideEffectsTestContext) clear() {
if ctx.states != nil {
ctx.states = ctx.states[:0]
}
}
func (ctx *sideEffectsTestContext) store(value string) {
ctx.states = append(ctx.states, value)
}
func (ctx *sideEffectsTestContext) format() string {
strs := make([]string, len(ctx.states))
for i := range ctx.states {
strs[i] = quoteState(ctx.states[i])
}
return "{" + strings.Join(strs, ", ") + "}"
}
func quoteState(s string) string {
const (
specials = ",{} \\'"
noescape = ",{} "
)
lit := ""
for {
i := strings.IndexAny(s, specials)
if i < 0 {
lit += s
break
}
lit += s[:i]
s = s[i:]
r, n := utf8.DecodeRuneInString(s)
if strings.ContainsRune(noescape, r) {
lit += s[:n]
} else {
lit += "\\" + s[:n]
}
s = s[n:]
}
return "'" + lit + "'"
}