bloaty/third_party/re2/re2/testing/parse_test.cc
Go to the documentation of this file.
1 // Copyright 2006 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 
5 // Test parse.cc, dump.cc, and tostring.cc.
6 
7 #include <string>
8 
9 #include "util/test.h"
10 #include "util/logging.h"
11 #include "re2/regexp.h"
12 
13 namespace re2 {
14 
15 // In the past, we used 1<<30 here and zeroed the bit later, but that
16 // has undefined behaviour, so now we use an internal-only flag because
17 // otherwise we would have to introduce a new flag value just for this.
19 
20 struct Test {
21  const char* regexp;
22  const char* parse;
24 };
25 
30 
31 static Test tests[] = {
32  // Base cases
33  { "a", "lit{a}" },
34  { "a.", "cat{lit{a}dot{}}" },
35  { "a.b", "cat{lit{a}dot{}lit{b}}" },
36  { "ab", "str{ab}" },
37  { "a.b.c", "cat{lit{a}dot{}lit{b}dot{}lit{c}}" },
38  { "abc", "str{abc}" },
39  { "a|^", "alt{lit{a}bol{}}" },
40  { "a|b", "cc{0x61-0x62}" },
41  { "(a)", "cap{lit{a}}" },
42  { "(a)|b", "alt{cap{lit{a}}lit{b}}" },
43  { "a*", "star{lit{a}}" },
44  { "a+", "plus{lit{a}}" },
45  { "a?", "que{lit{a}}" },
46  { "a{2}", "rep{2,2 lit{a}}" },
47  { "a{2,3}", "rep{2,3 lit{a}}" },
48  { "a{2,}", "rep{2,-1 lit{a}}" },
49  { "a*?", "nstar{lit{a}}" },
50  { "a+?", "nplus{lit{a}}" },
51  { "a??", "nque{lit{a}}" },
52  { "a{2}?", "nrep{2,2 lit{a}}" },
53  { "a{2,3}?", "nrep{2,3 lit{a}}" },
54  { "a{2,}?", "nrep{2,-1 lit{a}}" },
55  { "", "emp{}" },
56  { "|", "alt{emp{}emp{}}" },
57  { "|x|", "alt{emp{}lit{x}emp{}}" },
58  { ".", "dot{}" },
59  { "^", "bol{}" },
60  { "$", "eol{}" },
61  { "\\|", "lit{|}" },
62  { "\\(", "lit{(}" },
63  { "\\)", "lit{)}" },
64  { "\\*", "lit{*}" },
65  { "\\+", "lit{+}" },
66  { "\\?", "lit{?}" },
67  { "{", "lit{{}" },
68  { "}", "lit{}}" },
69  { "\\.", "lit{.}" },
70  { "\\^", "lit{^}" },
71  { "\\$", "lit{$}" },
72  { "\\\\", "lit{\\}" },
73  { "[ace]", "cc{0x61 0x63 0x65}" },
74  { "[abc]", "cc{0x61-0x63}" },
75  { "[a-z]", "cc{0x61-0x7a}" },
76  { "[a]", "lit{a}" },
77  { "\\-", "lit{-}" },
78  { "-", "lit{-}" },
79  { "\\_", "lit{_}" },
80 
81  // Posix and Perl extensions
82  { "[[:lower:]]", "cc{0x61-0x7a}" },
83  { "[a-z]", "cc{0x61-0x7a}" },
84  { "[^[:lower:]]", "cc{0-0x60 0x7b-0x10ffff}" },
85  { "[[:^lower:]]", "cc{0-0x60 0x7b-0x10ffff}" },
86  { "(?i)[[:lower:]]", "cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}" },
87  { "(?i)[a-z]", "cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}" },
88  { "(?i)[^[:lower:]]", "cc{0-0x40 0x5b-0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}" },
89  { "(?i)[[:^lower:]]", "cc{0-0x40 0x5b-0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}" },
90  { "\\d", "cc{0x30-0x39}" },
91  { "\\D", "cc{0-0x2f 0x3a-0x10ffff}" },
92  { "\\s", "cc{0x9-0xa 0xc-0xd 0x20}" },
93  { "\\S", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}" },
94  { "\\w", "cc{0x30-0x39 0x41-0x5a 0x5f 0x61-0x7a}" },
95  { "\\W", "cc{0-0x2f 0x3a-0x40 0x5b-0x5e 0x60 0x7b-0x10ffff}" },
96  { "(?i)\\w", "cc{0x30-0x39 0x41-0x5a 0x5f 0x61-0x7a 0x17f 0x212a}" },
97  { "(?i)\\W", "cc{0-0x2f 0x3a-0x40 0x5b-0x5e 0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}" },
98  { "[^\\\\]", "cc{0-0x5b 0x5d-0x10ffff}" },
99  { "\\C", "byte{}" },
100 
101  // Unicode, negatives, and a double negative.
102  { "\\p{Braille}", "cc{0x2800-0x28ff}" },
103  { "\\P{Braille}", "cc{0-0x27ff 0x2900-0x10ffff}" },
104  { "\\p{^Braille}", "cc{0-0x27ff 0x2900-0x10ffff}" },
105  { "\\P{^Braille}", "cc{0x2800-0x28ff}" },
106 
107  // More interesting regular expressions.
108  { "a{,2}", "str{a{,2}}" },
109  { "\\.\\^\\$\\\\", "str{.^$\\}" },
110  { "[a-zABC]", "cc{0x41-0x43 0x61-0x7a}" },
111  { "[^a]", "cc{0-0x60 0x62-0x10ffff}" },
112  { "[\xce\xb1-\xce\xb5\xe2\x98\xba]", "cc{0x3b1-0x3b5 0x263a}" }, // utf-8
113  { "a*{", "cat{star{lit{a}}lit{{}}" },
114 
115  // Test precedences
116  { "(?:ab)*", "star{str{ab}}" },
117  { "(ab)*", "star{cap{str{ab}}}" },
118  { "ab|cd", "alt{str{ab}str{cd}}" },
119  { "a(b|c)d", "cat{lit{a}cap{cc{0x62-0x63}}lit{d}}" },
120 
121  // Test squashing of **, ++, ?? et cetera.
122  { "(?:(?:a)*)*", "star{lit{a}}" },
123  { "(?:(?:a)+)+", "plus{lit{a}}" },
124  { "(?:(?:a)?)?", "que{lit{a}}" },
125  { "(?:(?:a)*)+", "star{lit{a}}" },
126  { "(?:(?:a)*)?", "star{lit{a}}" },
127  { "(?:(?:a)+)*", "star{lit{a}}" },
128  { "(?:(?:a)+)?", "star{lit{a}}" },
129  { "(?:(?:a)?)*", "star{lit{a}}" },
130  { "(?:(?:a)?)+", "star{lit{a}}" },
131 
132  // Test flattening.
133  { "(?:a)", "lit{a}" },
134  { "(?:ab)(?:cd)", "str{abcd}" },
135  { "(?:a|b)|(?:c|d)", "cc{0x61-0x64}" },
136  { "a|c", "cc{0x61 0x63}" },
137  { "a|[cd]", "cc{0x61 0x63-0x64}" },
138  { "a|.", "dot{}" },
139  { "[ab]|c", "cc{0x61-0x63}" },
140  { "[ab]|[cd]", "cc{0x61-0x64}" },
141  { "[ab]|.", "dot{}" },
142  { ".|c", "dot{}" },
143  { ".|[cd]", "dot{}" },
144  { ".|.", "dot{}" },
145 
146  // Test Perl quoted literals
147  { "\\Q+|*?{[\\E", "str{+|*?{[}" },
148  { "\\Q+\\E+", "plus{lit{+}}" },
149  { "\\Q\\\\E", "lit{\\}" },
150  { "\\Q\\\\\\E", "str{\\\\}" },
151  { "\\Qa\\E*", "star{lit{a}}" },
152  { "\\Qab\\E*", "cat{lit{a}star{lit{b}}}" },
153  { "\\Qabc\\E*", "cat{str{ab}star{lit{c}}}" },
154 
155  // Test Perl \A and \z
156  { "(?m)^", "bol{}" },
157  { "(?m)$", "eol{}" },
158  { "(?-m)^", "bot{}" },
159  { "(?-m)$", "eot{}" },
160  { "(?m)\\A", "bot{}" },
161  { "(?m)\\z", "eot{\\z}" },
162  { "(?-m)\\A", "bot{}" },
163  { "(?-m)\\z", "eot{\\z}" },
164 
165  // Test named captures
166  { "(?P<name>a)", "cap{name:lit{a}}" },
167 
168  // Case-folded literals
169  { "[Aa]", "litfold{a}" },
170 
171  // Strings
172  { "abcde", "str{abcde}" },
173  { "[Aa][Bb]cd", "cat{strfold{ab}str{cd}}" },
174 
175  // Reported bug involving \n leaking in despite use of NeverNL.
176  { "[^ ]", "cc{0-0x9 0xb-0x1f 0x21-0x10ffff}", TestZeroFlags },
177  { "[^ ]", "cc{0-0x9 0xb-0x1f 0x21-0x10ffff}", Regexp::FoldCase },
178  { "[^ ]", "cc{0-0x9 0xb-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
179  { "[^ ]", "cc{0-0x9 0xb-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
180  { "[^ \f]", "cc{0-0x9 0xb 0xd-0x1f 0x21-0x10ffff}", TestZeroFlags },
181  { "[^ \f]", "cc{0-0x9 0xb 0xd-0x1f 0x21-0x10ffff}", Regexp::FoldCase },
182  { "[^ \f]", "cc{0-0x9 0xb 0xd-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
183  { "[^ \f]", "cc{0-0x9 0xb 0xd-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
184  { "[^ \r]", "cc{0-0x9 0xb-0xc 0xe-0x1f 0x21-0x10ffff}", TestZeroFlags },
185  { "[^ \r]", "cc{0-0x9 0xb-0xc 0xe-0x1f 0x21-0x10ffff}", Regexp::FoldCase },
186  { "[^ \r]", "cc{0-0x9 0xb-0xc 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
187  { "[^ \r]", "cc{0-0x9 0xb-0xc 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
188  { "[^ \v]", "cc{0-0x9 0xc-0x1f 0x21-0x10ffff}", TestZeroFlags },
189  { "[^ \v]", "cc{0-0x9 0xc-0x1f 0x21-0x10ffff}", Regexp::FoldCase },
190  { "[^ \v]", "cc{0-0x9 0xc-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
191  { "[^ \v]", "cc{0-0x9 0xc-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
192  { "[^ \t]", "cc{0-0x8 0xb-0x1f 0x21-0x10ffff}", TestZeroFlags },
193  { "[^ \t]", "cc{0-0x8 0xb-0x1f 0x21-0x10ffff}", Regexp::FoldCase },
194  { "[^ \t]", "cc{0-0x8 0xb-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
195  { "[^ \t]", "cc{0-0x8 0xb-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
196  { "[^ \r\f\v]", "cc{0-0x9 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
197  { "[^ \r\f\v]", "cc{0-0x9 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
198  { "[^ \r\f\t\v]", "cc{0-0x8 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
199  { "[^ \r\f\t\v]", "cc{0-0x8 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
200  { "[^ \r\n\f\t\v]", "cc{0-0x8 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
201  { "[^ \r\n\f\t\v]", "cc{0-0x8 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
202  { "[^ \r\n\f\t]", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL },
203  { "[^ \r\n\f\t]", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}", Regexp::NeverNL | Regexp::FoldCase },
204  { "[^\t-\n\f-\r ]", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
206  { "[^\t-\n\f-\r ]", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
208  { "[^\t-\n\f-\r ]", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
210  { "[^\t-\n\f-\r ]", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
212  { "\\S", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
214  { "\\S", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
216  { "\\S", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
218  { "\\S", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
220 
221  // Bug in Regexp::ToString() that emitted [^], which
222  // would (obviously) fail to parse when fed back in.
223  { "[\\s\\S]", "cc{0-0x10ffff}" },
224 };
225 
227  return Regexp::Equal(a, b);
228 }
229 
231  const std::string& title) {
232  Regexp** re = new Regexp*[ntests];
233  for (int i = 0; i < ntests; i++) {
236  if (tests[i].flags != 0) {
237  f = tests[i].flags & ~TestZeroFlags;
238  }
239  re[i] = Regexp::Parse(tests[i].regexp, f, &status);
240  ASSERT_TRUE(re[i] != NULL)
241  << " " << tests[i].regexp << " " << status.Text();
242  std::string s = re[i]->Dump();
244  << "Regexp: " << tests[i].regexp
245  << "\nparse: " << std::string(tests[i].parse)
246  << " s: " << s << " flag=" << f;
247  }
248 
249  for (int i = 0; i < ntests; i++) {
250  for (int j = 0; j < ntests; j++) {
252  RegexpEqualTestingOnly(re[i], re[j]))
253  << "Regexp: " << tests[i].regexp << " " << tests[j].regexp;
254  }
255  }
256 
257  for (int i = 0; i < ntests; i++)
258  re[i]->Decref();
259  delete[] re;
260 }
261 
262 // Test that regexps parse to expected structures.
263 TEST(TestParse, SimpleRegexps) {
264  TestParse(tests, arraysize(tests), kTestFlags, "simple");
265 }
266 
268  { "AbCdE", "strfold{abcde}" },
269  { "[Aa]", "litfold{a}" },
270  { "a", "litfold{a}" },
271 
272  // 0x17F is an old English long s (looks like an f) and folds to s.
273  // 0x212A is the Kelvin symbol and folds to k.
274  { "A[F-g]", "cat{litfold{a}cc{0x41-0x7a 0x17f 0x212a}}" }, // [Aa][A-z...]
275  { "[[:upper:]]", "cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}" },
276  { "[[:lower:]]", "cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}" },
277 };
278 
279 // Test that parsing with FoldCase works.
280 TEST(TestParse, FoldCase) {
282 }
283 
285  { "(|)^$.[*+?]{5,10},\\", "str{(|)^$.[*+?]{5,10},\\}" },
286 };
287 
288 // Test that parsing with Literal works.
289 TEST(TestParse, Literal) {
291 }
292 
294  { ".", "dot{}" },
295  { "\n", "lit{\n}" },
296  { "[^a]", "cc{0-0x60 0x62-0x10ffff}" },
297  { "[a\\n]", "cc{0xa 0x61}" },
298 };
299 
300 // Test that parsing with MatchNL works.
301 // (Also tested above during simple cases.)
302 TEST(TestParse, MatchNL) {
304 }
305 
307  { ".", "cc{0-0x9 0xb-0x10ffff}" },
308  { "\n", "lit{\n}" },
309  { "[^a]", "cc{0-0x9 0xb-0x60 0x62-0x10ffff}" },
310  { "[a\\n]", "cc{0xa 0x61}" },
311 };
312 
313 // Test that parsing without MatchNL works.
314 TEST(TestParse, NoMatchNL) {
316 }
317 
319  { "abc|abd", "cat{str{ab}cc{0x63-0x64}}" },
320  { "a(?:b)c|abd", "cat{str{ab}cc{0x63-0x64}}" },
321  { "abc|abd|aef|bcx|bcy",
322  "alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}"
323  "cat{str{bc}cc{0x78-0x79}}}" },
324  { "abc|x|abd", "alt{str{abc}lit{x}str{abd}}" },
325  { "(?i)abc|ABD", "cat{strfold{ab}cc{0x43-0x44 0x63-0x64}}" },
326  { "[ab]c|[ab]d", "cat{cc{0x61-0x62}cc{0x63-0x64}}" },
327  { ".c|.d", "cat{cc{0-0x9 0xb-0x10ffff}cc{0x63-0x64}}" },
328  { "\\Cc|\\Cd", "cat{byte{}cc{0x63-0x64}}" },
329  { "x{2}|x{2}[0-9]",
330  "cat{rep{2,2 lit{x}}alt{emp{}cc{0x30-0x39}}}" },
331  { "x{2}y|x{2}[0-9]y",
332  "cat{rep{2,2 lit{x}}alt{lit{y}cat{cc{0x30-0x39}lit{y}}}}" },
333  { "n|r|rs",
334  "alt{lit{n}cat{lit{r}alt{emp{}lit{s}}}}" },
335  { "n|rs|r",
336  "alt{lit{n}cat{lit{r}alt{lit{s}emp{}}}}" },
337  { "r|rs|n",
338  "alt{cat{lit{r}alt{emp{}lit{s}}}lit{n}}" },
339  { "rs|r|n",
340  "alt{cat{lit{r}alt{lit{s}emp{}}}lit{n}}" },
341  { "a\\C*?c|a\\C*?b",
342  "cat{lit{a}alt{cat{nstar{byte{}}lit{c}}cat{nstar{byte{}}lit{b}}}}" },
343  { "^/a/bc|^/a/de",
344  "cat{bol{}cat{str{/a/}alt{str{bc}str{de}}}}" },
345  // In the past, factoring was limited to kFactorAlternationMaxDepth (8).
346  { "a|aa|aaa|aaaa|aaaaa|aaaaaa|aaaaaaa|aaaaaaaa|aaaaaaaaa|aaaaaaaaaa",
347  "cat{lit{a}alt{emp{}" "cat{lit{a}alt{emp{}" "cat{lit{a}alt{emp{}"
348  "cat{lit{a}alt{emp{}" "cat{lit{a}alt{emp{}" "cat{lit{a}alt{emp{}"
349  "cat{lit{a}alt{emp{}" "cat{lit{a}alt{emp{}" "cat{lit{a}alt{emp{}"
350  "lit{a}}}}}}}}}}}}}}}}}}}" },
351  { "a|aardvark|aardvarks|abaci|aback|abacus|abacuses|abaft|abalone|abalones",
352  "cat{lit{a}alt{emp{}cat{str{ardvark}alt{emp{}lit{s}}}"
353  "cat{str{ba}alt{cat{lit{c}alt{cc{0x69 0x6b}cat{str{us}alt{emp{}str{es}}}}}"
354  "str{ft}cat{str{lone}alt{emp{}lit{s}}}}}}}" },
355 };
356 
357 // Test that prefix factoring works.
358 TEST(TestParse, Prefix) {
360 }
361 
363  { "((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}))",
364  "cap{cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 lit{x}}}}}}}}}}}}}}}}}}}}" },
365  { "((((((((((x{1}){2}){2}){2}){2}){2}){2}){2}){2}){2})",
366  "cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{1,1 lit{x}}}}}}}}}}}}}}}}}}}}}" },
367  { "((((((((((x{0}){2}){2}){2}){2}){2}){2}){2}){2}){2})",
368  "cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 cap{rep{0,0 lit{x}}}}}}}}}}}}}}}}}}}}}" },
369  { "((((((x{2}){2}){2}){5}){5}){5})",
370  "cap{rep{5,5 cap{rep{5,5 cap{rep{5,5 cap{rep{2,2 cap{rep{2,2 cap{rep{2,2 lit{x}}}}}}}}}}}}}" },
371 };
372 
373 // Test that nested repetition works.
374 TEST(TestParse, Nested) {
376 }
377 
378 // Invalid regular expressions
379 const char* badtests[] = {
380  "(",
381  ")",
382  "(a",
383  "(a|b|",
384  "(a|b",
385  "[a-z",
386  "([a-z)",
387  "x{1001}",
388  "\xff", // Invalid UTF-8
389  "[\xff]",
390  "[\\\xff]",
391  "\\\xff",
392  "(?P<name>a",
393  "(?P<name>",
394  "(?P<name",
395  "(?P<x y>a)",
396  "(?P<>a)",
397  "[a-Z]",
398  "(?i)[a-Z]",
399  "a{100000}",
400  "a{100000,}",
401  "((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}){2})",
402  "(((x{7}){11}){13})",
403  "\\Q\\E*",
404 };
405 
406 // Valid in Perl, bad in POSIX
407 const char* only_perl[] = {
408  "[a-b-c]",
409  "\\Qabc\\E",
410  "\\Q*+?{[\\E",
411  "\\Q\\\\E",
412  "\\Q\\\\\\E",
413  "\\Q\\\\\\\\E",
414  "\\Q\\\\\\\\\\E",
415  "(?:a)",
416  "(?P<name>a)",
417 };
418 
419 // Valid in POSIX, bad in Perl.
420 const char* only_posix[] = {
421  "a++",
422  "a**",
423  "a?*",
424  "a+*",
425  "a{1}*",
426 };
427 
428 // Test that parser rejects bad regexps.
429 TEST(TestParse, InvalidRegexps) {
430  for (size_t i = 0; i < arraysize(badtests); i++) {
432  << " " << badtests[i];
434  << " " << badtests[i];
435  }
436  for (size_t i = 0; i < arraysize(only_posix); i++) {
438  << " " << only_posix[i];
440  ASSERT_TRUE(re != NULL) << " " << only_posix[i];
441  re->Decref();
442  }
443  for (size_t i = 0; i < arraysize(only_perl); i++) {
445  << " " << only_perl[i];
447  ASSERT_TRUE(re != NULL) << " " << only_perl[i];
448  re->Decref();
449  }
450 }
451 
452 // Test that ToString produces original regexp or equivalent one.
453 TEST(TestToString, EquivalentParse) {
454  for (size_t i = 0; i < arraysize(tests); i++) {
457  if (tests[i].flags != 0) {
458  f = tests[i].flags & ~TestZeroFlags;
459  }
460  Regexp* re = Regexp::Parse(tests[i].regexp, f, &status);
461  ASSERT_TRUE(re != NULL) << " " << tests[i].regexp << " " << status.Text();
462  std::string s = re->Dump();
464  << "Regexp: " << tests[i].regexp
465  << "\nparse: " << std::string(tests[i].parse)
466  << " s: " << s << " flag=" << f;
467  std::string t = re->ToString();
468  if (t != tests[i].regexp) {
469  // If ToString didn't return the original regexp,
470  // it must have found one with fewer parens.
471  // Unfortunately we can't check the length here, because
472  // ToString produces "\\{" for a literal brace,
473  // but "{" is a shorter equivalent.
474  // ASSERT_LT(t.size(), strlen(tests[i].regexp))
475  // << " t=" << t << " regexp=" << tests[i].regexp;
476 
477  // Test that if we parse the new regexp we get the same structure.
479  ASSERT_TRUE(nre != NULL) << " reparse " << t << " " << status.Text();
480  std::string ss = nre->Dump();
481  std::string tt = nre->ToString();
482  if (s != ss || t != tt)
483  LOG(INFO) << "ToString(" << tests[i].regexp << ") = " << t;
484  EXPECT_EQ(s, ss);
485  EXPECT_EQ(t, tt);
486  nre->Decref();
487  }
488  re->Decref();
489  }
490 }
491 
492 // Test that capture error args are correct.
493 TEST(NamedCaptures, ErrorArgs) {
495  Regexp* re;
496 
497  re = Regexp::Parse("test(?P<name", Regexp::LikePerl, &status);
498  EXPECT_TRUE(re == NULL);
500  EXPECT_EQ(status.error_arg(), "(?P<name");
501 
502  re = Regexp::Parse("test(?P<space bar>z)", Regexp::LikePerl, &status);
503  EXPECT_TRUE(re == NULL);
505  EXPECT_EQ(status.error_arg(), "(?P<space bar>");
506 }
507 
508 } // namespace re2
re2::Regexp::Decref
void Decref()
Definition: bloaty/third_party/re2/re2/regexp.cc:115
re2::foldcase_tests
Test foldcase_tests[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:267
re2::Regexp
Definition: bloaty/third_party/re2/re2/regexp.h:274
re2::prefix_tests
Test prefix_tests[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:318
re2::badtests
const char * badtests[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:379
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
re2::kTestFlags
static Regexp::ParseFlags kTestFlags
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:26
status
absl::Status status
Definition: rls.cc:251
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
re2::Regexp::UnicodeGroups
@ UnicodeGroups
Definition: bloaty/third_party/re2/re2/regexp.h:305
re2::RegexpStatus
Definition: bloaty/third_party/re2/re2/regexp.h:190
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
re2::Regexp::Literal
@ Literal
Definition: bloaty/third_party/re2/re2/regexp.h:281
re2::nomatchnl_tests
Test nomatchnl_tests[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:306
re2::literal_tests
Test literal_tests[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:284
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
re2::Regexp::PerlClasses
@ PerlClasses
Definition: bloaty/third_party/re2/re2/regexp.h:291
re2::Regexp::ToString
std::string ToString()
Definition: bloaty/third_party/re2/re2/tostring.cc:55
re2::Regexp::NoParseFlags
@ NoParseFlags
Definition: bloaty/third_party/re2/re2/regexp.h:279
re2::matchnl_tests
Test matchnl_tests[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:293
python_utils.jobset.INFO
INFO
Definition: jobset.py:111
LOG
#define LOG(severity)
Definition: bloaty/third_party/re2/util/logging.h:53
re2::Regexp::Parse
static Regexp * Parse(const StringPiece &s, ParseFlags flags, RegexpStatus *status)
Definition: bloaty/third_party/re2/re2/parse.cc:2200
re2::nested_tests
Test nested_tests[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:362
re2::only_perl
const char * only_perl[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:407
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
ntests
static int ntests
Definition: third_party/bloaty/third_party/re2/util/test.cc:17
re2::Regexp::ParseFlags
ParseFlags
Definition: bloaty/third_party/re2/re2/regexp.h:278
re2::Regexp::Dump
std::string Dump()
Definition: bloaty/third_party/re2/re2/testing/dump.cc:156
arraysize
#define arraysize(array)
Definition: benchmark/src/arraysize.h:28
re2::Test::flags
Regexp::ParseFlags flags
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:23
re2::kRegexpBadNamedCapture
@ kRegexpBadNamedCapture
Definition: bloaty/third_party/re2/re2/regexp.h:186
re2::Regexp::Equal
static bool Equal(Regexp *a, Regexp *b)
Definition: bloaty/third_party/re2/re2/regexp.cc:415
tests
Definition: src/python/grpcio_tests/tests/__init__.py:1
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
parse
static void parse(const char *s)
Definition: debug/trace.cc:121
re2::Test::parse
const char * parse
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:22
re2::Test
Definition: bloaty/third_party/re2/re2/testing/compile_test.cc:22
re2::only_posix
const char * only_posix[]
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:420
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
re2::Test::regexp
const char * regexp
Definition: bloaty/third_party/re2/re2/testing/compile_test.cc:23
re2::Regexp::WasDollar
@ WasDollar
Definition: bloaty/third_party/re2/re2/regexp.h:316
re2::TestZeroFlags
static const Regexp::ParseFlags TestZeroFlags
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:18
re2::Regexp::MatchNL
@ MatchNL
Definition: bloaty/third_party/re2/re2/regexp.h:285
re2::Regexp::NeverNL
@ NeverNL
Definition: bloaty/third_party/re2/re2/regexp.h:307
flags
uint32_t flags
Definition: retry_filter.cc:632
re2::TEST
TEST(TestCharClassBuilder, Adds)
Definition: bloaty/third_party/re2/re2/testing/charclass_test.cc:198
re2::Regexp::PerlX
@ PerlX
Definition: bloaty/third_party/re2/re2/regexp.h:293
re2::RegexpEqualTestingOnly
bool RegexpEqualTestingOnly(Regexp *a, Regexp *b)
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:226
absl::Status::code
absl::StatusCode code() const
Definition: third_party/abseil-cpp/absl/status/status.cc:233
re2::TestParse
void TestParse(const Test *tests, int ntests, Regexp::ParseFlags flags, const std::string &title)
Definition: bloaty/third_party/re2/re2/testing/parse_test.cc:230
re2::Regexp::FoldCase
@ FoldCase
Definition: bloaty/third_party/re2/re2/regexp.h:280
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
re2::Regexp::LikePerl
@ LikePerl
Definition: bloaty/third_party/re2/re2/regexp.h:312


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:39