Skip to content

Commit 1ef9368

Browse files
committed
Tests on the regex analuzer
1 parent b22120a commit 1ef9368

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

pkg/analyze/regex.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package analyzer
22

33
import (
4+
"fmt"
45
"path"
56
"regexp"
67
"strconv"
@@ -125,6 +126,7 @@ func compareRegex(conditional string, foundMatches map[string]string) (bool, err
125126
// if the value side of the conditional is an int, we assume it's an int
126127
lookForValueInt, err := strconv.Atoi(lookForValue)
127128
if err == nil {
129+
fmt.Printf("look for = %s, found = %s\n", lookForValue, foundValue)
128130
foundValueInt, err := strconv.Atoi(foundValue)
129131
if err != nil {
130132
// not an error but maybe it should be...
@@ -137,19 +139,19 @@ func compareRegex(conditional string, foundMatches map[string]string) (bool, err
137139
case "==":
138140
fallthrough
139141
case "===":
140-
return foundValueInt == lookForValueInt, nil
142+
return lookForValueInt == foundValueInt, nil
141143

142144
case "<":
143-
return foundValueInt < lookForValueInt, nil
145+
return lookForValueInt < foundValueInt, nil
144146

145147
case ">":
146-
return foundValueInt > lookForValueInt, nil
148+
return lookForValueInt > foundValueInt, nil
147149

148150
case "<=":
149-
return foundValueInt <= lookForValueInt, nil
151+
return lookForValueInt <= foundValueInt, nil
150152

151153
case ">=":
152-
return foundValueInt >= lookForValueInt, nil
154+
return lookForValueInt >= foundValueInt, nil
153155
}
154156
} else {
155157
// all we can support is "=" and "==" and "===" for now

pkg/analyze/regex_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package analyzer
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func Test_compareRegex(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
conditional string
14+
foundMatches map[string]string
15+
expected bool
16+
}{
17+
{
18+
name: "Loss < 5",
19+
conditional: "Loss < 5",
20+
foundMatches: map[string]string{
21+
"Transmitted": "5",
22+
"Received": "4",
23+
"Loss": "20",
24+
},
25+
expected: true,
26+
},
27+
{
28+
name: "Hostname = icecream",
29+
conditional: "Hostname = icecream",
30+
foundMatches: map[string]string{
31+
"Something": "5",
32+
"Hostname": "icecream",
33+
},
34+
expected: true,
35+
},
36+
{
37+
name: "Day >= 23",
38+
conditional: "Day >= 23",
39+
foundMatches: map[string]string{
40+
"day": "5",
41+
"Day": "24",
42+
},
43+
expected: false,
44+
},
45+
}
46+
for _, test := range tests {
47+
t.Run(test.name, func(t *testing.T) {
48+
req := require.New(t)
49+
50+
actual, err := compareRegex(test.conditional, test.foundMatches)
51+
req.NoError(err)
52+
53+
assert.Equal(t, test.expected, actual)
54+
})
55+
}
56+
57+
}

0 commit comments

Comments
 (0)