Skip to content

Commit 518a149

Browse files
dcormierboyan-soubachov
authored andcommitted
IsType: A type-safe way of checking argument type
Like `assert.IsType(...)`, `mock.IsType` is used to check that the type of an argument is the expected type. This is an alternative to `AnythingOfType`.
1 parent ea72eb9 commit 518a149

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

mock/mock.go

+23
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,23 @@ func AnythingOfType(t string) AnythingOfTypeArgument {
578578
return AnythingOfTypeArgument(t)
579579
}
580580

581+
// IsTypeArgument is a struct that contains the type of an argument
582+
// for use when type checking. This is an alternative to AnythingOfType.
583+
// Used in Diff and Assert.
584+
type IsTypeArgument struct {
585+
t interface{}
586+
}
587+
588+
// IsType returns an IsTypeArgument object containing the type to check for.
589+
// You can provide a zero-value of the type to check. This is an
590+
// alternative to AnythingOfType. Used in Diff and Assert.
591+
//
592+
// For example:
593+
// Assert(t, IsType(""), IsType(0))
594+
func IsType(t interface{}) *IsTypeArgument {
595+
return &IsTypeArgument{t: t}
596+
}
597+
581598
// argumentMatcher performs custom argument matching, returning whether or
582599
// not the argument is matched by the expectation fixture function.
583600
type argumentMatcher struct {
@@ -711,6 +728,12 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
711728
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
712729
}
713730

731+
} else if reflect.TypeOf(expected) == reflect.TypeOf((*IsTypeArgument)(nil)) {
732+
t := expected.(*IsTypeArgument).t
733+
if reflect.TypeOf(t) != reflect.TypeOf(actual) {
734+
differences++
735+
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
736+
}
714737
} else {
715738

716739
// normal checking

mock/mock_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,24 @@ func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) {
12571257

12581258
}
12591259

1260+
func Test_Arguments_Diff_WithIsTypeArgument(t *testing.T) {
1261+
var args = Arguments([]interface{}{"string", IsType(0), true})
1262+
var count int
1263+
_, count = args.Diff([]interface{}{"string", 123, true})
1264+
1265+
assert.Equal(t, 0, count)
1266+
}
1267+
1268+
func Test_Arguments_Diff_WithIsTypeArgument_Failing(t *testing.T) {
1269+
var args = Arguments([]interface{}{"string", IsType(""), true})
1270+
var count int
1271+
var diff string
1272+
diff, count = args.Diff([]interface{}{"string", 123, true})
1273+
1274+
assert.Equal(t, 1, count)
1275+
assert.Contains(t, diff, `string != type int - (int=123)`)
1276+
}
1277+
12601278
func Test_Arguments_Diff_WithArgMatcher(t *testing.T) {
12611279
matchFn := func(a int) bool {
12621280
return a == 123

0 commit comments

Comments
 (0)