From 4fff94abe1663a1aa4c5765520438f3caacc67a3 Mon Sep 17 00:00:00 2001 From: techfg Date: Tue, 9 Jul 2024 17:47:58 -0700 Subject: [PATCH] test: add tests for unset call matching #1621 --- mock/mock_test.go | 184 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) diff --git a/mock/mock_test.go b/mock/mock_test.go index b80a8a75b..b3cc83eaf 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -573,6 +573,190 @@ func Test_Mock_Chained_UnsetOnlyUnsetsLastCall(t *testing.T) { assert.Equal(t, expectedCalls, mockedService.ExpectedCalls) } +func Test_Mock_UnsetOnlyUnsetsCurrentCall(t *testing.T) { + var mockedService = new(TestExampleImplementation) + + // determine our current line number so we can assert the expected calls callerInfo properly + _, filename, line, _ := runtime.Caller(0) + mockedService. + On("TheExampleMethod1", Anything, Anything).Return(0). + On("TheExampleMethod1", 2, 2).Return(0) + + callToUnset := mockedService.On("TheExampleMethod1", 3, 3).Return(0) + callToUnset.Unset() + + expectedCalls := []*Call{ + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{Anything, Anything}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{2, 2}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+3)}, + }, + } + assert.Equal(t, 2, len(expectedCalls)) + assert.Equal(t, expectedCalls, mockedService.ExpectedCalls) +} + +func Test_Mock_UnsetOnlyUnsetsExactArgsMatchCall(t *testing.T) { + var mockedService = new(TestExampleImplementation) + + // determine our current line number so we can assert the expected calls callerInfo properly + _, filename, line, _ := runtime.Caller(0) + mockedService. + On("TheExampleMethod1", Anything, Anything).Return(0). + On("TheExampleMethod1", 2, 2).Return(0). + On("TheExampleMethod1", 3, 3).Return(0) + + mockedService.On("TheExampleMethod1", 3, 3).Unset() + + expectedCalls := []*Call{ + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{Anything, Anything}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{2, 2}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+3)}, + }, + } + assert.Equal(t, 2, len(expectedCalls)) + assert.Equal(t, expectedCalls, mockedService.ExpectedCalls) +} + +func Test_Mock_UnsetOnlyUnsetsExactArgsMatchCallWhenPartialMatch(t *testing.T) { + var mockedService = new(TestExampleImplementation) + + // determine our current line number so we can assert the expected calls callerInfo properly + _, filename, line, _ := runtime.Caller(0) + mockedService. + On("TheExampleMethod1", 2, 2).Return(0). + On("TheExampleMethod1", 3, 3).Return(0). + On("TheExampleMethod1", 3, Anything).Return(0). + On("TheExampleMethod1", Anything, Anything).Return(0) + + mockedService.On("TheExampleMethod1", 3, 3).Unset() + + expectedCalls := []*Call{ + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{2, 2}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{3, Anything}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+4)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{Anything, Anything}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+5)}, + }, + } + assert.Equal(t, 3, len(expectedCalls)) + assert.Equal(t, expectedCalls, mockedService.ExpectedCalls) +} + +func Test_Mock_UnsetOnlyUnsetsExactArgsMatchCallWhenAnythingSomeArg(t *testing.T) { + var mockedService = new(TestExampleImplementation) + + // determine our current line number so we can assert the expected calls callerInfo properly + _, filename, line, _ := runtime.Caller(0) + mockedService. + On("TheExampleMethod1", 2, 2).Return(0). + On("TheExampleMethod1", 3, 3).Return(0). + On("TheExampleMethod1", 3, Anything).Return(0). + On("TheExampleMethod1", Anything, Anything).Return(0) + + mockedService.On("TheExampleMethod1", 3, Anything).Unset() + + expectedCalls := []*Call{ + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{2, 2}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{3, 3}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+3)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{Anything, Anything}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+5)}, + }, + } + assert.Equal(t, 3, len(expectedCalls)) + assert.Equal(t, expectedCalls, mockedService.ExpectedCalls) +} + +func Test_Mock_UnsetOnlyUnsetsExactArgsMatchCallWhenAnythingEveryArg(t *testing.T) { + var mockedService = new(TestExampleImplementation) + + // determine our current line number so we can assert the expected calls callerInfo properly + _, filename, line, _ := runtime.Caller(0) + mockedService. + On("TheExampleMethod1", 2, 2).Return(0). + On("TheExampleMethod1", 3, 3).Return(0). + On("TheExampleMethod1", 3, Anything).Return(0). + On("TheExampleMethod1", Anything, Anything).Return(0) + + mockedService.On("TheExampleMethod1", Anything, Anything).Unset() + + expectedCalls := []*Call{ + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{2, 2}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+2)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{3, 3}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+3)}, + }, + { + Parent: &mockedService.Mock, + Method: "TheExampleMethod1", + Arguments: []interface{}{3, Anything}, + ReturnArguments: []interface{}{0}, + callerInfo: []string{fmt.Sprintf("%s:%d", filename, line+4)}, + }, + } + assert.Equal(t, 3, len(expectedCalls)) + assert.Equal(t, expectedCalls, mockedService.ExpectedCalls) +} + func Test_Mock_UnsetIfAlreadyUnsetFails(t *testing.T) { // make a test impl object var mockedService = new(TestExampleImplementation)