-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpcappats_test.go
275 lines (247 loc) · 8.06 KB
/
grpcappats_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
package peg
import (
"fmt"
"strconv"
"strings"
"testing"
)
// Tests for grouping and capturing.
// Tests Cgrp, Cname, Check, Trunc, Inject, Ref, RefB.
func TestGroupInjectAndRefer(t *testing.T) {
zeroes := func(s string) (int, bool) {
var i int
var r rune
for i, r = range s {
if r != '0' {
break
}
}
if i > 0 {
return i, true
}
return 0, false
}
patDigit := R('0', '9')
patSpaces := Q0(U("White_Space"))
patFloating := Check(func(s string) bool {
s = strings.ToLower(s)
s = strings.SplitN(s, "e", 2)[0] // fraction part
s = strings.TrimLeft(s, "+-") // fraction part without leading +/-
return s != "."
},
Seq(
Q01(S("+-")),
Alt(
Seq(Q0(patDigit), T("."), Q0(patDigit)),
Q1(patDigit)),
Q01(Seq(S("eE"), Q01(S("+-")), Q1(patDigit)))))
patJudge := Seq(
NG("num", patFloating),
patSpaces,
Alt(
Seq(T("=="), patSpaces, Ref("num")),
Seq(T("!="), patSpaces, Not(Ref("num")), patFloating)))
data := []patternTestData{
{"", true, 0, false, `""`, ``, G(True)},
{"", false, 0, false, ``, ``, G(False)},
{"", false, 0, false, ``, ``, Seq(True, Seq(G(True), False))},
{"", false, 0, false, ``, ``, Seq(G(True), False)},
{"AAA", true, 3, false, `"AAA"`, ``, G(Q0(T("A")))},
{"AAA", true, 3, false, `"A","A","A"`, ``, Q0(G(T("A")))},
{"", true, 0, false, `"cap"=""`, ``, NG("cap", True)},
{"", false, 0, false, ``, ``, NG("cap", False)},
{"AAA", true, 3, false, `"cap"="AAA"`, ``, NG("cap", Q0(T("A")))},
{"AAA", true, 3, false, `"cap"="A"`, ``, Q0(NG("cap", T("A")))},
{"ABC", true, 3, false, `"cap"="C"`, ``, Q0(NG("cap", S("ABC")))}, // overwrites
{"+1.e-2", true, 6, false, `"num"="+1.e-2"`, ``, NG("num", patFloating)},
{"", false, 0, false, ``, ``, Check(nil, Dot)},
{"0", true, 1, false, ``, ``, Check(nil, Dot)},
{"0", true, 1, false, ``, ``, Check(func(s string) bool { return s == "0" }, Dot)},
{"1", false, 0, false, ``, ``, Check(func(s string) bool { return s == "0" }, Dot)},
{"", true, 0, false, ``, ``, Trunc(2, Q0(Dot))},
{"A", true, 1, false, ``, ``, Trunc(2, Q0(Dot))},
{"AA", true, 2, false, ``, ``, Trunc(2, Q0(Dot))},
{"AAA", true, 2, false, ``, ``, Trunc(2, Q0(Dot))},
{"AAA", true, 0, false, ``, ``, Trunc(0, Q0(Dot))},
{"AAA", false, 0, false, ``, ``, Trunc(-1, Q0(Dot))},
{"", false, 0, false, ``, ``, Inject(nil, Dot)},
{"0", true, 1, false, ``, ``, Inject(nil, Dot)},
{"0246", true, 1, false, ``, ``, Inject(zeroes, Seq(Dot, Dot, Dot, Dot))},
{"0046", true, 2, false, ``, ``, Inject(zeroes, Seq(Dot, Dot, Dot, Dot))},
{"1246", false, 0, false, ``, ``, Inject(zeroes, Seq(Dot, Dot, Dot, Dot))},
{"+1.e-2 == +1.e-2", true, 16, false, `"num"="+1.e-2"`, ``, patJudge},
{"+1.e-2 == .1e2", false, 0, false, ``, ``, patJudge},
{"+1.e-2 != .1e2", true, 14, false, `"num"="+1.e-2"`, ``, patJudge},
{"+1.e-2 != +1.e-2", false, 0, false, ``, ``, patJudge},
{"ABC", false, 0, false, ``, ``, Seq(G(Dot), Q0(Dot), RefB(""))},
{"ABA", true, 3, false, `"A"`, ``, Seq(G(Dot), Q0(Dot), RefB(""))},
}
for _, d := range data {
runPatternTestData(t, d)
}
}
// Tests Let, Var, Cvar, Ctoken.
func TestGrammar(t *testing.T) {
scope := map[string]Pattern{
"T": True,
"F": False,
"D": Dot,
"v0": T("A"),
"vt": CK(0, T("A")),
"vq": Q1(CK(0, T("A"))),
"U": V("undefined_var"),
}
recurscope := map[string]Pattern{
"R0": V("R0"),
"R1": When(True, V("R1")),
"Ra": V("Rb"),
"Rb": V("Ra"),
}
data := []patternTestData{
{"", true, 0, false, ``, ``, Let(scope, V("T"))},
{"A", true, 1, false, ``, ``, Let(scope, V("v0"))},
{"", false, 0, true, ``, ``, Let(scope, V("undef"))}, // error variable undefined
{"", false, 0, true, ``, ``, Let(scope, V("U"))}, // error variable undefined
{"", true, 0, false, ``, ``,
Let(scope, Let(map[string]Pattern{ // override the definition of U
"U": True,
}, V("U")))}, // no error occurs
{"", false, 0, false, ``, ``, Let(scope,
CV("v0"))},
{"A", true, 1, false, ``, `v0()`, Let(scope,
CV("v0"))},
{"A", true, 1, false, ``, `vt(<0"A">)`, Let(scope,
CV("vt"))},
{"A", true, 1, false, ``, `vq(<0"A">)`, Let(scope,
CV("vq"))},
{"A", true, 1, false, ``, `v0()`, Let(scope,
Q0(CV("v0")))},
{"A", true, 1, false, ``, `vt(<0"A">)`, Let(scope,
Q0(CV("vt")))},
{"A", true, 1, false, ``, `vq(<0"A">)`, Let(scope,
Q0(CV("vq")))},
{"AA", true, 2, false, ``, `v0(), v0()`, Let(scope,
Q0(CV("v0")))},
{"AA", true, 2, false, ``, `vt(<0"A">), vt(<0"A">)`, Let(scope,
Q0(CV("vt")))},
{"AA", true, 2, false, ``, `vq(<0"A">, <0"A">)`, Let(scope,
Q0(CV("vq")))},
// Tests max recursion
{"", false, 0, true, ``, ``, Let(recurscope, V("R0"))},
{"", false, 0, true, ``, ``, Let(recurscope, V("R1"))},
{"", false, 0, true, ``, ``, Let(recurscope, V("Ra"))},
{"", false, 0, true, ``, ``, Let(recurscope, V("Rb"))},
}
for _, d := range data {
runPatternTestData(t, d)
}
}
// Tests Cterm, Ccons.
type termInt int32
func (t termInt) IsTerminal() bool {
return true
}
type termOp string
func (t termOp) IsTerminal() bool {
return true
}
func TestCustomedConstructor(t *testing.T) {
intcons := func(text string, pos Position) (Capture, error) {
i, err := strconv.ParseInt(text, 10, 32)
if err != nil {
return nil, err
}
return termInt(int32(i)), nil
}
opcons := func(text string, pos Position) (Capture, error) {
return termOp(text), nil
}
eval := func(caps []Capture) (Capture, error) {
if len(caps) <= 0 || len(caps)%2 != 1 {
return nil, fmt.Errorf("eval: invalid argument number %d", len(caps))
}
x, ok := caps[0].(termInt)
if !ok {
return nil, fmt.Errorf("eval: expect number but get %v", caps[0])
}
caps = caps[1:]
for len(caps) > 0 {
op, ok := caps[0].(termOp)
if !ok {
return nil, fmt.Errorf("eval: expect op but get %v", caps[0])
}
y, ok := caps[1].(termInt)
if !ok {
return nil, fmt.Errorf("eval: expect number but get %v", caps[1])
}
caps = caps[2:]
switch string(op) {
case "+":
x = termInt(int32(x) + int32(y))
case "-":
x = termInt(int32(x) - int32(y))
case "*":
x = termInt(int32(x) * int32(y))
case "/":
if int32(y) == 0 {
return nil, fmt.Errorf("eval: division by zero")
}
x = termInt(int32(x) / int32(y))
default:
return nil, fmt.Errorf("eval: unknown op: %q", string(op))
}
}
return x, nil
}
patDigit := R('0', '9')
patSpace := Q0(U("White_Space"))
patNumber := Seq(CT(intcons, Q1(patDigit)), patSpace)
patTermOp := Seq(CT(opcons, S("+-")), patSpace)
patFactorOp := Seq(CT(opcons, S("*/")), patSpace)
patOpen := Seq(T("("), patSpace)
patClose := Seq(T(")"), patSpace)
patExpr := Let(map[string]Pattern{
"factor": Alt(
patNumber,
Seq(patOpen, V("expr"), patClose)),
"term": CC(eval, Seq(V("factor"), Q0(Seq(patFactorOp, V("factor"))))),
"expr": CC(eval, Seq(V("term"), Q0(Seq(patTermOp, V("term"))))),
}, V("expr"))
data := []patternTestData{
{"", false, 0, false, ``, ``, patExpr},
{"A", false, 0, false, ``, ``, patExpr},
{"0", true, 1, false, ``, `<0>`, patExpr},
{"1", true, 1, false, ``, `<1>`, patExpr},
{"1+2", true, 3, false, ``, `<3>`, patExpr},
{"1-2", true, 3, false, ``, `<-1>`, patExpr},
{"3*(1+2)", true, 7, false, ``, `<9>`, patExpr},
{"1-2*((3+4)/5+6*(7-8))/9", true, 23, false, ``, `<2>`, patExpr},
{"10000000000", false, 0, true, ``, ``, patExpr}, // bigger than max int 32
{"1/0", false, 0, true, ``, ``, patExpr}, // division by zero
}
for _, d := range data {
runPatternTestData(t, d)
}
}
// Test Trigger.
func TestTrigger(t *testing.T) {
storing := func(pat Pattern) func(ctx *sideEffectsTestContext) Pattern {
return func(ctx *sideEffectsTestContext) Pattern {
return Trigger(func(s string, pos Position) error {
ctx.store(s)
return nil
}, pat)
}
}
data := []sideEffectsTestData{
{"", false, 0, false, "{}", storing(False)},
{"", true, 0, false, "{''}", storing(True)},
{"", false, 0, false, "{}", storing(T("A"))},
{"A", true, 1, false, "{'A'}", storing(T("A"))},
{"B", false, 0, false, "{}", storing(T("A"))},
}
ctx := newSideEffectsTestContext()
for _, d := range data {
runSideEffectsTestData(t, ctx, d)
}
}