github.com/netdata/go.d.plugin@v0.58.1/pkg/matcher/simple_patterns_test.go (about) 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 TestSimplePatterns_Match(t *testing.T) { 50 m, err := NewSimplePatternsMatcher("*foobar* !foo* !*bar *") 51 52 require.NoError(t, err) 53 54 cases := []struct { 55 expected bool 56 line string 57 }{ 58 { 59 expected: true, 60 line: "hello world", 61 }, 62 { 63 expected: false, 64 line: "hello world bar", 65 }, 66 { 67 expected: true, 68 line: "hello world foobar", 69 }, 70 } 71 72 for _, c := range cases { 73 t.Run(c.line, func(t *testing.T) { 74 assert.Equal(t, c.expected, m.MatchString(c.line)) 75 assert.Equal(t, c.expected, m.Match([]byte(c.line))) 76 }) 77 } 78 } 79 80 func TestSimplePatterns_Match2(t *testing.T) { 81 m, err := NewSimplePatternsMatcher("*foobar") 82 83 require.NoError(t, err) 84 85 assert.True(t, m.MatchString("foobar")) 86 assert.True(t, m.MatchString("foo foobar")) 87 assert.False(t, m.MatchString("foobar baz")) 88 }