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

     1  package expressions_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
     8  
     9  	. "github.com/smartystreets/goconvey/convey"
    10  
    11  	"github.com/MontFerret/ferret/pkg/runtime/core"
    12  	"github.com/MontFerret/ferret/pkg/runtime/expressions"
    13  	"github.com/MontFerret/ferret/pkg/runtime/values"
    14  )
    15  
    16  func TestNewParameterExpression(t *testing.T) {
    17  	Convey("Should create a parameter expression", t, func() {
    18  		sourceMap := core.NewSourceMap("test", 1, 1)
    19  		s, err := expressions.NewParameterExpression(sourceMap, "test")
    20  
    21  		So(err, ShouldBeNil)
    22  		So(s, ShouldHaveSameTypeAs, &expressions.ParameterExpression{})
    23  	})
    24  
    25  	Convey("Should not create a parameter expression with empty name", t, func() {
    26  		sourceMap := core.NewSourceMap("test", 1, 1)
    27  		s, err := expressions.NewParameterExpression(sourceMap, "")
    28  
    29  		So(err, ShouldNotBeNil)
    30  		So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument)
    31  		So(s, ShouldBeNil)
    32  	})
    33  }
    34  
    35  func TestParameterExpressionExec(t *testing.T) {
    36  	Convey("Should exec an existing parameter expression", t, func() {
    37  		sourceMap := core.NewSourceMap("test", 1, 10)
    38  		existExp, err := expressions.NewParameterExpression(sourceMap, "param1")
    39  
    40  		So(err, ShouldBeNil)
    41  
    42  		params := make(map[string]core.Value)
    43  		params["param1"] = values.NewInt(1)
    44  
    45  		ctx := core.ParamsWith(context.Background(), params)
    46  		value, err := existExp.Exec(ctx, &core.Scope{})
    47  
    48  		So(err, ShouldBeNil)
    49  		So(value.Type().Equals(types.Int), ShouldBeTrue)
    50  		So(value.String(), ShouldEqual, "1")
    51  	})
    52  
    53  	Convey("Should not exec a missing parameter expression", t, func() {
    54  		sourceMap := core.NewSourceMap("test", 1, 10)
    55  		notExistExp, err := expressions.NewParameterExpression(sourceMap, "param2")
    56  		So(err, ShouldBeNil)
    57  
    58  		params := make(map[string]core.Value)
    59  		params["param1"] = values.NewInt(1)
    60  
    61  		ctx := core.ParamsWith(context.Background(), params)
    62  		value, err := notExistExp.Exec(ctx, &core.Scope{})
    63  
    64  		So(err, ShouldNotBeNil)
    65  		So(err.(*core.SourceErrorDetail).BaseError, ShouldHaveSameTypeAs, core.ErrNotFound)
    66  		So(value.Type().Equals(types.None), ShouldBeTrue)
    67  	})
    68  }