1 package regexp2
2
3 import (
4 "testing"
5 )
6
7 func TestRE2CompatCapture(t *testing.T) {
8 r := MustCompile(`re(?P<a>2)`, RE2)
9 if m, err := r.FindStringMatch("blahre2blah"); m == nil {
10 t.Fatal("Expected match")
11 } else if err != nil {
12 t.Fatalf("Unexpected error: %v", err)
13 } else {
14 g := m.GroupByName("a")
15 if want, got := "2", g.String(); want != got {
16 t.Fatalf("Wanted %v got %v", want, got)
17 }
18 }
19 }
20
21 func TestRE2CompatCapture_Invalid(t *testing.T) {
22 bogus := []string{
23 `(?P<name>a`,
24 `(?P<name>`,
25 `(?P<name`,
26 `(?P<x y>a)`,
27 `(?P<>a)`,
28 }
29 for _, inp := range bogus {
30 t.Run(inp, func(t *testing.T) {
31 r, err := Compile(inp, RE2)
32 if err == nil {
33 t.Fatal("Expected failure to parse")
34 }
35 if r != nil {
36 t.Fatal("expected regexp to be nil")
37 }
38 })
39 }
40 }
41
42 func TestRE2NamedAscii(t *testing.T) {
43 table := []struct {
44 nm string
45 pos string
46 neg string
47 }{
48 {nm: "alnum", pos: "1", neg: "!"},
49 {nm: "alpha", pos: "g", neg: "0"},
50 {nm: "blank", pos: " ", neg: "_"},
51 {nm: "ascii", pos: "*", neg: "\x8f"},
52 {nm: "cntrl", pos: "\t", neg: "A"},
53 {nm: "graph", pos: "!", neg: " "},
54 {nm: "lower", pos: "a", neg: "A"},
55 {nm: "print", pos: " ", neg: "\r"},
56 {nm: "punct", pos: "@", neg: "A"},
57 {nm: "space", pos: " ", neg: "A"},
58 {nm: "digit", pos: "1", neg: "A"},
59 {nm: "upper", pos: "A", neg: "a"},
60 {nm: "word", pos: "_", neg: "-"},
61 {nm: "xdigit", pos: "A", neg: "G"},
62 }
63
64 for _, row := range table {
65 t.Run(row.nm, func(t *testing.T) {
66 r := MustCompile(`[[:`+row.nm+`:]]`, RE2)
67 if m, _ := r.MatchString(row.pos); !m {
68 t.Fatal("Expected match")
69 }
70 if m, _ := r.MatchString(row.neg); m {
71 t.Fatal("Expected no match")
72 }
73 })
74 t.Run(row.nm+" negate", func(t *testing.T) {
75 r := MustCompile(`[[:^`+row.nm+`:]]`, RE2)
76 if m, _ := r.MatchString(row.neg); !m {
77 t.Fatal("Expected match")
78 }
79 if m, _ := r.MatchString(row.pos); m {
80 t.Fatal("Expected no match")
81 }
82 })
83 }
84
85 }
86 func TestRE2NamedAscii_Concat(t *testing.T) {
87 r := MustCompile(`[[:digit:]a]`, RE2)
88 if m, _ := r.MatchString("b"); m {
89 t.Fatal("Expected no match")
90 }
91 if m, _ := r.MatchString("a"); !m {
92 t.Fatal("Expected match")
93 }
94 if m, _ := r.MatchString("["); m {
95 t.Fatal("Expected no match")
96 }
97 if m, _ := r.MatchString("5"); !m {
98 t.Fatal("Expected match")
99 }
100 }
101
102 func TestRE2Dollar_Singleline(t *testing.T) {
103
104 r := MustCompile(`^ac$\n`, RE2)
105 if m, _ := r.MatchString("ac"); m {
106 t.Fatal("Expected no match")
107 }
108 if m, _ := r.MatchString("ac\n"); m {
109 t.Fatal("Expected no match")
110 }
111 }
112
113 func TestRE2Dollar_Multiline(t *testing.T) {
114 r := MustCompile(`^ac$\n`, RE2|Multiline)
115 if m, _ := r.MatchString("ac"); m {
116 t.Fatal("Expected no match")
117 }
118 if m, err := r.MatchString("ac\n"); err != nil {
119 t.Fatal(err)
120 } else if !m {
121 t.Fatal("Expected match")
122 }
123 }
124
125 func TestRE2ExtendedZero(t *testing.T) {
126 notZero := "߀"
127 r := MustCompile(`^\d$`, RE2)
128 if m, _ := r.MatchString(notZero); m {
129 t.Fatal("Expected no match")
130 }
131
132 r = MustCompile(`^\D$`, RE2)
133 if m, _ := r.MatchString(notZero); !m {
134 t.Fatal("Expected match")
135 }
136 }
137
138 func TestRegularExtendedZero(t *testing.T) {
139 notZero := "߀"
140
141 r := MustCompile(`^\d$`, 0)
142 if m, _ := r.MatchString(notZero); !m {
143 t.Fatal("Expected match")
144 }
145
146 r = MustCompile(`^\D$`, 0)
147 if m, _ := r.MatchString(notZero); m {
148 t.Fatal("Expected no match")
149 }
150 }
151
152 func TestRE2Word(t *testing.T) {
153 r := MustCompile(`\w`, RE2)
154 if m, _ := r.MatchString("å"); m {
155 t.Fatal("Expected no match")
156 }
157
158 r = MustCompile(`\W`, RE2)
159 if m, _ := r.MatchString("å"); !m {
160 t.Fatal("Expected match")
161 }
162
163 }
164
165 func TestRegularWord(t *testing.T) {
166 r := MustCompile(`\w`, 0)
167 if m, _ := r.MatchString("å"); !m {
168 t.Fatal("Expected match")
169 }
170 r = MustCompile(`\W`, 0)
171 if m, _ := r.MatchString("å"); m {
172 t.Fatal("Expected no match")
173 }
174 }
175
176 func TestRE2Space(t *testing.T) {
177 r := MustCompile(`\s`, RE2)
178 if m, _ := r.MatchString("\x0b"); m {
179 t.Fatal("Expected no match")
180 }
181 r = MustCompile(`\S`, RE2)
182 if m, _ := r.MatchString("\x0b"); !m {
183 t.Fatal("Expected match")
184 }
185 }
186
187 func TestRegularSpace(t *testing.T) {
188 r := MustCompile(`\s`, 0)
189 if m, _ := r.MatchString("\x0b"); !m {
190 t.Fatal("Expected match")
191 }
192 r = MustCompile(`\S`, 0)
193 if m, _ := r.MatchString("\x0b"); m {
194 t.Fatal("Expected no match")
195 }
196 }
197
198 func TestEscapeLiteralDefaults(t *testing.T) {
199 _, err := Compile(`a\_test`, 0)
200 if err == nil {
201 t.Fatal("Expected compile fail")
202 }
203
204 r := MustCompile(`a\_test`, RE2)
205 if m, _ := r.MatchString("a_test"); !m {
206 t.Fatal("Expected match")
207 }
208 if m, _ := r.MatchString("a\\_test"); m {
209 t.Fatal("Expected no match")
210 }
211 }
212
213
224
View as plain text