From eb0ab9f7ec554bb21aec6a136155c5726337dace Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 10 Feb 2025 08:39:20 -0500 Subject: [PATCH] Wrote test for FindAllStringSubmatch() --- regex/re_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/regex/re_test.go b/regex/re_test.go index 8b9fc8d..f697e81 100644 --- a/regex/re_test.go +++ b/regex/re_test.go @@ -861,6 +861,60 @@ func TestFindStringSubmatch(t *testing.T) { }) } } + +func TestFindAllStringSubmatch(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) + } + } + matchStrs := regComp.FindAllStringSubmatch(test.str) + if matchStrs == nil { + if len(test.result) != 0 { + expectedStrs := funcMap(test.result, func(m Match) []string { + return funcMap(m, func(g Group) string { + if g.IsValid() { + return test.str[g.StartIdx:g.EndIdx] + } else { + return "" + } + }) + }) + t.Errorf("Wanted %v got no match\n", expectedStrs) + } + } else if len(test.result) == 0 { + t.Errorf("Wanted no match got %v\n", matchStrs) + } else { + expectedStrs := funcMap(test.result, func(m Match) []string { + return funcMap(m, func(g Group) string { + if g.IsValid() { + return test.str[g.StartIdx:g.EndIdx] + } else { + return "" + } + }) + }) + for i, matchStr := range matchStrs { + for j, groupStr := range matchStr { + if groupStr == "" { + if j < len(expectedStrs[i]) && expectedStrs[i][j] != "" { + t.Errorf("Wanted %v Got %v\n", expectedStrs, matchStrs) + } + } else { + if expectedStrs[i][j] != groupStr { + t.Errorf("Wanted %v Got %v\n", expectedStrs, matchStrs) + } + } + } + } + } + }) + } +} + func TestFindAllSubmatch(t *testing.T) { for _, test := range groupTests { t.Run(test.re+" "+test.str, func(t *testing.T) {