-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_pattern_test.go
127 lines (112 loc) · 5.3 KB
/
mqtt_pattern_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package mqttpattern_test
import "testing"
import mqttpattern "github.com/amir-yaghoubi/mqtt-pattern"
import "fmt"
func TestMattches(t *testing.T) {
testCases := []struct {
name string
pattern string
topic string
result bool
}{
{name: "Supports empty patterns", pattern: "", topic: "foo/bar/baz", result: false},
{name: "Supports empty patterns", pattern: "foo/bar/baz", topic: "", result: false},
{name: "Supports patterns with no wildcards", pattern: "foo/bar/baz", topic: "foo/bar/baz", result: true},
{name: "Doesn't match different pattern and topic", pattern: "foo/bar/baz", topic: "bar/foo/baz", result: false},
{name: "Supports # at the beginning", pattern: "#", topic: "foo/bar/baz", result: true},
{name: "Supports # at the end", pattern: "foo/#", topic: "foo/bar/baz", result: true},
{name: "Supports # at the end and topic has no children", pattern: "foo/bar/#", topic: "foo/bar/baz", result: true},
{name: "Doesn't support # wildcards with more after them", pattern: "#/foo/bar", topic: "foo/bar/baz", result: false},
{name: "Supports patterns with + at the beginning", pattern: "+/bar/baz", topic: "foo/bar/baz", result: true},
{name: "Supports patterns with + at the end", pattern: "foo/bar/+", topic: "foo/bar/baz", result: true},
{name: "Supports patterns with + at the middle", pattern: "foo/+/baz", topic: "foo/bar/baz", result: true},
{name: "Supports patterns with multiple wildcards", pattern: "foo/+/#", topic: "foo/bar/baz", result: true},
{name: "Supports named wildcards", pattern: "foo/+something/#else", topic: "foo/bar/baz", result: true},
{name: "Supports leading slashes", pattern: "/foo/bar/baz", topic: "/foo/bar/baz", result: true},
{name: "Supports leading slashes with invalid topic", pattern: "/foo/bar", topic: "/bar/foo", result: false},
}
for _, tt := range testCases {
if mqttpattern.Matches(tt.pattern, tt.topic) != tt.result {
t.Error(tt.name)
}
}
}
func TestExtract(t *testing.T) {
testCases := []struct {
name string
pattern string
topic string
result map[string]string
}{
{name: "Returns empty map if pattern is empty", pattern: "", topic: "foo/bar/baz", result: make(map[string]string)},
{name: "Returns empty map if topic is empty", pattern: "foo/+bar/+baz", topic: "", result: make(map[string]string)},
{name: "Returns empty map if pattern and topic are empty", pattern: "", topic: "", result: make(map[string]string)},
{name: "Returns empty map if wildcards don't have label", pattern: "foo/+/#", topic: "foo/bar/baz", result: make(map[string]string)},
{name: "Returns map with a rest of topic as string for # wildcard", pattern: "foo/#bar", topic: "foo/bar/baz", result: map[string]string{"bar": "bar/baz"}},
{name: "Returns map with a string for + wildcard", pattern: "foo/+bar/+baz", topic: "foo/bar/baz", result: map[string]string{"bar": "bar", "baz": "baz"}},
{name: "Parses params from all wildcards", pattern: "+foo/+bar/#baz", topic: "foo/bar/baz", result: map[string]string{"foo": "foo", "bar": "bar", "baz": "baz"}},
}
for _, tt := range testCases {
result := mqttpattern.Extract(tt.pattern, tt.topic)
rStr := fmt.Sprintf("%v", result)
trStr := fmt.Sprintf("%v", tt.result)
if rStr != trStr {
t.Errorf("%s | expected %s but received %s", tt.name, trStr, rStr)
}
}
}
func TestExec(t *testing.T) {
testCases := []struct {
name string
pattern string
topic string
result map[string]string
}{
{name: "Returns nil if doesn't match", pattern: "foo/bar", topic: "foo/bar/baz", result: nil},
{name: "Returns params if they can be parsed", pattern: "foo/+bar/#baz", topic: "foo/bar/baz", result: map[string]string{"bar": "bar", "baz": "baz"}},
}
for _, tt := range testCases {
result := mqttpattern.Exec(tt.pattern, tt.topic)
rStr := fmt.Sprintf("%v", result)
trStr := fmt.Sprintf("%v", tt.result)
if rStr != trStr {
t.Errorf("%s | expected %s but received %s", tt.name, trStr, rStr)
}
}
}
func TestFill(t *testing.T) {
testCases := []struct {
name string
pattern string
params map[string]string
result string
}{
{name: "Returns empty string for empty patterns", pattern: "", params: map[string]string{"bar": "BAR"}, result: ""},
{name: "Fills in pattern with both types of wildcards", pattern: "foo/+bar/#baz", params: map[string]string{"bar": "BAR", "baz": "BAZ"}, result: "foo/BAR/BAZ"},
{name: "Fills missing + params with \"\"", pattern: "foo/+bar", params: make(map[string]string), result: "foo/"},
{name: "Fills missing # params with \"\"", pattern: "foo/#bar", params: make(map[string]string), result: "foo/"},
}
for _, tt := range testCases {
result := mqttpattern.Fill(tt.pattern, tt.params)
if result != tt.result {
t.Errorf("%s | expected %s but received %s", tt.name, tt.result, result)
}
}
}
func TestClean(t *testing.T) {
testCases := []struct {
name string
pattern string
result string
}{
{name: "Returns empty string for empty patterns", pattern: "", result: ""},
{name: "Works when there aren't any named parameter", pattern: "foo/+/bar/#", result: "foo/+/bar/#"},
{name: "Removes named parameters", pattern: "foo/+something/bar/#otherthing", result: "foo/+/bar/#"},
}
for _, tt := range testCases {
result := mqttpattern.Clean(tt.pattern)
if result != tt.result {
t.Errorf("%s | expected %s but received %s", tt.name, tt.result, result)
}
}
}