Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 7f9c436

Browse files
author
Rob Percival
committed
Include stacktrace when call expectation is invalid
This helps with tracking down the offending expectation.
1 parent d508b4a commit 7f9c436

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

gomock/call.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package gomock
1717
import (
1818
"fmt"
1919
"reflect"
20+
"runtime/debug"
2021
"strconv"
2122
"strings"
2223
)
@@ -81,8 +82,8 @@ func (c *Call) Do(f interface{}) *Call {
8182
func (c *Call) Return(rets ...interface{}) *Call {
8283
mt := c.methodType
8384
if len(rets) != mt.NumOut() {
84-
c.t.Fatalf("wrong number of arguments to Return for %T.%v: got %d, want %d [%s]",
85-
c.receiver, c.method, len(rets), mt.NumOut(), c.origin)
85+
c.t.Fatalf("wrong number of arguments to Return for %T.%v: got %d, want %d\n%s",
86+
c.receiver, c.method, len(rets), mt.NumOut(), debug.Stack())
8687
}
8788
for i, ret := range rets {
8889
if got, want := reflect.TypeOf(ret), mt.Out(i); got == want {
@@ -93,8 +94,8 @@ func (c *Call) Return(rets ...interface{}) *Call {
9394
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
9495
// ok
9596
default:
96-
c.t.Fatalf("argument %d to Return for %T.%v is nil, but %v is not nillable [%s]",
97-
i, c.receiver, c.method, want, c.origin)
97+
c.t.Fatalf("argument %d to Return for %T.%v is nil, but %v is not nillable\n%s",
98+
i, c.receiver, c.method, want, debug.Stack())
9899
}
99100
} else if got.AssignableTo(want) {
100101
// Assignable type relation. Make the assignment now so that the generated code
@@ -103,8 +104,8 @@ func (c *Call) Return(rets ...interface{}) *Call {
103104
v.Set(reflect.ValueOf(ret))
104105
rets[i] = v.Interface()
105106
} else {
106-
c.t.Fatalf("wrong type of argument %d to Return for %T.%v: %v is not assignable to %v [%s]",
107-
i, c.receiver, c.method, got, want, c.origin)
107+
c.t.Fatalf("wrong type of argument %d to Return for %T.%v: %v is not assignable to %v\n%s",
108+
i, c.receiver, c.method, got, want, debug.Stack())
108109
}
109110
}
110111

@@ -128,8 +129,7 @@ func (c *Call) SetArg(n int, value interface{}) *Call {
128129
// TODO: This will break on variadic methods.
129130
// We will need to check those at invocation time.
130131
if n < 0 || n >= mt.NumIn() {
131-
c.t.Fatalf("SetArg(%d, ...) called for a method with %d args [%s]",
132-
n, mt.NumIn(), c.origin)
132+
c.t.Fatalf("SetArg(%d, ...) called for a method with %d args\n%s", n, mt.NumIn(), debug.Stack())
133133
}
134134
// Permit setting argument through an interface.
135135
// In the interface case, we don't (nay, can't) check the type here.
@@ -138,16 +138,14 @@ func (c *Call) SetArg(n int, value interface{}) *Call {
138138
case reflect.Ptr:
139139
dt := at.Elem()
140140
if vt := reflect.TypeOf(value); !vt.AssignableTo(dt) {
141-
c.t.Fatalf("SetArg(%d, ...) argument is a %v, not assignable to %v [%s]",
142-
n, vt, dt, c.origin)
141+
c.t.Fatalf("SetArg(%d, ...) argument is a %v, not assignable to %v\n%s", n, vt, dt, debug.Stack())
143142
}
144143
case reflect.Interface:
145144
// nothing to do
146145
case reflect.Slice:
147146
// nothing to do
148147
default:
149-
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v",
150-
n, at, c.origin)
148+
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface type %v\n%s", n, at, debug.Stack())
151149
}
152150
c.setArgs[n] = reflect.ValueOf(value)
153151
return c
@@ -166,7 +164,7 @@ func (c *Call) isPreReq(other *Call) bool {
166164
// After declares that the call may only match after preReq has been exhausted.
167165
func (c *Call) After(preReq *Call) *Call {
168166
if c == preReq {
169-
c.t.Fatalf("A call isn't allowed to be its own prerequisite")
167+
c.t.Fatalf("A call isn't allowed to be its own prerequisite\n%s", debug.Stack())
170168
}
171169
if preReq.isPreReq(c) {
172170
c.t.Fatalf("Loop in call order: %v is a prerequisite to %v (possibly indirectly).", c, preReq)

0 commit comments

Comments
 (0)