Skip to content

Commit

Permalink
Automatic conversion of return values
Browse files Browse the repository at this point in the history
  • Loading branch information
razzie committed Jan 20, 2025
1 parent 46026cd commit 813cf5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ func mockMethod(m *mock.Mock, method reflect.Method) any {
inValues = inValues[1:] // skip receiver
in := make([]any, len(inValues))
for i, val := range inValues {
if val.IsZero() {
switch {
case val.IsZero():
in[i] = nil
} else {
default:
in[i] = val.Interface()
}
}
Expand All @@ -73,9 +74,12 @@ func mockMethod(m *mock.Mock, method reflect.Method) any {
}
outValues = make([]reflect.Value, len(out))
for i, val := range out {
if val == nil {
switch {
case val == nil:
outValues[i] = reflect.Zero(outTypes[i])
} else {
case reflect.TypeOf(val) != outTypes[i]:
outValues[i] = reflect.ValueOf(val).Convert(outTypes[i])
default:
outValues[i] = reflect.ValueOf(val)
}
}
Expand Down
8 changes: 8 additions & 0 deletions mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestMock(t *testing.T) {
ManyArgs(a, b, c int16, d, e, f int32, g, h, i int64, j, k, l uintptr, x float32) (r1, r2 int, r3, r4 float32)
UncommonArgs(UncommonType, *UncommonType) (UncommonType, *UncommonType, reflect.Type)
StackArg([32]int)
ConvertReturnValue() (int16, <-chan int)
}
i, m := Mock[TestInterface]()

Expand Down Expand Up @@ -103,6 +104,13 @@ func TestMock(t *testing.T) {
m.AssertCalled(t, "StackArg", arg)
})

t.Run("convert return value", func(t *testing.T) {
ch := make(chan int)
m.On("ConvertReturnValue").Return(1, ch)
_, _ = i.ConvertReturnValue()
m.AssertCalled(t, "ConvertReturnValue")
})

t.Run("multiple instances", func(t *testing.T) {
type Fooer interface {
Foo() int
Expand Down

0 comments on commit 813cf5d

Please sign in to comment.