diff --git a/regex/re_test.go b/regex/re_test.go index 2cccc72..b8b1381 100644 --- a/regex/re_test.go +++ b/regex/re_test.go @@ -528,7 +528,7 @@ var groupTests = []struct { }{ {"(a)(b)", nil, "ab", []Match{[]Group{{0, 2}, {0, 1}, {1, 2}}}}, {"((a))(b)", nil, "ab", []Match{[]Group{{0, 2}, {0, 1}, {0, 1}, {1, 2}}}}, - {"(0)", nil, "ab", []Match{[]Group{}}}, + {"(0)", nil, "ab", []Match{}}, {"(a)b", nil, "ab", []Match{[]Group{{0, 2}, {0, 1}}}}, {"a(b)", nil, "ab", []Match{[]Group{{0, 2}, {1, 2}}}}, {"(a|b)", nil, "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 2}, {1, 2}}}}, @@ -538,9 +538,8 @@ var groupTests = []struct { {"(a+)(aa)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 2}, {2, 4}}}}, {"(aaaa)|(aaaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}}, {"(aaa)|(aaaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {-1, -1}, {0, 4}}}}, - {"(aaa)|(aaaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {-1, -1}, {0, 4}}}}, {"(aaaa)|(aaa)", nil, "aaaa", []Match{[]Group{{0, 4}, {0, 4}, {-1, -1}}}}, - {"(a)|(aa)", nil, "aa", []Match{[]Group{{0, 2}, {-1, -1}, {0, 2}}}}, + {"(a)|(aa)", nil, "aa", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 2}, {1, 2}}}}, {"(a?)a?", nil, "b", []Match{[]Group{{0, 0}, {0, 0}}, []Group{{1, 1}, {1, 1}}}}, {"(a?)a?", nil, "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 1}, {1, 1}}, []Group{{2, 2}, {2, 2}}}}, {"(a?)a?", nil, "aa", []Match{[]Group{{0, 2}, {0, 1}}, []Group{{2, 2}, {2, 2}}}}, @@ -578,7 +577,7 @@ var groupTests = []struct { {`(bc+d$|ef*g.|h?i(j|k))`, nil, `bcdd`, []Match{}}, {`(bc+d$|ef*g.|h?i(j|k))`, nil, `reffgz`, []Match{[]Group{{1, 6}, {1, 6}}}}, {`(((((((((a)))))))))`, nil, `a`, []Match{[]Group{{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}}}, - {`(((((((((a)))))))))\41`, nil, `a`, []Match{[]Group{{0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}}}, + {`(((((((((a)))))))))\41`, nil, `a!`, []Match{[]Group{{0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}}}, {`(.*)c(.*)`, nil, `abcde`, []Match{[]Group{{0, 5}, {0, 2}, {3, 5}}}}, {`\((.*), (.*)\)`, nil, `(a, b)`, []Match{[]Group{{0, 6}, {1, 2}, {4, 5}}}}, @@ -633,7 +632,7 @@ var groupTests = []struct { {`(bc+d$|ef*g.|h?i(j|k))`, []ReFlag{RE_CASE_INSENSITIVE}, `BCDD`, []Match{}}, {`(bc+d$|ef*g.|h?i(j|k))`, []ReFlag{RE_CASE_INSENSITIVE}, `reffgz`, []Match{[]Group{{1, 6}, {1, 6}}}}, {`(((((((((a)))))))))`, []ReFlag{RE_CASE_INSENSITIVE}, `A`, []Match{[]Group{{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}}}, - {`(((((((((a)))))))))\41`, []ReFlag{RE_CASE_INSENSITIVE}, `A`, []Match{[]Group{{0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}}}, + {`(((((((((a)))))))))\41`, []ReFlag{RE_CASE_INSENSITIVE}, `A!`, []Match{[]Group{{0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}}}, {`(.*)c(.*)`, []ReFlag{RE_CASE_INSENSITIVE}, `ABCDE`, []Match{[]Group{{0, 5}, {0, 2}, {3, 5}}}}, {`\((.*), (.*)\)`, []ReFlag{RE_CASE_INSENSITIVE}, `(A, B)`, []Match{[]Group{{0, 6}, {1, 2}, {4, 5}}}}, {`(a)(b)c|ab`, []ReFlag{RE_CASE_INSENSITIVE}, `AB`, []Match{[]Group{{0, 2}}}}, @@ -743,7 +742,7 @@ func TestFindString(t *testing.T) { foundString := regComp.FindString(test.str) if len(test.result) == 0 { if foundString != "" { - t.Errorf("Expected no match got %v\n", foundString) + t.Errorf("Wanted no match got %v\n", foundString) } } else { expectedString := test.str[test.result[0].StartIdx:test.result[0].EndIdx] @@ -796,6 +795,56 @@ func TestFindSubmatch(t *testing.T) { if test.result[0][i] != match[i] { t.Errorf("Wanted %v Got %v\n", test.result[0], match) } + } else { + if i < len(test.result) && test.result[0][i].IsValid() { + t.Errorf("Wanted %v Got %v\n", test.result[0], match) + } + } + } + }) + } +} +func TestFindStringSubmatch(t *testing.T) { + for _, test := range groupTests { + t.Run(test.re+" "+test.str, func(t *testing.T) { + regComp, err := Compile(test.re, test.flags...) + if err != nil { + if test.result != nil { + panic(err) + } + } + matchStr := regComp.FindStringSubmatch(test.str) + if matchStr == nil { + if len(test.result) != 0 { + expectedStr := funcMap(test.result[0], func(g Group) string { + if g.IsValid() { + return test.str[g.StartIdx:g.EndIdx] + } else { + return "" + } + }) + t.Errorf("Wanted %v got no match\n", expectedStr) + } + } else if len(test.result) == 0 { + t.Errorf("Wanted no match got %v\n", matchStr) + } else { + expectedStr := funcMap(test.result[0], func(g Group) string { + if g.IsValid() { + return test.str[g.StartIdx:g.EndIdx] + } else { + return "" + } + }) + for i, groupStr := range matchStr { + if groupStr == "" { + if i < len(expectedStr) && expectedStr[i] != "" { + t.Errorf("Wanted %v Got %v\n", expectedStr, matchStr) + } + } else { + if expectedStr[i] != groupStr { + t.Errorf("Wanted %v Got %v\n", expectedStr, matchStr) + } + } } } }) @@ -817,6 +866,10 @@ func TestFindAllSubmatch(t *testing.T) { if test.result[i][j] != matchIndices[i][j] { t.Errorf("Wanted %v Got %v\n", test.result, matchIndices) } + } else { + if i < len(test.result) && j < len(test.result[i]) && test.result[i][j].IsValid() { + t.Errorf("Wanted %v Got %v\n", test.result, matchIndices) + } } } }