Skip to content

Commit 01d75b8

Browse files
authored
Merge pull request #458 from rishinair11/fix_policy_test
Improve accuracy of filter_test.go
2 parents 0e0d700 + fff22d9 commit 01d75b8

File tree

1 file changed

+57
-12
lines changed

1 file changed

+57
-12
lines changed

internal/policy/filter_test.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ limitations under the License.
1717
package policy
1818

1919
import (
20-
"reflect"
2120
"sort"
2221
"testing"
22+
23+
. "github.com/onsi/gomega"
2324
)
2425

2526
func TestRegexFilter(t *testing.T) {
@@ -48,20 +49,64 @@ func TestRegexFilter(t *testing.T) {
4849
extract: `$1`,
4950
expected: []string{"1", "2", "3"},
5051
},
52+
{
53+
label: "valid pattern (complex regex 1)",
54+
tags: []string{
55+
"123-123.123.abcd123-debug",
56+
"123-123.123.abcd123",
57+
"123-123.123.abcd456-debug",
58+
"123-123.123.abcd456",
59+
},
60+
pattern: `^(123-[0-9]+\.[0-9]+\.[a-z0-9]+-debug)`,
61+
expected: []string{
62+
"123-123.123.abcd123-debug",
63+
"123-123.123.abcd456-debug",
64+
},
65+
},
66+
{
67+
label: "valid pattern with capture group (complex regex 2)",
68+
tags: []string{
69+
"123-123.123.abcd123-debug",
70+
"123-123.123.abcd123",
71+
"123-123.123.abcd456-debug",
72+
"123-123.123.abcd456",
73+
},
74+
pattern: `^(?P<tag>123-[0-9]+\.[0-9]+\.[a-z0-9]+[^-debug])`,
75+
extract: `$tag`,
76+
expected: []string{
77+
"123-123.123.abcd123",
78+
"123-123.123.abcd456",
79+
},
80+
},
81+
{
82+
label: "valid pattern with capture group (complex regex 3)",
83+
tags: []string{
84+
"123-123.123.abcd123-debug",
85+
"123-123.123.abcd123",
86+
"123-123.123.abcd456-debug",
87+
"123-123.123.abcd456",
88+
},
89+
pattern: `^(?P<tag>123-[0-9]+\.[0-9]+\.[a-z0-9]+$)`,
90+
extract: `$tag`,
91+
expected: []string{
92+
"123-123.123.abcd123",
93+
"123-123.123.abcd456",
94+
},
95+
},
5196
}
97+
5298
for _, tt := range cases {
5399
t.Run(tt.label, func(t *testing.T) {
54-
filter := newRegexFilter(tt.pattern, tt.extract)
55-
filter.Apply(tt.tags)
56-
r := sort.StringSlice(filter.Items())
57-
if reflect.DeepEqual(r, tt.expected) {
58-
t.Errorf("incorrect value returned, got '%s', expected '%s'", r, tt.expected)
59-
}
100+
g := NewWithT(t)
101+
102+
f, err := NewRegexFilter(tt.pattern, tt.extract)
103+
g.Expect(err).ToNot(HaveOccurred())
104+
105+
f.Apply(tt.tags)
106+
r := f.Items()
107+
sort.Strings(r)
108+
109+
g.Expect(r).To(Equal(tt.expected))
60110
})
61111
}
62112
}
63-
64-
func newRegexFilter(pattern string, extract string) *RegexFilter {
65-
f, _ := NewRegexFilter(pattern, extract)
66-
return f
67-
}

0 commit comments

Comments
 (0)