1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/expressions/func_call_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

90 lines
2.1 KiB
Go

package expressions_test
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/expressions"
"github.com/MontFerret/ferret/pkg/runtime/expressions/literals"
"github.com/MontFerret/ferret/pkg/runtime/values"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestFunctionCallExpression(t *testing.T) {
Convey(".Exec", t, func() {
Convey("Should execute an underlying function without arguments", func() {
f, err := expressions.NewFunctionCallExpression(
core.SourceMap{},
func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
So(args, ShouldHaveLength, 0)
return values.True, nil
},
)
So(err, ShouldBeNil)
rootScope, _ := core.NewRootScope()
out, err := f.Exec(context.Background(), rootScope.Fork())
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("Should execute an underlying function with arguments", func() {
args := []core.Expression{
literals.NewIntLiteral(1),
literals.NewStringLiteral("foo"),
}
f, err := expressions.NewFunctionCallExpression(
core.SourceMap{},
func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
So(args, ShouldHaveLength, len(args))
return values.True, nil
},
args...,
)
So(err, ShouldBeNil)
rootScope, _ := core.NewRootScope()
out, err := f.Exec(context.Background(), rootScope.Fork())
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("Should stop an execution when context is cancelled", func() {
args := []core.Expression{
literals.NewIntLiteral(1),
literals.NewStringLiteral("foo"),
}
f, err := expressions.NewFunctionCallExpression(
core.SourceMap{},
func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
So(args, ShouldHaveLength, len(args))
return values.True, nil
},
args...,
)
So(err, ShouldBeNil)
rootScope, _ := core.NewRootScope()
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = f.Exec(ctx, rootScope.Fork())
So(err, ShouldEqual, core.ErrTerminated)
})
})
}