1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/expressions/block_test.go
Tim Voronov 5620be211c
Next (#214)
* Renamed DOCUMENT to PAGE

* Added PageLoadParams

* Added PageLoadParams

* Renamed LoadPageParams -> PageLoadParams

* Added support for context.Done() (#201)

* Bug/#189 operators precedence (#202)

* Fixed math operators precedence

* Fixed logical operators precedence

* Fixed array operator

* Added support for parentheses to enforce a different operator evaluation order

* Feature/#200 drivers (#209)

* Added new interfaces

* Renamed dynamic to cdp driver

* Renamed drivers

* Added ELEMENT_EXISTS function (#210)

* Renamed back PAGE to DOCUMENT (#211)

* Added Getter and Setter interfaces
2018-12-21 23:14:41 -05:00

159 lines
4.3 KiB
Go

package expressions_test
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/collections"
"testing"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/expressions"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
)
type IterableFn func(ctx context.Context, scope *core.Scope) (collections.Iterator, error)
func (f IterableFn) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
return f(ctx, scope)
}
type ExpressionFn func(ctx context.Context, scope *core.Scope) (core.Value, error)
func (f ExpressionFn) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
return f(ctx, scope)
}
func TestBlock(t *testing.T) {
newExp := func(values []core.Value) (*expressions.BlockExpression, error) {
iter, err := collections.NewDefaultSliceIterator(values)
if err != nil {
return nil, err
}
return expressions.NewBlockExpression(IterableFn(func(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
return iter, nil
}))
}
Convey("Should create a block expression", t, func() {
s, err := newExp(make([]core.Value, 0, 10))
So(err, ShouldBeNil)
So(s, ShouldHaveSameTypeAs, &expressions.BlockExpression{})
})
Convey("Should add a new expression of a default type", t, func() {
s, _ := newExp(make([]core.Value, 0, 10))
sourceMap := core.NewSourceMap("test", 1, 1)
exp, err := expressions.NewVariableExpression(sourceMap, "testExp")
So(err, ShouldBeNil)
s.Add(exp)
})
Convey("Should exec a block expression", t, func() {
s, _ := newExp(make([]core.Value, 0, 10))
sourceMap := core.NewSourceMap("test", 1, 1)
exp, err := expressions.NewVariableDeclarationExpression(sourceMap, "test", ExpressionFn(func(ctx context.Context, scope *core.Scope) (core.Value, error) {
return values.NewString("value"), nil
}))
So(err, ShouldBeNil)
s.Add(exp)
rootScope, _ := core.NewRootScope()
scope := rootScope.Fork()
_, err = s.Exec(context.Background(), scope)
So(err, ShouldBeNil)
val, err := scope.GetVariable("test")
So(err, ShouldBeNil)
So(val, ShouldEqual, "value")
})
Convey("Should not exec a nil block expression", t, func() {
s, _ := newExp(make([]core.Value, 0, 10))
sourceMap := core.NewSourceMap("test", 1, 1)
exp, err := expressions.NewVariableExpression(sourceMap, "test")
So(err, ShouldBeNil)
s.Add(exp)
So(err, ShouldBeNil)
rootScope, fn := core.NewRootScope()
scope := rootScope.Fork()
scope.SetVariable("test", values.NewString("value"))
fn()
value, err := s.Exec(context.Background(), scope)
So(err, ShouldBeNil)
So(value, ShouldHaveSameTypeAs, values.None)
})
Convey("Should return an iterator", t, func() {
s, _ := newExp([]core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
})
sourceMap := core.NewSourceMap("test", 1, 1)
exp, _ := expressions.NewVariableExpression(sourceMap, "test")
s.Add(exp)
rootScope, _ := core.NewRootScope()
scope := rootScope.Fork()
scope.SetVariable("test", values.NewString("value"))
iter, err := s.Iterate(context.Background(), scope)
So(err, ShouldBeNil)
items, err := collections.ToSlice(context.Background(), scope, iter)
So(err, ShouldBeNil)
So(items, ShouldHaveLength, 3)
})
Convey("Should stop an execution when context is cancelled", t, func() {
s, _ := newExp(make([]core.Value, 0, 10))
sourceMap := core.NewSourceMap("test", 1, 1)
exp, _ := expressions.NewVariableExpression(sourceMap, "test")
s.Add(exp)
rootScope, _ := core.NewRootScope()
scope := rootScope.Fork()
scope.SetVariable("test", values.NewString("value"))
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := s.Exec(ctx, scope)
So(err, ShouldEqual, core.ErrTerminated)
})
Convey("Should stop an execution when context is cancelled 2", t, func() {
s, _ := newExp([]core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
})
sourceMap := core.NewSourceMap("test", 1, 1)
exp, _ := expressions.NewVariableExpression(sourceMap, "test")
s.Add(exp)
rootScope, _ := core.NewRootScope()
scope := rootScope.Fork()
scope.SetVariable("test", values.NewString("value"))
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := s.Iterate(ctx, scope)
So(err, ShouldEqual, core.ErrTerminated)
})
}