Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Allow Any to take a list of matchers #673

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 28 additions & 8 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,33 @@ func GotFormatterAdapter(s GotFormatter, m Matcher) Matcher {
}
}

type anyMatcher struct{}
type anyMatcher struct {
matchers []Matcher
}

func (anyMatcher) Matches(interface{}) bool {
return true
func (am anyMatcher) Matches(x interface{}) bool {
if len(am.matchers) == 0 {
return true
}

for _, m := range am.matchers {
if m.Matches(x) {
return true
}
}
return false
}

func (anyMatcher) String() string {
return "is anything"
func (am anyMatcher) String() string {
if len(am.matchers) == 0 {
return "is anything"
}

ss := make([]string, 0, len(am.matchers))
for _, matcher := range am.matchers {
ss = append(ss, matcher.String())
}
return strings.Join(ss, " OR ")
}

type eqMatcher struct {
Expand Down Expand Up @@ -273,12 +292,13 @@ func (m inAnyOrderMatcher) String() string {

// Constructors

// All returns a composite Matcher that returns true if and only all of the
// All returns a composite Matcher that returns true if and only if all of the
// matchers return true.
func All(ms ...Matcher) Matcher { return allMatcher{ms} }

// Any returns a matcher that always matches.
func Any() Matcher { return anyMatcher{} }
// All returns a composite Matcher that returns true if any of the
// matchers return true.
func Any(ms ...Matcher) Matcher { return anyMatcher{ms} }

// Eq returns a matcher that matches on equality.
//
Expand Down
1 change: 1 addition & 0 deletions gomock/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestMatchers(t *testing.T) {
yes, no []e
}{
{"test Any", gomock.Any(), []e{3, nil, "foo"}, nil},
{"test Any", gomock.Any(gomock.Eq(3), gomock.Eq(4)), []e{3, 4}, []e{5}},
{"test All", gomock.Eq(4), []e{4}, []e{3, "blah", nil, int64(4)}},
{"test Nil", gomock.Nil(),
[]e{nil, (error)(nil), (chan bool)(nil), (*int)(nil)},
Expand Down