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

Commit 2b473a1

Browse files
authored
Merge pull request #113 from bhcleek/call-site
mark gomock functions as test helpers
2 parents 73b8d2c + 0ee1ab2 commit 2b473a1

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

gomock/call.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func (c *Call) Do(f interface{}) *Call {
8080

8181
// Return declares the values to be returned by the mocked function call.
8282
func (c *Call) Return(rets ...interface{}) *Call {
83+
if h, ok := c.t.(testHelper); ok {
84+
h.Helper()
85+
}
86+
8387
mt := c.methodType
8488
if len(rets) != mt.NumOut() {
8589
c.t.Fatalf("wrong number of arguments to Return for %T.%v: got %d, want %d [%s]",
@@ -123,6 +127,10 @@ func (c *Call) Times(n int) *Call {
123127
// indirected through a pointer. Or, in the case of a slice, SetArg
124128
// will copy value's elements into the nth argument.
125129
func (c *Call) SetArg(n int, value interface{}) *Call {
130+
if h, ok := c.t.(testHelper); ok {
131+
h.Helper()
132+
}
133+
126134
if c.setArgs == nil {
127135
c.setArgs = make(map[int]reflect.Value)
128136
}
@@ -167,6 +175,10 @@ func (c *Call) isPreReq(other *Call) bool {
167175

168176
// After declares that the call may only match after preReq has been exhausted.
169177
func (c *Call) After(preReq *Call) *Call {
178+
if h, ok := c.t.(testHelper); ok {
179+
h.Helper()
180+
}
181+
170182
if c == preReq {
171183
c.t.Fatalf("A call isn't allowed to be its own prerequisite")
172184
}

gomock/call_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func (o *mockTestReporter) Fatalf(format string, args ...interface{}) {
1818
o.fatalCalls++
1919
}
2020

21+
func (o *mockTestReporter) Helper() {}
22+
2123
func TestCall_After(t *testing.T) {
2224
t.Run("SelfPrereqCallsFatalf", func(t *testing.T) {
2325
tr1 := &mockTestReporter{}
@@ -53,6 +55,7 @@ func TestCall_SetArg(t *testing.T) {
5355
t.Run("SetArgSlice", func(t *testing.T) {
5456
c := &Call{
5557
methodType: reflect.TypeOf(func([]byte) {}),
58+
t: &mockTestReporter{},
5659
}
5760
c.SetArg(0, []byte{1, 2, 3})
5861

@@ -67,6 +70,7 @@ func TestCall_SetArg(t *testing.T) {
6770
t.Run("SetArgPointer", func(t *testing.T) {
6871
c := &Call{
6972
methodType: reflect.TypeOf(func(*int) {}),
73+
t: &mockTestReporter{},
7074
}
7175
c.SetArg(0, 42)
7276

gomock/controller.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func NewController(t TestReporter) *Controller {
8686
}
8787

8888
func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call {
89+
if h, ok := ctrl.t.(testHelper); ok {
90+
h.Helper()
91+
}
92+
8993
recv := reflect.ValueOf(receiver)
9094
for i := 0; i < recv.Type().NumMethod(); i++ {
9195
if recv.Type().Method(i).Name == method {
@@ -123,6 +127,10 @@ func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method st
123127
}
124128

125129
func (ctrl *Controller) Call(receiver interface{}, method string, args ...interface{}) []interface{} {
130+
if h, ok := ctrl.t.(testHelper); ok {
131+
h.Helper()
132+
}
133+
126134
ctrl.mu.Lock()
127135
defer ctrl.mu.Unlock()
128136

@@ -159,6 +167,10 @@ func (ctrl *Controller) Call(receiver interface{}, method string, args ...interf
159167
}
160168

161169
func (ctrl *Controller) Finish() {
170+
if h, ok := ctrl.t.(testHelper); ok {
171+
h.Helper()
172+
}
173+
162174
ctrl.mu.Lock()
163175
defer ctrl.mu.Unlock()
164176

@@ -191,3 +203,8 @@ func callerInfo(skip int) string {
191203
}
192204
return "unknown file"
193205
}
206+
207+
type testHelper interface {
208+
TestReporter
209+
Helper()
210+
}

0 commit comments

Comments
 (0)