| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package matcher |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestNewSimplePatternsMatcher(t *testing.T) { |
| 13 | tests := []struct { |
| 14 | expr string |
| 15 | expected Matcher |
| 16 | }{ |
| 17 | {"", FALSE()}, |
| 18 | {" ", FALSE()}, |
| 19 | {"foo", simplePatternsMatcher{ |
| 20 | {stringFullMatcher("foo"), true}, |
| 21 | }}, |
| 22 | {"!foo", simplePatternsMatcher{ |
| 23 | {stringFullMatcher("foo"), false}, |
| 24 | }}, |
| 25 | {"foo bar", simplePatternsMatcher{ |
| 26 | {stringFullMatcher("foo"), true}, |
| 27 | {stringFullMatcher("bar"), true}, |
| 28 | }}, |
| 29 | {"*foobar* !foo* !*bar *", simplePatternsMatcher{ |
| 30 | {stringPartialMatcher("foobar"), true}, |
| 31 | {stringPrefixMatcher("foo"), false}, |
| 32 | {stringSuffixMatcher("bar"), false}, |
| 33 | {TRUE(), true}, |
| 34 | }}, |
| 35 | {`ab\`, nil}, |
| 36 | } |
| 37 | for _, test := range tests { |
| 38 | t.Run(test.expr, func(t *testing.T) { |
| 39 | matcher, err := NewSimplePatternsMatcher(test.expr) |
| 40 | if test.expected == nil { |
| 41 | assert.Error(t, err) |
| 42 | } else { |
| 43 | assert.Equal(t, test.expected, matcher) |
| 44 | } |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestNewSimplePatternListMatcher(t *testing.T) { |
| 50 | tests := map[string]struct { |
| 51 | patterns []string |
| 52 | want Matcher |
| 53 | wantErr string |
| 54 | }{ |
| 55 | "empty list returns false": { |
| 56 | want: FALSE(), |
| 57 | }, |
| 58 | "blank entries return false": { |
| 59 | patterns: []string{"", " ", "\t"}, |
| 60 | want: FALSE(), |
| 61 | }, |
| 62 | "single glob": { |
| 63 | patterns: []string{"foo*"}, |
| 64 | want: simplePatternsMatcher{ |
| 65 | {stringPrefixMatcher("foo"), true}, |
| 66 | }, |
| 67 | }, |
| 68 | "preserves whitespace inside pattern": { |
| 69 | patterns: []string{"Business Unit"}, |
| 70 | want: simplePatternsMatcher{ |
| 71 | {stringFullMatcher("Business Unit"), true}, |
| 72 | }, |
| 73 | }, |
| 74 | "bare negative marker is invalid": { |
| 75 | patterns: []string{"!"}, |
| 76 | wantErr: "invalid empty negative pattern", |
| 77 | }, |
| 78 | "blank negative pattern is invalid": { |
| 79 | patterns: []string{"! "}, |
| 80 | wantErr: "invalid empty negative pattern", |
| 81 | }, |
| 82 | "invalid glob is wrapped": { |
| 83 | patterns: []string{"["}, |
| 84 | wantErr: "invalid pattern", |
| 85 | }, |
| 86 | "all negative patterns are invalid": { |
| 87 | patterns: []string{"!Business Secret"}, |
| 88 | wantErr: "must include at least one positive pattern", |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | for name, test := range tests { |
| 93 | t.Run(name, func(t *testing.T) { |
| 94 | matcher, err := NewSimplePatternListMatcher(test.patterns) |
| 95 | if test.wantErr != "" { |
| 96 | require.ErrorContains(t, err, test.wantErr) |
| 97 | return |
| 98 | } |
| 99 | require.NoError(t, err) |
| 100 | assert.Equal(t, test.want, matcher) |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestSimplePatternList_Match(t *testing.T) { |
| 106 | tests := map[string]struct { |
| 107 | patterns []string |
| 108 | value string |
| 109 | want bool |
| 110 | }{ |
| 111 | "positive before negative wins": { |
| 112 | patterns: []string{"Business*", "!Business Secret"}, |
| 113 | value: "Business Secret", |
| 114 | want: true, |
| 115 | }, |
| 116 | "negative before positive wins": { |
| 117 | patterns: []string{"!Business Secret", "Business*"}, |
| 118 | value: "Business Secret", |
| 119 | want: false, |
| 120 | }, |
| 121 | "later positive matches": { |
| 122 | patterns: []string{"!Business Secret", "Cost Center", "Business*"}, |
| 123 | value: "Cost Center", |
| 124 | want: true, |
| 125 | }, |
| 126 | "no matching pattern": { |
| 127 | patterns: []string{"!Business Secret", "Cost Center", "Business*"}, |
| 128 | value: "Cost", |
| 129 | want: false, |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | for name, test := range tests { |
| 134 | t.Run(name, func(t *testing.T) { |
| 135 | m, err := NewSimplePatternListMatcher(test.patterns) |
| 136 | require.NoError(t, err) |
| 137 | |
| 138 | assert.Equal(t, test.want, m.MatchString(test.value)) |
| 139 | assert.Equal(t, test.want, m.Match([]byte(test.value))) |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestSimplePatterns_Match(t *testing.T) { |
| 145 | m, err := NewSimplePatternsMatcher("*foobar* !foo* !*bar *") |
| 146 | |
| 147 | require.NoError(t, err) |
| 148 | |
| 149 | cases := []struct { |
| 150 | expected bool |
| 151 | line string |
| 152 | }{ |
| 153 | { |
| 154 | expected: true, |
| 155 | line: "hello world", |
| 156 | }, |
| 157 | { |
| 158 | expected: false, |
| 159 | line: "hello world bar", |
| 160 | }, |
| 161 | { |
| 162 | expected: true, |
| 163 | line: "hello world foobar", |
| 164 | }, |
| 165 | } |
| 166 | |
| 167 | for _, c := range cases { |
| 168 | t.Run(c.line, func(t *testing.T) { |
| 169 | assert.Equal(t, c.expected, m.MatchString(c.line)) |
| 170 | assert.Equal(t, c.expected, m.Match([]byte(c.line))) |
| 171 | }) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func TestSimplePatterns_Match2(t *testing.T) { |
| 176 | m, err := NewSimplePatternsMatcher("*foobar") |
| 177 | |
| 178 | require.NoError(t, err) |
| 179 | |
| 180 | assert.True(t, m.MatchString("foobar")) |
| 181 | assert.True(t, m.MatchString("foo foobar")) |
| 182 | assert.False(t, m.MatchString("foobar baz")) |
| 183 | } |