Skip to content
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

Allow type-safe calls to be used in InOrder #78

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 32 additions & 1 deletion gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,43 @@ func (c *Call) call() []func([]any) []any {
}

// InOrder declares that the given calls should occur in order.
func InOrder(calls ...*Call) {
// It panics if the type of any of the arguments isn't *Call or a generated
// mock with an embedded *Call.
func InOrder(args ...any) {
var calls []*Call
for i := 0; i < len(args); i++ {
if call := getCall(args[i]); call != nil {
calls = append(calls, call)
continue
}
panic("InOrder parameters type must be either a *Call or a generated mock with an embedded *Call")
EstebanOlmedo marked this conversation as resolved.
Show resolved Hide resolved
}
for i := 1; i < len(calls); i++ {
calls[i].After(calls[i-1])
}
}

func getCall(arg any) *Call {
if call, ok := arg.(*Call); ok {
return call
}
t := reflect.ValueOf(arg)
if t.Kind() != reflect.Ptr && t.Kind() != reflect.Interface {
return nil
}
t = t.Elem()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if !f.CanInterface() {
continue
}
if call, ok := f.Interface().(*Call); ok {
return call
}
}
return nil
}

func setSlice(arg any, v reflect.Value) {
va := reflect.ValueOf(arg)
for i := 0; i < v.Len(); i++ {
Expand Down
33 changes: 33 additions & 0 deletions gomock/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type b struct {
foo string
}

type c struct {
*Call
}

func (testObj b) Foo() string {
return testObj.foo
}
Expand Down Expand Up @@ -616,3 +620,32 @@ func TestCall_DoAndReturn(t *testing.T) {
})
}
}

func TestInOrder(t *testing.T) {
t.Run("process only *Call or its wrappers", func(t *testing.T) {
tr1 := &mockTestReporter{}
tr2 := &mockTestReporter{}
c1 := &Call{t: tr1}
c2 := &c{Call: &Call{t: tr2}}
InOrder(c1, c2)
if len(c2.preReqs) != 1 {
t.Fatalf("expected 1 preReq in c2, found %d", len(c2.preReqs))
}
if len(c1.preReqs) != 0 {
t.Fatalf("expected 0 preReq in c1, found %d", len(c1.preReqs))
}
})
t.Run("panic when the argument isn't a *Call or has one embeded", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected InOrder to panic")
}
}()
tr := &mockTestReporter{}
c := &Call{t: tr}
a := &a{
name: "Foo",
}
InOrder(c, a)
})
}
14 changes: 14 additions & 0 deletions mockgen/internal/tests/typed_inorder/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package typed_inorder

//go:generate mockgen -package typed_inorder -source=input.go -destination=mock.go -typed
type Animal interface {
GetSound() string
Feed(string) error
}

func Interact(a Animal, food string) (string, error){
if err := a.Feed(food); err != nil {
return "", err
}
return a.GetSound(), nil
}
26 changes: 26 additions & 0 deletions mockgen/internal/tests/typed_inorder/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package typed_inorder

import (
"testing"
"fmt"
"go.uber.org/mock/gomock"
)

func TestInteract(t * testing.T) {
ctrl := gomock.NewController(t)

mockAnimal := NewMockAnimal(ctrl)
gomock.InOrder(
mockAnimal.EXPECT().Feed("burguir").DoAndReturn(func(s string) error {
if s != "chocolate" {
return nil
}
return fmt.Errorf("Dogs can't eat chocolate!")
}),
mockAnimal.EXPECT().GetSound().Return("Woof!"),
)
_, err := Interact(mockAnimal, "burguir")
if err != nil {
t.Fatalf("sad")
}
}
114 changes: 114 additions & 0 deletions mockgen/internal/tests/typed_inorder/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.