Skip to content

Commit

Permalink
simpler implementation per review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gburt committed Mar 6, 2024
1 parent cc10451 commit da29738
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ type TestingT interface {
Call
*/

type returnArgumentsFunc func(args Arguments) Arguments

// Call represents a method call and is used for setting expectations,
// as well as recording activity.
type Call struct {
Expand All @@ -51,9 +49,9 @@ type Call struct {
// on each call of the mock to determine what to return.
ReturnArguments Arguments

// if the first arg in ReturnArguments is a returnArgumentsFunc, this
// if the first arg in ReturnArguments is a func(args Arguments) Arguments, this
// stores that ready for use
returnFunc returnArgumentsFunc
returnFunc func(args Arguments) Arguments

// Holds the caller info for the On() call
callerInfo []string
Expand Down Expand Up @@ -135,21 +133,15 @@ func (c *Call) Return(returnArguments ...interface{}) *Call {
c.lock()
defer c.unlock()

c.ReturnArguments = returnArguments

if len(c.ReturnArguments) == 1 {
fn := reflect.ValueOf(c.ReturnArguments[0])
if fn.Kind() == reflect.Func {
fnType := fn.Type()
if fnType.NumIn() == 1 && fnType.NumOut() == 1 && fnType.In(0) == argumentsType && fnType.Out(0) == argumentsType {
c.returnFunc = func(args Arguments) Arguments {
ret := fn.Call([]reflect.Value{reflect.ValueOf(args)})
return ret[0].Interface().(Arguments)
}
}
if len(returnArguments) == 1 {
if fn, ok := returnArguments[0].(func(Arguments) Arguments); ok {
c.returnFunc = fn
return c
}
}

c.returnFunc = nil
c.ReturnArguments = returnArguments
return c
}

Expand Down

0 comments on commit da29738

Please sign in to comment.