From f1df803a706ef8a5133970c1c25d429d5ae8c615 Mon Sep 17 00:00:00 2001 From: Tai Date: Thu, 3 Jan 2019 21:02:10 +0800 Subject: [PATCH] Preserve stack frame for mock parent method call Go tip contains following commmit, that inlines function with single call bodies. https://github.com/golang/go/commit/13baf4b2cd34dfb41c570e35b48ec287713f4d7f `(*Call).On()` is the exact target for the improvement in Go repo. Due to the inlining, assert.CallerInfo() can't not detect the file and line number of the call to `(*Mock).On()` from `(*Call).On()`. Thus, the test fails. Adding the compiler directive `go:noinline` prevent this effect and make mock package works with go tip as before. --- mock/mock.go | 1 + mock/mock_test.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/mock/mock.go b/mock/mock.go index 6e5f6f2db..d6694ed78 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -176,6 +176,7 @@ func (c *Call) Maybe() *Call { // Mock. // On("MyMethod", 1).Return(nil). // On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error")) +//go:noinline func (c *Call) On(methodName string, arguments ...interface{}) *Call { return c.Parent.On(methodName, arguments...) } diff --git a/mock/mock_test.go b/mock/mock_test.go index 978eae264..2608f5a36 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -32,6 +32,7 @@ func (i *TestExampleImplementation) TheExampleMethod(a, b, c int) (int, error) { return args.Int(0), errors.New("Whoops") } +//go:noinline func (i *TestExampleImplementation) TheExampleMethod2(yesorno bool) { i.Called(yesorno) } @@ -1492,6 +1493,7 @@ func unexpectedCallRegex(method, calledArg, expectedArg, diff string) string { rMethod, calledArg, rMethod, expectedArg, diff) } +//go:noinline func ConcurrencyTestMethod(m *Mock) { m.Called() }