forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_template_test.go
46 lines (41 loc) · 1.33 KB
/
regex_template_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
package topdown
import (
"fmt"
"regexp"
"testing"
)
func TestRegexCompiler(t *testing.T) {
for _, tc := range []struct {
template string
delimiterStart byte
delimiterEnd byte
failCompile bool
matchAgainst string
failMatch bool
}{
{"urn:foo:{.*}", '{', '}', false, "urn:foo:bar:baz", false},
{"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.bar.com:bar:baz", false},
{"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.com:bar:baz", true},
{"urn:foo.bar.com:{.*}", '{', '}', false, "foobar", true},
{"urn:foo.bar.com:{.{1,2}}", '{', '}', false, "urn:foo.bar.com:aa", false},
{"urn:foo.bar.com:{.*{}", '{', '}', true, "", true},
{"urn:foo:<.*>", '<', '>', false, "urn:foo:bar:baz", false},
} {
t.Run(fmt.Sprintf("template=%s", tc.template), func(t *testing.T) {
result, err := compileRegexTemplate(tc.template, tc.delimiterStart, tc.delimiterEnd)
if tc.failCompile != (err != nil) {
t.Fatalf("failed regex template compilation: %t != %t", tc.failCompile, err != nil)
}
if tc.failCompile || err != nil {
return
}
ok, err := regexp.MatchString(result.String(), tc.matchAgainst)
if err != nil {
t.Fatalf("unexpected error while matching string: %s", err)
}
if !tc.failMatch != ok {
t.Logf("match result %t is not expected value %t", ok, !tc.failMatch)
}
})
}
}