forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_test.go
368 lines (357 loc) · 6.42 KB
/
type_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
package expr_test
import (
"fmt"
"strings"
"testing"
"github.com/antonmedv/expr"
)
type typeTest string
type typeErrorTest struct {
input string
err string
}
var typeTests = []typeTest{
"Foo.Bar.Baz",
"Arr[0].Bar.Baz",
"Map['string'].Bar.Baz",
"Map.id.Bar.Baz",
"Any.Thing.Is.Ok",
"Irr['string'].next.goes['any thing']",
"Fn(Any)",
"Foo.Fn()",
"true ? Any : Any",
"Str ~ (true ? Str : Str)",
"Ok && Any",
"Str matches 'ok'",
"Str matches Any",
"Any matches Any",
"len([])",
"true == false",
"nil",
"!Ok",
"[1,2,3]",
"{id: Foo.Bar.Baz, (1+1): Ok}",
"Abc()",
"Foo.Abc()",
"'a' == 'b' ~ 'c'",
"Num == 1",
"Int == Num",
"Num == Abc",
"Abc == Num",
"1 == 2 and true or Ok",
"Int == Any",
"IntPtr == Int",
"!OkPtr == Ok",
"1 == NumPtr",
"Foo.Bar == Map.id.Bar",
"StrPtr == nil",
"nil == nil",
"nil == IntPtr",
"Foo2p.Bar.Baz",
"Str in Foo",
"Str in Arr",
"nil in Arr",
"Str not in Foo2p",
"Int | Num",
"Int ^ Num",
"Int & Num",
"Int < Num",
"Int > Num",
"Int >= Num",
"Int <= Num",
"Int + Num",
"Int - Num",
"Int * Num",
"Int / Num",
"Int % Num",
"Int ** Num",
"Int .. Num",
"Int + Int + Int",
"Int % Int > 1",
"Int in Int..Int",
"EmbStr == ''",
"Embedded.EmbStr",
"EmbPtrStr == ''",
"EmbeddedPtr.EmbPtrStr ~ Str",
"SubStr ~ ''",
"SubEmbedded.SubStr",
"OkFn() and OkFn()",
"Foo.Fn() or Foo.Fn()",
"Method() > 1",
"Embedded.Method() ~ Str",
}
var typeErrorTests = []typeErrorTest{
{
"Foo.Bar.Not",
"Foo.Bar.Not undefined (type expr_test.bar has no field Not)",
},
{
"Noo",
"unknown name Noo",
},
{
"Noo()",
"unknown func Noo()",
},
{
"Foo()",
"unknown func Foo()",
},
{
"Foo['string']",
`invalid operation: Foo["string"] (type *expr_test.foo does not support indexing)`,
},
{
"Foo.Fn(Not)",
"unknown name Not",
},
{
"Foo.Bar()",
"Foo.Bar() undefined (type *expr_test.foo has no method Bar)",
},
{
"Foo.Bar.Not()",
"Foo.Bar.Not() undefined (type expr_test.bar has no method Not)",
},
{
"Arr[0].Not",
"Arr[0].Not undefined (type *expr_test.foo has no field Not)",
},
{
"Arr[Not]",
"unknown name Not",
},
{
"Not[0]",
"unknown name Not",
},
{
"Not.Bar",
"unknown name Not",
},
{
"Arr.Not",
"Arr.Not undefined (type []*expr_test.foo has no field Not)",
},
{
"Fn(Not)",
"unknown name Not",
},
{
"Map['str'].Not",
`Map["str"].Not undefined (type *expr_test.foo has no field Not)`,
},
{
"Ok && IntPtr",
"invalid operation: Ok && IntPtr (mismatched types bool and *int)",
},
{
"No ? Any.Ok : Any.Not",
"unknown name No",
},
{
"Any.Cond ? No : Any.Not",
"unknown name No",
},
{
"Any.Cond ? Any.Ok : No",
"unknown name No",
},
{
"Many ? Any : Any",
"non-bool Many (type map[string]interface {}) used as condition",
},
{
"Str matches Int",
"invalid operation: (Str matches Int) (mismatched types string and int)",
},
{
"Int matches Str",
"invalid operation: (Int matches Str) (mismatched types int and string)",
},
{
"!Not",
"unknown name Not",
},
{
"Not == Any",
"unknown name Not",
},
{
"[Not]",
"unknown name Not",
},
{
"{id: Not}",
"unknown name Not",
},
{
"{(Not): Any}",
"unknown name Not",
},
{
"(nil).Foo",
"nil.Foo undefined (type <nil> has no field Foo)",
},
{
"(nil)['Foo']",
`invalid operation: nil["Foo"] (type <nil> does not support indexing)`,
},
{
"1 and false",
"invalid operation: 1 and false (mismatched types float64 and bool)",
},
{
"true or 0",
"invalid operation: true or 0 (mismatched types bool and float64)",
},
{
"not IntPtr",
"invalid operation: not IntPtr (mismatched type *int)",
},
{
"len(Not)",
"unknown name Not",
},
{
"Int | Ok",
"invalid operation: Int | Ok (mismatched types int and bool)",
},
{
"Int ^ Ok",
"invalid operation: Int ^ Ok (mismatched types int and bool)",
},
{
"Int & Ok",
"invalid operation: Int & Ok (mismatched types int and bool)",
},
{
"Int < Ok",
"invalid operation: Int < Ok (mismatched types int and bool)",
},
{
"Int > Ok",
"invalid operation: Int > Ok (mismatched types int and bool)",
},
{
"Int >= Ok",
"invalid operation: Int >= Ok (mismatched types int and bool)",
},
{
"Int <= Ok",
"invalid operation: Int <= Ok (mismatched types int and bool)",
},
{
"Int + Ok",
"invalid operation: Int + Ok (mismatched types int and bool)",
},
{
"Int - Ok",
"invalid operation: Int - Ok (mismatched types int and bool)",
},
{
"Int * Ok",
"invalid operation: Int * Ok (mismatched types int and bool)",
},
{
"Int / Ok",
"invalid operation: Int / Ok (mismatched types int and bool)",
},
{
"Int % Ok",
"invalid operation: Int % Ok (mismatched types int and bool)",
},
{
"Int ** Ok",
"invalid operation: Int ** Ok (mismatched types int and bool)",
},
{
"Int .. Ok",
"invalid operation: Int .. Ok (mismatched types int and bool)",
},
{
"NilFn() and OkFn()",
"invalid operation: NilFn() and OkFn() (mismatched types <nil> and bool)",
},
{
"'str' in Str",
`invalid operation: "str" in Str (mismatched types string and string)`,
},
{
"1 in Foo",
"invalid operation: 1 in Foo (mismatched types float64 and *expr_test.foo)",
},
{
"1 ~ ''",
`invalid operation: 1 ~ "" (mismatched types float64 and string)`,
},
}
type abc interface {
Abc()
}
type bar struct {
Baz string
}
type foo struct {
Bar bar
Fn func() bool
Abc abc
}
type SubEmbedded struct {
SubStr string
}
type Embedded struct {
SubEmbedded
EmbStr string
}
type EmbeddedPtr struct {
EmbPtrStr string
}
type payload struct {
Embedded
*EmbeddedPtr
Abc abc
Foo *foo
Arr []*foo
Map map[string]*foo
Any interface{}
Irr []interface{}
Many map[string]interface{}
Fn func()
Ok bool
Num float64
Int int
Str string
OkPtr *bool
NumPtr *float64
IntPtr *int
StrPtr *string
Foo2p **foo
OkFn func() bool
NilFn func()
}
func (p payload) Method() int {
return 0
}
func (p Embedded) Method() string {
return ""
}
func TestType(t *testing.T) {
for _, test := range typeTests {
_, err := expr.Parse(string(test), expr.Env(&payload{}))
if err != nil {
t.Errorf("%s:\n\t%+v", test, err.Error())
}
}
}
func TestType_error(t *testing.T) {
for _, test := range typeErrorTests {
_, err := expr.Parse(test.input, expr.Env(&payload{}))
if err == nil {
err = fmt.Errorf("<nil>")
}
if !strings.HasPrefix(err.Error(), test.err) || test.err == "" {
t.Errorf("%s:\ngot\n\t%+v\nexpected\n\t%v", test.input, err.Error(), test.err)
}
}
}