Skip to content

Commit

Permalink
Merge pull request #1441 from stretchr/mock-deprecate-AnythingOfTypeA…
Browse files Browse the repository at this point in the history
…rgument

mock: deprecate AnythingOfTypeArgument (#1434)
  • Loading branch information
dolmen authored Aug 8, 2023
2 parents 0e90845 + f24f3ba commit 882382d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,18 +760,26 @@ const (
Anything = "mock.Anything"
)

// AnythingOfTypeArgument is a string that contains the type of an argument
// AnythingOfTypeArgument contains the type of an argument
// for use when type checking. Used in Diff and Assert.
type AnythingOfTypeArgument string
//
// Deprecated: this is an implementation detail that must not be used. Use [AnythingOfType] instead.
type AnythingOfTypeArgument = anythingOfTypeArgument

// anythingOfTypeArgument is a string that contains the type of an argument
// for use when type checking. Used in Diff and Assert.
type anythingOfTypeArgument string

// AnythingOfType returns an AnythingOfTypeArgument object containing the
// name of the type to check for. Used in Diff and Assert.
// AnythingOfType returns a special value containing the
// name of the type to check for. The type name will be matched against the type name returned by [reflect.Type.String].
//
// Used in Diff and Assert.
//
// For example:
//
// Assert(t, AnythingOfType("string"), AnythingOfType("int"))
func AnythingOfType(t string) AnythingOfTypeArgument {
return AnythingOfTypeArgument(t)
return anythingOfTypeArgument(t)
}

// IsTypeArgument is a struct that contains the type of an argument
Expand Down Expand Up @@ -952,9 +960,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
differences++
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 if reflect.TypeOf(expected) == reflect.TypeOf((*anythingOfTypeArgument)(nil)).Elem() {
// type checking
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
if reflect.TypeOf(actual).Name() != string(expected.(anythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(anythingOfTypeArgument)) {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
Expand Down

0 comments on commit 882382d

Please sign in to comment.