1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-21 21:47:43 +02:00
ferret/pkg/compiler/compiler_let_test.go
Tim Voronov 742bdae0ae
Feature/#263 waitfor event (#590)
* Added new WAITFOR syntax

* Added support of event options

* Added support of options

* Added support of using WAITFOR EVENT in variable assignment
2021-07-13 21:34:22 -04:00

266 lines
5.2 KiB
Go

package compiler_test
import (
"context"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestLet(t *testing.T) {
Convey("Should compile LET i = NONE RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = NONE
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "null")
})
Convey("Should compile LET i = TRUE RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = TRUE
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "true")
})
Convey("Should compile LET i = 1 RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = 1
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1")
})
Convey("Should compile LET i = 1.1 RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = 1.1
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1.1")
})
Convey("Should compile LET i = 'foo' RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = "foo"
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "\"foo\"")
})
Convey("Should compile LET i = [] RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = []
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[]")
})
Convey("Should compile LET i = [1, 2, 3] RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = [1, 2, 3]
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3]")
})
Convey("Should compile LET i = {} RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = {}
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{}")
})
Convey("Should compile LET i = {a: 'foo', b: 1, c: TRUE, d: [], e: {}} RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = {a: 'foo', b: 1, c: TRUE, d: [], e: {}}
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{\"a\":\"foo\",\"b\":1,\"c\":true,\"d\":[],\"e\":{}}")
})
Convey("Should compile LET i = (FOR i IN [1,2,3] RETURN i) RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = (FOR i IN [1,2,3] RETURN i)
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3]")
})
Convey("Should compile LET i = (FOR i WHILE 0 > 1 RETURN i) RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET i = (FOR i WHILE 0 > 1 RETURN i)
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[]")
})
Convey("Should compile LET i = { items: [1,2,3]} FOR el IN i.items RETURN i", t, func() {
c := compiler.New()
p, err := c.Compile(`
LET obj = { items: [1,2,3] }
FOR i IN obj.items
RETURN i
`)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3]")
})
Convey("Should not compile FOR foo IN foo", t, func() {
c := compiler.New()
_, err := c.Compile(`
FOR foo IN foo
RETURN foo
`)
So(err, ShouldNotBeNil)
})
Convey("Should not compile if a variable not defined", t, func() {
c := compiler.New()
_, err := c.Compile(`
RETURN foo
`)
So(err, ShouldNotBeNil)
})
Convey("Should not compile if a variable is not unique", t, func() {
c := compiler.New()
_, err := c.Compile(`
LET foo = "bar"
LET foo = "baz"
RETURN foo
`)
So(err, ShouldNotBeNil)
})
Convey("Should use value returned from WAITFOR EVENT", t, func() {
out, err := newCompilerWithObservable().MustCompile(`
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event", "data", 100)
LET res = (WAITFOR EVENT "event" IN obj)
RETURN res
`).Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"data"`)
})
}