Merge pull request #1416 from stretchr/mock-simplify-matcher-check

mock: optimize argument matching checks
pull/1412/head^2
Sean Marciniak 2024-01-22 08:48:09 +10:30 committed by GitHub
commit 648a7937c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 41 deletions

View File

@ -786,7 +786,7 @@ func AnythingOfType(t string) AnythingOfTypeArgument {
// for use when type checking. This is an alternative to AnythingOfType. // for use when type checking. This is an alternative to AnythingOfType.
// Used in Diff and Assert. // Used in Diff and Assert.
type IsTypeArgument struct { type IsTypeArgument struct {
t interface{} t reflect.Type
} }
// IsType returns an IsTypeArgument object containing the type to check for. // IsType returns an IsTypeArgument object containing the type to check for.
@ -796,7 +796,7 @@ type IsTypeArgument struct {
// For example: // For example:
// Assert(t, IsType(""), IsType(0)) // Assert(t, IsType(""), IsType(0))
func IsType(t interface{}) *IsTypeArgument { 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 // FunctionalOptionsArgument is a struct that contains the type and value of an functional option argument
@ -960,21 +960,23 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
differences++ differences++
output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher) output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher)
} }
} else if reflect.TypeOf(expected) == reflect.TypeOf((*anythingOfTypeArgument)(nil)).Elem() { } else {
switch expected := expected.(type) {
case anythingOfTypeArgument:
// type checking // type checking
if reflect.TypeOf(actual).Name() != string(expected.(anythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(anythingOfTypeArgument)) { if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) {
// not match // not match
differences++ differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt) output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
} }
} else if reflect.TypeOf(expected) == reflect.TypeOf((*IsTypeArgument)(nil)) { case *IsTypeArgument:
t := expected.(*IsTypeArgument).t actualT := reflect.TypeOf(actual)
if reflect.TypeOf(t) != reflect.TypeOf(actual) { if actualT != expected.t {
differences++ 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, expected.t.Name(), actualT.Name(), actualFmt)
} }
} else if reflect.TypeOf(expected) == reflect.TypeOf((*FunctionalOptionsArgument)(nil)) { case *FunctionalOptionsArgument:
t := expected.(*FunctionalOptionsArgument).value t := expected.value
var name string var name string
tValue := reflect.ValueOf(t) tValue := reflect.ValueOf(t)
@ -996,9 +998,8 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef) output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef)
} }
} }
} else {
// normal checking
default:
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
// match // match
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt) output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
@ -1008,6 +1009,7 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt) output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
} }
} }
}
} }