github.com/netdata/go.d.plugin@v0.58.1/pkg/matcher/string_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 ) 10 11 var stringMatcherTestCases = []struct { 12 line string 13 expr string 14 full, prefix, suffix, partial bool 15 }{ 16 {"", "", true, true, true, true}, 17 {"abc", "", false, true, true, true}, 18 {"power", "pow", false, true, false, true}, 19 {"netdata", "data", false, false, true, true}, 20 {"abc", "def", false, false, false, false}, 21 {"soon", "o", false, false, false, true}, 22 } 23 24 func TestStringFullMatcher_MatchString(t *testing.T) { 25 for _, c := range stringMatcherTestCases { 26 t.Run(c.line, func(t *testing.T) { 27 m := stringFullMatcher(c.expr) 28 assert.Equal(t, c.full, m.Match([]byte(c.line))) 29 assert.Equal(t, c.full, m.MatchString(c.line)) 30 }) 31 } 32 } 33 34 func TestStringPrefixMatcher_MatchString(t *testing.T) { 35 for _, c := range stringMatcherTestCases { 36 t.Run(c.line, func(t *testing.T) { 37 m := stringPrefixMatcher(c.expr) 38 assert.Equal(t, c.prefix, m.Match([]byte(c.line))) 39 assert.Equal(t, c.prefix, m.MatchString(c.line)) 40 }) 41 } 42 } 43 44 func TestStringSuffixMatcher_MatchString(t *testing.T) { 45 for _, c := range stringMatcherTestCases { 46 t.Run(c.line, func(t *testing.T) { 47 m := stringSuffixMatcher(c.expr) 48 assert.Equal(t, c.suffix, m.Match([]byte(c.line))) 49 assert.Equal(t, c.suffix, m.MatchString(c.line)) 50 }) 51 } 52 } 53 54 func TestStringPartialMatcher_MatchString(t *testing.T) { 55 for _, c := range stringMatcherTestCases { 56 t.Run(c.line, func(t *testing.T) { 57 m := stringPartialMatcher(c.expr) 58 assert.Equal(t, c.partial, m.Match([]byte(c.line))) 59 assert.Equal(t, c.partial, m.MatchString(c.line)) 60 }) 61 } 62 }