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

Commit 9be4808

Browse files
authoredDec 20, 2019
add All matcher (#367)
This matcher is to be used to combine multiple matches into one statement. Implmentation inspired by #189.
1 parent 693e3a8 commit 9be4808

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed
 

‎gomock/matchers.go

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package gomock
1717
import (
1818
"fmt"
1919
"reflect"
20+
"strings"
2021
)
2122

2223
// A Matcher is a representation of a class of values.
@@ -154,8 +155,33 @@ func (m assignableToTypeOfMatcher) String() string {
154155
return "is assignable to " + m.targetType.Name()
155156
}
156157

158+
type allMatcher struct {
159+
matchers []Matcher
160+
}
161+
162+
func (am allMatcher) Matches(x interface{}) bool {
163+
for _, m := range am.matchers {
164+
if !m.Matches(x) {
165+
return false
166+
}
167+
}
168+
return true
169+
}
170+
171+
func (am allMatcher) String() string {
172+
ss := make([]string, 0, len(am.matchers))
173+
for _, matcher := range am.matchers {
174+
ss = append(ss, matcher.String())
175+
}
176+
return strings.Join(ss, "; ")
177+
}
178+
157179
// Constructors
158180

181+
// All returns a composite Matcher that returns true if and only all of the
182+
// matchers return true.
183+
func All(ms ...Matcher) Matcher { return allMatcher{ms} }
184+
159185
// Any returns a matcher that always matches.
160186
func Any() Matcher { return anyMatcher{} }
161187

‎gomock/matchers_test.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,32 @@ import (
2626

2727
func TestMatchers(t *testing.T) {
2828
type e interface{}
29-
type testCase struct {
29+
tests := []struct {
30+
name string
3031
matcher gomock.Matcher
3132
yes, no []e
32-
}
33-
tests := []testCase{
34-
{gomock.Any(), []e{3, nil, "foo"}, nil},
35-
{gomock.Eq(4), []e{4}, []e{3, "blah", nil, int64(4)}},
36-
{gomock.Nil(),
33+
}{
34+
{"test Any", gomock.Any(), []e{3, nil, "foo"}, nil},
35+
{"test All", gomock.Eq(4), []e{4}, []e{3, "blah", nil, int64(4)}},
36+
{"test Nil", gomock.Nil(),
3737
[]e{nil, (error)(nil), (chan bool)(nil), (*int)(nil)},
3838
[]e{"", 0, make(chan bool), errors.New("err"), new(int)}},
39-
{gomock.Not(gomock.Eq(4)), []e{3, "blah", nil, int64(4)}, []e{4}},
40-
}
41-
for i, test := range tests {
42-
for _, x := range test.yes {
43-
if !test.matcher.Matches(x) {
44-
t.Errorf(`test %d: "%v %s" should be true.`, i, x, test.matcher)
39+
{"test Not", gomock.Not(gomock.Eq(4)), []e{3, "blah", nil, int64(4)}, []e{4}},
40+
{"test All", gomock.All(gomock.Any(), gomock.Eq(4)), []e{4}, []e{3, "blah", nil, int64(4)}},
41+
}
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
for _, x := range tt.yes {
45+
if !tt.matcher.Matches(x) {
46+
t.Errorf(`"%v %s": got false, want true.`, x, tt.matcher)
47+
}
4548
}
46-
}
47-
for _, x := range test.no {
48-
if test.matcher.Matches(x) {
49-
t.Errorf(`test %d: "%v %s" should be false.`, i, x, test.matcher)
49+
for _, x := range tt.no {
50+
if tt.matcher.Matches(x) {
51+
t.Errorf(`"%v %s": got true, want false.`, x, tt.matcher)
52+
}
5053
}
51-
}
54+
})
5255
}
5356
}
5457

0 commit comments

Comments
 (0)
This repository has been archived.