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,53 +960,55 @@ 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 {
// type checking switch expected := expected.(type) {
if reflect.TypeOf(actual).Name() != string(expected.(anythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(anythingOfTypeArgument)) { case anythingOfTypeArgument:
// not match // type checking
differences++ if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) {
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt) // not match
} differences++
} else if reflect.TypeOf(expected) == reflect.TypeOf((*IsTypeArgument)(nil)) { output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
t := expected.(*IsTypeArgument).t }
if reflect.TypeOf(t) != reflect.TypeOf(actual) { case *IsTypeArgument:
differences++ actualT := reflect.TypeOf(actual)
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt) if actualT != expected.t {
} differences++
} else if reflect.TypeOf(expected) == reflect.TypeOf((*FunctionalOptionsArgument)(nil)) { output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt)
t := expected.(*FunctionalOptionsArgument).value }
case *FunctionalOptionsArgument:
t := expected.value
var name string var name string
tValue := reflect.ValueOf(t) tValue := reflect.ValueOf(t)
if tValue.Len() > 0 { if tValue.Len() > 0 {
name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String() name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String()
} }
tName := reflect.TypeOf(t).Name() tName := reflect.TypeOf(t).Name()
if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 { if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 {
differences++ differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt) output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt)
} else { } else {
if ef, af := assertOpts(t, actual); ef == "" && af == "" { if ef, af := assertOpts(t, actual); ef == "" && af == "" {
// match
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, tName, tName)
} else {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef)
}
}
default:
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, tName, tName) output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
} else { } else {
// not match // not match
differences++ differences++
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, actualFmt, expectedFmt)
} }
} }
} else {
// normal checking
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
// match
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
} else {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
}
} }
} }