Skip to content

Commit

Permalink
fix: compare functional option names for indirect calls
Browse files Browse the repository at this point in the history
Closes Functional Options testing broken for indirect calls stretchr#1380
  • Loading branch information
arjun-1 committed Oct 24, 2024
1 parent 7efaf15 commit 9c174eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,5 +1260,12 @@ func assertOpts(expected, actual interface{}) (expectedFmt, actualFmt string) {

func funcName(opt interface{}) string {
n := runtime.FuncForPC(reflect.ValueOf(opt).Pointer()).Name()
return strings.TrimSuffix(path.Base(n), path.Ext(n))
trimmed := strings.TrimSuffix(path.Base(n), path.Ext(n))
splitted := strings.Split(trimmed, ".")

if len(splitted) == 0 {
return trimmed
}

return splitted[len(splitted)-1]
}
35 changes: 35 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (i *TestExampleImplementation) TheExampleMethodFunctionalOptions(x string,
return args.Error(0)
}

func TheExampleMethodFunctionalOptionsIndirect(i *TestExampleImplementation) {
i.TheExampleMethodFunctionalOptions("test", OpNum(1), OpStr("foo"))
}

//go:noinline
func (i *TestExampleImplementation) TheExampleMethod2(yesorno bool) {
i.Called(yesorno)
Expand Down Expand Up @@ -1505,6 +1509,23 @@ func Test_Mock_AssertExpectationsFunctionalOptionsType(t *testing.T) {

}

func Test_Mock_AssertExpectationsFunctionalOptionsTypeIndirectly(t *testing.T) {

var mockedService = new(TestExampleImplementation)

mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions(OpNum(1), OpStr("foo"))).Return(nil).Once()

tt := new(testing.T)
assert.False(t, mockedService.AssertExpectations(tt))

// make the call now
TheExampleMethodFunctionalOptionsIndirect(mockedService)

// now assert expectations
assert.True(t, mockedService.AssertExpectations(tt))

}

func Test_Mock_AssertExpectationsFunctionalOptionsType_Empty(t *testing.T) {

var mockedService = new(TestExampleImplementation)
Expand All @@ -1522,6 +1543,20 @@ func Test_Mock_AssertExpectationsFunctionalOptionsType_Empty(t *testing.T) {

}

func Test_Mock_AssertExpectationsFunctionalOptionsType_Diff(t *testing.T) {

var mockedService = new(TestExampleImplementation)

mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions(OpNum(1))).Return(nil).Once()

tt := new(testing.T)
assert.False(t, mockedService.AssertExpectations(tt))

assert.Panics(t, func() {
mockedService.TheExampleMethodFunctionalOptions("test", OpStr("1"))
})
}

func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {

var mockedService = new(TestExampleImplementation)
Expand Down

0 comments on commit 9c174eb

Please sign in to comment.