Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Jun 13, 2024
1 parent 84e41c6 commit 3824927
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 33 deletions.
17 changes: 3 additions & 14 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,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} }

// CondTyped returns a matcher that matches when the given function returns true
// after passing it the parameter to the mock function.
// In contrast to Cond the given function doesn't take an any value as parameter but can use a specific type.
// This is particularly useful in case you want to match over a field of a custom struct, or dynamic logic.
//
// Example usage:
//
// CondTyped(func(x int){return x == 1}).Matches(1) // returns true
// CondTyped(func(x int){return x == 2}).Matches(1) // returns false
func CondTyped[T any](fn func(x T) bool) Matcher { return condTypedMatcher[T]{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 condTypedMatcher[T]{fn} }

// AnyOf returns a composite Matcher that returns true if at least one of the
// matchers returns true.
Expand Down
4 changes: 2 additions & 2 deletions gomock/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ 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 CondTyped", gomock.CondTyped(func(x B) bool { return x.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 any", 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
18 changes: 1 addition & 17 deletions sample/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,7 @@ 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.ForeignFour(imp_four.Imp4{Field: "Cool"})
}

func TestExpectCondTypedForeignFour(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockIndex := NewMockIndex(ctrl)
mockIndex.EXPECT().ForeignFour(gomock.CondTyped(func(x imp_four.Imp4) bool {
mockIndex.EXPECT().ForeignFour(gomock.Cond(func(x imp_four.Imp4) bool {
return x.Field == "Cool"
}))

Expand Down

0 comments on commit 3824927

Please sign in to comment.