-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently when type-safe return values are used, one can't use `InOrder` neither `After` with them as they expect `*gomock.Call` as parameters. This changes the type these functions expect to be an interface which implements the `GetCall() *Call` method, which returns the embedded `*gomock.Call` type for generated mock types. Using this approach helps to have compile-time error/warnings instead of runtime errors with a reflection based solution. Fixes (#70)
- Loading branch information
1 parent
837f20a
commit 9b18c60
Showing
6 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# `gomock.InOrder` and `*gomock.Call.After()` with typed generated code | ||
|
||
From #70, this tests that `InOrder` and `*gomock.Call.After` work with code | ||
generated usign the `typed` flag. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package inorder | ||
|
||
|
||
//go:generate mockgen -package inorder -source=inorder.go -destination=mock.go -typed | ||
type Animal interface { | ||
GetNoise() string | ||
Feed(string) error | ||
} | ||
|
||
func Interact(a Animal, food string) (string, error) { | ||
if err := a.Feed(food); err != nil { | ||
return "", err | ||
} | ||
return a.GetNoise(), nil | ||
} |
45 changes: 45 additions & 0 deletions
45
mockgen/internal/tests/typed_after_in_order/inorder_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package inorder | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestInteract_InOrder(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().GetNoise().Return("Woof!"), | ||
) | ||
_, err := Interact(mockAnimal, "burguir") | ||
if err != nil { | ||
t.Fatalf("sad") | ||
} | ||
} | ||
|
||
func TestInteract_After(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
mockAnimal := NewMockAnimal(ctrl) | ||
mockAnimal.EXPECT().GetNoise().Return("Woof!").After( | ||
mockAnimal.EXPECT().Feed("burguir").DoAndReturn(func(s string) error { | ||
if s != "chocolate" { | ||
return nil | ||
} | ||
return fmt.Errorf("Dogs can't eat chocolate!") | ||
}), | ||
) | ||
_, err := Interact(mockAnimal, "burguir") | ||
if err != nil { | ||
t.Fatalf("sad") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters