master
go 122 lines 3.66 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package matcher
4
5 import (
6 "log"
7 "reflect"
8 "regexp"
9 "testing"
10
11 "github.com/stretchr/testify/require"
12
13 "github.com/stretchr/testify/assert"
14 )
15
16 func TestParse(t *testing.T) {
17 tests := []struct {
18 valid bool
19 line string
20 matcher Matcher
21 }{
22 {false, "", nil},
23 {false, "abc", nil},
24 {false, `~ abc\`, nil},
25 {false, `invalid_fmt:abc`, nil},
26
27 {true, "=", stringFullMatcher("")},
28 {true, "= ", stringFullMatcher("")},
29 {true, "=full", stringFullMatcher("full")},
30 {true, "= full", stringFullMatcher("full")},
31 {true, "= \t\ffull", stringFullMatcher("full")},
32
33 {true, "string:", stringFullMatcher("")},
34 {true, "string:full", stringFullMatcher("full")},
35
36 {true, "!=", Not(stringFullMatcher(""))},
37 {true, "!=full", Not(stringFullMatcher("full"))},
38 {true, "!= full", Not(stringFullMatcher("full"))},
39 {true, "!= \t\ffull", Not(stringFullMatcher("full"))},
40
41 {true, "!string:", Not(stringFullMatcher(""))},
42 {true, "!string:full", Not(stringFullMatcher("full"))},
43
44 {true, "~", TRUE()},
45 {true, "~ ", TRUE()},
46 {true, `~ ^$`, stringFullMatcher("")},
47 {true, "~ partial", stringPartialMatcher("partial")},
48 {true, `~ part\.ial`, stringPartialMatcher("part.ial")},
49 {true, "~ ^prefix", stringPrefixMatcher("prefix")},
50 {true, "~ suffix$", stringSuffixMatcher("suffix")},
51 {true, "~ ^full$", stringFullMatcher("full")},
52 {true, "~ [0-9]+", regexp.MustCompile(`[0-9]+`)},
53 {true, `~ part\s1`, regexp.MustCompile(`part\s1`)},
54
55 {true, "!~", FALSE()},
56 {true, "!~ ", FALSE()},
57 {true, "!~ partial", Not(stringPartialMatcher("partial"))},
58 {true, `!~ part\.ial`, Not(stringPartialMatcher("part.ial"))},
59 {true, "!~ ^prefix", Not(stringPrefixMatcher("prefix"))},
60 {true, "!~ suffix$", Not(stringSuffixMatcher("suffix"))},
61 {true, "!~ ^full$", Not(stringFullMatcher("full"))},
62 {true, "!~ [0-9]+", Not(regexp.MustCompile(`[0-9]+`))},
63
64 {true, `regexp:partial`, stringPartialMatcher("partial")},
65 {true, `!regexp:partial`, Not(stringPartialMatcher("partial"))},
66
67 {true, `*`, stringFullMatcher("")},
68 {true, `* foo`, stringFullMatcher("foo")},
69 {true, `* foo*`, stringPrefixMatcher("foo")},
70 {true, `* *foo`, stringSuffixMatcher("foo")},
71 {true, `* *foo*`, stringPartialMatcher("foo")},
72 {true, `* foo*bar`, globMatcher("foo*bar")},
73 {true, `* *foo*bar`, globMatcher("*foo*bar")},
74 {true, `* foo?bar`, globMatcher("foo?bar")},
75
76 {true, `!*`, Not(stringFullMatcher(""))},
77 {true, `!* foo`, Not(stringFullMatcher("foo"))},
78 {true, `!* foo*`, Not(stringPrefixMatcher("foo"))},
79 {true, `!* *foo`, Not(stringSuffixMatcher("foo"))},
80 {true, `!* *foo*`, Not(stringPartialMatcher("foo"))},
81 {true, `!* foo*bar`, Not(globMatcher("foo*bar"))},
82 {true, `!* *foo*bar`, Not(globMatcher("*foo*bar"))},
83 {true, `!* foo?bar`, Not(globMatcher("foo?bar"))},
84
85 {true, "glob:foo*bar", globMatcher("foo*bar")},
86 {true, "!glob:foo*bar", Not(globMatcher("foo*bar"))},
87
88 {true, `simple_patterns:`, FALSE()},
89 {true, `simple_patterns: `, FALSE()},
90 {true, `simple_patterns: foo`, simplePatternsMatcher{
91 {stringFullMatcher("foo"), true},
92 }},
93 {true, `simple_patterns: !foo`, simplePatternsMatcher{
94 {stringFullMatcher("foo"), false},
95 }},
96 }
97 for _, test := range tests {
98 t.Run(test.line, func(t *testing.T) {
99 m, err := Parse(test.line)
100 if test.valid {
101 require.NoError(t, err)
102 if test.matcher != nil {
103 log.Printf("%s %#v", reflect.TypeOf(m).Name(), m)
104 assert.Equal(t, test.matcher, m)
105 }
106 } else {
107 assert.Error(t, err)
108 }
109 })
110 }
111 }
112
113 func TestMust(t *testing.T) {
114 assert.NotPanics(t, func() {
115 m := Must(New(FmtRegExp, `[0-9]+`))
116 assert.NotNil(t, m)
117 })
118
119 assert.Panics(t, func() {
120 Must(New(FmtRegExp, `[0-9]+\`))
121 })
122 }