github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_regexp_test.go (about) 1 package compiler_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/MontFerret/ferret/pkg/runtime/core" 9 "github.com/MontFerret/ferret/pkg/runtime/values" 10 11 . "github.com/smartystreets/goconvey/convey" 12 13 "github.com/MontFerret/ferret/pkg/compiler" 14 ) 15 16 func TestRegexpOperator(t *testing.T) { 17 Convey("Should be possible to use positive regular expression operator", t, func() { 18 out := compiler.New(). 19 MustCompile(` 20 RETURN "foo" =~ "^f[o].$" 21 `). 22 MustRun(context.Background()) 23 24 So(string(out), ShouldEqual, `true`) 25 }) 26 27 Convey("Should be possible to use negative regular expression operator", t, func() { 28 out := compiler.New(). 29 MustCompile(` 30 RETURN "foo" !~ "[a-z]+bar$" 31 `). 32 MustRun(context.Background()) 33 34 So(string(out), ShouldEqual, `true`) 35 }) 36 37 Convey("Should be possible to use negative regular expression operator", t, func() { 38 c := compiler.New() 39 c.Namespace("T").RegisterFunction("REGEXP", func(_ context.Context, _ ...core.Value) (value core.Value, e error) { 40 return values.NewString("[a-z]+bar$"), nil 41 }) 42 43 out := c. 44 MustCompile(` 45 RETURN "foo" !~ T::REGEXP() 46 `). 47 MustRun(context.Background()) 48 49 So(string(out), ShouldEqual, `true`) 50 }) 51 52 Convey("Should return an error during compilation when a regexp string invalid", t, func() { 53 _, err := compiler.New(). 54 Compile(` 55 RETURN "foo" !~ "[ ]\K(?<!\d )(?=(?: ?\d){8})(?!(?: ?\d){9})\d[ \d]+\d" 56 `) 57 58 So(err, ShouldBeError) 59 }) 60 61 Convey("Should return an error during compilation when a regexp is not a string", t, func() { 62 right := []string{ 63 "[]", 64 "{}", 65 "1", 66 "1.1", 67 "TRUE", 68 } 69 70 for _, r := range right { 71 _, err := compiler.New(). 72 Compile(fmt.Sprintf(` 73 RETURN "foo" !~ %s 74 `, r)) 75 76 So(err, ShouldBeError) 77 } 78 }) 79 }