Skip to content

Commit 8c8317b

Browse files
authored
Merge pull request #1 from Kryniol/feature/new-matchers
New All and ContentTypeEquals matchers
2 parents 2344592 + ab24ed0 commit 8c8317b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

matcher/all.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package matcher
2+
3+
import (
4+
"github.com/riid/messenger"
5+
)
6+
7+
func All(matchers ...messenger.Matcher) messenger.Matcher {
8+
return MatchFunc(
9+
func(e messenger.Envelope) bool {
10+
for _, m := range matchers {
11+
if !m.Matches(e) {
12+
return false
13+
}
14+
}
15+
16+
return true
17+
},
18+
)
19+
}

matcher/all_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package matcher
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/riid/messenger"
9+
"github.com/riid/messenger/envelope"
10+
)
11+
12+
type matcherStub struct {
13+
ret bool
14+
}
15+
16+
func (m *matcherStub) Matches(_ messenger.Envelope) bool {
17+
return m.ret
18+
}
19+
20+
func TestAll(t *testing.T) {
21+
positive := &matcherStub{true}
22+
negative := &matcherStub{false}
23+
e := envelope.FromMessage([]byte{})
24+
25+
type testCase struct {
26+
matchers []messenger.Matcher
27+
expected bool
28+
}
29+
30+
suite := map[string]testCase{
31+
"no matchers": {
32+
expected: true,
33+
},
34+
"all positive matchers": {
35+
matchers: []messenger.Matcher{
36+
positive,
37+
positive,
38+
},
39+
expected: true,
40+
},
41+
"all negative matchers": {
42+
matchers: []messenger.Matcher{
43+
negative,
44+
negative,
45+
},
46+
expected: false,
47+
},
48+
"one negative matcher": {
49+
matchers: []messenger.Matcher{
50+
positive,
51+
negative,
52+
positive,
53+
},
54+
expected: false,
55+
},
56+
}
57+
58+
for name, tc := range suite {
59+
tc := tc
60+
t.Run(
61+
name, func(t *testing.T) {
62+
t.Parallel()
63+
64+
m := All(tc.matchers...)
65+
assert.Equal(t, tc.expected, m.Matches(e))
66+
},
67+
)
68+
}
69+
}

matcher/content_type_equals.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package matcher
2+
3+
import (
4+
"github.com/riid/messenger"
5+
"github.com/riid/messenger/envelope"
6+
)
7+
8+
func ContentTypeEquals(t string) messenger.Matcher {
9+
return MatchFunc(
10+
func(e messenger.Envelope) bool {
11+
return envelope.ContentType(e) == t
12+
},
13+
)
14+
}

matcher/content_type_equals_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package matcher
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/riid/messenger/envelope"
9+
)
10+
11+
func TestContentTypeEquals(t *testing.T) {
12+
e := envelope.FromMessage([]byte{})
13+
14+
m := ContentTypeEquals("application/json")
15+
assert.False(t, m.Matches(e))
16+
assert.True(t, m.Matches(envelope.WithContentType(e, "application/json")))
17+
assert.False(t, m.Matches(envelope.WithContentType(e, "text/plain")))
18+
}

0 commit comments

Comments
 (0)