Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 12 additions & 8 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,19 @@ func (anyMatcher) String() string {
return "is anything"
}

type condMatcher struct {
fn func(x any) bool
type condMatcher[T any] struct {
fn func(x T) bool
}

func (c condMatcher) Matches(x any) bool {
return c.fn(x)
func (c condMatcher[T]) Matches(x any) bool {
typed, ok := x.(T)
if !ok {
return false
}
return c.fn(typed)
}

func (condMatcher) String() string {
func (c condMatcher[T]) String() string {
return "adheres to a custom condition"
}

Expand Down Expand Up @@ -339,9 +343,9 @@ func Any() Matcher { return anyMatcher{} }
//
// Example usage:
//
// Cond(func(x any){return x.(int) == 1}).Matches(1) // returns true
// Cond(func(x any){return x.(int) == 2}).Matches(1) // returns false
func Cond(fn func(x any) bool) Matcher { return condMatcher{fn} }
// Cond(func(x int){return x == 1}).Matches(1) // returns true
// Cond(func(x int){return x == 2}).Matches(1) // returns false
func Cond[T any](fn func(x T) bool) Matcher { return condMatcher[T]{fn} }

// AnyOf returns a composite Matcher that returns true if at least one of the
// matchers returns true.
Expand Down
4 changes: 3 additions & 1 deletion gomock/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func TestMatchers(t *testing.T) {
[]e{[]string{"a", "b"}, A{"a", "b"}},
[]e{[]string{"a"}, A{"b"}},
},
{"test Cond", gomock.Cond(func(x any) bool { return x.(B).Name == "Dam" }), []e{B{Name: "Dam"}}, []e{B{Name: "Dave"}}},
{"test Cond", gomock.Cond(func(x B) bool { return x.Name == "Dam" }), []e{B{Name: "Dam"}}, []e{B{Name: "Dave"}}},
{"test Cond wrong type", gomock.Cond(func(x B) bool { return x.Name == "Dam" }), []e{B{Name: "Dam"}}, []e{"Dave"}},
{"test Cond any type", gomock.Cond(func(x any) bool { return x.(B).Name == "Dam" }), []e{B{Name: "Dam"}}, []e{B{Name: "Dave"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
8 changes: 2 additions & 6 deletions sample/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,8 @@ func TestExpectCondForeignFour(t *testing.T) {
defer ctrl.Finish()

mockIndex := NewMockIndex(ctrl)
mockIndex.EXPECT().ForeignFour(gomock.Cond(func(x any) bool {
four, ok := x.(imp_four.Imp4)
if !ok {
return false
}
return four.Field == "Cool"
mockIndex.EXPECT().ForeignFour(gomock.Cond(func(x imp_four.Imp4) bool {
return x.Field == "Cool"
}))

mockIndex.ForeignFour(imp_four.Imp4{Field: "Cool"})
Expand Down