2 Commits

Author SHA1 Message Date
6869cd00a2 Return error instead of nil when 'Find' fails 2025-01-31 10:52:38 -05:00
02bc8f30a2 Added test for 'Find' 2025-01-31 10:52:27 -05:00
2 changed files with 30 additions and 4 deletions

View File

@@ -148,7 +148,7 @@ func pruneIndices(indices []Match) []Match {
func (regex Reg) Find(str string) (Group, error) {
match, err := regex.FindNthMatch(str, 1)
if err != nil {
return Group{}, nil
return Group{}, fmt.Errorf("no matches found")
}
return getZeroGroup(match), nil
}

View File

@@ -673,7 +673,33 @@ var groupTests = []struct {
{`(<389-400>)`, nil, `391`, []Match{[]Group{{0, 3}, {0, 3}}}},
}
func TestFindAllMatches(t *testing.T) {
func TestFind(t *testing.T) {
for _, test := range reTests {
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(fmt.Errorf("Test Error: %v", err))
}
} else {
groupIndex, err := regComp.Find(test.str)
if err != nil { // No matches found
if len(test.result) == 0 {
return // Manually pass the test, because this is the expected behavior
} else {
t.Errorf("Wanted no match Got %v\n", groupIndex)
}
} else {
if groupIndex != test.result[0] {
t.Errorf("Wanted %v Got %v\n", test.result, groupIndex)
}
}
}
})
}
}
func TestFindAll(t *testing.T) {
for _, test := range reTests {
t.Run(test.re+" "+test.str, func(t *testing.T) {
regComp, err := Compile(test.re, test.flags...)
@@ -716,7 +742,7 @@ func TestFindString(t *testing.T) {
}
}
func TestFindAllStrings(t *testing.T) {
func TestFindAllString(t *testing.T) {
for _, test := range reTests {
t.Run(test.re+" "+test.str, func(t *testing.T) {
regComp, err := Compile(test.re, test.flags...)
@@ -741,7 +767,7 @@ func TestFindAllStrings(t *testing.T) {
}
}
func TestFindAllGroups(t *testing.T) {
func TestFindAllSubmatch(t *testing.T) {
for _, test := range groupTests {
t.Run(test.re+" "+test.str, func(t *testing.T) {
regComp, err := Compile(test.re, test.flags...)