-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow type-safe calls to be used in InOrder (#78)
This modifies `InOrder` to receive variadic `any` instead of `*Call`, allowing users to use this function with type-safe generated Calls. This implementation uses a type assertion to check if the provided arguments are `*Call`s or reflection to get the `*Call` when the arguments wrap one (generated code). If neither of the two cases are fullfiled then `InOrder` panics. Fix #70. --------- Co-authored-by: Sung Yoon Whang <[email protected]>
- Loading branch information
1 parent
837f20a
commit dde4646
Showing
5 changed files
with
226 additions
and
1 deletion.
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
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,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 | ||
} |
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,27 @@ | ||
package typed_inorder | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"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") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.