Skip to content

Commit 5021857

Browse files
authored
Make Cond Matcher generic (#183)
`gomock.Cond` at the moment expects an matcher function with the signature `func(x any) bool` requiring a manual cast to the expected type. By making this matcher generic the callback function can be typed removing the need for a manual cast to the expected type. The change should be backwards compatible with existing tests using `gomock.Cond`.
1 parent b59039c commit 5021857

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

gomock/matchers.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,19 @@ func (anyMatcher) String() string {
9898
return "is anything"
9999
}
100100

101-
type condMatcher struct {
102-
fn func(x any) bool
101+
type condMatcher[T any] struct {
102+
fn func(x T) bool
103103
}
104104

105-
func (c condMatcher) Matches(x any) bool {
106-
return c.fn(x)
105+
func (c condMatcher[T]) Matches(x any) bool {
106+
typed, ok := x.(T)
107+
if !ok {
108+
return false
109+
}
110+
return c.fn(typed)
107111
}
108112

109-
func (condMatcher) String() string {
113+
func (c condMatcher[T]) String() string {
110114
return "adheres to a custom condition"
111115
}
112116

@@ -339,9 +343,9 @@ func Any() Matcher { return anyMatcher{} }
339343
//
340344
// Example usage:
341345
//
342-
// Cond(func(x any){return x.(int) == 1}).Matches(1) // returns true
343-
// Cond(func(x any){return x.(int) == 2}).Matches(1) // returns false
344-
func Cond(fn func(x any) bool) Matcher { return condMatcher{fn} }
346+
// Cond(func(x int){return x == 1}).Matches(1) // returns true
347+
// Cond(func(x int){return x == 2}).Matches(1) // returns false
348+
func Cond[T any](fn func(x T) bool) Matcher { return condMatcher[T]{fn} }
345349

346350
// AnyOf returns a composite Matcher that returns true if at least one of the
347351
// matchers returns true.

gomock/matchers_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func TestMatchers(t *testing.T) {
5757
[]e{[]string{"a", "b"}, A{"a", "b"}},
5858
[]e{[]string{"a"}, A{"b"}},
5959
},
60-
{"test Cond", gomock.Cond(func(x any) bool { return x.(B).Name == "Dam" }), []e{B{Name: "Dam"}}, []e{B{Name: "Dave"}}},
60+
{"test Cond", gomock.Cond(func(x B) bool { return x.Name == "Dam" }), []e{B{Name: "Dam"}}, []e{B{Name: "Dave"}}},
61+
{"test Cond wrong type", gomock.Cond(func(x B) bool { return x.Name == "Dam" }), []e{B{Name: "Dam"}}, []e{"Dave"}},
62+
{"test Cond any type", gomock.Cond(func(x any) bool { return x.(B).Name == "Dam" }), []e{B{Name: "Dam"}}, []e{B{Name: "Dave"}}},
6163
}
6264
for _, tt := range tests {
6365
t.Run(tt.name, func(t *testing.T) {

sample/user_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,8 @@ func TestExpectCondForeignFour(t *testing.T) {
193193
defer ctrl.Finish()
194194

195195
mockIndex := NewMockIndex(ctrl)
196-
mockIndex.EXPECT().ForeignFour(gomock.Cond(func(x any) bool {
197-
four, ok := x.(imp_four.Imp4)
198-
if !ok {
199-
return false
200-
}
201-
return four.Field == "Cool"
196+
mockIndex.EXPECT().ForeignFour(gomock.Cond(func(x imp_four.Imp4) bool {
197+
return x.Field == "Cool"
202198
}))
203199

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

0 commit comments

Comments
 (0)