-
Notifications
You must be signed in to change notification settings - Fork 20
/
string_match_test.go
117 lines (102 loc) · 2.05 KB
/
string_match_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
package condition
import (
"context"
"testing"
"github.com/brexhq/substation/v2/config"
"github.com/brexhq/substation/v2/message"
)
var _ Conditioner = &stringMatch{}
var stringMatchTests = []struct {
name string
cfg config.Config
test []byte
expected bool
}{
{
"pass",
config.Config{
Settings: map[string]interface{}{
"pattern": "^Test",
},
},
[]byte("Test"),
true,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"pattern": "^Test",
},
},
[]byte("-Test"),
false,
},
}
func TestStringMatch(t *testing.T) {
ctx := context.TODO()
for _, test := range stringMatchTests {
t.Run(test.name, func(t *testing.T) {
message := message.New().SetData(test.test)
insp, err := newStringMatch(ctx, test.cfg)
if err != nil {
t.Fatal(err)
}
check, err := insp.Condition(ctx, message)
if err != nil {
t.Error(err)
}
if test.expected != check {
t.Errorf("expected %v, got %v", test.expected, check)
}
})
}
}
func benchmarkStringMatchByte(b *testing.B, insp *stringMatch, message *message.Message) {
ctx := context.TODO()
for i := 0; i < b.N; i++ {
_, _ = insp.Condition(ctx, message)
}
}
func BenchmarkStringMatchByte(b *testing.B) {
for _, test := range stringMatchTests {
insp, err := newStringMatch(context.TODO(), test.cfg)
if err != nil {
b.Fatal(err)
}
b.Run(test.name,
func(b *testing.B) {
message := message.New().SetData(test.test)
benchmarkStringMatchByte(b, insp, message)
},
)
}
}
func FuzzTestStringMatch(f *testing.F) {
testcases := [][]byte{
[]byte("Test"),
[]byte("-Test"),
[]byte("AnotherTest"),
[]byte("123Test"),
[]byte(""),
}
for _, tc := range testcases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newStringMatch(ctx, config.Config{
Settings: map[string]interface{}{
"pattern": "^Test",
},
})
if err != nil {
return
}
_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}