Skip to content

Commit

Permalink
mock: refactor IsType
Browse files Browse the repository at this point in the history
Reduce calls to reflect.Type in implementation of mock.IsType by
extracting the type early.

This also avoids to keep alive references to the argument value.
  • Loading branch information
dolmen committed Jul 10, 2023
1 parent 3ebcb55 commit dd77307
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions mock/mock.go
Expand Up @@ -776,7 +776,7 @@ func AnythingOfType(t string) AnythingOfTypeArgument {
// for use when type checking. This is an alternative to AnythingOfType.
// Used in Diff and Assert.
type IsTypeArgument struct {
t interface{}
t reflect.Type
}

// IsType returns an IsTypeArgument object containing the type to check for.
Expand All @@ -786,7 +786,7 @@ type IsTypeArgument struct {
// For example:
// Assert(t, IsType(""), IsType(0))
func IsType(t interface{}) *IsTypeArgument {
return &IsTypeArgument{t: t}
return &IsTypeArgument{t: reflect.TypeOf(t)}
}

// FunctionalOptionsArgument is a struct that contains the type and value of an functional option argument
Expand Down Expand Up @@ -961,9 +961,10 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
}
case *IsTypeArgument:
t := expected.t
if reflect.TypeOf(t) != reflect.TypeOf(actual) {
actualT := reflect.TypeOf(actual)
if reflect.TypeOf(actual) != expected.t {
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, t.Name(), actualT.Name(), actualFmt)
}
case *FunctionalOptionsArgument:
t := expected.value
Expand Down

0 comments on commit dd77307

Please sign in to comment.