github.com/rajeev159/opa@v0.45.0/topdown/regex_template_test.go (about) 1 package topdown 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 ) 8 9 func TestRegexCompiler(t *testing.T) { 10 for _, tc := range []struct { 11 template string 12 delimiterStart byte 13 delimiterEnd byte 14 failCompile bool 15 matchAgainst string 16 failMatch bool 17 }{ 18 {"urn:foo:{.*}", '{', '}', false, "urn:foo:bar:baz", false}, 19 {"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.bar.com:bar:baz", false}, 20 {"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.com:bar:baz", true}, 21 {"urn:foo.bar.com:{.*}", '{', '}', false, "foobar", true}, 22 {"urn:foo.bar.com:{.{1,2}}", '{', '}', false, "urn:foo.bar.com:aa", false}, 23 {"urn:foo.bar.com:{.*{}", '{', '}', true, "", true}, 24 {"urn:foo:<.*>", '<', '>', false, "urn:foo:bar:baz", false}, 25 } { 26 t.Run(fmt.Sprintf("template=%s", tc.template), func(t *testing.T) { 27 result, err := compileRegexTemplate(tc.template, tc.delimiterStart, tc.delimiterEnd) 28 if tc.failCompile != (err != nil) { 29 t.Fatalf("failed regex template compilation: %t != %t", tc.failCompile, err != nil) 30 } 31 32 if tc.failCompile || err != nil { 33 return 34 } 35 36 ok, err := regexp.MatchString(result.String(), tc.matchAgainst) 37 if err != nil { 38 t.Fatalf("unexpected error while matching string: %s", err) 39 } 40 41 if !tc.failMatch != ok { 42 t.Logf("match result %t is not expected value %t", ok, !tc.failMatch) 43 } 44 }) 45 } 46 }