-
Notifications
You must be signed in to change notification settings - Fork 84
/
regexp_re2_test.go
223 lines (201 loc) · 4.86 KB
/
regexp_re2_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
package regexp2
import (
"testing"
)
func TestRE2CompatCapture(t *testing.T) {
r := MustCompile(`re(?P<a>2)`, RE2)
if m, err := r.FindStringMatch("blahre2blah"); m == nil {
t.Fatal("Expected match")
} else if err != nil {
t.Fatalf("Unexpected error: %v", err)
} else {
g := m.GroupByName("a")
if want, got := "2", g.String(); want != got {
t.Fatalf("Wanted %v got %v", want, got)
}
}
}
func TestRE2CompatCapture_Invalid(t *testing.T) {
bogus := []string{
`(?P<name>a`,
`(?P<name>`,
`(?P<name`,
`(?P<x y>a)`,
`(?P<>a)`,
}
for _, inp := range bogus {
t.Run(inp, func(t *testing.T) {
r, err := Compile(inp, RE2)
if err == nil {
t.Fatal("Expected failure to parse")
}
if r != nil {
t.Fatal("expected regexp to be nil")
}
})
}
}
func TestRE2NamedAscii(t *testing.T) {
table := []struct {
nm string
pos string
neg string
}{
{nm: "alnum", pos: "1", neg: "!"},
{nm: "alpha", pos: "g", neg: "0"},
{nm: "blank", pos: " ", neg: "_"},
{nm: "ascii", pos: "*", neg: "\x8f"},
{nm: "cntrl", pos: "\t", neg: "A"},
{nm: "graph", pos: "!", neg: " "},
{nm: "lower", pos: "a", neg: "A"},
{nm: "print", pos: " ", neg: "\r"},
{nm: "punct", pos: "@", neg: "A"},
{nm: "space", pos: " ", neg: "A"},
{nm: "digit", pos: "1", neg: "A"},
{nm: "upper", pos: "A", neg: "a"},
{nm: "word", pos: "_", neg: "-"},
{nm: "xdigit", pos: "A", neg: "G"},
}
for _, row := range table {
t.Run(row.nm, func(t *testing.T) {
r := MustCompile(`[[:`+row.nm+`:]]`, RE2)
if m, _ := r.MatchString(row.pos); !m {
t.Fatal("Expected match")
}
if m, _ := r.MatchString(row.neg); m {
t.Fatal("Expected no match")
}
})
t.Run(row.nm+" negate", func(t *testing.T) {
r := MustCompile(`[[:^`+row.nm+`:]]`, RE2)
if m, _ := r.MatchString(row.neg); !m {
t.Fatal("Expected match")
}
if m, _ := r.MatchString(row.pos); m {
t.Fatal("Expected no match")
}
})
}
}
func TestRE2NamedAscii_Concat(t *testing.T) {
r := MustCompile(`[[:digit:]a]`, RE2)
if m, _ := r.MatchString("b"); m {
t.Fatal("Expected no match")
}
if m, _ := r.MatchString("a"); !m {
t.Fatal("Expected match")
}
if m, _ := r.MatchString("["); m {
t.Fatal("Expected no match")
}
if m, _ := r.MatchString("5"); !m {
t.Fatal("Expected match")
}
}
func TestRE2Dollar_Singleline(t *testing.T) {
// PCRE allows for \n after the $ and RE2 doesn't
r := MustCompile(`^ac$\n`, RE2)
if m, _ := r.MatchString("ac"); m {
t.Fatal("Expected no match")
}
if m, _ := r.MatchString("ac\n"); m {
t.Fatal("Expected no match")
}
}
func TestRE2Dollar_Multiline(t *testing.T) {
r := MustCompile(`^ac$\n`, RE2|Multiline)
if m, _ := r.MatchString("ac"); m {
t.Fatal("Expected no match")
}
if m, err := r.MatchString("ac\n"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestRE2ExtendedZero(t *testing.T) {
notZero := "߀" // \u07c0
r := MustCompile(`^\d$`, RE2)
if m, _ := r.MatchString(notZero); m {
t.Fatal("Expected no match")
}
r = MustCompile(`^\D$`, RE2)
if m, _ := r.MatchString(notZero); !m {
t.Fatal("Expected match")
}
}
func TestRegularExtendedZero(t *testing.T) {
notZero := "߀" // \u07c0
r := MustCompile(`^\d$`, 0)
if m, _ := r.MatchString(notZero); !m {
t.Fatal("Expected match")
}
r = MustCompile(`^\D$`, 0)
if m, _ := r.MatchString(notZero); m {
t.Fatal("Expected no match")
}
}
func TestRE2Word(t *testing.T) {
r := MustCompile(`\w`, RE2)
if m, _ := r.MatchString("å"); m {
t.Fatal("Expected no match")
}
r = MustCompile(`\W`, RE2)
if m, _ := r.MatchString("å"); !m {
t.Fatal("Expected match")
}
}
func TestRegularWord(t *testing.T) {
r := MustCompile(`\w`, 0)
if m, _ := r.MatchString("å"); !m {
t.Fatal("Expected match")
}
r = MustCompile(`\W`, 0)
if m, _ := r.MatchString("å"); m {
t.Fatal("Expected no match")
}
}
func TestRE2Space(t *testing.T) {
r := MustCompile(`\s`, RE2)
if m, _ := r.MatchString("\x0b"); m {
t.Fatal("Expected no match")
}
r = MustCompile(`\S`, RE2)
if m, _ := r.MatchString("\x0b"); !m {
t.Fatal("Expected match")
}
}
func TestRegularSpace(t *testing.T) {
r := MustCompile(`\s`, 0)
if m, _ := r.MatchString("\x0b"); !m {
t.Fatal("Expected match")
}
r = MustCompile(`\S`, 0)
if m, _ := r.MatchString("\x0b"); m {
t.Fatal("Expected no match")
}
}
func TestEscapeLiteralDefaults(t *testing.T) {
_, err := Compile(`a\_test`, 0)
if err == nil {
t.Fatal("Expected compile fail")
}
r := MustCompile(`a\_test`, RE2)
if m, _ := r.MatchString("a_test"); !m {
t.Fatal("Expected match")
}
if m, _ := r.MatchString("a\\_test"); m {
t.Fatal("Expected no match")
}
}
/*
func TestRE2EndZ_Singleline(t *testing.T) {
// PCRE allows for \n after the $ and RE2 doesn't
r := MustCompile(`^ac$\Z`, RE2|Debug)
if m, _ := r.MatchString("ac"); m {
t.Fatal("Expected no match")
}
if m, _ := r.MatchString("ac\n"); m {
t.Fatal("Expected no match")
}
}*/