Skip to content

Commit 9204cde

Browse files
committed
mock: refactor IsType
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.
1 parent 3ebcb55 commit 9204cde

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

mock/mock.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ func AnythingOfType(t string) AnythingOfTypeArgument {
776776
// for use when type checking. This is an alternative to AnythingOfType.
777777
// Used in Diff and Assert.
778778
type IsTypeArgument struct {
779-
t interface{}
779+
t reflect.Type
780780
}
781781

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

792792
// FunctionalOptionsArgument is a struct that contains the type and value of an functional option argument
@@ -960,10 +960,10 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
960960
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
961961
}
962962
case *IsTypeArgument:
963-
t := expected.t
964-
if reflect.TypeOf(t) != reflect.TypeOf(actual) {
963+
actualT := reflect.TypeOf(actual)
964+
if actualT != expected.t {
965965
differences++
966-
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
966+
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt)
967967
}
968968
case *FunctionalOptionsArgument:
969969
t := expected.value

0 commit comments

Comments
 (0)