Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Cond matcher #60

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ func (anyMatcher) String() string {
return "is anything"
}

type fnMatcher struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on making this type just a redefinition of the func type since it doesn't really need to be a struct?

type fnMatcher fn(any) bool

func (f fnMatcher) Matches(x any) bool {
    return f(x)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am cool with both as I don't see this as a red flag.

I based this implementation on the fact that most of the currently implemented Matcher are structs with one or no fields at all, reducing the implementation difference as much as possible.

fn func(x any) bool
}

func (f fnMatcher) Matches(x any) bool {
return f.fn(x)
}

func (fnMatcher) String() string {
return "a customizable behaviour"
damianopetrungaro marked this conversation as resolved.
Show resolved Hide resolved
}

type eqMatcher struct {
x any
}
Expand Down Expand Up @@ -280,6 +292,15 @@ func All(ms ...Matcher) Matcher { return allMatcher{ms} }
// Any returns a matcher that always matches.
func Any() Matcher { return anyMatcher{} }

// Fn returns a specialized matchers customizable for complex matching behaviour.
// This is particularly useful in case you want to match over a field of a custom struct, or dynamic logic.
//
// Example usage:
//
// Fn(func(x any){return x.(int) == 1}).Matches(1) // returns true
// Fn(func(x any){return x.(int) == 2}).Matches(1) // returns false
func Fn(fn func(x any) bool) Matcher { return fnMatcher{fn} }

// Eq returns a matcher that matches on equality.
//
// Example usage:
Expand Down
4 changes: 4 additions & 0 deletions gomock/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
)

type A []string
type B struct {
Name string
}

func TestMatchers(t *testing.T) {
type e any
Expand All @@ -50,6 +53,7 @@ func TestMatchers(t *testing.T) {
[]e{[]string{"a", "b"}, A{"a", "b"}},
[]e{[]string{"a"}, A{"b"}},
},
{"test Fn", gomock.Fn(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
4 changes: 3 additions & 1 deletion sample/imp4/imp4.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package imp_four

type Imp4 struct{}
type Imp4 struct {
Field string
}
17 changes: 17 additions & 0 deletions sample/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go.uber.org/mock/gomock"
user "go.uber.org/mock/sample"
"go.uber.org/mock/sample/imp1"
imp_four "go.uber.org/mock/sample/imp4"
)

func TestRemember(t *testing.T) {
Expand Down Expand Up @@ -193,3 +194,19 @@ func TestDoAndReturnSignature(t *testing.T) {
mockIndex.Slice([]int{0}, []byte("meow"))
})
}

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

mockIndex := NewMockIndex(ctrl)
mockIndex.EXPECT().ForeignFour(gomock.Fn(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"})
}