2018-10-16 00:50:55 +02:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/MontFerret/ferret/pkg/compiler"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParam(t *testing.T) {
|
|
|
|
Convey("Should be possible to use as a return value", t, func() {
|
|
|
|
out := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
RETURN @param
|
|
|
|
`).
|
|
|
|
MustRun(context.Background(), runtime.WithParam("param", "foobar"))
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, `"foobar"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be possible to use as a FOR source", t, func() {
|
|
|
|
out := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
FOR i IN @values
|
|
|
|
SORT i
|
|
|
|
RETURN i
|
|
|
|
`).
|
|
|
|
MustRun(context.Background(), runtime.WithParam("values", []int{1, 2, 3, 4}))
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, `[1,2,3,4]`)
|
|
|
|
|
|
|
|
out2 := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
FOR i IN @values
|
|
|
|
SORT i
|
|
|
|
RETURN i
|
|
|
|
`).
|
|
|
|
MustRun(context.Background(), runtime.WithParam("values", map[string]int{
|
|
|
|
"foo": 1,
|
|
|
|
"bar": 2,
|
|
|
|
"faz": 3,
|
|
|
|
"qaz": 4,
|
|
|
|
}))
|
|
|
|
|
|
|
|
So(string(out2), ShouldEqual, `[1,2,3,4]`)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be possible to use in range", t, func() {
|
2018-10-25 03:30:05 +02:00
|
|
|
prog := compiler.New().
|
2018-10-16 00:50:55 +02:00
|
|
|
MustCompile(`
|
|
|
|
FOR i IN @start..@end
|
|
|
|
SORT i
|
|
|
|
RETURN i
|
2018-10-25 03:30:05 +02:00
|
|
|
`)
|
|
|
|
|
|
|
|
out := prog.MustRun(
|
|
|
|
context.Background(),
|
|
|
|
runtime.WithParam("start", 1),
|
|
|
|
runtime.WithParam("end", 4),
|
|
|
|
)
|
2018-10-16 00:50:55 +02:00
|
|
|
|
|
|
|
So(string(out), ShouldEqual, `[1,2,3,4]`)
|
|
|
|
|
|
|
|
})
|
2019-09-07 07:21:43 +02:00
|
|
|
|
|
|
|
Convey("Should be possible to use in member expression", t, func() {
|
|
|
|
prog := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
RETURN @param.value
|
|
|
|
`)
|
|
|
|
|
|
|
|
out := prog.MustRun(
|
|
|
|
context.Background(),
|
|
|
|
runtime.WithParam("param", map[string]interface{}{
|
|
|
|
"value": "foobar",
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, `"foobar"`)
|
|
|
|
|
|
|
|
})
|
2019-09-07 07:47:58 +02:00
|
|
|
|
|
|
|
Convey("Should be possible to use in member expression as a computed property", t, func() {
|
|
|
|
prog := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
LET obj = { foo: "bar" }
|
|
|
|
RETURN obj[@param]
|
|
|
|
`)
|
|
|
|
|
|
|
|
out := prog.MustRun(
|
|
|
|
context.Background(),
|
|
|
|
runtime.WithParam("param", "foo"),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, `"bar"`)
|
|
|
|
})
|
2019-09-07 07:57:33 +02:00
|
|
|
|
|
|
|
Convey("Should be possible to use in member expression as segments", t, func() {
|
|
|
|
prog := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
LET doc = { foo: { bar: "baz" } }
|
|
|
|
|
|
|
|
RETURN doc.@attr.@subattr
|
|
|
|
`)
|
|
|
|
|
|
|
|
out := prog.MustRun(
|
|
|
|
context.Background(),
|
|
|
|
runtime.WithParam("attr", "foo"),
|
|
|
|
runtime.WithParam("subattr", "bar"),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, `"baz"`)
|
2019-10-11 23:31:23 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should return an error if param values are not passed", t, func() {
|
|
|
|
prog := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
LET doc = { foo: { bar: "baz" } }
|
|
|
|
|
|
|
|
RETURN doc.@attr.@subattr
|
|
|
|
`)
|
|
|
|
|
|
|
|
_, err := prog.Run(
|
|
|
|
context.Background(),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(err, ShouldNotBeNil)
|
2019-10-12 03:30:42 +02:00
|
|
|
So(err.Error(), ShouldContainSubstring, runtime.ErrMissedParam.Error())
|
2019-10-11 23:31:23 +02:00
|
|
|
})
|
2019-10-12 03:30:42 +02:00
|
|
|
|
2019-10-11 23:31:23 +02:00
|
|
|
Convey("Should be possible to use in member expression as segments", t, func() {
|
|
|
|
prog := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
LET doc = { foo: { bar: "baz" } }
|
|
|
|
|
|
|
|
RETURN doc.@attr.@subattr
|
|
|
|
`)
|
|
|
|
|
|
|
|
_, err := prog.Run(
|
|
|
|
context.Background(),
|
|
|
|
runtime.WithParam("attr", "foo"),
|
|
|
|
)
|
2019-09-07 07:57:33 +02:00
|
|
|
|
2019-10-11 23:31:23 +02:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err.Error(), ShouldContainSubstring, "subattr")
|
2019-09-07 07:57:33 +02:00
|
|
|
})
|
2018-10-16 00:50:55 +02:00
|
|
|
}
|