Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ func (c *Call) Unset() *Call {
// in-place filter slice for calls to be removed - iterate from 0'th to last skipping unnecessary ones
var index int // write index
for _, call := range c.Parent.ExpectedCalls {
if call == c {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With two identical expectations (On("F", 1).Once() then On("F", 1)), unsetting the second still removes both, because the argument match below also runs, same as on master. Now that the pointer check is here, is the argument match still needed?

foundMatchingCall = true
// Remove from ExpectedCalls - just skip it
continue
}
if call.Method == c.Method {
_, diffCount := call.Arguments.Diff(c.Arguments)
if diffCount == 0 {
Expand Down Expand Up @@ -1031,16 +1036,36 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
switch expected := expected.(type) {
case anythingOfTypeArgument:
// type checking
if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
switch actual := actual.(type) {
case anythingOfTypeArgument:
if string(actual) != string(expected) {
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, actual, actualFmt)
}
default:
if actual == nil || (reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected)) {
// not match
differences++
actualName := "nil"
if actual != nil {
actualName = reflect.TypeOf(actual).Name()
}
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, actualName, actualFmt)
}
}
case *IsTypeArgument:
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)
switch actual := actual.(type) {
case *IsTypeArgument:
if actual.t != expected.t {
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, safeTypeName(expected.t), safeTypeName(actual.t), actualFmt)
}
default:
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)
}
}
case *FunctionalOptionsArgument:
var name string
Expand Down
48 changes: 48 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,54 @@ func Test_Mock_UnsetByOnMethodSpecAmongOthers(t *testing.T) {
assert.Equal(t, expectedCalls, mockedService.ExpectedCalls)
}

func Test_Mock_Unset_WithAnythingOfType(t *testing.T) {
t.Parallel()

var mockedService = new(TestExampleImplementation)
call := mockedService.On("TheExampleMethod3", AnythingOfType("int"), AnythingOfType("int"), AnythingOfType("int")).Return(nil)

assert.NotPanics(t, func() {
call.Unset()
})
assert.Equal(t, 0, len(mockedService.ExpectedCalls))
}

func Test_Mock_Unset_WithIsType(t *testing.T) {
t.Parallel()

var mockedService = new(TestExampleImplementation)
call := mockedService.On("TheExampleMethod3", IsType(0), IsType(0), IsType(0)).Return(nil)

assert.NotPanics(t, func() {
call.Unset()
})
assert.Equal(t, 0, len(mockedService.ExpectedCalls))
}

func Test_Mock_Unset_WithMatchedBy(t *testing.T) {
t.Parallel()

var mockedService = new(TestExampleImplementation)
call := mockedService.On("TheExampleMethod3", MatchedBy(func(x int) bool { return true }), 0, 0).Return(nil)

assert.NotPanics(t, func() {
call.Unset()
})
assert.Equal(t, 0, len(mockedService.ExpectedCalls))
}

func Test_Mock_UnsetByOnMethodSpec_WithAnythingOfType(t *testing.T) {
t.Parallel()

var mockedService = new(TestExampleImplementation)
mockedService.On("TheExampleMethod3", AnythingOfType("int"), AnythingOfType("int"), AnythingOfType("int")).Return(nil)

assert.Equal(t, 1, len(mockedService.ExpectedCalls))
mockedService.On("TheExampleMethod3", AnythingOfType("int"), AnythingOfType("int"), AnythingOfType("int")).Return(nil).Unset()

assert.Equal(t, 0, len(mockedService.ExpectedCalls))
}

func Test_Mock_Unset_WithFuncPanics(t *testing.T) {
t.Parallel()

Expand Down