From 7561f77e0574412326f731f5d8087fb01e14a296 Mon Sep 17 00:00:00 2001 From: Gabriel Crispino Date: Sun, 22 Feb 2026 19:50:40 -0300 Subject: [PATCH 1/3] feat: add AnythingImplementing type --- mock/mock.go | 25 +++++++++++++++++++++++ mock/mock_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/mock/mock.go b/mock/mock.go index a13c37f3b..e7926c726 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -820,6 +820,25 @@ func AnythingOfType(t string) AnythingOfTypeArgument { return anythingOfTypeArgument(t) } +type anythingImplementing struct { + interfaceType reflect.Type +} + +func (a *anythingImplementing) isImplementedBy(val interface{}) bool { + t2 := reflect.TypeOf(val) + + return t2.Implements(a.interfaceType) +} + +func AnythingImplementing(val interface{}) anythingImplementing { + // Get the dynamic type + t := reflect.TypeOf(val) + fmt.Println("Type using reflect.TypeOf():", t) + interfaceType := t.Elem() + + return anythingImplementing{interfaceType: interfaceType} +} + // IsTypeArgument is a struct that contains the type of an argument // for use when type checking. This is an alternative to [AnythingOfType]. // Used in [Arguments.Diff] and [Arguments.Assert]. @@ -1013,6 +1032,12 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { differences++ output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt) } + case anythingImplementing: + expectedToImplement := expected.interfaceType + if !expected.isImplementedBy(actual) { + differences++ + output = fmt.Sprintf("%s\t%d: FAIL: value of type %T does not implement interface %s\n", output, i, actual, expectedToImplement) + } case *IsTypeArgument: actualT := reflect.TypeOf(actual) if actualT != expected.t { diff --git a/mock/mock_test.go b/mock/mock_test.go index 3dc9e0b1e..7ea0e6fcb 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -1772,6 +1772,18 @@ func Test_Mock_AssertCalled_WithAnythingOfTypeArgument(t *testing.T) { } +func Test_Mock_AssertCalled_WithAnythingImplementingArgument(t *testing.T) { + t.Parallel() + + var mockedService = new(TestExampleImplementation) + + mockedService. + On("TheExampleMethod4", AnythingImplementing((*ExampleInterface)(nil))). + Return(nil) + + mockedService.TheExampleMethod4(mockedService) +} + func Test_Mock_AssertCalled_WithArguments(t *testing.T) { t.Parallel() @@ -1978,6 +1990,33 @@ func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) { } +func Test_Arguments_Diff_WithAnythingImplementingArgument(t *testing.T) { + t.Parallel() + + var args = Arguments([]interface{}{AnythingImplementing((*ExampleInterface)(nil))}) + + var mockedService = new(TestExampleImplementation) + var count int + _, count = args.Diff([]interface{}{mockedService}) + + assert.Equal(t, 0, count) +} + +func Test_Arguments_Diff_WithAnythingImplementingArgument_Failing(t *testing.T) { + t.Parallel() + + var args = Arguments([]interface{}{ + AnythingImplementing((*ExampleInterface)(nil)), + }) + var count int + var diff string + diff, count = args.Diff([]interface{}{123}) + + assert.Equal(t, 1, count) + assert.Contains(t, diff, `value of type int does not implement interface mock.ExampleInterface`) + +} + func Test_Arguments_Diff_WithIsTypeArgument(t *testing.T) { t.Parallel() @@ -2467,3 +2506,15 @@ func TestIssue1227AssertExpectationsForObjectsWithMock(t *testing.T) { AssertExpectationsForObjects(mockT, Mock{}) assert.Equal(t, 1, mockT.errorfCount) } + +func TestTest(t *testing.T) { + + a := AnythingImplementing((*context.Context)(nil)) + backgroundCtx := context.Background() + cancelCtx, _ := context.WithCancel(backgroundCtx) + otherVal := "other" + + assert.True(t, a.isImplementedBy(backgroundCtx)) + assert.True(t, a.isImplementedBy(cancelCtx)) + assert.False(t, a.isImplementedBy(otherVal)) +} From 234397c9d6c757e84bc6b70634f223b3dde870ce Mon Sep 17 00:00:00 2001 From: Gabriel Crispino Date: Sun, 22 Feb 2026 21:31:48 -0300 Subject: [PATCH 2/3] fix: remove print statement and add comments --- mock/mock.go | 7 ++++++- mock/mock_test.go | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mock/mock.go b/mock/mock.go index e7926c726..1cbbc22a2 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -830,10 +830,15 @@ func (a *anythingImplementing) isImplementedBy(val interface{}) bool { return t2.Implements(a.interfaceType) } +// AnythingImplementing is just like AnythingOfType, but instead of checking against a concrete type, it is used to check if a value is of a type that implements a given interface +// +// For example, for checking if a value implements the context.Context interface: +// +// var args = Arguments([]interface{}{AnythingImplementing((*context.Context)(nil))}) +// args.Assert(t, AnythingImplementing(context.Background()) func AnythingImplementing(val interface{}) anythingImplementing { // Get the dynamic type t := reflect.TypeOf(val) - fmt.Println("Type using reflect.TypeOf():", t) interfaceType := t.Elem() return anythingImplementing{interfaceType: interfaceType} diff --git a/mock/mock_test.go b/mock/mock_test.go index 7ea0e6fcb..342f5ddd7 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -1999,6 +1999,7 @@ func Test_Arguments_Diff_WithAnythingImplementingArgument(t *testing.T) { var count int _, count = args.Diff([]interface{}{mockedService}) + assert.True(t, args.Assert(t, mockedService)) assert.Equal(t, 0, count) } @@ -2010,7 +2011,8 @@ func Test_Arguments_Diff_WithAnythingImplementingArgument_Failing(t *testing.T) }) var count int var diff string - diff, count = args.Diff([]interface{}{123}) + intVal := 123 + diff, count = args.Diff([]interface{}{intVal}) assert.Equal(t, 1, count) assert.Contains(t, diff, `value of type int does not implement interface mock.ExampleInterface`) From e4ba227401c42d07cca606c7dc3a3f85249a8fe3 Mon Sep 17 00:00:00 2001 From: Gabriel Crispino Date: Sun, 22 Feb 2026 22:05:18 -0300 Subject: [PATCH 3/3] test: remove temp test --- mock/mock_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/mock/mock_test.go b/mock/mock_test.go index 342f5ddd7..5441dca78 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -2508,15 +2508,3 @@ func TestIssue1227AssertExpectationsForObjectsWithMock(t *testing.T) { AssertExpectationsForObjects(mockT, Mock{}) assert.Equal(t, 1, mockT.errorfCount) } - -func TestTest(t *testing.T) { - - a := AnythingImplementing((*context.Context)(nil)) - backgroundCtx := context.Background() - cancelCtx, _ := context.WithCancel(backgroundCtx) - otherVal := "other" - - assert.True(t, a.isImplementedBy(backgroundCtx)) - assert.True(t, a.isImplementedBy(cancelCtx)) - assert.False(t, a.isImplementedBy(otherVal)) -}