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

proposal: Add OneOf Matcher #91

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 32 additions & 0 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ func (m lenMatcher) String() string {
return fmt.Sprintf("has length %d", m.i)
}

type oneOfMatcher struct {
matches []interface{}
}

func oneOf(matches []interface{}) Matcher {
return oneOfMatcher{matches: matches}
}

func (m oneOfMatcher) Matches(x interface{}) bool {
for _, u := range m.matches {
if u == x {
return true
}
}
return false
}

func (m oneOfMatcher) String() string {
return fmt.Sprintf("is one of %+v", m.matches)
}

type inAnyOrderMatcher struct {
x any
}
Expand Down Expand Up @@ -366,3 +387,14 @@ func AssignableToTypeOf(x any) Matcher {
func InAnyOrder(x any) Matcher {
return inAnyOrderMatcher{x}
}

// OneOf is a Matcher that returns true when the parameter of the mock is
// part of the set provided to the matcher
//
// Example Usage:
//
// OneOf([]any{100,200,300})
// OneOf([]any{"Go", "Gopher"})
func OneOf(x []any) Matcher {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can use ... instead of slice?
func OneOf(x any...) Matcher

Copy link
Contributor

@favonia favonia Sep 16, 2023

Choose a reason for hiding this comment

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

@tulzke I personally agree, and I did that in #63. 😉

return oneOfMatcher{x}
}
49 changes: 49 additions & 0 deletions gomock/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestMatchers(t *testing.T) {
[]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 OneOf", gomock.OneOf([]any{1, 2, 3}), []e{1, 2, 3}, []e{4, 5, 6}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -145,6 +146,54 @@ func TestAssignableToTypeOfMatcher(t *testing.T) {
}
}

func TestOneOf(t *testing.T) {
tests := []struct {
name string
wanted []any
given any
wantMatch bool
}{
{
name: "match for one of many",
wanted: []any{1, 2, 3},
given: 1,
wantMatch: true,
},
{
name: "match for single",
wanted: []any{1},
given: 1,
wantMatch: true,
},
{
name: "match for mixed types",
wanted: []any{1, "5", 0x4},
given: 0x4,
wantMatch: true,
},
{
name: "not match for empty slice",
wanted: []any{},
given: 1,
wantMatch: false,
},
{
name: "not match for type mismatch",
wanted: []any{1, 2, 3},
given: "a",
wantMatch: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := gomock.OneOf(tt.wanted).Matches(tt.given); got != tt.wantMatch {
t.Errorf("got = %v, wantMatch %v", got, tt.wantMatch)
}
})
}
}

func TestInAnyOrder(t *testing.T) {
tests := []struct {
name string
Expand Down