1 package templatex
2
3 import (
4 "regexp"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestRegexCompiler(t *testing.T) {
11 for k, c := range []struct {
12 template string
13 delimiterStart byte
14 delimiterEnd byte
15 failCompile bool
16 matchAgainst string
17 failMatch bool
18 }{
19 {"urn:foo:{.*}", '{', '}', false, "urn:foo:bar:baz", false},
20 {"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.bar.com:bar:baz", false},
21 {"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.com:bar:baz", true},
22 {"urn:foo.bar.com:{.*}", '{', '}', false, "foobar", true},
23 {"urn:foo.bar.com:{.{1,2}}", '{', '}', false, "urn:foo.bar.com:aa", false},
24
25 {"urn:foo.bar.com:{.*{}", '{', '}', true, "", true},
26 {"urn:foo:<.*>", '<', '>', false, "urn:foo:bar:baz", false},
27
28
29
30 } {
31 k++
32 result, err := CompileRegex(c.template, c.delimiterStart, c.delimiterEnd)
33 assert.Equal(t, c.failCompile, err != nil, "Case %d", k)
34 if c.failCompile || err != nil {
35 continue
36 }
37
38 t.Logf("Case %d compiled to: %s", k, result.String())
39 ok, err := regexp.MatchString(result.String(), c.matchAgainst)
40 assert.Nil(t, err, "Case %d", k)
41 assert.Equal(t, !c.failMatch, ok, "Case %d", k)
42 }
43 }
44
View as plain text