From 70b083c6d27ce2496e16f71b6674628488eee124 Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Mon, 14 Aug 2023 02:43:42 +0900 Subject: [PATCH 1/8] Add unit tests for for.go --- pkg/runtime/expressions/for_test.go | 533 ++++++++++++++++++++++++++++ 1 file changed, 533 insertions(+) create mode 100644 pkg/runtime/expressions/for_test.go diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go new file mode 100644 index 00000000..ba246b4f --- /dev/null +++ b/pkg/runtime/expressions/for_test.go @@ -0,0 +1,533 @@ +package expressions + +import ( + "context" + "testing" + + "github.com/MontFerret/ferret/pkg/runtime/collections" + "github.com/MontFerret/ferret/pkg/runtime/core" + "github.com/MontFerret/ferret/pkg/runtime/expressions/clauses" + "github.com/MontFerret/ferret/pkg/runtime/expressions/literals" + "github.com/MontFerret/ferret/pkg/runtime/expressions/operators" + "github.com/MontFerret/ferret/pkg/runtime/values" + . "github.com/smartystreets/goconvey/convey" +) + +func testIntArrayLiteral() (collections.Iterable, string) { + dataSource, _ := NewForInIterableExpression( + core.SourceMap{}, + "val", + "", + literals.NewArrayLiteralWith([]core.Expression{ + literals.NewIntLiteral(0), + literals.NewIntLiteral(1), + literals.NewIntLiteral(2), + literals.NewIntLiteral(3), + literals.NewIntLiteral(4), + }), + ) + + return dataSource, "val" +} + +func testErrorArrayliteral() (collections.Iterable, string) { + dataSource, _ := NewForInIterableExpression( + core.SourceMap{}, + "val", + "", + literals.NewIntLiteral(1), // + ) + + return dataSource, "val" +} + +func testElementErrorArrayliteral() (collections.Iterable, string) { + errorEle, _ := NewVariableExpression(core.SourceMap{}, "a") + dataSource, _ := NewForInIterableExpression( + core.SourceMap{}, + "val", + "", + literals.NewArrayLiteralWith([]core.Expression{ + literals.NewIntLiteral(0), + literals.NewIntLiteral(1), + literals.NewIntLiteral(2), + errorEle, + }), + ) + + return dataSource, "val" +} + +func TestNewForExpression(t *testing.T) { + dataSource, _ := testIntArrayLiteral() + returnExp, _ := NewVariableExpression(core.SourceMap{}, "testExp") + + Convey("NewForExpression", t, func() { + Convey("should return new ForExPresssion.", func() { + forExp, err := NewForExpression(core.SourceMap{}, dataSource, returnExp, false, false, false) + So(forExp, ShouldNotBeNil) + So(err, ShouldBeNil) + }) + Convey("should return core.ErrMissedArgument.", func() { + forExp, err := NewForExpression(core.SourceMap{}, nil, returnExp, false, false, false) + So(forExp, ShouldBeNil) + So(err, ShouldNotBeNil) + So(err, ShouldEqual, err) + + forExp, err = NewForExpression(core.SourceMap{}, dataSource, nil, false, false, false) + So(forExp, ShouldBeNil) + So(err, ShouldNotBeNil) + So(err, ShouldEqual, err) + }) + }) +} + +func TestAddLimit(t *testing.T) { + dataSource, valName := testIntArrayLiteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + Convey("AddLimit", t, func() { + Convey("should success.", func() { + rootScope, closeFn := core.NewRootScope() + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + err := forExp.AddLimit(core.SourceMap{}, literals.NewIntLiteral(3), literals.NewIntLiteral(0)) + So(err, ShouldBeNil) + + resultVal, err := forExp.Exec(context.Background(), rootScope) + resultArr, ok := resultVal.(*values.Array) + So(ok, ShouldBeTrue) + So(resultArr.Length(), ShouldEqual, values.NewInt(3)) + So(err, ShouldBeNil) + + closeFn() + }) + + Convey("should return a emptyData error.", func() { + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + forExp.dataSource = nil + err := forExp.AddLimit(core.SourceMap{}, literals.NewIntLiteral(3), literals.NewIntLiteral(0)) + So(err, ShouldNotBeNil) + }) + }) +} + +func TestAddFilter(t *testing.T) { + dataSource, valName := testIntArrayLiteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + testFilter := func() core.Expression { + filterLeftExp, _ := NewVariableExpression(core.SourceMap{}, valName) + filterRightExp := literals.NewIntLiteral(3) + filterExp, _ := operators.NewEqualityOperator(core.SourceMap{}, filterLeftExp, filterRightExp, "<") + return filterExp + } + + Convey("AddFilter", t, func() { + Convey("should success.", func() { + rootScope, closeFn := core.NewRootScope() + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + + err := forExp.AddFilter(core.SourceMap{}, testFilter()) + So(err, ShouldBeNil) + + resultVal, err := forExp.Exec(context.Background(), rootScope) + resultArr, ok := resultVal.(*values.Array) + So(ok, ShouldBeTrue) + So(resultArr.Length(), ShouldEqual, values.NewInt(3)) + compareArr := values.NewArrayOf([]core.Value{ + values.NewInt(0), + values.NewInt(1), + values.NewInt(2), + }) + So(resultArr.Compare(compareArr), ShouldEqual, 0) + So(err, ShouldBeNil) + + closeFn() + }) + + Convey("should return a error.", func() { + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + forExp.dataSource = nil + err := forExp.AddFilter(core.SourceMap{}, testFilter()) + So(err, ShouldNotBeNil) + }) + }) + +} + +func TestAddSort(t *testing.T) { + dataSource, valName := testIntArrayLiteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + testSort := func() *clauses.SorterExpression { + valExp, _ := NewVariableExpression(core.SourceMap{}, valName) + sortExp, _ := clauses.NewSorterExpression(valExp, collections.SortDirectionDesc) + return sortExp + } + + Convey("AddSort", t, func() { + Convey("should success.", func() { + rootScope, closeFn := core.NewRootScope() + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + + err := forExp.AddSort(core.SourceMap{}, testSort()) + So(err, ShouldBeNil) + + resultVal, err := forExp.Exec(context.Background(), rootScope) + So(err, ShouldBeNil) + resultArr, ok := resultVal.(*values.Array) + So(ok, ShouldBeTrue) + So(resultArr.Length(), ShouldEqual, values.NewInt(5)) + compareArr := values.NewArrayOf([]core.Value{ + values.NewInt(4), + values.NewInt(3), + values.NewInt(2), + values.NewInt(1), + values.NewInt(0), + }) + So(resultArr.Compare(compareArr), ShouldEqual, 0) + + closeFn() + }) + + Convey("should return a error.", func() { + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + forExp.dataSource = nil + err := forExp.AddSort(core.SourceMap{}, testSort()) + So(err, ShouldNotBeNil) + }) + }) +} + +func TestAddCollect(t *testing.T) { + dataSource, valName := testIntArrayLiteral() + + selectorValName := "selector" + + testCollect := func() *clauses.Collect { + eleVal, _ := NewVariableExpression(core.SourceMap{}, valName) + selector, _ := clauses.NewCollectSelector(selectorValName, eleVal) + collect, _ := clauses.NewCollect( + []*clauses.CollectSelector{selector}, nil, nil, nil, + ) + + return collect + } + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, selectorValName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + Convey("AddCollect", t, func() { + Convey("should success.", func() { + rootScope, closeFn := core.NewRootScope() + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + + err := forExp.AddCollect(core.SourceMap{}, testCollect()) + So(err, ShouldBeNil) + + resultVal, err := forExp.Exec(context.Background(), rootScope) + resultArr, ok := resultVal.(*values.Array) + So(err, ShouldBeNil) + So(ok, ShouldBeTrue) + So(resultArr.Length(), ShouldEqual, values.NewInt(5)) + compareArr := values.NewArrayOf([]core.Value{ + values.NewInt(0), + values.NewInt(1), + values.NewInt(2), + values.NewInt(3), + values.NewInt(4), + }) + So(resultArr.Compare(compareArr), ShouldEqual, 0) + So(err, ShouldBeNil) + + closeFn() + }) + + Convey("should return a error.", func() { + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + forExp.dataSource = nil + err := forExp.AddCollect(core.SourceMap{}, testCollect()) + So(err, ShouldNotBeNil) + }) + }) +} + +func TestAddStatement(t *testing.T) { + dataSource, valName := testIntArrayLiteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + testStatement, _ := NewVariableDeclarationExpression(core.SourceMap{}, "newVal", literals.NewIntLiteral(0)) + + Convey("AddStatement", t, func() { + Convey("should success.", func() { + rootScope, closeFn := core.NewRootScope() + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + + err := forExp.AddStatement(testStatement) + So(err, ShouldBeNil) + + resultVal, err := forExp.Exec(context.Background(), rootScope) + So(err, ShouldBeNil) + resultArr, ok := resultVal.(*values.Array) + So(ok, ShouldBeTrue) + So(resultArr.Length(), ShouldEqual, values.NewInt(5)) + compareArr := values.NewArrayOf([]core.Value{ + values.NewInt(0), + values.NewInt(1), + values.NewInt(2), + values.NewInt(3), + values.NewInt(4), + }) + So(resultArr.Compare(compareArr), ShouldEqual, 0) + + closeFn() + }) + + Convey("should return a error.", func() { + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + forExp.dataSource = nil + err := forExp.AddStatement(testStatement) + So(err, ShouldNotBeNil) + }) + }) +} + +func TestExec(t *testing.T) { + Convey("Exec", t, func() { + Convey("should success.", func() { + dataSource, valName := testIntArrayLiteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + rootScope, closeFn := core.NewRootScope() + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + + resultVal, err := forExp.Exec(context.Background(), rootScope) + So(err, ShouldBeNil) + resultArr, ok := resultVal.(*values.Array) + So(ok, ShouldBeTrue) + So(resultArr.Length(), ShouldEqual, values.NewInt(5)) + compareArr := values.NewArrayOf([]core.Value{ + values.NewInt(0), + values.NewInt(1), + values.NewInt(2), + values.NewInt(3), + values.NewInt(4), + }) + So(resultArr.Compare(compareArr), ShouldEqual, 0) + + closeFn() + }) + + Convey("should return contextdone error.", func() { + dataSource, valName := testIntArrayLiteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + rootScope, closeFn := core.NewRootScope() + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + + ctx0 := context.Background() + ctx1, cancelFn := context.WithCancel(ctx0) + cancelFn() + + _, err := forExp.Exec(ctx1, rootScope) + So(err, ShouldNotBeNil) + + closeFn() + }) + + Convey("should return a emptyData error.", func() { + rootScope, closeFn := core.NewRootScope() + errorDataSource, valName := testErrorArrayliteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + forExp, _ := NewForExpression( + core.SourceMap{}, + errorDataSource, + returnExp, + false, + false, + false, + ) + + result, err := forExp.Exec(context.Background(), rootScope) + So(result, ShouldEqual, values.None) + So(err, ShouldNotBeNil) + + closeFn() + }) + + Convey("should return a elementData error.", func() { + rootScope, closeFn := core.NewRootScope() + errorDataSource, valName := testElementErrorArrayliteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + forExp, _ := NewForExpression( + core.SourceMap{}, + errorDataSource, + returnExp, + false, + false, + false, + ) + + result, err := forExp.Exec(context.Background(), rootScope) + So(result, ShouldEqual, values.None) + So(err, ShouldNotBeNil) + + closeFn() + }) + + Convey("should return a errorreturn error.", func() { + rootScope, closeFn := core.NewRootScope() + dataSource, _ := testIntArrayLiteral() + + returnedValExp, _ := NewVariableExpression(core.SourceMap{}, "notExistVal") + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnedValExp, + ) + + forExp, _ := NewForExpression( + core.SourceMap{}, + dataSource, + returnExp, + false, + false, + false, + ) + + result, err := forExp.Exec(context.Background(), rootScope) + So(result, ShouldEqual, values.None) + So(err, ShouldNotBeNil) + + closeFn() + }) + }) +} From aadfa073cbe75f2bf9154dbd1e0fe205c623cdde Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Thu, 24 Aug 2023 01:52:13 +0900 Subject: [PATCH 2/8] Remove needless codes. Deleted codes are results of test of Exec results after addXXX methods These should be written in XXXClause test. --- pkg/runtime/expressions/for_test.go | 78 +---------------------------- 1 file changed, 1 insertion(+), 77 deletions(-) diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go index ba246b4f..c09c372e 100644 --- a/pkg/runtime/expressions/for_test.go +++ b/pkg/runtime/expressions/for_test.go @@ -35,7 +35,7 @@ func testErrorArrayliteral() (collections.Iterable, string) { core.SourceMap{}, "val", "", - literals.NewIntLiteral(1), // + literals.NewIntLiteral(1), ) return dataSource, "val" @@ -93,7 +93,6 @@ func TestAddLimit(t *testing.T) { Convey("AddLimit", t, func() { Convey("should success.", func() { - rootScope, closeFn := core.NewRootScope() forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -104,14 +103,6 @@ func TestAddLimit(t *testing.T) { ) err := forExp.AddLimit(core.SourceMap{}, literals.NewIntLiteral(3), literals.NewIntLiteral(0)) So(err, ShouldBeNil) - - resultVal, err := forExp.Exec(context.Background(), rootScope) - resultArr, ok := resultVal.(*values.Array) - So(ok, ShouldBeTrue) - So(resultArr.Length(), ShouldEqual, values.NewInt(3)) - So(err, ShouldBeNil) - - closeFn() }) Convey("should return a emptyData error.", func() { @@ -148,7 +139,6 @@ func TestAddFilter(t *testing.T) { Convey("AddFilter", t, func() { Convey("should success.", func() { - rootScope, closeFn := core.NewRootScope() forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -160,20 +150,6 @@ func TestAddFilter(t *testing.T) { err := forExp.AddFilter(core.SourceMap{}, testFilter()) So(err, ShouldBeNil) - - resultVal, err := forExp.Exec(context.Background(), rootScope) - resultArr, ok := resultVal.(*values.Array) - So(ok, ShouldBeTrue) - So(resultArr.Length(), ShouldEqual, values.NewInt(3)) - compareArr := values.NewArrayOf([]core.Value{ - values.NewInt(0), - values.NewInt(1), - values.NewInt(2), - }) - So(resultArr.Compare(compareArr), ShouldEqual, 0) - So(err, ShouldBeNil) - - closeFn() }) Convey("should return a error.", func() { @@ -210,7 +186,6 @@ func TestAddSort(t *testing.T) { Convey("AddSort", t, func() { Convey("should success.", func() { - rootScope, closeFn := core.NewRootScope() forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -222,22 +197,6 @@ func TestAddSort(t *testing.T) { err := forExp.AddSort(core.SourceMap{}, testSort()) So(err, ShouldBeNil) - - resultVal, err := forExp.Exec(context.Background(), rootScope) - So(err, ShouldBeNil) - resultArr, ok := resultVal.(*values.Array) - So(ok, ShouldBeTrue) - So(resultArr.Length(), ShouldEqual, values.NewInt(5)) - compareArr := values.NewArrayOf([]core.Value{ - values.NewInt(4), - values.NewInt(3), - values.NewInt(2), - values.NewInt(1), - values.NewInt(0), - }) - So(resultArr.Compare(compareArr), ShouldEqual, 0) - - closeFn() }) Convey("should return a error.", func() { @@ -279,7 +238,6 @@ func TestAddCollect(t *testing.T) { Convey("AddCollect", t, func() { Convey("should success.", func() { - rootScope, closeFn := core.NewRootScope() forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -291,23 +249,6 @@ func TestAddCollect(t *testing.T) { err := forExp.AddCollect(core.SourceMap{}, testCollect()) So(err, ShouldBeNil) - - resultVal, err := forExp.Exec(context.Background(), rootScope) - resultArr, ok := resultVal.(*values.Array) - So(err, ShouldBeNil) - So(ok, ShouldBeTrue) - So(resultArr.Length(), ShouldEqual, values.NewInt(5)) - compareArr := values.NewArrayOf([]core.Value{ - values.NewInt(0), - values.NewInt(1), - values.NewInt(2), - values.NewInt(3), - values.NewInt(4), - }) - So(resultArr.Compare(compareArr), ShouldEqual, 0) - So(err, ShouldBeNil) - - closeFn() }) Convey("should return a error.", func() { @@ -339,7 +280,6 @@ func TestAddStatement(t *testing.T) { Convey("AddStatement", t, func() { Convey("should success.", func() { - rootScope, closeFn := core.NewRootScope() forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -351,22 +291,6 @@ func TestAddStatement(t *testing.T) { err := forExp.AddStatement(testStatement) So(err, ShouldBeNil) - - resultVal, err := forExp.Exec(context.Background(), rootScope) - So(err, ShouldBeNil) - resultArr, ok := resultVal.(*values.Array) - So(ok, ShouldBeTrue) - So(resultArr.Length(), ShouldEqual, values.NewInt(5)) - compareArr := values.NewArrayOf([]core.Value{ - values.NewInt(0), - values.NewInt(1), - values.NewInt(2), - values.NewInt(3), - values.NewInt(4), - }) - So(resultArr.Compare(compareArr), ShouldEqual, 0) - - closeFn() }) Convey("should return a error.", func() { From 0e6b780290e5d5b27aaffdb4f3886c50d7936beb Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Thu, 24 Aug 2023 02:40:09 +0900 Subject: [PATCH 3/8] Fix test descriptions and clear up codes --- pkg/runtime/expressions/for_test.go | 134 ++++++++++++++-------------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go index c09c372e..344942fe 100644 --- a/pkg/runtime/expressions/for_test.go +++ b/pkg/runtime/expressions/for_test.go @@ -30,51 +30,26 @@ func testIntArrayLiteral() (collections.Iterable, string) { return dataSource, "val" } -func testErrorArrayliteral() (collections.Iterable, string) { - dataSource, _ := NewForInIterableExpression( - core.SourceMap{}, - "val", - "", - literals.NewIntLiteral(1), - ) - - return dataSource, "val" -} - -func testElementErrorArrayliteral() (collections.Iterable, string) { - errorEle, _ := NewVariableExpression(core.SourceMap{}, "a") - dataSource, _ := NewForInIterableExpression( - core.SourceMap{}, - "val", - "", - literals.NewArrayLiteralWith([]core.Expression{ - literals.NewIntLiteral(0), - literals.NewIntLiteral(1), - literals.NewIntLiteral(2), - errorEle, - }), - ) - - return dataSource, "val" -} - func TestNewForExpression(t *testing.T) { dataSource, _ := testIntArrayLiteral() returnExp, _ := NewVariableExpression(core.SourceMap{}, "testExp") Convey("NewForExpression", t, func() { - Convey("should return new ForExPresssion.", func() { + Convey("should return new ForExpresssion.", func() { forExp, err := NewForExpression(core.SourceMap{}, dataSource, returnExp, false, false, false) So(forExp, ShouldNotBeNil) So(err, ShouldBeNil) }) - Convey("should return core.ErrMissedArgument.", func() { + + Convey("should return error when a dataSource is nil", func() { forExp, err := NewForExpression(core.SourceMap{}, nil, returnExp, false, false, false) So(forExp, ShouldBeNil) So(err, ShouldNotBeNil) So(err, ShouldEqual, err) + }) - forExp, err = NewForExpression(core.SourceMap{}, dataSource, nil, false, false, false) + Convey("should return error when a predicate is nil", func() { + forExp, err := NewForExpression(core.SourceMap{}, dataSource, nil, false, false, false) So(forExp, ShouldBeNil) So(err, ShouldNotBeNil) So(err, ShouldEqual, err) @@ -85,14 +60,14 @@ func TestNewForExpression(t *testing.T) { func TestAddLimit(t *testing.T) { dataSource, valName := testIntArrayLiteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) Convey("AddLimit", t, func() { - Convey("should success.", func() { + Convey("should success. (An Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -105,7 +80,7 @@ func TestAddLimit(t *testing.T) { So(err, ShouldBeNil) }) - Convey("should return a emptyData error.", func() { + Convey("should return an error.", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -124,10 +99,10 @@ func TestAddLimit(t *testing.T) { func TestAddFilter(t *testing.T) { dataSource, valName := testIntArrayLiteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) testFilter := func() core.Expression { @@ -138,7 +113,7 @@ func TestAddFilter(t *testing.T) { } Convey("AddFilter", t, func() { - Convey("should success.", func() { + Convey("should success. (An Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -152,7 +127,7 @@ func TestAddFilter(t *testing.T) { So(err, ShouldBeNil) }) - Convey("should return a error.", func() { + Convey("should return an error.", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -172,10 +147,10 @@ func TestAddFilter(t *testing.T) { func TestAddSort(t *testing.T) { dataSource, valName := testIntArrayLiteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) testSort := func() *clauses.SorterExpression { @@ -185,7 +160,7 @@ func TestAddSort(t *testing.T) { } Convey("AddSort", t, func() { - Convey("should success.", func() { + Convey("should success.(An Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -199,7 +174,7 @@ func TestAddSort(t *testing.T) { So(err, ShouldBeNil) }) - Convey("should return a error.", func() { + Convey("should return an error.", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -230,14 +205,14 @@ func TestAddCollect(t *testing.T) { return collect } - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, selectorValName) + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, selectorValName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) Convey("AddCollect", t, func() { - Convey("should success.", func() { + Convey("should success. (Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -251,7 +226,7 @@ func TestAddCollect(t *testing.T) { So(err, ShouldBeNil) }) - Convey("should return a error.", func() { + Convey("should return an error.", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -270,10 +245,10 @@ func TestAddCollect(t *testing.T) { func TestAddStatement(t *testing.T) { dataSource, valName := testIntArrayLiteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) testStatement, _ := NewVariableDeclarationExpression(core.SourceMap{}, "newVal", literals.NewIntLiteral(0)) @@ -293,7 +268,7 @@ func TestAddStatement(t *testing.T) { So(err, ShouldBeNil) }) - Convey("should return a error.", func() { + Convey("should return an error. (Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -314,10 +289,10 @@ func TestExec(t *testing.T) { Convey("should success.", func() { dataSource, valName := testIntArrayLiteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) rootScope, closeFn := core.NewRootScope() @@ -347,13 +322,13 @@ func TestExec(t *testing.T) { closeFn() }) - Convey("should return contextdone error.", func() { + Convey("should stop an execution when context is cancelled.", func() { dataSource, valName := testIntArrayLiteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) rootScope, closeFn := core.NewRootScope() @@ -376,14 +351,23 @@ func TestExec(t *testing.T) { closeFn() }) - Convey("should return a emptyData error.", func() { + Convey("should return an error when a dataSource expression is invalidated.", func() { rootScope, closeFn := core.NewRootScope() - errorDataSource, valName := testErrorArrayliteral() - - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + errorDataSource, valName := func() (collections.Iterable, string) { + dataSource, _ := NewForInIterableExpression( + core.SourceMap{}, + "val", + "", + literals.NewIntLiteral(1), + ) + + return dataSource, "val" + }() + + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) forExp, _ := NewForExpression( @@ -402,14 +386,30 @@ func TestExec(t *testing.T) { closeFn() }) - Convey("should return a elementData error.", func() { + Convey("should return an error when element expressions of dataSource is invalidated.", func() { rootScope, closeFn := core.NewRootScope() - errorDataSource, valName := testElementErrorArrayliteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + errorDataSource, valName := func() (collections.Iterable, string) { + errorEle, _ := NewVariableExpression(core.SourceMap{}, "a") + dataSource, _ := NewForInIterableExpression( + core.SourceMap{}, + "val", + "", + literals.NewArrayLiteralWith([]core.Expression{ + literals.NewIntLiteral(0), + literals.NewIntLiteral(1), + literals.NewIntLiteral(2), + errorEle, + }), + ) + + return dataSource, "val" + }() + + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) forExp, _ := NewForExpression( @@ -428,14 +428,14 @@ func TestExec(t *testing.T) { closeFn() }) - Convey("should return a errorreturn error.", func() { + Convey("should return an error when an predicate expression is invalidated.", func() { rootScope, closeFn := core.NewRootScope() dataSource, _ := testIntArrayLiteral() - returnedValExp, _ := NewVariableExpression(core.SourceMap{}, "notExistVal") + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, "notExistVal") returnExp, _ := NewReturnExpression( core.SourceMap{}, - returnedValExp, + returnInValExp, ) forExp, _ := NewForExpression( From 7fb6ca03563f69d2335680a53ca24e0508c6453f Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Thu, 24 Aug 2023 04:49:57 +0900 Subject: [PATCH 4/8] Fix --- pkg/runtime/expressions/for_test.go | 54 ++++++++++++----------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go index 344942fe..efd3ac8a 100644 --- a/pkg/runtime/expressions/for_test.go +++ b/pkg/runtime/expressions/for_test.go @@ -13,7 +13,7 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func testIntArrayLiteral() (collections.Iterable, string) { +func testForInIterableExpression() (collections.Iterable, string) { dataSource, _ := NewForInIterableExpression( core.SourceMap{}, "val", @@ -31,10 +31,10 @@ func testIntArrayLiteral() (collections.Iterable, string) { } func TestNewForExpression(t *testing.T) { - dataSource, _ := testIntArrayLiteral() + dataSource, _ := testForInIterableExpression() returnExp, _ := NewVariableExpression(core.SourceMap{}, "testExp") - Convey("NewForExpression", t, func() { + Convey(".NewForExpression", t, func() { Convey("should return new ForExpresssion.", func() { forExp, err := NewForExpression(core.SourceMap{}, dataSource, returnExp, false, false, false) So(forExp, ShouldNotBeNil) @@ -58,7 +58,7 @@ func TestNewForExpression(t *testing.T) { } func TestAddLimit(t *testing.T) { - dataSource, valName := testIntArrayLiteral() + dataSource, valName := testForInIterableExpression() returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( @@ -66,7 +66,7 @@ func TestAddLimit(t *testing.T) { returnInValExp, ) - Convey("AddLimit", t, func() { + Convey(".AddLimit", t, func() { Convey("should success. (An Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, @@ -97,7 +97,7 @@ func TestAddLimit(t *testing.T) { } func TestAddFilter(t *testing.T) { - dataSource, valName := testIntArrayLiteral() + dataSource, valName := testForInIterableExpression() returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( @@ -112,7 +112,7 @@ func TestAddFilter(t *testing.T) { return filterExp } - Convey("AddFilter", t, func() { + Convey(".AddFilter", t, func() { Convey("should success. (An Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, @@ -145,7 +145,7 @@ func TestAddFilter(t *testing.T) { } func TestAddSort(t *testing.T) { - dataSource, valName := testIntArrayLiteral() + dataSource, valName := testForInIterableExpression() returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( @@ -159,7 +159,7 @@ func TestAddSort(t *testing.T) { return sortExp } - Convey("AddSort", t, func() { + Convey(".AddSort", t, func() { Convey("should success.(An Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, @@ -191,7 +191,7 @@ func TestAddSort(t *testing.T) { } func TestAddCollect(t *testing.T) { - dataSource, valName := testIntArrayLiteral() + dataSource, valName := testForInIterableExpression() selectorValName := "selector" @@ -211,7 +211,7 @@ func TestAddCollect(t *testing.T) { returnInValExp, ) - Convey("AddCollect", t, func() { + Convey(".AddCollect", t, func() { Convey("should success. (Error should be nil.)", func() { forExp, _ := NewForExpression( core.SourceMap{}, @@ -243,7 +243,7 @@ func TestAddCollect(t *testing.T) { } func TestAddStatement(t *testing.T) { - dataSource, valName := testIntArrayLiteral() + dataSource, valName := testForInIterableExpression() returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) returnExp, _ := NewReturnExpression( @@ -253,7 +253,7 @@ func TestAddStatement(t *testing.T) { testStatement, _ := NewVariableDeclarationExpression(core.SourceMap{}, "newVal", literals.NewIntLiteral(0)) - Convey("AddStatement", t, func() { + Convey(".AddStatement (Error should be nil.)", t, func() { Convey("should success.", func() { forExp, _ := NewForExpression( core.SourceMap{}, @@ -268,7 +268,7 @@ func TestAddStatement(t *testing.T) { So(err, ShouldBeNil) }) - Convey("should return an error. (Error should be nil.)", func() { + Convey("should return an error.", func() { forExp, _ := NewForExpression( core.SourceMap{}, dataSource, @@ -285,16 +285,15 @@ func TestAddStatement(t *testing.T) { } func TestExec(t *testing.T) { - Convey("Exec", t, func() { - Convey("should success.", func() { - dataSource, valName := testIntArrayLiteral() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) + Convey(".Exec", t, func() { + dataSource, valName := testForInIterableExpression() + returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) + returnExp, _ := NewReturnExpression( + core.SourceMap{}, + returnInValExp, + ) + Convey("should success.", func() { rootScope, closeFn := core.NewRootScope() forExp, _ := NewForExpression( core.SourceMap{}, @@ -323,14 +322,6 @@ func TestExec(t *testing.T) { }) Convey("should stop an execution when context is cancelled.", func() { - dataSource, valName := testIntArrayLiteral() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) - rootScope, closeFn := core.NewRootScope() forExp, _ := NewForExpression( core.SourceMap{}, @@ -430,7 +421,6 @@ func TestExec(t *testing.T) { Convey("should return an error when an predicate expression is invalidated.", func() { rootScope, closeFn := core.NewRootScope() - dataSource, _ := testIntArrayLiteral() returnInValExp, _ := NewVariableExpression(core.SourceMap{}, "notExistVal") returnExp, _ := NewReturnExpression( From d6b957393a821a0c9f4becbb3aa7c7216813226a Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Wed, 30 Aug 2023 08:52:39 +0900 Subject: [PATCH 5/8] Execute make fmt --- pkg/runtime/expressions/for_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go index efd3ac8a..db33efde 100644 --- a/pkg/runtime/expressions/for_test.go +++ b/pkg/runtime/expressions/for_test.go @@ -4,13 +4,14 @@ import ( "context" "testing" + . "github.com/smartystreets/goconvey/convey" + "github.com/MontFerret/ferret/pkg/runtime/collections" "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/expressions/clauses" "github.com/MontFerret/ferret/pkg/runtime/expressions/literals" "github.com/MontFerret/ferret/pkg/runtime/expressions/operators" "github.com/MontFerret/ferret/pkg/runtime/values" - . "github.com/smartystreets/goconvey/convey" ) func testForInIterableExpression() (collections.Iterable, string) { From 22cf120137ae5c1f4e8c9dd6ffa36af577e2813e Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Sun, 29 Dec 2024 05:33:32 +0900 Subject: [PATCH 6/8] fix test --- pkg/runtime/expressions/for_test.go | 418 +++++++++++----------------- 1 file changed, 163 insertions(+), 255 deletions(-) diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go index db33efde..6bb32b84 100644 --- a/pkg/runtime/expressions/for_test.go +++ b/pkg/runtime/expressions/for_test.go @@ -9,48 +9,78 @@ import ( "github.com/MontFerret/ferret/pkg/runtime/collections" "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/expressions/clauses" - "github.com/MontFerret/ferret/pkg/runtime/expressions/literals" - "github.com/MontFerret/ferret/pkg/runtime/expressions/operators" "github.com/MontFerret/ferret/pkg/runtime/values" ) -func testForInIterableExpression() (collections.Iterable, string) { - dataSource, _ := NewForInIterableExpression( - core.SourceMap{}, - "val", - "", - literals.NewArrayLiteralWith([]core.Expression{ - literals.NewIntLiteral(0), - literals.NewIntLiteral(1), - literals.NewIntLiteral(2), - literals.NewIntLiteral(3), - literals.NewIntLiteral(4), - }), - ) - - return dataSource, "val" +type ( + testIterator struct { + values []*core.Scope + pos int + causeErrorInNext bool + } + testIterable struct { + values []*core.Scope + causeErrorInIterate bool + causeErrorInNext bool + } + testExpression struct{ + causeErrorInExec bool + } + testError struct{} +) + +func (iterator *testIterator) Next(ctx context.Context, scope *core.Scope) (*core.Scope, error) { + if iterator.causeErrorInNext { + return nil, testError{} + } + + if len(iterator.values) > iterator.pos { + val := iterator.values[iterator.pos] + iterator.pos++ + + return val, nil + } + + return nil, core.ErrNoMoreData } -func TestNewForExpression(t *testing.T) { - dataSource, _ := testForInIterableExpression() - returnExp, _ := NewVariableExpression(core.SourceMap{}, "testExp") +func (iterable *testIterable) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) { + if iterable.causeErrorInIterate { + return nil, testError{} + } + + return &testIterator{iterable.values, 0, iterable.causeErrorInNext}, nil +} + +func (expression *testExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) { + return nil, nil +} + +func (error testError) Error() string { + return "error" +} + +func testInitTestIterable(values []*core.Scope, causeErrorInIterate, causeErrorInNext bool) *testIterable { + return &testIterable{values, causeErrorInIterate, causeErrorInNext} +} +func TestNewForExpression(t *testing.T) { Convey(".NewForExpression", t, func() { - Convey("should return new ForExpresssion.", func() { - forExp, err := NewForExpression(core.SourceMap{}, dataSource, returnExp, false, false, false) + Convey("Should return new ForExpresssion.", func() { + forExp, err := NewForExpression(core.SourceMap{}, testInitTestIterable([]*core.Scope{}, false, false), &testExpression{}, false, false, false) So(forExp, ShouldNotBeNil) So(err, ShouldBeNil) }) - Convey("should return error when a dataSource is nil", func() { - forExp, err := NewForExpression(core.SourceMap{}, nil, returnExp, false, false, false) + Convey("Should return error when a dataSource is nil", func() { + forExp, err := NewForExpression(core.SourceMap{}, nil, &testExpression{}, false, false, false) So(forExp, ShouldBeNil) So(err, ShouldNotBeNil) So(err, ShouldEqual, err) }) - Convey("should return error when a predicate is nil", func() { - forExp, err := NewForExpression(core.SourceMap{}, dataSource, nil, false, false, false) + Convey("Should return error when a predicate is nil", func() { + forExp, err := NewForExpression(core.SourceMap{}, testInitTestIterable([]*core.Scope{}, false, false), nil, false, false, false) So(forExp, ShouldBeNil) So(err, ShouldNotBeNil) So(err, ShouldEqual, err) @@ -59,86 +89,70 @@ func TestNewForExpression(t *testing.T) { } func TestAddLimit(t *testing.T) { - dataSource, valName := testForInIterableExpression() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) - Convey(".AddLimit", t, func() { - Convey("should success. (An Error should be nil.)", func() { - forExp, _ := NewForExpression( + Convey("Should success. (An Error Should be nil.)", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - err := forExp.AddLimit(core.SourceMap{}, literals.NewIntLiteral(3), literals.NewIntLiteral(0)) + + err := forExpression.AddLimit(core.SourceMap{}, &testExpression{}, &testExpression{}) + So(err, ShouldBeNil) }) - Convey("should return an error.", func() { - forExp, _ := NewForExpression( + Convey("Should return an error.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - forExp.dataSource = nil - err := forExp.AddLimit(core.SourceMap{}, literals.NewIntLiteral(3), literals.NewIntLiteral(0)) + forExpression.dataSource = nil + + err := forExpression.AddLimit(core.SourceMap{}, &testExpression{}, &testExpression{}) + So(err, ShouldNotBeNil) }) }) } func TestAddFilter(t *testing.T) { - dataSource, valName := testForInIterableExpression() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) - - testFilter := func() core.Expression { - filterLeftExp, _ := NewVariableExpression(core.SourceMap{}, valName) - filterRightExp := literals.NewIntLiteral(3) - filterExp, _ := operators.NewEqualityOperator(core.SourceMap{}, filterLeftExp, filterRightExp, "<") - return filterExp - } - Convey(".AddFilter", t, func() { - Convey("should success. (An Error should be nil.)", func() { - forExp, _ := NewForExpression( + Convey("Should success. (An Error Should be nil.)", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - err := forExp.AddFilter(core.SourceMap{}, testFilter()) + err := forExpression.AddFilter(core.SourceMap{}, &testExpression{}) + So(err, ShouldBeNil) }) - Convey("should return an error.", func() { - forExp, _ := NewForExpression( + Convey("Should return an error.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - forExp.dataSource = nil - err := forExp.AddFilter(core.SourceMap{}, testFilter()) + forExpression.dataSource = nil + + err := forExpression.AddFilter(core.SourceMap{}, &testExpression{}) + So(err, ShouldNotBeNil) }) }) @@ -146,140 +160,105 @@ func TestAddFilter(t *testing.T) { } func TestAddSort(t *testing.T) { - dataSource, valName := testForInIterableExpression() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) - - testSort := func() *clauses.SorterExpression { - valExp, _ := NewVariableExpression(core.SourceMap{}, valName) - sortExp, _ := clauses.NewSorterExpression(valExp, collections.SortDirectionDesc) - return sortExp - } - Convey(".AddSort", t, func() { - Convey("should success.(An Error should be nil.)", func() { - forExp, _ := NewForExpression( + Convey("Should success.(An Error Should be nil.)", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - err := forExp.AddSort(core.SourceMap{}, testSort()) + err := forExpression.AddSort(core.SourceMap{}, &clauses.SorterExpression{}) + So(err, ShouldBeNil) }) - Convey("should return an error.", func() { - forExp, _ := NewForExpression( + Convey("Should return an error.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - forExp.dataSource = nil - err := forExp.AddSort(core.SourceMap{}, testSort()) + forExpression.dataSource = nil + + err := forExpression.AddSort(core.SourceMap{}, &clauses.SorterExpression{}) + So(err, ShouldNotBeNil) }) }) } func TestAddCollect(t *testing.T) { - dataSource, valName := testForInIterableExpression() - - selectorValName := "selector" - - testCollect := func() *clauses.Collect { - eleVal, _ := NewVariableExpression(core.SourceMap{}, valName) - selector, _ := clauses.NewCollectSelector(selectorValName, eleVal) - collect, _ := clauses.NewCollect( - []*clauses.CollectSelector{selector}, nil, nil, nil, - ) - - return collect - } - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, selectorValName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) - Convey(".AddCollect", t, func() { - Convey("should success. (Error should be nil.)", func() { - forExp, _ := NewForExpression( + Convey("Should success. (Error Should be nil.)", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - err := forExp.AddCollect(core.SourceMap{}, testCollect()) + err := forExpression.AddCollect(core.SourceMap{}, &clauses.Collect{}) + So(err, ShouldBeNil) }) - Convey("should return an error.", func() { - forExp, _ := NewForExpression( + Convey("Should return an error.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - forExp.dataSource = nil - err := forExp.AddCollect(core.SourceMap{}, testCollect()) + forExpression.dataSource = nil + + err := forExpression.AddCollect(core.SourceMap{}, &clauses.Collect{}) + So(err, ShouldNotBeNil) }) }) } func TestAddStatement(t *testing.T) { - dataSource, valName := testForInIterableExpression() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) - - testStatement, _ := NewVariableDeclarationExpression(core.SourceMap{}, "newVal", literals.NewIntLiteral(0)) - - Convey(".AddStatement (Error should be nil.)", t, func() { - Convey("should success.", func() { - forExp, _ := NewForExpression( + Convey(".AddStatement (Error Should be nil.)", t, func() { + Convey("Should success.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - err := forExp.AddStatement(testStatement) + err := forExpression.AddStatement(&testExpression{}) + So(err, ShouldBeNil) }) - Convey("should return an error.", func() { - forExp, _ := NewForExpression( + Convey("Should return an error.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, false), + &testExpression{}, false, false, false, ) - forExp.dataSource = nil - err := forExp.AddStatement(testStatement) + forExpression.dataSource = nil + + err := forExpression.AddStatement(&testExpression{}) + So(err, ShouldNotBeNil) }) }) @@ -287,47 +266,34 @@ func TestAddStatement(t *testing.T) { func TestExec(t *testing.T) { Convey(".Exec", t, func() { - dataSource, valName := testForInIterableExpression() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( - core.SourceMap{}, - returnInValExp, - ) - Convey("should success.", func() { - rootScope, closeFn := core.NewRootScope() - forExp, _ := NewForExpression( + Convey("Should success.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{ + &core.Scope{}, + &core.Scope{}, + &core.Scope{}, + }, false, false), + &testExpression{}, false, false, false, ) - resultVal, err := forExp.Exec(context.Background(), rootScope) + _, err := forExpression.Exec(context.Background(), &core.Scope{}) + So(err, ShouldBeNil) - resultArr, ok := resultVal.(*values.Array) - So(ok, ShouldBeTrue) - So(resultArr.Length(), ShouldEqual, values.NewInt(5)) - compareArr := values.NewArrayOf([]core.Value{ - values.NewInt(0), - values.NewInt(1), - values.NewInt(2), - values.NewInt(3), - values.NewInt(4), - }) - So(resultArr.Compare(compareArr), ShouldEqual, 0) - - closeFn() }) - Convey("should stop an execution when context is cancelled.", func() { - rootScope, closeFn := core.NewRootScope() - forExp, _ := NewForExpression( + Convey("Should stop an execution when context is cancelled.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{ + &core.Scope{}, + &core.Scope{}, + &core.Scope{}, + }, false, false), + &testExpression{}, false, false, false, @@ -337,112 +303,54 @@ func TestExec(t *testing.T) { ctx1, cancelFn := context.WithCancel(ctx0) cancelFn() - _, err := forExp.Exec(ctx1, rootScope) - So(err, ShouldNotBeNil) + _, err := forExpression.Exec(ctx1, &core.Scope{}) - closeFn() + So(err, ShouldNotBeNil) }) - Convey("should return an error when a dataSource expression is invalidated.", func() { - rootScope, closeFn := core.NewRootScope() - errorDataSource, valName := func() (collections.Iterable, string) { - dataSource, _ := NewForInIterableExpression( - core.SourceMap{}, - "val", - "", - literals.NewIntLiteral(1), - ) - - return dataSource, "val" - }() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( + Convey("Should return an error when a dataSource expression is invalid.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - returnInValExp, - ) - - forExp, _ := NewForExpression( - core.SourceMap{}, - errorDataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, true, false), + &testExpression{}, false, false, false, ) - result, err := forExp.Exec(context.Background(), rootScope) + result, err := forExpression.Exec(context.Background(), &core.Scope{}) So(result, ShouldEqual, values.None) So(err, ShouldNotBeNil) - - closeFn() }) - Convey("should return an error when element expressions of dataSource is invalidated.", func() { - rootScope, closeFn := core.NewRootScope() - - errorDataSource, valName := func() (collections.Iterable, string) { - errorEle, _ := NewVariableExpression(core.SourceMap{}, "a") - dataSource, _ := NewForInIterableExpression( - core.SourceMap{}, - "val", - "", - literals.NewArrayLiteralWith([]core.Expression{ - literals.NewIntLiteral(0), - literals.NewIntLiteral(1), - literals.NewIntLiteral(2), - errorEle, - }), - ) - - return dataSource, "val" - }() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, valName) - returnExp, _ := NewReturnExpression( + Convey("Should return an error when element expressions of dataSource is invalidated.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - returnInValExp, - ) - - forExp, _ := NewForExpression( - core.SourceMap{}, - errorDataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, true), + &testExpression{}, false, false, false, ) - result, err := forExp.Exec(context.Background(), rootScope) + result, err := forExpression.Exec(context.Background(), &core.Scope{}) So(result, ShouldEqual, values.None) So(err, ShouldNotBeNil) - - closeFn() }) - Convey("should return an error when an predicate expression is invalidated.", func() { - rootScope, closeFn := core.NewRootScope() - - returnInValExp, _ := NewVariableExpression(core.SourceMap{}, "notExistVal") - returnExp, _ := NewReturnExpression( + Convey("Should return an error when an predicate expression is invalidated.", func() { + forExpression, _ := NewForExpression( core.SourceMap{}, - returnInValExp, - ) - - forExp, _ := NewForExpression( - core.SourceMap{}, - dataSource, - returnExp, + testInitTestIterable([]*core.Scope{}, false, true), + &testExpression{true}, false, false, false, ) - result, err := forExp.Exec(context.Background(), rootScope) + result, err := forExpression.Exec(context.Background(), &core.Scope{}) So(result, ShouldEqual, values.None) So(err, ShouldNotBeNil) - - closeFn() }) }) } From 290adecb3537ff47329c790f76e86807267daade Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Sun, 29 Dec 2024 05:41:14 +0900 Subject: [PATCH 7/8] fmt --- pkg/runtime/expressions/for_test.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go index 6bb32b84..1731e847 100644 --- a/pkg/runtime/expressions/for_test.go +++ b/pkg/runtime/expressions/for_test.go @@ -19,14 +19,14 @@ type ( causeErrorInNext bool } testIterable struct { - values []*core.Scope + values []*core.Scope causeErrorInIterate bool - causeErrorInNext bool + causeErrorInNext bool } - testExpression struct{ + testExpression struct { causeErrorInExec bool } - testError struct{} + testError struct{} ) func (iterator *testIterator) Next(ctx context.Context, scope *core.Scope) (*core.Scope, error) { @@ -269,11 +269,7 @@ func TestExec(t *testing.T) { Convey("Should success.", func() { forExpression, _ := NewForExpression( core.SourceMap{}, - testInitTestIterable([]*core.Scope{ - &core.Scope{}, - &core.Scope{}, - &core.Scope{}, - }, false, false), + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, false), &testExpression{}, false, false, @@ -288,11 +284,7 @@ func TestExec(t *testing.T) { Convey("Should stop an execution when context is cancelled.", func() { forExpression, _ := NewForExpression( core.SourceMap{}, - testInitTestIterable([]*core.Scope{ - &core.Scope{}, - &core.Scope{}, - &core.Scope{}, - }, false, false), + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, false), &testExpression{}, false, false, From 6bfdd589effb2ff555dddde5daecc001ca819b51 Mon Sep 17 00:00:00 2001 From: Rikinyan Date: Sun, 29 Dec 2024 05:52:40 +0900 Subject: [PATCH 8/8] fix --- pkg/runtime/expressions/for_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/runtime/expressions/for_test.go b/pkg/runtime/expressions/for_test.go index 1731e847..9101e2d2 100644 --- a/pkg/runtime/expressions/for_test.go +++ b/pkg/runtime/expressions/for_test.go @@ -53,6 +53,9 @@ func (iterable *testIterable) Iterate(ctx context.Context, scope *core.Scope) (c } func (expression *testExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) { + if expression.causeErrorInExec { + return nil, testError{} + } return nil, nil } @@ -303,7 +306,7 @@ func TestExec(t *testing.T) { Convey("Should return an error when a dataSource expression is invalid.", func() { forExpression, _ := NewForExpression( core.SourceMap{}, - testInitTestIterable([]*core.Scope{}, true, false), + testInitTestIterable([]*core.Scope{{}, {}, {}}, true, false), &testExpression{}, false, false, @@ -315,10 +318,10 @@ func TestExec(t *testing.T) { So(err, ShouldNotBeNil) }) - Convey("Should return an error when element expressions of dataSource is invalidated.", func() { + Convey("Should return an error when element expressions of dataSource is invalid.", func() { forExpression, _ := NewForExpression( core.SourceMap{}, - testInitTestIterable([]*core.Scope{}, false, true), + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, true), &testExpression{}, false, false, @@ -333,7 +336,7 @@ func TestExec(t *testing.T) { Convey("Should return an error when an predicate expression is invalidated.", func() { forExpression, _ := NewForExpression( core.SourceMap{}, - testInitTestIterable([]*core.Scope{}, false, true), + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, false), &testExpression{true}, false, false,