Wrote tests for backreferences

implementBackreferences
Aadhavan Srinivasan 4 weeks ago
parent f466d4a8d5
commit 2934e7a20f

@ -314,10 +314,6 @@ var reTests = []struct {
{`\0009`, nil, "\x009", []Group{{0, 2}}}, {`\0009`, nil, "\x009", []Group{{0, 2}}},
{`\0141`, nil, "a", []Group{{0, 1}}}, {`\0141`, nil, "a", []Group{{0, 1}}},
// At this point, the python test suite has a bunch
// of backreference tests. Since my engine doesn't
// implement backreferences, I've skipped those tests.
{`*a`, nil, ``, nil}, {`*a`, nil, ``, nil},
{`(*)b`, nil, ``, nil}, {`(*)b`, nil, ``, nil},
{`a**`, nil, ``, nil}, {`a**`, nil, ``, nil},
@ -585,9 +581,29 @@ var groupTests = []struct {
{`(.*)c(.*)`, nil, `abcde`, []Match{[]Group{{0, 5}, {0, 2}, {3, 5}}}}, {`(.*)c(.*)`, nil, `abcde`, []Match{[]Group{{0, 5}, {0, 2}, {3, 5}}}},
{`\((.*), (.*)\)`, nil, `(a, b)`, []Match{[]Group{{0, 6}, {1, 2}, {4, 5}}}}, {`\((.*), (.*)\)`, nil, `(a, b)`, []Match{[]Group{{0, 6}, {1, 2}, {4, 5}}}},
// At this point, the python test suite has a bunch // Backreference tests
// of backreference tests. Since my engine doesn't {`(abc)\1`, nil, `abcabc`, []Match{[]Group{{0, 6}, {0, 3}}}},
// implement backreferences, I've skipped those tests. {`([a-c]+)\1`, nil, `abcabc`, []Match{[]Group{{0, 6}, {0, 3}}}},
{`([a-c]*)\1`, nil, `abcabc`, []Match{[]Group{{0, 6}, {0, 3}}, []Group{{6, 6}, {6, 6}}}},
{`^(.+)?B`, nil, `AB`, []Match{[]Group{{0, 2}, {0, 1}}}},
{`(a+).\1$`, nil, `aaaaa`, []Match{[]Group{{0, 5}, {0, 2}}}},
{`^(a+).\1$`, nil, `aaaa`, []Match{}},
{`(a)\1`, nil, `aa`, []Match{[]Group{{0, 2}, {0, 1}}}},
{`(a+)\1`, nil, `aa`, []Match{[]Group{{0, 2}, {0, 1}}}},
{`(a+)+\1`, nil, `aa`, []Match{[]Group{{0, 2}, {0, 1}}}},
{`(a).+\1`, nil, `aba`, []Match{[]Group{{0, 3}, {0, 1}}}},
{`(a)ba*\1`, nil, `aba`, []Match{[]Group{{0, 3}, {0, 1}}}},
{`(aa|a)a\1$`, nil, `aaa`, []Match{[]Group{{0, 3}, {0, 1}}}},
{`(a|aa)a\1$`, nil, `aaa`, []Match{[]Group{{0, 3}, {0, 1}}}},
{`(a+)a\1$`, nil, `aaa`, []Match{[]Group{{0, 3}, {0, 1}}}},
{`([abc]*)\1`, nil, `abcabc`, []Match{[]Group{{0, 6}, {0, 3}}, []Group{{6, 6}, {6, 6}}}},
{`(a)(?:b)\1`, nil, `aba`, []Match{[]Group{{0, 3}, {0, 1}}}},
{`(a)(?:b)\1`, nil, `abb`, []Match{}},
{`(?:a)(b)\1`, nil, `aba`, []Match{}},
{`(?:a)(b)\1`, nil, `abb`, []Match{[]Group{{0, 3}, {1, 2}}}},
{`(?:(cat)|(dog))\2`, nil, `catdog`, []Match{}},
{`(?:a)\1`, nil, `aa`, nil},
{`((cat)|(dog)|(cow)|(bat))\4`, nil, `cowcow`, []Match{[]Group{{0, 6}, {0, 3}, {-1, -1}, {-1, -1}, {0, 3}, {-1, -1}}}},
{`(a)(b)c|ab`, nil, `ab`, []Match{[]Group{{0, 2}}}}, {`(a)(b)c|ab`, nil, `ab`, []Match{[]Group{{0, 2}}}},
{`(a)+x`, nil, `aaax`, []Match{[]Group{{0, 4}, {2, 3}}}}, {`(a)+x`, nil, `aaax`, []Match{[]Group{{0, 4}, {2, 3}}}},
@ -792,7 +808,7 @@ func TestFindSubmatch(t *testing.T) {
if test.result != nil { if test.result != nil {
panic(err) panic(err)
} }
} } else {
match, err := regComp.FindSubmatch(test.str) match, err := regComp.FindSubmatch(test.str)
if err != nil { if err != nil {
if len(test.result) != 0 { if len(test.result) != 0 {
@ -812,6 +828,7 @@ func TestFindSubmatch(t *testing.T) {
} }
} }
} }
}
}) })
} }
} }
@ -823,7 +840,7 @@ func TestFindStringSubmatch(t *testing.T) {
if test.result != nil { if test.result != nil {
panic(err) panic(err)
} }
} } else {
matchStr := regComp.FindStringSubmatch(test.str) matchStr := regComp.FindStringSubmatch(test.str)
if matchStr == nil { if matchStr == nil {
if len(test.result) != 0 { if len(test.result) != 0 {
@ -858,6 +875,7 @@ func TestFindStringSubmatch(t *testing.T) {
} }
} }
} }
}
}) })
} }
} }
@ -870,7 +888,7 @@ func TestFindAllStringSubmatch(t *testing.T) {
if test.result != nil { if test.result != nil {
panic(err) panic(err)
} }
} } else {
matchStrs := regComp.FindAllStringSubmatch(test.str) matchStrs := regComp.FindAllStringSubmatch(test.str)
if matchStrs == nil { if matchStrs == nil {
if len(test.result) != 0 { if len(test.result) != 0 {
@ -911,6 +929,7 @@ func TestFindAllStringSubmatch(t *testing.T) {
} }
} }
} }
}
}) })
} }
} }
@ -923,7 +942,7 @@ func TestFindAllSubmatch(t *testing.T) {
if test.result != nil { if test.result != nil {
panic(err) panic(err)
} }
} } else {
matchIndices := regComp.FindAllSubmatch(test.str) matchIndices := regComp.FindAllSubmatch(test.str)
for i := range matchIndices { for i := range matchIndices {
for j := range matchIndices[i] { for j := range matchIndices[i] {
@ -938,6 +957,7 @@ func TestFindAllSubmatch(t *testing.T) {
} }
} }
} }
}
}) })
} }
} }

Loading…
Cancel
Save