diff --git a/mock/mock.go b/mock/mock.go index c95eeeca8..efc89deff 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -12,9 +12,10 @@ import ( "time" "github.com/davecgh/go-spew/spew" + "github.com/pmezard/go-difflib/difflib" "github.com/stretchr/objx" + "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/internal/difflib" ) // regex for GCCGO functions @@ -603,12 +604,11 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool { h.Helper() } for _, obj := range testObjects { - m, ok := obj.(assertExpectationiser) - if !ok { - t.Errorf("Invalid test object type %T. Expected reference to a mock.Mock, eg: 'AssertExpectationsForObjects(t, myMock)' or 'AssertExpectationsForObjects(t, &myMock.Mock)'", obj) - continue - + if m, ok := obj.(*Mock); ok { + t.Logf("Deprecated mock.AssertExpectationsForObjects(myMock.Mock) use mock.AssertExpectationsForObjects(myMock)") + obj = m } + m := obj.(assertExpectationiser) if !m.AssertExpectations(t) { t.Logf("Expectations didn't match for Mock: %+v", reflect.TypeOf(m)) return false @@ -712,8 +712,8 @@ func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...inter return true } -// IsMethodCallable returns true if given methodName and arguments have an -// unsatisfied expected call registered in the Mock. +// IsMethodCallable checking that the method can be called +// If the method was called more than `Repeatability` return false func (m *Mock) IsMethodCallable(t TestingT, methodName string, arguments ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -833,10 +833,6 @@ type IsTypeArgument struct { // For example: // // args.Assert(t, IsType(""), IsType(0)) -// -// Mock cannot match interface types because the contained type will be passed -// to both IsType and Mock.Called, for the zero value of all interfaces this -// will be type. func IsType(t interface{}) *IsTypeArgument { return &IsTypeArgument{t: reflect.TypeOf(t)} } @@ -1016,7 +1012,7 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { actualT := reflect.TypeOf(actual) if actualT != expected.t { differences++ - output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, safeTypeName(expected.t), safeTypeName(actualT), actualFmt) + output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt) } case *FunctionalOptionsArgument: var name string @@ -1145,15 +1141,6 @@ func (args Arguments) Bool(index int) bool { return s } -// safeTypeName returns the reflect.Type's name without causing a panic. -// If the provided reflect.Type is nil, it returns the placeholder string "" -func safeTypeName(t reflect.Type) string { - if t == nil { - return "" - } - return t.Name() -} - func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) { t := reflect.TypeOf(v) k := t.Kind() diff --git a/mock/mock_test.go b/mock/mock_test.go index 3dc9e0b1e..eafc9fb1a 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -1,7 +1,6 @@ package mock import ( - "context" "errors" "fmt" "regexp" @@ -2000,25 +1999,6 @@ func Test_Arguments_Diff_WithIsTypeArgument_Failing(t *testing.T) { assert.Contains(t, diff, `string != type int - (int=123)`) } -func Test_Arguments_Diff_WithIsTypeArgument_InterfaceType(t *testing.T) { - t.Parallel() - var ctx = context.Background() - args := Arguments([]interface{}{IsType(ctx)}) - _, count := args.Diff([]interface{}{context.Background()}) - assert.Equal(t, 0, count) -} - -func Test_Arguments_Diff_WithIsTypeArgument_InterfaceType_Failing(t *testing.T) { - t.Parallel() - - var ctx context.Context - var args = Arguments([]interface{}{IsType(ctx)}) - diff, count := args.Diff([]interface{}{context.Background()}) - assert.Equal(t, 1, count) - assert.Contains(t, diff, `type != type `) - -} - func Test_Arguments_Diff_WithArgMatcher(t *testing.T) { t.Parallel() @@ -2461,9 +2441,3 @@ func TestIssue1785ArgumentWithMutatingStringer(t *testing.T) { m.MethodCalled("Method", &mutatingStringer{N: 2}) m.AssertExpectations(t) } - -func TestIssue1227AssertExpectationsForObjectsWithMock(t *testing.T) { - mockT := &MockTestingT{} - AssertExpectationsForObjects(mockT, Mock{}) - assert.Equal(t, 1, mockT.errorfCount) -}