forked from quasilyte/gogrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_error_test.go
68 lines (58 loc) · 1.7 KB
/
compile_error_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
package gogrep
import (
"fmt"
"go/token"
"strings"
"testing"
)
func TestCompileError(t *testing.T) {
strict := func(s string) string {
return "STRICT " + s
}
isStrict := func(s string) bool {
return strings.HasPrefix(s, "STRICT ")
}
unwrapPattern := func(s string) string {
s = strings.TrimPrefix(s, "STRICT ")
return s
}
intStatements := func() string {
parts := make([]string, 260)
for i := range parts {
parts[i] = fmt.Sprint(i)
}
return strings.Join(parts, ";")
}()
tests := map[string]string{
`$$`: `$ must be followed by ident, got ILLEGAL`,
`$`: `$ must be followed by ident, got EOF`,
``: `empty source code`,
"\t": `empty source code`,
`foo)`: `expected statement, found ')'`,
`$x)`: `expected statement, found ')'`,
`$x(`: `expected operand, found '}'`,
`$*x)`: `expected statement, found ')'`,
"a\n$x)": `expected statement, found ')'`,
`0xabci`: `can't convert 0xabci (IMAG) value`,
intStatements: `implementation limitation: too many values`,
strict(intStatements): `implementation limitation: too many string values`,
// Below is a list of patterns that caused gogrep to panic.
// Found with fuzzing.
`()`: `expected operand, found ')'`,
`=0`: `invalid pattern syntax`,
}
for input, want := range tests {
fset := token.NewFileSet()
testPattern := unwrapPattern(input)
config := CompileConfig{Fset: fset, Src: testPattern, Strict: isStrict(input)}
_, _, err := Compile(config)
if err == nil {
t.Errorf("compile `%s`: expected error, got none", input)
continue
}
if !strings.Contains(err.Error(), want) {
t.Errorf("compile `%s`: error substring not found\nerror: %s\nsubstr: %s",
input, err, want)
}
}
}