Skip to content

feature: use "Do" return value. Overlap "Return". #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (c *Call) dropPrereqs() (preReqs []*Call) {
return
}

func (c *Call) call(args []interface{}) (rets []interface{}, action func()) {
func (c *Call) call(args []interface{}) (action func() []interface {}) {
c.numCalls++

// Actions
Expand All @@ -218,22 +218,31 @@ func (c *Call) call(args []interface{}) (rets []interface{}, action func()) {
doArgs[i] = reflect.Zero(ft.In(i))
}
}
action = func() { c.doFunc.Call(doArgs) }
action = func() []interface {} {
result := c.doFunc.Call(doArgs)
rets := make([]interface{}, len(result))
for i:=0; i<len(result); i++{
rets[i] = result[i].Interface()
}
return rets
}
} else {
action = func() []interface {} {
rets := c.rets
if rets == nil {
// Synthesize the zero value for each of the return args' types.
mt := c.methodType()
rets = make([]interface{}, mt.NumOut())
for i := 0; i < mt.NumOut(); i++ {
rets[i] = reflect.Zero(mt.Out(i)).Interface()
}
}
return rets
}
}
for n, v := range c.setArgs {
reflect.ValueOf(args[n]).Elem().Set(v)
}

rets = c.rets
if rets == nil {
// Synthesize the zero value for each of the return args' types.
mt := c.methodType()
rets = make([]interface{}, mt.NumOut())
for i := 0; i < mt.NumOut(); i++ {
rets[i] = reflect.Zero(mt.Out(i)).Interface()
}
}

return
}

Expand Down
7 changes: 2 additions & 5 deletions gomock/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (ctrl *Controller) Call(receiver interface{}, method string, args ...interf
ctrl.expectedCalls.Remove(preReqCall)
}

rets, action := expected.call(args)
action := expected.call(args)
if expected.exhausted() {
ctrl.expectedCalls.Remove(expected)
}
Expand All @@ -137,11 +137,8 @@ func (ctrl *Controller) Call(receiver interface{}, method string, args ...interf
// here we add a deferred Lock to balance it.
ctrl.mu.Unlock()
defer ctrl.mu.Lock()
if action != nil {
action()
}

return rets
return action()
}

func (ctrl *Controller) Finish() {
Expand Down