@@ -418,24 +418,32 @@ func TestLiteralPrefix(t *testing.T) {
418
418
}
419
419
}
420
420
421
+ type subexpIndex struct {
422
+ name string
423
+ index int
424
+ }
425
+
421
426
type subexpCase struct {
422
- input string
423
- num int
424
- names []string
427
+ input string
428
+ num int
429
+ names []string
430
+ indices []subexpIndex
425
431
}
426
432
433
+ var emptySubexpIndices = []subexpIndex {{"" , - 1 }, {"missing" , - 1 }}
434
+
427
435
var subexpCases = []subexpCase {
428
- {`` , 0 , nil },
429
- {`.*` , 0 , nil },
430
- {`abba` , 0 , nil },
431
- {`ab(b)a` , 1 , []string {"" , "" }},
432
- {`ab(.*)a` , 1 , []string {"" , "" }},
433
- {`(.*)ab(.*)a` , 2 , []string {"" , "" , "" }},
434
- {`(.*)(ab)(.*)a` , 3 , []string {"" , "" , "" , "" }},
435
- {`(.*)((a)b)(.*)a` , 4 , []string {"" , "" , "" , "" , "" }},
436
- {`(.*)(\(ab)(.*)a` , 3 , []string {"" , "" , "" , "" }},
437
- {`(.*)(\(a\)b)(.*)a` , 3 , []string {"" , "" , "" , "" }},
438
- {`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a` , 4 , []string {"" , "foo" , "bar" , "" , "foo" }},
436
+ {`` , 0 , nil , emptySubexpIndices },
437
+ {`.*` , 0 , nil , emptySubexpIndices },
438
+ {`abba` , 0 , nil , emptySubexpIndices },
439
+ {`ab(b)a` , 1 , []string {"" , "" }, emptySubexpIndices },
440
+ {`ab(.*)a` , 1 , []string {"" , "" }, emptySubexpIndices },
441
+ {`(.*)ab(.*)a` , 2 , []string {"" , "" , "" }, emptySubexpIndices },
442
+ {`(.*)(ab)(.*)a` , 3 , []string {"" , "" , "" , "" }, emptySubexpIndices },
443
+ {`(.*)((a)b)(.*)a` , 4 , []string {"" , "" , "" , "" , "" }, emptySubexpIndices },
444
+ {`(.*)(\(ab)(.*)a` , 3 , []string {"" , "" , "" , "" }, emptySubexpIndices },
445
+ {`(.*)(\(a\)b)(.*)a` , 3 , []string {"" , "" , "" , "" }, emptySubexpIndices },
446
+ {`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a` , 4 , []string {"" , "foo" , "bar" , "" , "foo" }, [] subexpIndex {{ "" , - 1 }, { "missing" , - 1 }, { "foo" , 1 }, { "bar" , 2 }} },
439
447
}
440
448
441
449
func TestSubexp (t * testing.T ) {
@@ -458,6 +466,12 @@ func TestSubexp(t *testing.T) {
458
466
}
459
467
}
460
468
}
469
+ for _ , subexp := range c .indices {
470
+ index := re .SubexpIndex (subexp .name )
471
+ if index != subexp .index {
472
+ t .Errorf ("%q: SubexpIndex(%q) = %d, want %d" , c .input , subexp .name , index , subexp .index )
473
+ }
474
+ }
461
475
}
462
476
}
463
477
0 commit comments