Skip to content

Commit b8b8d51

Browse files
assert: handle []byte array properly
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.
1 parent 352d243 commit b8b8d51

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

‎assert/assertions.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,15 +1615,21 @@ func ErrorContains(t TestingT, theError error, contains string, msgAndArgs ...in
16151615

16161616
// matchRegexp return true if a specified regexp matches a string.
16171617
func matchRegexp(rx interface{}, str interface{}) bool {
1618-
16191618
var r *regexp.Regexp
16201619
if rr, ok := rx.(*regexp.Regexp); ok {
16211620
r = rr
16221621
} else {
16231622
r = regexp.MustCompile(fmt.Sprint(rx))
16241623
}
16251624

1626-
return (r.FindStringIndex(fmt.Sprint(str)) != nil)
1625+
switch v := str.(type) {
1626+
case []byte:
1627+
return r.Match(v)
1628+
case string:
1629+
return r.MatchString(v)
1630+
default:
1631+
return r.MatchString(fmt.Sprint(v))
1632+
}
16271633

16281634
}
16291635

0 commit comments

Comments
 (0)