1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-19 21:28:32 +02:00
ferret/pkg/runtime/expressions/func_call_test.go
Tim Voronov 5f361e9e1b
Added support of optional ignoring of errors to function calls (#652)
* Added support of optional ignoring of errors to function calls

* Added support of handling of source failure to optional chaining

* Updated unit tests
2021-09-06 22:21:20 -04:00

110 lines
2.7 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"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"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
},
false,
)
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
},
false,
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
},
false,
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)
})
Convey("Should ignore errors and return NONE", func() {
f, err := expressions.NewFunctionCallExpression(
core.SourceMap{},
func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
return values.NewString("booo"), core.ErrNotImplemented
},
true,
)
So(err, ShouldBeNil)
out, err := f.Exec(context.Background(), rootScope.Fork())
So(err, ShouldBeNil)
So(out.Type().String(), ShouldEqual, types.None.String())
})
})
}