Skip to content

Commit

Permalink
cast mocks to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitrii Ovechkin committed Jul 14, 2023
1 parent 10b69d9 commit 05e7e16
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/ovechkin-dm/mockio
go 1.20

require (
github.com/ovechkin-dm/go-dyno v0.0.8
github.com/ovechkin-dm/go-dyno v0.0.11
github.com/timandy/routine v1.1.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/ovechkin-dm/go-dyno v0.0.8 h1:KA6iJfCswpVRnKtjUbIZr6cf51sdKhW2X0vsluKNkR8=
github.com/ovechkin-dm/go-dyno v0.0.8/go.mod h1:06OT6ztP0Y4rO1lXEn3oB2u/ErA5dxCCjkEpa54L5Jw=
github.com/ovechkin-dm/go-dyno v0.0.11 h1:inouInAWkQf6ySi8lrQ+AaAJdj8/3f4fhVDboVB1RZQ=
github.com/ovechkin-dm/go-dyno v0.0.11/go.mod h1:06OT6ztP0Y4rO1lXEn3oB2u/ErA5dxCCjkEpa54L5Jw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/timandy/routine v1.1.1 h1:6/Z7qLFZj3GrzuRksBFzIG8YGUh8CLhjnnMePBQTrEI=
Expand Down
7 changes: 7 additions & 0 deletions registry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package registry
import (
"fmt"
"github.com/ovechkin-dm/go-dyno/pkg/dyno"
"github.com/ovechkin-dm/go-dyno/proxy"
"github.com/ovechkin-dm/mockio/matchers"
"reflect"
"sync"
Expand Down Expand Up @@ -268,6 +269,12 @@ func (h *invocationHandler) validateReturnValues(result []any, method reflect.Me
if retActual == nil {
return false
}
switch v := result[i].(type) {
case *proxy.DynamicStruct:
retActual = v.IFaceValue.Type()
default:
}

if !retActual.AssignableTo(retExpected) {
return false
}
Expand Down
21 changes: 16 additions & 5 deletions registry/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package registry

import (
"github.com/ovechkin-dm/go-dyno/proxy"
"reflect"
)

Expand All @@ -15,19 +16,29 @@ func createDefaultReturnValues(m reflect.Method) []reflect.Value {
func valueSliceToInterfaceSlice(values []reflect.Value) []any {
result := make([]any, len(values))
for i := range values {
result[i] = values[i].Interface()
switch v := values[i].Interface().(type) {
case *proxy.DynamicStruct:
result[i] = v.IFaceValue
default:
result[i] = values[i].Interface()
}
}
return result
}

func interfaceSliceToValueSlice(values []any, m reflect.Method) []reflect.Value {
result := make([]reflect.Value, len(values))
for i := range values {
retV := reflect.New(m.Type.Out(i)).Elem()
if values[i] != nil {
retV.Set(reflect.ValueOf(values[i]))
switch v := values[i].(type) {
case *proxy.DynamicStruct:
result[i] = v.IFaceValue
default:
retV := reflect.New(m.Type.Out(i)).Elem()
if values[i] != nil {
retV.Set(reflect.ValueOf(values[i]))
}
result[i] = retV
}
result[i] = retV
}
return result
}
19 changes: 19 additions & 0 deletions tests/when/when_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ type WhenInterface interface {
Foo(a int) (int, string)
Bar(a int, b string, c string) (int, string)
Empty() int
RespondWithMock() Nested
}

type WhenInterface2 interface {
Bar(a int, b string, c string) (int, string)
}

type Nested interface {
Foo() int
}

type WhenStruct struct {
}

Expand Down Expand Up @@ -106,3 +111,17 @@ func TestWhenMultipleIfaces(t *testing.T) {
r.AssertEqual("test1", s2)
r.AssertNoError()
}

func TestWhenWithinWhen(t *testing.T) {
r := common.NewMockReporter(t)
SetUp(r)
m1 := Mock[WhenInterface]()
When(m1.RespondWithMock()).ThenAnswer(func(args []any) []any {
n := Mock[Nested]()
WhenSingle(n.Foo()).ThenReturn(10)
return []any{n}
})
nested := m1.RespondWithMock()
result := nested.Foo()
r.AssertEqual(10, result)
}

0 comments on commit 05e7e16

Please sign in to comment.