github.com/netdata/go.d.plugin@v0.58.1/pkg/matcher/expr_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 func TestSimpleExpr_none(t *testing.T) { 12 expr := &SimpleExpr{} 13 14 m, err := expr.Parse() 15 assert.EqualError(t, err, ErrEmptyExpr.Error()) 16 assert.Nil(t, m) 17 } 18 19 func TestSimpleExpr_include(t *testing.T) { 20 expr := &SimpleExpr{ 21 Includes: []string{ 22 "~ /api/", 23 "~ .php$", 24 }, 25 } 26 27 m, err := expr.Parse() 28 assert.NoError(t, err) 29 30 assert.True(t, m.MatchString("/api/a.php")) 31 assert.True(t, m.MatchString("/api/a.php2")) 32 assert.True(t, m.MatchString("/api2/a.php")) 33 assert.True(t, m.MatchString("/api/img.php")) 34 assert.False(t, m.MatchString("/api2/img.php2")) 35 } 36 37 func TestSimpleExpr_exclude(t *testing.T) { 38 expr := &SimpleExpr{ 39 Excludes: []string{ 40 "~ /api/img", 41 }, 42 } 43 44 m, err := expr.Parse() 45 assert.NoError(t, err) 46 47 assert.True(t, m.MatchString("/api/a.php")) 48 assert.True(t, m.MatchString("/api/a.php2")) 49 assert.True(t, m.MatchString("/api2/a.php")) 50 assert.False(t, m.MatchString("/api/img.php")) 51 assert.True(t, m.MatchString("/api2/img.php2")) 52 } 53 54 func TestSimpleExpr_both(t *testing.T) { 55 expr := &SimpleExpr{ 56 Includes: []string{ 57 "~ /api/", 58 "~ .php$", 59 }, 60 Excludes: []string{ 61 "~ /api/img", 62 }, 63 } 64 65 m, err := expr.Parse() 66 assert.NoError(t, err) 67 68 assert.True(t, m.MatchString("/api/a.php")) 69 assert.True(t, m.MatchString("/api/a.php2")) 70 assert.True(t, m.MatchString("/api2/a.php")) 71 assert.False(t, m.MatchString("/api/img.php")) 72 assert.False(t, m.MatchString("/api2/img.php2")) 73 } 74 75 func TestSimpleExpr_Parse_NG(t *testing.T) { 76 { 77 expr := &SimpleExpr{ 78 Includes: []string{ 79 "~ (ab", 80 "~ .php$", 81 }, 82 } 83 84 m, err := expr.Parse() 85 assert.Error(t, err) 86 assert.Nil(t, m) 87 } 88 { 89 expr := &SimpleExpr{ 90 Excludes: []string{ 91 "~ (ab", 92 "~ .php$", 93 }, 94 } 95 96 m, err := expr.Parse() 97 assert.Error(t, err) 98 assert.Nil(t, m) 99 } 100 }