-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp_test.go
116 lines (111 loc) Β· 5.15 KB
/
regexp_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
package regexp
import (
"fmt"
"testing"
)
func TestCases(t *testing.T) {
for _, c := range []testCase{
{"a+", "a", true, false},
{"a+", "", false, false},
{"a", "aa", false, false},
{"abc", "abc", true, false},
{"abd", "abc", false, false},
{"a.c", "a.c", true, false},
{"a.c", "abc", true, false},
{"a?bc", "abc", true, false},
{"a?bc", "bc", true, false},
{"a?ab", "ab", true, false},
{"a+a+a+aa", "aaaaaa", true, false},
{"a+a+a+aa", "aaaaaaaaaaa", true, false},
{"a+a+a+aa", "aaaaaaaaaaaaaaaaaaa", true, false},
{"a+a*a*aa", "aaaaaaaaaaaaaaaaaaa", true, false},
{".+.", "asdfasdfasdf", true, false},
{"..", "aaa", false, false},
{"...", "aaa", true, false},
{"....", "aa", false, false},
{"ERROR: .*", "ERROR: file not found", true, false},
{"ERROR: .*", "WARNING: file not found", false, false},
{"**", "", false, true},
{"(**)", "", false, true},
{"*.*", "", false, true},
{"+.?.", "aa", false, true},
{"+.*", "a", false, true},
{
"a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
true, false,
},
{"", "", true, false},
{"", "a", false, false},
{"a", "", false, false},
{"π+", "ππππππ", true, false},
{"a|b", "a", true, false},
{"a|b", "b", true, false},
{"(ab)?a", "aba", true, false},
{"(ab)?a", "a", true, false},
{"(ab)*a", "ababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababa", true, false},
{"(ab)+a", "a", false, false},
{"(ab)+a", "ababa", true, false},
{"(ab)+a", "abababa", true, false},
{"\\*\\(ab\\)a", "abababa", false, false},
{"\\*\\(ab\\)a", "*(ab)a", true, false},
{")", "", false, true},
{"as)", "", false, true},
{"(as", "", false, true},
{"(a(as)())(", "", false, true},
{"(a(as)())()", "aas", true, false},
{"]", "*(ab)a", false, false},
{"\\y", "", false, true},
{"()|a", "a", true, false},
{"()||", "a", false, true},
{"|", "", false, true},
{"|a", "", false, true},
{"()", "", true, false},
{"()|()", "", true, false},
{"\u0000", "\u0000", true, false},
{"", "\u0000", false, false},
} {
t.Run(c.String(), c.run)
}
}
type testCase struct {
pattern, in string
result bool
compileError bool
}
func (c *testCase) String() string {
if c.compileError {
return fmt.Sprintf("error on #%q", c.pattern)
}
return fmt.Sprintf("#%q.MatchString(%q) == %v", c.pattern, c.in, c.result)
}
func (c *testCase) run(t *testing.T) {
r, err := Compile(c.pattern)
if err == nil && c.compileError {
t.Fatalf("Expected error for #%q", c.pattern)
} else if err != nil && !c.compileError {
t.Fatalf("Unexpected error for %#q: %v", c.pattern, err)
} else if c.compileError {
} else if matches := r.MatchString(c.in); matches != c.result {
t.Fatalf("match(%v, %v) != %v", c.pattern, c.in, c.result)
}
// TODO: compare against the stdlib implementation
// re, reErr := regexp.Compile("^"+c.pattern+"$")
// if reErr == nil && err != nil {
// t.Fatalf("regexp returned nil but we returned %v", err)
// } else if reErr != nil && err == nil {
// t.Fatalf("regexp returned %v but we returned nil", reErr)
// } else if reErr == nil && err == nil && re.MatchString(c.in) != c.result {
// t.Fatalf("regexp results in %v for %q/%q unlike us", !c.result, c.pattern, c.in)
// }
defer func() {
if r := recover(); r != nil {
if !c.compileError {
t.Fatalf("MustCompile panicked for #%q", c.pattern)
}
} else if c.compileError {
t.Fatalf("MustCompile didn't panic for #%q", c.pattern)
}
}()
MustCompile(c.pattern)
}