Skip to content

Commit

Permalink
assert: handle []byte array properly
Browse files Browse the repository at this point in the history
The regexp package works more efficiently on bytes; if you have bytes,
it is easier to pass these directly to assert.Regexp than to convert
them to a string first.

In addition, FindIndex/FindStringIndex are unnecessary here because we
immediately throw away the result - let's just call Match() instead.
  • Loading branch information
kevinburkesegment committed Apr 11, 2024
1 parent 352d243 commit b8b8d51
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions assert/assertions.go
Expand Up @@ -1615,15 +1615,21 @@ func ErrorContains(t TestingT, theError error, contains string, msgAndArgs ...in

// matchRegexp return true if a specified regexp matches a string.
func matchRegexp(rx interface{}, str interface{}) bool {

var r *regexp.Regexp
if rr, ok := rx.(*regexp.Regexp); ok {
r = rr
} else {
r = regexp.MustCompile(fmt.Sprint(rx))
}

return (r.FindStringIndex(fmt.Sprint(str)) != nil)
switch v := str.(type) {
case []byte:
return r.Match(v)
case string:
return r.MatchString(v)
default:
return r.MatchString(fmt.Sprint(v))
}

}

Expand Down

0 comments on commit b8b8d51

Please sign in to comment.