|
| 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/values" |
| 10 | + . "github.com/smartystreets/goconvey/convey" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNewParameterExpression(t *testing.T) { |
| 14 | + Convey("Should create a parameter expression", t, func() { |
| 15 | + sourceMap := core.NewSourceMap("test", 1, 1) |
| 16 | + s, err := expressions.NewParameterExpression(sourceMap, "test") |
| 17 | + |
| 18 | + So(err, ShouldBeNil) |
| 19 | + So(s, ShouldHaveSameTypeAs, &expressions.ParameterExpression{}) |
| 20 | + }) |
| 21 | + |
| 22 | + Convey("Should not create a parameter expression with empty name", t, func() { |
| 23 | + sourceMap := core.NewSourceMap("test", 1, 1) |
| 24 | + s, err := expressions.NewParameterExpression(sourceMap, "") |
| 25 | + |
| 26 | + So(err, ShouldNotBeNil) |
| 27 | + So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument) |
| 28 | + So(s, ShouldBeNil) |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +func TestParameterExpressionExec(t *testing.T) { |
| 33 | + Convey("Should exec an existing parameter expression", t, func() { |
| 34 | + sourceMap := core.NewSourceMap("test", 1, 10) |
| 35 | + existExp, err := expressions.NewParameterExpression(sourceMap, "param1") |
| 36 | + |
| 37 | + params := make(map[string]core.Value) |
| 38 | + params["param1"] = values.NewInt(1) |
| 39 | + |
| 40 | + ctx := core.ParamsWith(context.Background(), params) |
| 41 | + value, err := existExp.Exec(ctx, &core.Scope{}) |
| 42 | + |
| 43 | + So(err, ShouldBeNil) |
| 44 | + So(value.Type(), ShouldEqual, core.IntType) |
| 45 | + So(value.String(), ShouldEqual, "1") |
| 46 | + }) |
| 47 | + |
| 48 | + Convey("Should not exec a missing parameter expression", t, func() { |
| 49 | + sourceMap := core.NewSourceMap("test", 1, 10) |
| 50 | + notExistExp, err := expressions.NewParameterExpression(sourceMap, "param2") |
| 51 | + |
| 52 | + params := make(map[string]core.Value) |
| 53 | + params["param1"] = values.NewInt(1) |
| 54 | + |
| 55 | + ctx := core.ParamsWith(context.Background(), params) |
| 56 | + value, err := notExistExp.Exec(ctx, &core.Scope{}) |
| 57 | + |
| 58 | + So(err, ShouldNotBeNil) |
| 59 | + So(err, ShouldHaveSameTypeAs, core.ErrNotFound) |
| 60 | + So(value.Type(), ShouldEqual, core.NoneType) |
| 61 | + }) |
| 62 | +} |
0 commit comments