github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/variable_test.go (about) 1 package expressions_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 9 "github.com/MontFerret/ferret/pkg/runtime/core" 10 "github.com/MontFerret/ferret/pkg/runtime/expressions" 11 "github.com/MontFerret/ferret/pkg/runtime/values" 12 ) 13 14 var sourceMap = core.NewSourceMap("hello", 2, 3) 15 var rootScope, _ = core.NewRootScope() 16 var _ = rootScope.SetVariable("key", values.NewString("value")) 17 18 func TestNewVariableExpression(t *testing.T) { 19 20 Convey("Should not throw error and create a VariableExpression given correct arguments", t, func() { 21 ret, err := expressions.NewVariableExpression(sourceMap, "foo") 22 23 So(ret, ShouldHaveSameTypeAs, &expressions.VariableExpression{}) 24 So(err, ShouldBeNil) 25 }) 26 27 Convey("Should throw error when given no name argument", t, func() { 28 _, err := expressions.NewVariableExpression(sourceMap, "") 29 30 So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument) 31 }) 32 33 Convey("Calling .Exec should return the correct variable set in the given scope", t, func() { 34 ret, _ := expressions.NewVariableExpression(sourceMap, "key") 35 value, err := ret.Exec(context.TODO(), rootScope) 36 37 So(value, ShouldEqual, "value") 38 So(err, ShouldBeNil) 39 }) 40 } 41 42 func TestNewVariableDeclarationExpression(t *testing.T) { 43 Convey("Should not throw error and create a NewVariableDeclarationExpression given correct arguments", t, func() { 44 variableExpression, _ := expressions.NewVariableExpression(sourceMap, "foo") 45 ret, err := expressions.NewVariableDeclarationExpression(sourceMap, "test", variableExpression) 46 47 So(ret, ShouldHaveSameTypeAs, &expressions.VariableDeclarationExpression{}) 48 So(err, ShouldBeNil) 49 }) 50 51 Convey("Should throw error if init argument is nil", t, func() { 52 _, err := expressions.NewVariableDeclarationExpression(sourceMap, "test", nil) 53 54 So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument) 55 }) 56 57 Convey("Calling .Exec should add the value retrieved by its VariableExpression with its own name as key to the given scope", t, func() { 58 variableExpression, _ := expressions.NewVariableExpression(sourceMap, "key") 59 variableDeclarationExpression, _ := expressions.NewVariableDeclarationExpression(sourceMap, "keyTwo", variableExpression) 60 _, err := variableDeclarationExpression.Exec(context.TODO(), rootScope) 61 62 So(err, ShouldBeNil) 63 64 value, _ := rootScope.GetVariable("key") 65 value2, _ := rootScope.GetVariable("keyTwo") 66 67 So(value, ShouldEqual, "value") 68 So(value2, ShouldEqual, "value") 69 }) 70 }