зеркало из https://github.com/golang/mock.git
Allow Any to take a list of matchers
This works like an OR the same way that All works like an AND. If you pass 0 parameters it works the same way it always has.
This commit is contained in:
Родитель
5b455625bd
Коммит
c45008e63f
|
@ -87,16 +87,35 @@ func GotFormatterAdapter(s GotFormatter, m Matcher) Matcher {
|
|||
}
|
||||
}
|
||||
|
||||
type anyMatcher struct{}
|
||||
type anyMatcher struct {
|
||||
matchers []Matcher
|
||||
}
|
||||
|
||||
func (anyMatcher) Matches(interface{}) bool {
|
||||
func (am anyMatcher) Matches(x interface{}) bool {
|
||||
if len(am.matchers) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
func (anyMatcher) String() string {
|
||||
for _, m := range am.matchers {
|
||||
if m.Matches(x) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
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 {
|
||||
x interface{}
|
||||
}
|
||||
|
@ -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.
|
||||
//
|
||||
|
|
|
@ -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)},
|
||||
|
|
Загрузка…
Ссылка в новой задаче