github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_like_test.go (about) 1 package compiler_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 9 "github.com/MontFerret/ferret/pkg/compiler" 10 ) 11 12 func TestLikeOperator(t *testing.T) { 13 Convey("RETURN \"foo\" LIKE \"f*\" ", t, func() { 14 c := compiler.New() 15 16 out1, err := c.MustCompile(` 17 RETURN "foo" LIKE "f*" 18 `).Run(context.Background()) 19 20 So(err, ShouldBeNil) 21 So(string(out1), ShouldEqual, `true`) 22 }) 23 24 Convey("RETURN LIKE('foo', 'f*')", t, func() { 25 c := compiler.New() 26 27 out1, err := c.MustCompile(` 28 RETURN LIKE('foo', 'f*') 29 `).Run(context.Background()) 30 31 So(err, ShouldBeNil) 32 So(string(out1), ShouldEqual, `true`) 33 }) 34 35 Convey("RETURN \"foo\" NOT LIKE \"b*\" ", t, func() { 36 c := compiler.New() 37 38 out1, err := c.MustCompile(` 39 RETURN "foo" NOT LIKE "b*" 40 `).Run(context.Background()) 41 42 So(err, ShouldBeNil) 43 So(string(out1), ShouldEqual, `true`) 44 }) 45 46 Convey("LET t = \"foo\" LIKE \"f*\" ", t, func() { 47 c := compiler.New() 48 49 out1, err := c.MustCompile(` 50 LET res = "foo" LIKE "f*" 51 52 RETURN res 53 `).Run(context.Background()) 54 55 So(err, ShouldBeNil) 56 So(string(out1), ShouldEqual, `true`) 57 }) 58 59 Convey("FOR IN LIKE", t, func() { 60 c := compiler.New() 61 62 out1, err := c.MustCompile(` 63 FOR str IN ["foo", "bar", "qaz"] 64 FILTER str LIKE "*a*" 65 RETURN str 66 `).Run(context.Background()) 67 68 So(err, ShouldBeNil) 69 So(string(out1), ShouldEqual, `["bar","qaz"]`) 70 }) 71 72 Convey("FOR IN LIKE 2", t, func() { 73 c := compiler.New() 74 75 out1, err := c.MustCompile(` 76 FOR str IN ["foo", "bar", "qaz"] 77 FILTER str LIKE "*a*" 78 RETURN str 79 `).Run(context.Background()) 80 81 So(err, ShouldBeNil) 82 So(string(out1), ShouldEqual, `["bar","qaz"]`) 83 }) 84 85 Convey("LIKE ternary", t, func() { 86 c := compiler.New() 87 88 out1, err := c.MustCompile(` 89 RETURN ("foo" NOT LIKE "b*") ? "foo" : "bar" 90 `).Run(context.Background()) 91 92 So(err, ShouldBeNil) 93 So(string(out1), ShouldEqual, `"foo"`) 94 }) 95 96 Convey("LIKE ternary 2", t, func() { 97 c := compiler.New() 98 99 out1, err := c.MustCompile(` 100 RETURN true ? ("foo" NOT LIKE "b*") : false 101 `).Run(context.Background()) 102 103 So(err, ShouldBeNil) 104 So(string(out1), ShouldEqual, `true`) 105 }) 106 107 Convey("LIKE ternary 3", t, func() { 108 c := compiler.New() 109 110 out1, err := c.MustCompile(` 111 RETURN true ? false : ("foo" NOT LIKE "b*") 112 `).Run(context.Background()) 113 114 So(err, ShouldBeNil) 115 So(string(out1), ShouldEqual, `false`) 116 }) 117 }