1
1
package expr_test
2
2
3
3
import (
4
- "os"
5
4
"regexp"
6
5
"strings"
7
6
"testing"
8
7
9
8
"github.com/antonmedv/expr"
10
- "github.com/antonmedv/expr/test/playground"
11
9
)
12
10
13
11
func FuzzExpr (f * testing.F ) {
14
- env := playground .ExampleData ()
15
-
16
- b , err := os .ReadFile ("testdata/corpus.txt" )
17
- if err != nil {
18
- f .Fatal (err )
12
+ env := map [string ]interface {}{
13
+ "i" : 1 ,
14
+ "j" : 2 ,
15
+ "a" : []int {1 , 2 , 3 },
16
+ "m" : map [string ]interface {}{"a" : 1 , "b" : 2 , "m" : map [string ]int {"a" : 1 }},
17
+ "s" : "abc" ,
19
18
}
20
- for _ , testcase := range strings .Split (string (b ), "\n " ) {
21
- f .Add (testcase )
19
+
20
+ fn := expr .Function (
21
+ "fn" ,
22
+ func (params ... interface {}) (interface {}, error ) {
23
+ return params [0 ], nil
24
+ },
25
+ new (func (int ) int ),
26
+ )
27
+
28
+ corpus := []string {
29
+ `.5 + .5` ,
30
+ `i + j` ,
31
+ `i - j` ,
32
+ `i * j` ,
33
+ `i / j` ,
34
+ `i % j` ,
35
+ `true || false` ,
36
+ `true && false` ,
37
+ `i == j` ,
38
+ `i != j` ,
39
+ `i > j` ,
40
+ `i >= j` ,
41
+ `i < j` ,
42
+ `i <= j` ,
43
+ `i in a` ,
44
+ `i not in a` ,
45
+ `i in m` ,
46
+ `m.a` ,
47
+ `m.m.a` ,
48
+ `a[0]` ,
49
+ `a[i]` ,
50
+ `a[i:j]` ,
51
+ `a[i:]` ,
52
+ `a[:j]` ,
53
+ `a[:]` ,
54
+ `a[1:-1]` ,
55
+ `len(a)` ,
56
+ `abs(-1)` ,
57
+ `int(0.5)` ,
58
+ `float(42)` ,
59
+ `string(i)` ,
60
+ `trim(" a ")` ,
61
+ `trim("_a_", "_")` ,
62
+ `trimPrefix(" a")` ,
63
+ `trimSuffix("a ")` ,
64
+ `upper("a")` ,
65
+ `lower("A")` ,
66
+ `split("a,b,c", ",")` ,
67
+ `replace("a,b,c", ",", "_")` ,
68
+ `repeat("a", 3)` ,
69
+ `join(["a", "b", "c"], ",")` ,
70
+ `indexOf("abc", "b")` ,
71
+ `max(1,2,3)` ,
72
+ `min(1,2,3)` ,
73
+ `toJSON(a)` ,
74
+ `fromJSON("[1,2,3]")` ,
75
+ `now()` ,
76
+ `duration("1s")` ,
77
+ `first(a)` ,
78
+ `last(a)` ,
79
+ `get(m, "a")` ,
80
+ `1..9 | filter(i > 5) | map(i * 2)` ,
81
+ `s startsWith "a"` ,
82
+ `s endsWith "c"` ,
83
+ `s contains "a"` ,
84
+ `s matches "a"` ,
85
+ `s matches "a+"` ,
86
+ `true ? 1 : 2` ,
87
+ `fn(1)` ,
88
+ `{a: 1, b: 2}` ,
89
+ `[1, 2, 3]` ,
22
90
}
23
91
24
- f .Add (`Posts[0].Comments[0].AuthorEmail()` )
25
- f .Add (`Posts[0].Comments[0].Upvoted()` )
26
- f .Add (`Posts[0].Comments[0].CommentDate.Add(now())` )
27
- f .Add (`Posts[0].Comments[0].CommentDate.AddDate(0, 0, 1)` )
28
- f .Add (`Authors[Posts[0].Author.ID].Profile.Biography` )
29
- f .Add (`Authors[2].Profile.Age()` )
30
- f .Add (`Authors[2].Profile.Website` )
92
+ for _ , s := range corpus {
93
+ f .Add (s )
94
+ }
31
95
32
96
okCases := []* regexp.Regexp {
97
+ regexp .MustCompile (`cannot fetch .* from .*` ),
98
+ regexp .MustCompile (`cannot get .* from .*` ),
33
99
regexp .MustCompile (`cannot slice` ),
100
+ regexp .MustCompile (`slice index out of range` ),
101
+ regexp .MustCompile (`error parsing regexp` ),
34
102
regexp .MustCompile (`integer divide by zero` ),
35
- regexp .MustCompile (`invalid operation` ),
36
103
regexp .MustCompile (`interface conversion` ),
37
- regexp .MustCompile (`memory budget exceeded` ),
38
- regexp .MustCompile (`slice index out of range` ),
39
- regexp .MustCompile (`cannot fetch .* from .*` ),
40
- regexp .MustCompile (`cannot get .* from .*` ),
41
104
regexp .MustCompile (`invalid argument for .*` ),
42
- regexp .MustCompile (`json: unsupported value` ),
43
- regexp .MustCompile (`error parsing regexp` ),
105
+ regexp .MustCompile (`invalid character` ),
106
+ regexp .MustCompile (`invalid operation` ),
107
+ regexp .MustCompile (`invalid duration` ),
44
108
regexp .MustCompile (`time: missing unit in duration` ),
109
+ regexp .MustCompile (`time: unknown unit .* in duration` ),
110
+ regexp .MustCompile (`json: unsupported value` ),
111
+ regexp .MustCompile (`unexpected end of JSON input` ),
112
+ regexp .MustCompile (`memory budget exceeded` ),
45
113
regexp .MustCompile (`using interface \{} as type .*` ),
46
114
regexp .MustCompile (`reflect.Value.MapIndex: value of type .* is not assignable to type .*` ),
47
- regexp .MustCompile (`reflect: call of reflect.Value.Call on zero Value` ),
48
- regexp .MustCompile (`reflect: call of reflect.Value.Len on bool Value` ),
49
115
regexp .MustCompile (`reflect: Call using .* as type .*` ),
116
+ regexp .MustCompile (`reflect: call of reflect.Value.Call on .* Value` ),
50
117
regexp .MustCompile (`reflect: call of reflect.Value.Index on map Value` ),
118
+ regexp .MustCompile (`reflect: call of reflect.Value.Len on .* Value` ),
51
119
}
52
120
53
121
skipCode := []string {
@@ -62,7 +130,7 @@ func FuzzExpr(f *testing.F) {
62
130
}
63
131
}
64
132
65
- program , err := expr .Compile (code , expr .Env (playground. Blog {}) )
133
+ program , err := expr .Compile (code , expr .Env (env ), fn )
66
134
if err != nil {
67
135
t .Skip ()
68
136
}
0 commit comments