File tree Expand file tree Collapse file tree 4 files changed +120
-0
lines changed Expand file tree Collapse file tree 4 files changed +120
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments