github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_return_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 "github.com/MontFerret/ferret/pkg/runtime" 11 ) 12 13 func TestReturn(t *testing.T) { 14 Convey("Should compile RETURN NONE", t, func() { 15 c := compiler.New() 16 17 p, err := c.Compile(` 18 RETURN NONE 19 `) 20 21 So(err, ShouldBeNil) 22 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 23 24 out, err := p.Run(context.Background()) 25 26 So(err, ShouldBeNil) 27 So(string(out), ShouldEqual, "null") 28 }) 29 30 Convey("Should compile RETURN TRUE", t, func() { 31 c := compiler.New() 32 33 p, err := c.Compile(` 34 RETURN TRUE 35 `) 36 37 So(err, ShouldBeNil) 38 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 39 40 out, err := p.Run(context.Background()) 41 42 So(err, ShouldBeNil) 43 So(string(out), ShouldEqual, "true") 44 }) 45 46 Convey("Should compile RETURN 1", t, func() { 47 c := compiler.New() 48 49 p, err := c.Compile(` 50 RETURN 1 51 `) 52 53 So(err, ShouldBeNil) 54 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 55 56 out, err := p.Run(context.Background()) 57 58 So(err, ShouldBeNil) 59 So(string(out), ShouldEqual, "1") 60 }) 61 62 Convey("Should compile RETURN 1.1", t, func() { 63 c := compiler.New() 64 65 p, err := c.Compile(` 66 RETURN 1.1 67 `) 68 69 So(err, ShouldBeNil) 70 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 71 72 out, err := p.Run(context.Background()) 73 74 So(err, ShouldBeNil) 75 So(string(out), ShouldEqual, "1.1") 76 }) 77 78 Convey("Should compile RETURN 'foo'", t, func() { 79 c := compiler.New() 80 81 p, err := c.Compile(` 82 RETURN 'foo' 83 `) 84 85 So(err, ShouldBeNil) 86 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 87 88 out, err := p.Run(context.Background()) 89 90 So(err, ShouldBeNil) 91 So(string(out), ShouldEqual, "\"foo\"") 92 }) 93 94 Convey("Should compile RETURN \"foo\"", t, func() { 95 c := compiler.New() 96 97 p, err := c.Compile(` 98 RETURN "foo" 99 `) 100 101 So(err, ShouldBeNil) 102 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 103 104 out, err := p.Run(context.Background()) 105 106 So(err, ShouldBeNil) 107 So(string(out), ShouldEqual, "\"foo\"") 108 }) 109 110 Convey("Should compile RETURN \"\"", t, func() { 111 c := compiler.New() 112 113 p, err := c.Compile(` 114 RETURN "" 115 `) 116 117 So(err, ShouldBeNil) 118 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 119 120 out, err := p.Run(context.Background()) 121 122 So(err, ShouldBeNil) 123 So(string(out), ShouldEqual, "\"\"") 124 }) 125 126 Convey("Should compile RETURN []", t, func() { 127 c := compiler.New() 128 129 p, err := c.Compile(` 130 RETURN [] 131 `) 132 133 So(err, ShouldBeNil) 134 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 135 136 out, err := p.Run(context.Background()) 137 138 So(err, ShouldBeNil) 139 So(string(out), ShouldEqual, "[]") 140 }) 141 142 Convey("Should compile RETURN [1, 2, 3, 4]", t, func() { 143 c := compiler.New() 144 145 p, err := c.Compile(` 146 RETURN [1, 2, 3, 4] 147 `) 148 149 So(err, ShouldBeNil) 150 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 151 152 out, err := p.Run(context.Background()) 153 154 So(err, ShouldBeNil) 155 So(string(out), ShouldEqual, "[1,2,3,4]") 156 }) 157 158 Convey("Should compile RETURN ['foo', 'bar', 'qaz']", t, func() { 159 c := compiler.New() 160 161 p, err := c.Compile(` 162 RETURN ['foo', 'bar', 'qaz'] 163 `) 164 165 So(err, ShouldBeNil) 166 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 167 168 out, err := p.Run(context.Background()) 169 170 So(err, ShouldBeNil) 171 So(string(out), ShouldEqual, "[\"foo\",\"bar\",\"qaz\"]") 172 }) 173 174 Convey("Should compile RETURN ['foo', 'bar', 1, 2]", t, func() { 175 c := compiler.New() 176 177 p, err := c.Compile(` 178 RETURN ['foo', 'bar', 1, 2] 179 `) 180 181 So(err, ShouldBeNil) 182 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 183 184 out, err := p.Run(context.Background()) 185 186 So(err, ShouldBeNil) 187 So(string(out), ShouldEqual, "[\"foo\",\"bar\",1,2]") 188 }) 189 190 Convey("Should compile RETURN {}", t, func() { 191 c := compiler.New() 192 193 p, err := c.Compile(` 194 RETURN {} 195 `) 196 197 So(err, ShouldBeNil) 198 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 199 200 out, err := p.Run(context.Background()) 201 202 So(err, ShouldBeNil) 203 So(string(out), ShouldEqual, "{}") 204 }) 205 206 Convey("Should compile RETURN {a: 'foo', b: 'bar'}", t, func() { 207 c := compiler.New() 208 209 p, err := c.Compile(` 210 RETURN { a: "foo", b: "bar" } 211 `) 212 213 So(err, ShouldBeNil) 214 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 215 216 out, err := p.Run(context.Background()) 217 218 So(err, ShouldBeNil) 219 So(string(out), ShouldEqual, "{\"a\":\"foo\",\"b\":\"bar\"}") 220 }) 221 222 Convey("Should compile RETURN {['a']: 'foo'}", t, func() { 223 c := compiler.New() 224 225 p, err := c.Compile(` 226 RETURN { ["a"]: "foo" } 227 `) 228 229 So(err, ShouldBeNil) 230 So(p, ShouldHaveSameTypeAs, &runtime.Program{}) 231 232 out, err := p.Run(context.Background()) 233 234 So(err, ShouldBeNil) 235 So(string(out), ShouldEqual, "{\"a\":\"foo\"}") 236 }) 237 238 SkipConvey("Should compile RETURN (WAITFOR EVENT \"event\" IN obj)", t, func() { 239 c := newCompilerWithObservable() 240 241 out, err := c.MustCompile(` 242 LET obj = X::VAL("event", ["data"]) 243 244 RETURN (WAITFOR EVENT "event" IN obj) 245 `).Run(context.Background()) 246 247 So(err, ShouldBeNil) 248 So(string(out), ShouldEqual, `"data"`) 249 }) 250 }