github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/regexp/syntax/parse_test.go (about) 1 // Copyright 2011 The Go 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 package syntax 6 7 import ( 8 "fmt" 9 "strings" 10 "testing" 11 "unicode" 12 ) 13 14 type parseTest struct { 15 Regexp string 16 Dump string 17 } 18 19 var parseTests = []parseTest{ 20 // Base cases 21 {`a`, `lit{a}`}, 22 {`a.`, `cat{lit{a}dot{}}`}, 23 {`a.b`, `cat{lit{a}dot{}lit{b}}`}, 24 {`ab`, `str{ab}`}, 25 {`a.b.c`, `cat{lit{a}dot{}lit{b}dot{}lit{c}}`}, 26 {`abc`, `str{abc}`}, 27 {`a|^`, `alt{lit{a}bol{}}`}, 28 {`a|b`, `cc{0x61-0x62}`}, 29 {`(a)`, `cap{lit{a}}`}, 30 {`(a)|b`, `alt{cap{lit{a}}lit{b}}`}, 31 {`a*`, `star{lit{a}}`}, 32 {`a+`, `plus{lit{a}}`}, 33 {`a?`, `que{lit{a}}`}, 34 {`a{2}`, `rep{2,2 lit{a}}`}, 35 {`a{2,3}`, `rep{2,3 lit{a}}`}, 36 {`a{2,}`, `rep{2,-1 lit{a}}`}, 37 {`a*?`, `nstar{lit{a}}`}, 38 {`a+?`, `nplus{lit{a}}`}, 39 {`a??`, `nque{lit{a}}`}, 40 {`a{2}?`, `nrep{2,2 lit{a}}`}, 41 {`a{2,3}?`, `nrep{2,3 lit{a}}`}, 42 {`a{2,}?`, `nrep{2,-1 lit{a}}`}, 43 // Malformed { } are treated as literals. 44 {`x{1001`, `str{x{1001}`}, 45 {`x{9876543210`, `str{x{9876543210}`}, 46 {`x{9876543210,`, `str{x{9876543210,}`}, 47 {`x{2,1`, `str{x{2,1}`}, 48 {`x{1,9876543210`, `str{x{1,9876543210}`}, 49 {``, `emp{}`}, 50 {`|`, `emp{}`}, // alt{emp{}emp{}} but got factored 51 {`|x|`, `alt{emp{}lit{x}emp{}}`}, 52 {`.`, `dot{}`}, 53 {`^`, `bol{}`}, 54 {`$`, `eol{}`}, 55 {`\|`, `lit{|}`}, 56 {`\(`, `lit{(}`}, 57 {`\)`, `lit{)}`}, 58 {`\*`, `lit{*}`}, 59 {`\+`, `lit{+}`}, 60 {`\?`, `lit{?}`}, 61 {`{`, `lit{{}`}, 62 {`}`, `lit{}}`}, 63 {`\.`, `lit{.}`}, 64 {`\^`, `lit{^}`}, 65 {`\$`, `lit{$}`}, 66 {`\\`, `lit{\}`}, 67 {`[ace]`, `cc{0x61 0x63 0x65}`}, 68 {`[abc]`, `cc{0x61-0x63}`}, 69 {`[a-z]`, `cc{0x61-0x7a}`}, 70 {`[a]`, `lit{a}`}, 71 {`\-`, `lit{-}`}, 72 {`-`, `lit{-}`}, 73 {`\_`, `lit{_}`}, 74 {`abc`, `str{abc}`}, 75 {`abc|def`, `alt{str{abc}str{def}}`}, 76 {`abc|def|ghi`, `alt{str{abc}str{def}str{ghi}}`}, 77 78 // Posix and Perl extensions 79 {`[[:lower:]]`, `cc{0x61-0x7a}`}, 80 {`[a-z]`, `cc{0x61-0x7a}`}, 81 {`[^[:lower:]]`, `cc{0x0-0x60 0x7b-0x10ffff}`}, 82 {`[[:^lower:]]`, `cc{0x0-0x60 0x7b-0x10ffff}`}, 83 {`(?i)[[:lower:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`}, 84 {`(?i)[a-z]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`}, 85 {`(?i)[^[:lower:]]`, `cc{0x0-0x40 0x5b-0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}`}, 86 {`(?i)[[:^lower:]]`, `cc{0x0-0x40 0x5b-0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}`}, 87 {`\d`, `cc{0x30-0x39}`}, 88 {`\D`, `cc{0x0-0x2f 0x3a-0x10ffff}`}, 89 {`\s`, `cc{0x9-0xa 0xc-0xd 0x20}`}, 90 {`\S`, `cc{0x0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}`}, 91 {`\w`, `cc{0x30-0x39 0x41-0x5a 0x5f 0x61-0x7a}`}, 92 {`\W`, `cc{0x0-0x2f 0x3a-0x40 0x5b-0x5e 0x60 0x7b-0x10ffff}`}, 93 {`(?i)\w`, `cc{0x30-0x39 0x41-0x5a 0x5f 0x61-0x7a 0x17f 0x212a}`}, 94 {`(?i)\W`, `cc{0x0-0x2f 0x3a-0x40 0x5b-0x5e 0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}`}, 95 {`[^\\]`, `cc{0x0-0x5b 0x5d-0x10ffff}`}, 96 // { `\C`, `byte{}` }, // probably never 97 98 // Unicode, negatives, and a double negative. 99 {`\p{Braille}`, `cc{0x2800-0x28ff}`}, 100 {`\P{Braille}`, `cc{0x0-0x27ff 0x2900-0x10ffff}`}, 101 {`\p{^Braille}`, `cc{0x0-0x27ff 0x2900-0x10ffff}`}, 102 {`\P{^Braille}`, `cc{0x2800-0x28ff}`}, 103 {`\pZ`, `cc{0x20 0xa0 0x1680 0x2000-0x200a 0x2028-0x2029 0x202f 0x205f 0x3000}`}, 104 {`[\p{Braille}]`, `cc{0x2800-0x28ff}`}, 105 {`[\P{Braille}]`, `cc{0x0-0x27ff 0x2900-0x10ffff}`}, 106 {`[\p{^Braille}]`, `cc{0x0-0x27ff 0x2900-0x10ffff}`}, 107 {`[\P{^Braille}]`, `cc{0x2800-0x28ff}`}, 108 {`[\pZ]`, `cc{0x20 0xa0 0x1680 0x2000-0x200a 0x2028-0x2029 0x202f 0x205f 0x3000}`}, 109 {`\p{Lu}`, mkCharClass(unicode.IsUpper)}, 110 {`[\p{Lu}]`, mkCharClass(unicode.IsUpper)}, 111 {`(?i)[\p{Lu}]`, mkCharClass(isUpperFold)}, 112 {`\p{Any}`, `dot{}`}, 113 {`\p{^Any}`, `cc{}`}, 114 115 // Hex, octal. 116 {`[\012-\234]\141`, `cat{cc{0xa-0x9c}lit{a}}`}, 117 {`[\x{41}-\x7a]\x61`, `cat{cc{0x41-0x7a}lit{a}}`}, 118 119 // More interesting regular expressions. 120 {`a{,2}`, `str{a{,2}}`}, 121 {`\.\^\$\\`, `str{.^$\}`}, 122 {`[a-zABC]`, `cc{0x41-0x43 0x61-0x7a}`}, 123 {`[^a]`, `cc{0x0-0x60 0x62-0x10ffff}`}, 124 {`[α-ε☺]`, `cc{0x3b1-0x3b5 0x263a}`}, // utf-8 125 {`a*{`, `cat{star{lit{a}}lit{{}}`}, 126 127 // Test precedences 128 {`(?:ab)*`, `star{str{ab}}`}, 129 {`(ab)*`, `star{cap{str{ab}}}`}, 130 {`ab|cd`, `alt{str{ab}str{cd}}`}, 131 {`a(b|c)d`, `cat{lit{a}cap{cc{0x62-0x63}}lit{d}}`}, 132 133 // Test flattening. 134 {`(?:a)`, `lit{a}`}, 135 {`(?:ab)(?:cd)`, `str{abcd}`}, 136 {`(?:a+b+)(?:c+d+)`, `cat{plus{lit{a}}plus{lit{b}}plus{lit{c}}plus{lit{d}}}`}, 137 {`(?:a+|b+)|(?:c+|d+)`, `alt{plus{lit{a}}plus{lit{b}}plus{lit{c}}plus{lit{d}}}`}, 138 {`(?:a|b)|(?:c|d)`, `cc{0x61-0x64}`}, 139 {`a|.`, `dot{}`}, 140 {`.|a`, `dot{}`}, 141 {`(?:[abc]|A|Z|hello|world)`, `alt{cc{0x41 0x5a 0x61-0x63}str{hello}str{world}}`}, 142 {`(?:[abc]|A|Z)`, `cc{0x41 0x5a 0x61-0x63}`}, 143 144 // Test Perl quoted literals 145 {`\Q+|*?{[\E`, `str{+|*?{[}`}, 146 {`\Q+\E+`, `plus{lit{+}}`}, 147 {`\Qab\E+`, `cat{lit{a}plus{lit{b}}}`}, 148 {`\Q\\E`, `lit{\}`}, 149 {`\Q\\\E`, `str{\\}`}, 150 151 // Test Perl \A and \z 152 {`(?m)^`, `bol{}`}, 153 {`(?m)$`, `eol{}`}, 154 {`(?-m)^`, `bot{}`}, 155 {`(?-m)$`, `eot{}`}, 156 {`(?m)\A`, `bot{}`}, 157 {`(?m)\z`, `eot{\z}`}, 158 {`(?-m)\A`, `bot{}`}, 159 {`(?-m)\z`, `eot{\z}`}, 160 161 // Test named captures 162 {`(?P<name>a)`, `cap{name:lit{a}}`}, 163 {`(?<name>a)`, `cap{name:lit{a}}`}, 164 165 // Case-folded literals 166 {`[Aa]`, `litfold{A}`}, 167 {`[\x{100}\x{101}]`, `litfold{Ā}`}, 168 {`[Δδ]`, `litfold{Δ}`}, 169 170 // Strings 171 {`abcde`, `str{abcde}`}, 172 {`[Aa][Bb]cd`, `cat{strfold{AB}str{cd}}`}, 173 174 // Factoring. 175 {`abc|abd|aef|bcx|bcy`, `alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}cat{str{bc}cc{0x78-0x79}}}`}, 176 {`ax+y|ax+z|ay+w`, `cat{lit{a}alt{cat{plus{lit{x}}lit{y}}cat{plus{lit{x}}lit{z}}cat{plus{lit{y}}lit{w}}}}`}, 177 178 // Bug fixes. 179 {`(?:.)`, `dot{}`}, 180 {`(?:x|(?:xa))`, `cat{lit{x}alt{emp{}lit{a}}}`}, 181 {`(?:.|(?:.a))`, `cat{dot{}alt{emp{}lit{a}}}`}, 182 {`(?:A(?:A|a))`, `cat{lit{A}litfold{A}}`}, 183 {`(?:A|a)`, `litfold{A}`}, 184 {`A|(?:A|a)`, `litfold{A}`}, 185 {`(?s).`, `dot{}`}, 186 {`(?-s).`, `dnl{}`}, 187 {`(?:(?:^).)`, `cat{bol{}dot{}}`}, 188 {`(?-s)(?:(?:^).)`, `cat{bol{}dnl{}}`}, 189 {`[\s\S]a`, `cat{cc{0x0-0x10ffff}lit{a}}`}, 190 191 // RE2 prefix_tests 192 {`abc|abd`, `cat{str{ab}cc{0x63-0x64}}`}, 193 {`a(?:b)c|abd`, `cat{str{ab}cc{0x63-0x64}}`}, 194 {`abc|abd|aef|bcx|bcy`, 195 `alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}` + 196 `cat{str{bc}cc{0x78-0x79}}}`}, 197 {`abc|x|abd`, `alt{str{abc}lit{x}str{abd}}`}, 198 {`(?i)abc|ABD`, `cat{strfold{AB}cc{0x43-0x44 0x63-0x64}}`}, 199 {`[ab]c|[ab]d`, `cat{cc{0x61-0x62}cc{0x63-0x64}}`}, 200 {`.c|.d`, `cat{dot{}cc{0x63-0x64}}`}, 201 {`x{2}|x{2}[0-9]`, 202 `cat{rep{2,2 lit{x}}alt{emp{}cc{0x30-0x39}}}`}, 203 {`x{2}y|x{2}[0-9]y`, 204 `cat{rep{2,2 lit{x}}alt{lit{y}cat{cc{0x30-0x39}lit{y}}}}`}, 205 {`a.*?c|a.*?b`, 206 `cat{lit{a}alt{cat{nstar{dot{}}lit{c}}cat{nstar{dot{}}lit{b}}}}`}, 207 208 // Valid repetitions. 209 {`((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}))`, ``}, 210 {`((((((((((x{1}){2}){2}){2}){2}){2}){2}){2}){2}){2})`, ``}, 211 212 // Valid nesting. 213 {strings.Repeat("(", 999) + strings.Repeat(")", 999), ``}, 214 {strings.Repeat("(?:", 999) + strings.Repeat(")*", 999), ``}, 215 {"(" + strings.Repeat("|", 12345) + ")", ``}, // not nested at all 216 } 217 218 const testFlags = MatchNL | PerlX | UnicodeGroups 219 220 func TestParseSimple(t *testing.T) { 221 testParseDump(t, parseTests, testFlags) 222 } 223 224 var foldcaseTests = []parseTest{ 225 {`AbCdE`, `strfold{ABCDE}`}, 226 {`[Aa]`, `litfold{A}`}, 227 {`a`, `litfold{A}`}, 228 229 // 0x17F is an old English long s (looks like an f) and folds to s. 230 // 0x212A is the Kelvin symbol and folds to k. 231 {`A[F-g]`, `cat{litfold{A}cc{0x41-0x7a 0x17f 0x212a}}`}, // [Aa][A-z...] 232 {`[[:upper:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`}, 233 {`[[:lower:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`}, 234 } 235 236 func TestParseFoldCase(t *testing.T) { 237 testParseDump(t, foldcaseTests, FoldCase) 238 } 239 240 var literalTests = []parseTest{ 241 {"(|)^$.[*+?]{5,10},\\", "str{(|)^$.[*+?]{5,10},\\}"}, 242 } 243 244 func TestParseLiteral(t *testing.T) { 245 testParseDump(t, literalTests, Literal) 246 } 247 248 var matchnlTests = []parseTest{ 249 {`.`, `dot{}`}, 250 {"\n", "lit{\n}"}, 251 {`[^a]`, `cc{0x0-0x60 0x62-0x10ffff}`}, 252 {`[a\n]`, `cc{0xa 0x61}`}, 253 } 254 255 func TestParseMatchNL(t *testing.T) { 256 testParseDump(t, matchnlTests, MatchNL) 257 } 258 259 var nomatchnlTests = []parseTest{ 260 {`.`, `dnl{}`}, 261 {"\n", "lit{\n}"}, 262 {`[^a]`, `cc{0x0-0x9 0xb-0x60 0x62-0x10ffff}`}, 263 {`[a\n]`, `cc{0xa 0x61}`}, 264 } 265 266 func TestParseNoMatchNL(t *testing.T) { 267 testParseDump(t, nomatchnlTests, 0) 268 } 269 270 // Test Parse -> Dump. 271 func testParseDump(t *testing.T, tests []parseTest, flags Flags) { 272 for _, tt := range tests { 273 re, err := Parse(tt.Regexp, flags) 274 if err != nil { 275 t.Errorf("Parse(%#q): %v", tt.Regexp, err) 276 continue 277 } 278 if tt.Dump == "" { 279 // It parsed. That's all we care about. 280 continue 281 } 282 d := dump(re) 283 if d != tt.Dump { 284 t.Errorf("Parse(%#q).Dump() = %#q want %#q", tt.Regexp, d, tt.Dump) 285 } 286 } 287 } 288 289 // dump prints a string representation of the regexp showing 290 // the structure explicitly. 291 func dump(re *Regexp) string { 292 var b strings.Builder 293 dumpRegexp(&b, re) 294 return b.String() 295 } 296 297 var opNames = []string{ 298 OpNoMatch: "no", 299 OpEmptyMatch: "emp", 300 OpLiteral: "lit", 301 OpCharClass: "cc", 302 OpAnyCharNotNL: "dnl", 303 OpAnyChar: "dot", 304 OpBeginLine: "bol", 305 OpEndLine: "eol", 306 OpBeginText: "bot", 307 OpEndText: "eot", 308 OpWordBoundary: "wb", 309 OpNoWordBoundary: "nwb", 310 OpCapture: "cap", 311 OpStar: "star", 312 OpPlus: "plus", 313 OpQuest: "que", 314 OpRepeat: "rep", 315 OpConcat: "cat", 316 OpAlternate: "alt", 317 } 318 319 // dumpRegexp writes an encoding of the syntax tree for the regexp re to b. 320 // It is used during testing to distinguish between parses that might print 321 // the same using re's String method. 322 func dumpRegexp(b *strings.Builder, re *Regexp) { 323 if int(re.Op) >= len(opNames) || opNames[re.Op] == "" { 324 fmt.Fprintf(b, "op%d", re.Op) 325 } else { 326 switch re.Op { 327 default: 328 b.WriteString(opNames[re.Op]) 329 case OpStar, OpPlus, OpQuest, OpRepeat: 330 if re.Flags&NonGreedy != 0 { 331 b.WriteByte('n') 332 } 333 b.WriteString(opNames[re.Op]) 334 case OpLiteral: 335 if len(re.Rune) > 1 { 336 b.WriteString("str") 337 } else { 338 b.WriteString("lit") 339 } 340 if re.Flags&FoldCase != 0 { 341 for _, r := range re.Rune { 342 if unicode.SimpleFold(r) != r { 343 b.WriteString("fold") 344 break 345 } 346 } 347 } 348 } 349 } 350 b.WriteByte('{') 351 switch re.Op { 352 case OpEndText: 353 if re.Flags&WasDollar == 0 { 354 b.WriteString(`\z`) 355 } 356 case OpLiteral: 357 for _, r := range re.Rune { 358 b.WriteRune(r) 359 } 360 case OpConcat, OpAlternate: 361 for _, sub := range re.Sub { 362 dumpRegexp(b, sub) 363 } 364 case OpStar, OpPlus, OpQuest: 365 dumpRegexp(b, re.Sub[0]) 366 case OpRepeat: 367 fmt.Fprintf(b, "%d,%d ", re.Min, re.Max) 368 dumpRegexp(b, re.Sub[0]) 369 case OpCapture: 370 if re.Name != "" { 371 b.WriteString(re.Name) 372 b.WriteByte(':') 373 } 374 dumpRegexp(b, re.Sub[0]) 375 case OpCharClass: 376 sep := "" 377 for i := 0; i < len(re.Rune); i += 2 { 378 b.WriteString(sep) 379 sep = " " 380 lo, hi := re.Rune[i], re.Rune[i+1] 381 if lo == hi { 382 fmt.Fprintf(b, "%#x", lo) 383 } else { 384 fmt.Fprintf(b, "%#x-%#x", lo, hi) 385 } 386 } 387 } 388 b.WriteByte('}') 389 } 390 391 func mkCharClass(f func(rune) bool) string { 392 re := &Regexp{Op: OpCharClass} 393 lo := rune(-1) 394 for i := rune(0); i <= unicode.MaxRune; i++ { 395 if f(i) { 396 if lo < 0 { 397 lo = i 398 } 399 } else { 400 if lo >= 0 { 401 re.Rune = append(re.Rune, lo, i-1) 402 lo = -1 403 } 404 } 405 } 406 if lo >= 0 { 407 re.Rune = append(re.Rune, lo, unicode.MaxRune) 408 } 409 return dump(re) 410 } 411 412 func isUpperFold(r rune) bool { 413 if unicode.IsUpper(r) { 414 return true 415 } 416 c := unicode.SimpleFold(r) 417 for c != r { 418 if unicode.IsUpper(c) { 419 return true 420 } 421 c = unicode.SimpleFold(c) 422 } 423 return false 424 } 425 426 func TestFoldConstants(t *testing.T) { 427 last := rune(-1) 428 for i := rune(0); i <= unicode.MaxRune; i++ { 429 if unicode.SimpleFold(i) == i { 430 continue 431 } 432 if last == -1 && minFold != i { 433 t.Errorf("minFold=%#U should be %#U", minFold, i) 434 } 435 last = i 436 } 437 if maxFold != last { 438 t.Errorf("maxFold=%#U should be %#U", maxFold, last) 439 } 440 } 441 442 func TestAppendRangeCollapse(t *testing.T) { 443 // AppendRange should collapse each of the new ranges 444 // into the earlier ones (it looks back two ranges), so that 445 // the slice never grows very large. 446 // Note that we are not calling cleanClass. 447 var r []rune 448 for i := rune('A'); i <= 'Z'; i++ { 449 r = appendRange(r, i, i) 450 r = appendRange(r, i+'a'-'A', i+'a'-'A') 451 } 452 if string(r) != "AZaz" { 453 t.Errorf("appendRange interlaced A-Z a-z = %s, want AZaz", string(r)) 454 } 455 } 456 457 var invalidRegexps = []string{ 458 `(`, 459 `)`, 460 `(a`, 461 `a)`, 462 `(a))`, 463 `(a|b|`, 464 `a|b|)`, 465 `(a|b|))`, 466 `(a|b`, 467 `a|b)`, 468 `(a|b))`, 469 `[a-z`, 470 `([a-z)`, 471 `[a-z)`, 472 `([a-z]))`, 473 `x{1001}`, 474 `x{9876543210}`, 475 `x{2,1}`, 476 `x{1,9876543210}`, 477 "\xff", // Invalid UTF-8 478 "[\xff]", 479 "[\\\xff]", 480 "\\\xff", 481 `(?P<name>a`, 482 `(?P<name>`, 483 `(?P<name`, 484 `(?P<x y>a)`, 485 `(?P<>a)`, 486 `(?<name>a`, 487 `(?<name>`, 488 `(?<name`, 489 `(?<x y>a)`, 490 `(?<>a)`, 491 `[a-Z]`, 492 `(?i)[a-Z]`, 493 `\Q\E*`, 494 `a{100000}`, // too much repetition 495 `a{100000,}`, // too much repetition 496 "((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}){2})", // too much repetition 497 strings.Repeat("(", 1000) + strings.Repeat(")", 1000), // too deep 498 strings.Repeat("(?:", 1000) + strings.Repeat(")*", 1000), // too deep 499 "(" + strings.Repeat("(xx?)", 1000) + "){1000}", // too long 500 strings.Repeat("(xx?){1000}", 1000), // too long 501 strings.Repeat(`\pL`, 27000), // too many runes 502 } 503 504 var onlyPerl = []string{ 505 `[a-b-c]`, 506 `\Qabc\E`, 507 `\Q*+?{[\E`, 508 `\Q\\E`, 509 `\Q\\\E`, 510 `\Q\\\\E`, 511 `\Q\\\\\E`, 512 `(?:a)`, 513 `(?P<name>a)`, 514 } 515 516 var onlyPOSIX = []string{ 517 "a++", 518 "a**", 519 "a?*", 520 "a+*", 521 "a{1}*", 522 ".{1}{2}.{3}", 523 } 524 525 func TestParseInvalidRegexps(t *testing.T) { 526 for _, regexp := range invalidRegexps { 527 if re, err := Parse(regexp, Perl); err == nil { 528 t.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp, dump(re)) 529 } 530 if re, err := Parse(regexp, POSIX); err == nil { 531 t.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp, dump(re)) 532 } 533 } 534 for _, regexp := range onlyPerl { 535 if _, err := Parse(regexp, Perl); err != nil { 536 t.Errorf("Parse(%#q, Perl): %v", regexp, err) 537 } 538 if re, err := Parse(regexp, POSIX); err == nil { 539 t.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp, dump(re)) 540 } 541 } 542 for _, regexp := range onlyPOSIX { 543 if re, err := Parse(regexp, Perl); err == nil { 544 t.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp, dump(re)) 545 } 546 if _, err := Parse(regexp, POSIX); err != nil { 547 t.Errorf("Parse(%#q, POSIX): %v", regexp, err) 548 } 549 } 550 } 551 552 func TestToStringEquivalentParse(t *testing.T) { 553 for _, tt := range parseTests { 554 re, err := Parse(tt.Regexp, testFlags) 555 if err != nil { 556 t.Errorf("Parse(%#q): %v", tt.Regexp, err) 557 continue 558 } 559 if tt.Dump == "" { 560 // It parsed. That's all we care about. 561 continue 562 } 563 d := dump(re) 564 if d != tt.Dump { 565 t.Errorf("Parse(%#q).Dump() = %#q want %#q", tt.Regexp, d, tt.Dump) 566 continue 567 } 568 569 s := re.String() 570 if s != tt.Regexp { 571 // If ToString didn't return the original regexp, 572 // it must have found one with fewer parens. 573 // Unfortunately we can't check the length here, because 574 // ToString produces "\\{" for a literal brace, 575 // but "{" is a shorter equivalent in some contexts. 576 nre, err := Parse(s, testFlags) 577 if err != nil { 578 t.Errorf("Parse(%#q.String() = %#q): %v", tt.Regexp, s, err) 579 continue 580 } 581 nd := dump(nre) 582 if d != nd { 583 t.Errorf("Parse(%#q) -> %#q; %#q vs %#q", tt.Regexp, s, d, nd) 584 } 585 586 ns := nre.String() 587 if s != ns { 588 t.Errorf("Parse(%#q) -> %#q -> %#q", tt.Regexp, s, ns) 589 } 590 } 591 } 592 } 593 594 var stringTests = []struct { 595 re string 596 out string 597 }{ 598 {`x(?i:ab*c|d?e)1`, `x(?i:AB*C|D?E)1`}, 599 {`x(?i:ab*cd?e)1`, `x(?i:AB*CD?E)1`}, 600 {`0(?i:ab*c|d?e)1`, `(?i:0(?:AB*C|D?E)1)`}, 601 {`0(?i:ab*cd?e)1`, `(?i:0AB*CD?E1)`}, 602 {`x(?i:ab*c|d?e)`, `x(?i:AB*C|D?E)`}, 603 {`x(?i:ab*cd?e)`, `x(?i:AB*CD?E)`}, 604 {`0(?i:ab*c|d?e)`, `(?i:0(?:AB*C|D?E))`}, 605 {`0(?i:ab*cd?e)`, `(?i:0AB*CD?E)`}, 606 {`(?i:ab*c|d?e)1`, `(?i:(?:AB*C|D?E)1)`}, 607 {`(?i:ab*cd?e)1`, `(?i:AB*CD?E1)`}, 608 {`(?i:ab)[123](?i:cd)`, `(?i:AB[1-3]CD)`}, 609 {`(?i:ab*c|d?e)`, `(?i:AB*C|D?E)`}, 610 {`[Aa][Bb]`, `(?i:AB)`}, 611 {`[Aa][Bb]*[Cc]`, `(?i:AB*C)`}, 612 {`A(?:[Bb][Cc]|[Dd])[Zz]`, `A(?i:(?:BC|D)Z)`}, 613 {`[Aa](?:[Bb][Cc]|[Dd])Z`, `(?i:A(?:BC|D))Z`}, 614 } 615 616 func TestString(t *testing.T) { 617 for _, tt := range stringTests { 618 re, err := Parse(tt.re, Perl) 619 if err != nil { 620 t.Errorf("Parse(%#q): %v", tt.re, err) 621 continue 622 } 623 out := re.String() 624 if out != tt.out { 625 t.Errorf("Parse(%#q).String() = %#q, want %#q", tt.re, out, tt.out) 626 } 627 } 628 }