github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/func_call_test.go (about)

     1  package expressions_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/expressions"
     9  	"github.com/MontFerret/ferret/pkg/runtime/expressions/literals"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values"
    11  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    12  
    13  	. "github.com/smartystreets/goconvey/convey"
    14  )
    15  
    16  func TestFunctionCallExpression(t *testing.T) {
    17  	Convey(".Exec", t, func() {
    18  		Convey("Should execute an underlying function without arguments", func() {
    19  			f, err := expressions.NewFunctionCallExpressionWith(
    20  				core.SourceMap{},
    21  				func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
    22  					So(args, ShouldHaveLength, 0)
    23  
    24  					return values.True, nil
    25  				},
    26  			)
    27  
    28  			So(err, ShouldBeNil)
    29  
    30  			rootScope, _ := core.NewRootScope()
    31  
    32  			out, err := f.Exec(context.Background(), rootScope.Fork())
    33  
    34  			So(err, ShouldBeNil)
    35  			So(out, ShouldEqual, values.True)
    36  		})
    37  
    38  		Convey("Should execute an underlying function with arguments", func() {
    39  			args := []core.Expression{
    40  				literals.NewIntLiteral(1),
    41  				literals.NewStringLiteral("foo"),
    42  			}
    43  
    44  			f, err := expressions.NewFunctionCallExpressionWith(
    45  				core.SourceMap{},
    46  				func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
    47  					So(args, ShouldHaveLength, len(args))
    48  
    49  					return values.True, nil
    50  				},
    51  				args...,
    52  			)
    53  
    54  			So(err, ShouldBeNil)
    55  
    56  			rootScope, _ := core.NewRootScope()
    57  
    58  			out, err := f.Exec(context.Background(), rootScope.Fork())
    59  
    60  			So(err, ShouldBeNil)
    61  			So(out, ShouldEqual, values.True)
    62  		})
    63  
    64  		Convey("Should stop an execution when context is cancelled", func() {
    65  			args := []core.Expression{
    66  				literals.NewIntLiteral(1),
    67  				literals.NewStringLiteral("foo"),
    68  			}
    69  
    70  			f, err := expressions.NewFunctionCallExpressionWith(
    71  				core.SourceMap{},
    72  				func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
    73  					So(args, ShouldHaveLength, len(args))
    74  
    75  					return values.True, nil
    76  				},
    77  				args...,
    78  			)
    79  
    80  			So(err, ShouldBeNil)
    81  
    82  			rootScope, _ := core.NewRootScope()
    83  			ctx, cancel := context.WithCancel(context.Background())
    84  			cancel()
    85  
    86  			_, err = f.Exec(ctx, rootScope.Fork())
    87  
    88  			So(err, ShouldEqual, core.ErrTerminated)
    89  		})
    90  
    91  		Convey("Should ignore errors and return NONE", func() {
    92  			f, err := expressions.NewFunctionCallExpressionWith(
    93  				core.SourceMap{},
    94  				func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
    95  					return values.NewString("booo"), core.ErrNotImplemented
    96  				},
    97  			)
    98  
    99  			So(err, ShouldBeNil)
   100  
   101  			fse, err := expressions.SuppressErrors(f)
   102  
   103  			So(err, ShouldBeNil)
   104  
   105  			out, err := fse.Exec(context.Background(), rootScope.Fork())
   106  
   107  			So(err, ShouldBeNil)
   108  			So(out.Type().String(), ShouldEqual, types.None.String())
   109  		})
   110  	})
   111  }