From c56afd4fc9f29b66e942103f365e948f1bb6b623 Mon Sep 17 00:00:00 2001 From: ConaGo Date: Tue, 6 Jul 2021 10:42:53 +0200 Subject: [PATCH 1/3] added tests for runtime/expressions/variable --- pkg/runtime/expressions/variable_test.go | 69 ++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 pkg/runtime/expressions/variable_test.go diff --git a/pkg/runtime/expressions/variable_test.go b/pkg/runtime/expressions/variable_test.go new file mode 100644 index 00000000..5fd771f7 --- /dev/null +++ b/pkg/runtime/expressions/variable_test.go @@ -0,0 +1,69 @@ +package expressions_test + +import ( + "context" + "testing" + + . "github.com/smartystreets/goconvey/convey" + + "github.com/MontFerret/ferret/pkg/runtime/core" + "github.com/MontFerret/ferret/pkg/runtime/expressions" + "github.com/MontFerret/ferret/pkg/runtime/values" +) +var sourceMap = core.NewSourceMap("hello", 2, 3) +var rootScope, _ = core.NewRootScope() +var _ = rootScope.SetVariable("key", values.NewString("value")) + +func TestNewVariableExpression(t *testing.T) { + + Convey("Should not throw error and create a VariableExpression given correct arguments", t, func() { + ret, err := expressions.NewVariableExpression(sourceMap, "foo") + + So(ret, ShouldHaveSameTypeAs, &expressions.VariableExpression{}) + So(err, ShouldBeNil) + }) + + Convey("Should throw error when given no name argument", t, func() { + _, err := expressions.NewVariableExpression(sourceMap, "") + + So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument) + }) + + Convey("Calling .Exec should return the correct variable set in the given scope", t, func() { + ret, _ := expressions.NewVariableExpression(sourceMap, "key") + value, err := ret.Exec(context.TODO(), rootScope) + + So(value, ShouldEqual, "value") + So(err, ShouldBeNil) + }) +} + +func TestNewVariableDeclarationExpression(t *testing.T) { + Convey("Should not throw error and create a NewVariableDeclarationExpression given correct arguments", t, func() { + variableExpression, _ := expressions.NewVariableExpression(sourceMap, "foo") + ret, err := expressions.NewVariableDeclarationExpression(sourceMap, "test", variableExpression) + + So(ret, ShouldHaveSameTypeAs, &expressions.VariableDeclarationExpression{}) + So(err, ShouldBeNil) + }) + + Convey("Should throw error if init argument is nil", t, func() { + _, err := expressions.NewVariableDeclarationExpression(sourceMap, "test", nil) + + So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument) + }) + + Convey("Calling .Exec should add the value retrieved by its VariableExpression with its own name as key to the given scope", t, func() { + variableExpression, _ := expressions.NewVariableExpression(sourceMap, "key") + variableDeclarationExpression, _ := expressions.NewVariableDeclarationExpression(sourceMap, "keyTwo", variableExpression) + _, err := variableDeclarationExpression.Exec(context.TODO(), rootScope) + + So(err, ShouldBeNil) + + value, _ := rootScope.GetVariable("key") + value2, _ := rootScope.GetVariable("keyTwo") + + So(value, ShouldEqual, "value") + So(value2, ShouldEqual, "value") + }) +} \ No newline at end of file From 1c1b6e957e9e3be92fddad98e8182a2443f1a442 Mon Sep 17 00:00:00 2001 From: Lennart Jasper Date: Fri, 9 Jul 2021 11:11:40 +0200 Subject: [PATCH 2/3] added newline to statisfy the linter --- pkg/runtime/expressions/variable_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/runtime/expressions/variable_test.go b/pkg/runtime/expressions/variable_test.go index 5fd771f7..57d53507 100644 --- a/pkg/runtime/expressions/variable_test.go +++ b/pkg/runtime/expressions/variable_test.go @@ -66,4 +66,4 @@ func TestNewVariableDeclarationExpression(t *testing.T) { So(value, ShouldEqual, "value") So(value2, ShouldEqual, "value") }) -} \ No newline at end of file +} From fe35614c343b32d8ed90db2d53a39631e8df9329 Mon Sep 17 00:00:00 2001 From: Lennart Jasper Date: Sat, 10 Jul 2021 07:01:50 +0200 Subject: [PATCH 3/3] added tests for range --- .../expressions/operators/range_test.go | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 pkg/runtime/expressions/operators/range_test.go diff --git a/pkg/runtime/expressions/operators/range_test.go b/pkg/runtime/expressions/operators/range_test.go new file mode 100644 index 00000000..f69e8d89 --- /dev/null +++ b/pkg/runtime/expressions/operators/range_test.go @@ -0,0 +1,107 @@ +package operators_test + +import ( + "context" + "errors" + "testing" + + "github.com/MontFerret/ferret/pkg/runtime/core" + "github.com/MontFerret/ferret/pkg/runtime/expressions/operators" + "github.com/MontFerret/ferret/pkg/runtime/values" + . "github.com/smartystreets/goconvey/convey" +) + +type MockExpressionInt int + +func (e MockExpressionInt) Exec(_ context.Context, _ *core.Scope) (core.Value, error) { + return values.NewInt(int(e)), nil +} + +type MockExpressionFloat float64 + +func (e MockExpressionFloat) Exec(_ context.Context, _ *core.Scope) (core.Value, error) { + return values.NewFloat(float64(e)), nil +} + +type MockExpressionString string + +func (e MockExpressionString) Exec(_ context.Context, _ *core.Scope) (core.Value, error) { + return values.NewString(string(e)), nil +} + +type FailingMockExpression int + +func (e FailingMockExpression) Exec(_ context.Context, _ *core.Scope) (core.Value, error) { + return nil, errors.New("MockError") +} + +var sourceMap = core.NewSourceMap("hello", 2, 3) +var rootScope, _ = core.NewRootScope() + +func TestRangeOperator(t *testing.T) { + var correctEx MockExpressionInt = 10 + var failEx FailingMockExpression = 0 + Convey("NewRangeOperator", t, func() { + Convey("Should return *RangeOperator, given correct arguments", func() { + res, _ := operators.NewRangeOperator(sourceMap, correctEx, correctEx) + So(res, ShouldHaveSameTypeAs, &operators.RangeOperator{}) + }) + Convey("Should return error if no left operator is given", func() { + _, err := operators.NewRangeOperator(sourceMap, nil, correctEx) + So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument) + }) + Convey("Should return error if no right operator is given", func() { + _, err := operators.NewRangeOperator(sourceMap, correctEx, nil) + So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument) + }) + }) + Convey(".Exec", t, func() { + Convey("Should return error if left expression fails", func() { + rangeOp, _ := operators.NewRangeOperator(sourceMap, failEx, correctEx) + _, err := rangeOp.Exec(context.TODO(), rootScope) + So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{}) + }) + Convey("Should return error if right expression fails", func() { + rangeOp, _ := operators.NewRangeOperator(sourceMap, correctEx, failEx) + _, err := rangeOp.Exec(context.TODO(), rootScope) + So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{}) + }) + }) + Convey(".Eval", t, func() { + var stringEx MockExpressionString = "noInt/noFloat" + /* var intExZero MockExpressionInt = 0 + var intExTwenty MockExpressionInt = 20 + var floatExNegative MockExpressionFloat = -2.2 + var floatExPositive MockExpressionFloat = 12.4 */ + Convey("Should return error if given non int/float left Expression", func() { + rangeOp, _ := operators.NewRangeOperator(sourceMap, stringEx, correctEx) + _, err := rangeOp.Exec(context.TODO(), rootScope) + So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{}) + }) + Convey("Should return error if given non int/float right Expression", func() { + rangeOp, _ := operators.NewRangeOperator(sourceMap, correctEx, stringEx) + _, err := rangeOp.Exec(context.TODO(), rootScope) + So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{}) + }) + Convey("Should return Array with range [start,...end] given ints", func() { + var start MockExpressionInt = 1 + var end MockExpressionInt = 14 + + rangeOp, _ := operators.NewRangeOperator(sourceMap, start, end) + arr, err := rangeOp.Exec(context.TODO(), rootScope) + So(err, ShouldHaveSameTypeAs, nil) + So(arr, ShouldHaveSameTypeAs, &values.Array{}) + So(arr.String(), ShouldEqual, "[1,2,3,4,5,6,7,8,9,10,11,12,13,14]") + }) + Convey("Should return Array with range [start,...end] given floats", func() { + var start MockExpressionFloat = -2.2 + var end MockExpressionFloat = 12.4 + + rangeOp, _ := operators.NewRangeOperator(sourceMap, start, end) + arr, err := rangeOp.Exec(context.TODO(), rootScope) + So(err, ShouldHaveSameTypeAs, nil) + So(arr, ShouldHaveSameTypeAs, &values.Array{}) + So(arr.String(), ShouldEqual, "[-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12]") + }) + }) +}