github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/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 "bytes" 9 "fmt" 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 164 // Case-folded literals 165 {`[Aa]`, `litfold{A}`}, 166 {`[\x{100}\x{101}]`, `litfold{Ā}`}, 167 {`[Δδ]`, `litfold{Δ}`}, 168 169 // Strings 170 {`abcde`, `str{abcde}`}, 171 {`[Aa][Bb]cd`, `cat{strfold{AB}str{cd}}`}, 172 173 // Factoring. 174 {`abc|abd|aef|bcx|bcy`, `alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}cat{str{bc}cc{0x78-0x79}}}`}, 175 {`ax+y|ax+z|ay+w`, `cat{lit{a}alt{cat{plus{lit{x}}cc{0x79-0x7a}}cat{plus{lit{y}}lit{w}}}}`}, 176 177 // Bug fixes. 178 {`(?:.)`, `dot{}`}, 179 {`(?:x|(?:xa))`, `cat{lit{x}alt{emp{}lit{a}}}`}, 180 {`(?:.|(?:.a))`, `cat{dot{}alt{emp{}lit{a}}}`}, 181 {`(?:A(?:A|a))`, `cat{lit{A}litfold{A}}`}, 182 {`(?:A|a)`, `litfold{A}`}, 183 {`A|(?:A|a)`, `litfold{A}`}, 184 {`(?s).`, `dot{}`}, 185 {`(?-s).`, `dnl{}`}, 186 {`(?:(?:^).)`, `cat{bol{}dot{}}`}, 187 {`(?-s)(?:(?:^).)`, `cat{bol{}dnl{}}`}, 188 189 // RE2 prefix_tests 190 {`abc|abd`, `cat{str{ab}cc{0x63-0x64}}`}, 191 {`a(?:b)c|abd`, `cat{str{ab}cc{0x63-0x64}}`}, 192 {`abc|abd|aef|bcx|bcy`, 193 `alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}` + 194 `cat{str{bc}cc{0x78-0x79}}}`}, 195 {`abc|x|abd`, `alt{str{abc}lit{x}str{abd}}`}, 196 {`(?i)abc|ABD`, `cat{strfold{AB}cc{0x43-0x44 0x63-0x64}}`}, 197 {`[ab]c|[ab]d`, `cat{cc{0x61-0x62}cc{0x63-0x64}}`}, 198 {`(?:xx|yy)c|(?:xx|yy)d`, 199 `cat{alt{str{xx}str{yy}}cc{0x63-0x64}}`}, 200 {`x{2}|x{2}[0-9]`, 201 `cat{rep{2,2 lit{x}}alt{emp{}cc{0x30-0x39}}}`}, 202 {`x{2}y|x{2}[0-9]y`, 203 `cat{rep{2,2 lit{x}}alt{lit{y}cat{cc{0x30-0x39}lit{y}}}}`}, 204 205 // Valid repetitions. 206 {`((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}))`, ``}, 207 {`((((((((((x{1}){2}){2}){2}){2}){2}){2}){2}){2}){2})`, ``}, 208 } 209 210 const testFlags = MatchNL | PerlX | UnicodeGroups 211 212 func TestParseSimple(t *testing.T) { 213 testParseDump(t, parseTests, testFlags) 214 } 215 216 var foldcaseTests = []parseTest{ 217 {`AbCdE`, `strfold{ABCDE}`}, 218 {`[Aa]`, `litfold{A}`}, 219 {`a`, `litfold{A}`}, 220 221 // 0x17F is an old English long s (looks like an f) and folds to s. 222 // 0x212A is the Kelvin symbol and folds to k. 223 {`A[F-g]`, `cat{litfold{A}cc{0x41-0x7a 0x17f 0x212a}}`}, // [Aa][A-z...] 224 {`[[:upper:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`}, 225 {`[[:lower:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`}, 226 } 227 228 func TestParseFoldCase(t *testing.T) { 229 testParseDump(t, foldcaseTests, FoldCase) 230 } 231 232 var literalTests = []parseTest{ 233 {"(|)^$.[*+?]{5,10},\\", "str{(|)^$.[*+?]{5,10},\\}"}, 234 } 235 236 func TestParseLiteral(t *testing.T) { 237 testParseDump(t, literalTests, Literal) 238 } 239 240 var matchnlTests = []parseTest{ 241 {`.`, `dot{}`}, 242 {"\n", "lit{\n}"}, 243 {`[^a]`, `cc{0x0-0x60 0x62-0x10ffff}`}, 244 {`[a\n]`, `cc{0xa 0x61}`}, 245 } 246 247 func TestParseMatchNL(t *testing.T) { 248 testParseDump(t, matchnlTests, MatchNL) 249 } 250 251 var nomatchnlTests = []parseTest{ 252 {`.`, `dnl{}`}, 253 {"\n", "lit{\n}"}, 254 {`[^a]`, `cc{0x0-0x9 0xb-0x60 0x62-0x10ffff}`}, 255 {`[a\n]`, `cc{0xa 0x61}`}, 256 } 257 258 func TestParseNoMatchNL(t *testing.T) { 259 testParseDump(t, nomatchnlTests, 0) 260 } 261 262 // Test Parse -> Dump. 263 func testParseDump(t *testing.T, tests []parseTest, flags Flags) { 264 for _, tt := range tests { 265 re, err := Parse(tt.Regexp, flags) 266 if err != nil { 267 t.Errorf("Parse(%#q): %v", tt.Regexp, err) 268 continue 269 } 270 if tt.Dump == "" { 271 // It parsed. That's all we care about. 272 continue 273 } 274 d := dump(re) 275 if d != tt.Dump { 276 t.Errorf("Parse(%#q).Dump() = %#q want %#q", tt.Regexp, d, tt.Dump) 277 } 278 } 279 } 280 281 // dump prints a string representation of the regexp showing 282 // the structure explicitly. 283 func dump(re *Regexp) string { 284 var b bytes.Buffer 285 dumpRegexp(&b, re) 286 return b.String() 287 } 288 289 var opNames = []string{ 290 OpNoMatch: "no", 291 OpEmptyMatch: "emp", 292 OpLiteral: "lit", 293 OpCharClass: "cc", 294 OpAnyCharNotNL: "dnl", 295 OpAnyChar: "dot", 296 OpBeginLine: "bol", 297 OpEndLine: "eol", 298 OpBeginText: "bot", 299 OpEndText: "eot", 300 OpWordBoundary: "wb", 301 OpNoWordBoundary: "nwb", 302 OpCapture: "cap", 303 OpStar: "star", 304 OpPlus: "plus", 305 OpQuest: "que", 306 OpRepeat: "rep", 307 OpConcat: "cat", 308 OpAlternate: "alt", 309 } 310 311 // dumpRegexp writes an encoding of the syntax tree for the regexp re to b. 312 // It is used during testing to distinguish between parses that might print 313 // the same using re's String method. 314 func dumpRegexp(b *bytes.Buffer, re *Regexp) { 315 if int(re.Op) >= len(opNames) || opNames[re.Op] == "" { 316 fmt.Fprintf(b, "op%d", re.Op) 317 } else { 318 switch re.Op { 319 default: 320 b.WriteString(opNames[re.Op]) 321 case OpStar, OpPlus, OpQuest, OpRepeat: 322 if re.Flags&NonGreedy != 0 { 323 b.WriteByte('n') 324 } 325 b.WriteString(opNames[re.Op]) 326 case OpLiteral: 327 if len(re.Rune) > 1 { 328 b.WriteString("str") 329 } else { 330 b.WriteString("lit") 331 } 332 if re.Flags&FoldCase != 0 { 333 for _, r := range re.Rune { 334 if unicode.SimpleFold(r) != r { 335 b.WriteString("fold") 336 break 337 } 338 } 339 } 340 } 341 } 342 b.WriteByte('{') 343 switch re.Op { 344 case OpEndText: 345 if re.Flags&WasDollar == 0 { 346 b.WriteString(`\z`) 347 } 348 case OpLiteral: 349 for _, r := range re.Rune { 350 b.WriteRune(r) 351 } 352 case OpConcat, OpAlternate: 353 for _, sub := range re.Sub { 354 dumpRegexp(b, sub) 355 } 356 case OpStar, OpPlus, OpQuest: 357 dumpRegexp(b, re.Sub[0]) 358 case OpRepeat: 359 fmt.Fprintf(b, "%d,%d ", re.Min, re.Max) 360 dumpRegexp(b, re.Sub[0]) 361 case OpCapture: 362 if re.Name != "" { 363 b.WriteString(re.Name) 364 b.WriteByte(':') 365 } 366 dumpRegexp(b, re.Sub[0]) 367 case OpCharClass: 368 sep := "" 369 for i := 0; i < len(re.Rune); i += 2 { 370 b.WriteString(sep) 371 sep = " " 372 lo, hi := re.Rune[i], re.Rune[i+1] 373 if lo == hi { 374 fmt.Fprintf(b, "%#x", lo) 375 } else { 376 fmt.Fprintf(b, "%#x-%#x", lo, hi) 377 } 378 } 379 } 380 b.WriteByte('}') 381 } 382 383 func mkCharClass(f func(rune) bool) string { 384 re := &Regexp{Op: OpCharClass} 385 lo := rune(-1) 386 for i := rune(0); i <= unicode.MaxRune; i++ { 387 if f(i) { 388 if lo < 0 { 389 lo = i 390 } 391 } else { 392 if lo >= 0 { 393 re.Rune = append(re.Rune, lo, i-1) 394 lo = -1 395 } 396 } 397 } 398 if lo >= 0 { 399 re.Rune = append(re.Rune, lo, unicode.MaxRune) 400 } 401 return dump(re) 402 } 403 404 func isUpperFold(r rune) bool { 405 if unicode.IsUpper(r) { 406 return true 407 } 408 c := unicode.SimpleFold(r) 409 for c != r { 410 if unicode.IsUpper(c) { 411 return true 412 } 413 c = unicode.SimpleFold(c) 414 } 415 return false 416 } 417 418 func TestFoldConstants(t *testing.T) { 419 last := rune(-1) 420 for i := rune(0); i <= unicode.MaxRune; i++ { 421 if unicode.SimpleFold(i) == i { 422 continue 423 } 424 if last == -1 && minFold != i { 425 t.Errorf("minFold=%#U should be %#U", minFold, i) 426 } 427 last = i 428 } 429 if maxFold != last { 430 t.Errorf("maxFold=%#U should be %#U", maxFold, last) 431 } 432 } 433 434 func TestAppendRangeCollapse(t *testing.T) { 435 // AppendRange should collapse each of the new ranges 436 // into the earlier ones (it looks back two ranges), so that 437 // the slice never grows very large. 438 // Note that we are not calling cleanClass. 439 var r []rune 440 for i := rune('A'); i <= 'Z'; i++ { 441 r = appendRange(r, i, i) 442 r = appendRange(r, i+'a'-'A', i+'a'-'A') 443 } 444 if string(r) != "AZaz" { 445 t.Errorf("appendRange interlaced A-Z a-z = %s, want AZaz", string(r)) 446 } 447 } 448 449 var invalidRegexps = []string{ 450 `(`, 451 `)`, 452 `(a`, 453 `a)`, 454 `(a))`, 455 `(a|b|`, 456 `a|b|)`, 457 `(a|b|))`, 458 `(a|b`, 459 `a|b)`, 460 `(a|b))`, 461 `[a-z`, 462 `([a-z)`, 463 `[a-z)`, 464 `([a-z]))`, 465 `x{1001}`, 466 `x{9876543210}`, 467 `x{2,1}`, 468 `x{1,9876543210}`, 469 "\xff", // Invalid UTF-8 470 "[\xff]", 471 "[\\\xff]", 472 "\\\xff", 473 `(?P<name>a`, 474 `(?P<name>`, 475 `(?P<name`, 476 `(?P<x y>a)`, 477 `(?P<>a)`, 478 `[a-Z]`, 479 `(?i)[a-Z]`, 480 `a{100000}`, 481 `a{100000,}`, 482 "((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}){2})", 483 `\Q\E*`, 484 } 485 486 var onlyPerl = []string{ 487 `[a-b-c]`, 488 `\Qabc\E`, 489 `\Q*+?{[\E`, 490 `\Q\\E`, 491 `\Q\\\E`, 492 `\Q\\\\E`, 493 `\Q\\\\\E`, 494 `(?:a)`, 495 `(?P<name>a)`, 496 } 497 498 var onlyPOSIX = []string{ 499 "a++", 500 "a**", 501 "a?*", 502 "a+*", 503 "a{1}*", 504 ".{1}{2}.{3}", 505 } 506 507 func TestParseInvalidRegexps(t *testing.T) { 508 for _, regexp := range invalidRegexps { 509 if re, err := Parse(regexp, Perl); err == nil { 510 t.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp, dump(re)) 511 } 512 if re, err := Parse(regexp, POSIX); err == nil { 513 t.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp, dump(re)) 514 } 515 } 516 for _, regexp := range onlyPerl { 517 if _, err := Parse(regexp, Perl); err != nil { 518 t.Errorf("Parse(%#q, Perl): %v", regexp, err) 519 } 520 if re, err := Parse(regexp, POSIX); err == nil { 521 t.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp, dump(re)) 522 } 523 } 524 for _, regexp := range onlyPOSIX { 525 if re, err := Parse(regexp, Perl); err == nil { 526 t.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp, dump(re)) 527 } 528 if _, err := Parse(regexp, POSIX); err != nil { 529 t.Errorf("Parse(%#q, POSIX): %v", regexp, err) 530 } 531 } 532 } 533 534 func TestToStringEquivalentParse(t *testing.T) { 535 for _, tt := range parseTests { 536 re, err := Parse(tt.Regexp, testFlags) 537 if err != nil { 538 t.Errorf("Parse(%#q): %v", tt.Regexp, err) 539 continue 540 } 541 if tt.Dump == "" { 542 // It parsed. That's all we care about. 543 continue 544 } 545 d := dump(re) 546 if d != tt.Dump { 547 t.Errorf("Parse(%#q).Dump() = %#q want %#q", tt.Regexp, d, tt.Dump) 548 continue 549 } 550 551 s := re.String() 552 if s != tt.Regexp { 553 // If ToString didn't return the original regexp, 554 // it must have found one with fewer parens. 555 // Unfortunately we can't check the length here, because 556 // ToString produces "\\{" for a literal brace, 557 // but "{" is a shorter equivalent in some contexts. 558 nre, err := Parse(s, testFlags) 559 if err != nil { 560 t.Errorf("Parse(%#q.String() = %#q): %v", tt.Regexp, s, err) 561 continue 562 } 563 nd := dump(nre) 564 if d != nd { 565 t.Errorf("Parse(%#q) -> %#q; %#q vs %#q", tt.Regexp, s, d, nd) 566 } 567 568 ns := nre.String() 569 if s != ns { 570 t.Errorf("Parse(%#q) -> %#q -> %#q", tt.Regexp, s, ns) 571 } 572 } 573 } 574 }