From 4bc085cc3be1c09aa2a248cf115acb3c13f061d5 Mon Sep 17 00:00:00 2001 From: Rusinov Artem Date: Sun, 13 Jul 2025 20:59:14 +0300 Subject: [PATCH] Added WithStack method to Call struct This method allow to see not only file:line where the mock was called from, but also some file:line entries down the stack --- gomock/call.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/gomock/call.go b/gomock/call.go index e1fe222a..4a707567 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -17,6 +17,7 @@ package gomock import ( "fmt" "reflect" + "runtime" "strconv" "strings" ) @@ -300,6 +301,22 @@ func (c *Call) After(preReq *Call) *Call { return c } +// WithStack add call stack to origin +func (c *Call) WithStack(lvl int) *Call { + if lvl < 1 { + return c + } + strb := &strings.Builder{} + for i := lvl; i > 0; i-- { + if _, file, line, ok := runtime.Caller(i); ok { + strb.WriteString(fmt.Sprintf("\n%s:%d", file, line)) + } + } + strb.WriteString("\n") + c.origin = strb.String() + return c +} + // Returns true if the minimum number of calls have been made. func (c *Call) satisfied() bool { return c.numCalls >= c.minCalls