github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/body_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  func TestBody(t *testing.T) {
    15  	Convey("Should create a block expression", t, func() {
    16  		s := expressions.NewBodyExpression(1)
    17  
    18  		So(s, ShouldHaveSameTypeAs, &expressions.BodyExpression{})
    19  	})
    20  
    21  	Convey("Should add a new expression of a default type", t, func() {
    22  		s := expressions.NewBodyExpression(0)
    23  
    24  		sourceMap := core.NewSourceMap("test", 1, 1)
    25  		exp, err := expressions.NewVariableExpression(sourceMap, "testExp")
    26  		So(err, ShouldBeNil)
    27  
    28  		err = s.Add(exp)
    29  		So(err, ShouldBeNil)
    30  	})
    31  
    32  	Convey("Should add a new Return expression", t, func() {
    33  		s := expressions.NewBodyExpression(0)
    34  
    35  		sourceMap := core.NewSourceMap("test", 1, 1)
    36  		predicate, err := expressions.NewVariableExpression(sourceMap, "testExp")
    37  		So(err, ShouldBeNil)
    38  
    39  		exp, err := expressions.NewReturnExpression(sourceMap, predicate)
    40  		So(err, ShouldBeNil)
    41  
    42  		err = s.Add(exp)
    43  		So(err, ShouldBeNil)
    44  	})
    45  
    46  	Convey("Should not add an already defined Return expression", t, func() {
    47  		s := expressions.NewBodyExpression(0)
    48  
    49  		sourceMap := core.NewSourceMap("test", 1, 1)
    50  		predicate, err := expressions.NewVariableExpression(sourceMap, "testExp")
    51  		So(err, ShouldBeNil)
    52  
    53  		exp, err := expressions.NewReturnExpression(sourceMap, predicate)
    54  		So(err, ShouldBeNil)
    55  
    56  		err = s.Add(exp)
    57  		So(err, ShouldBeNil)
    58  
    59  		err = s.Add(exp)
    60  		So(err, ShouldBeError)
    61  		So(err.Error(), ShouldEqual, "invalid operation: return expression is already defined")
    62  	})
    63  
    64  	Convey("Should exec a block expression", t, func() {
    65  		s := expressions.NewBodyExpression(1)
    66  
    67  		sourceMap := core.NewSourceMap("test", 1, 1)
    68  		predicate, err := expressions.NewVariableExpression(sourceMap, "test")
    69  		So(err, ShouldBeNil)
    70  
    71  		retexp, err := expressions.NewReturnExpression(sourceMap, predicate)
    72  		So(err, ShouldBeNil)
    73  
    74  		err = s.Add(retexp)
    75  		So(err, ShouldBeNil)
    76  
    77  		rootScope, fn := core.NewRootScope()
    78  		scope := rootScope.Fork()
    79  		scope.SetVariable("test", values.NewString("value"))
    80  		fn()
    81  
    82  		value, err := s.Exec(context.Background(), scope)
    83  		So(err, ShouldBeNil)
    84  		So(value, ShouldNotBeNil)
    85  		So(value, ShouldEqual, "value")
    86  	})
    87  
    88  	Convey("Should not found a missing statement", t, func() {
    89  		s := expressions.NewBodyExpression(1)
    90  
    91  		sourceMap := core.NewSourceMap("test", 1, 1)
    92  		predicate, err := expressions.NewVariableExpression(sourceMap, "testExp")
    93  		So(err, ShouldBeNil)
    94  
    95  		exp, err := expressions.NewReturnExpression(sourceMap, predicate)
    96  		So(err, ShouldBeNil)
    97  
    98  		err = s.Add(exp)
    99  		So(err, ShouldBeNil)
   100  
   101  		rootScope, fn := core.NewRootScope()
   102  		scope := rootScope.Fork()
   103  		scope.SetVariable("test", values.NewString("value"))
   104  		fn()
   105  
   106  		value, err := s.Exec(context.Background(), scope)
   107  		So(err, ShouldNotBeNil)
   108  		So(err.(*core.SourceErrorDetail).BaseError, ShouldHaveSameTypeAs, core.ErrNotFound)
   109  		So(value, ShouldHaveSameTypeAs, values.None)
   110  	})
   111  
   112  	Convey("Should not exec a nil block expression", t, func() {
   113  		s := expressions.NewBodyExpression(1)
   114  
   115  		sourceMap := core.NewSourceMap("test", 1, 1)
   116  		exp, err := expressions.NewVariableExpression(sourceMap, "test")
   117  		So(err, ShouldBeNil)
   118  
   119  		err = s.Add(exp)
   120  		So(err, ShouldBeNil)
   121  
   122  		rootScope, fn := core.NewRootScope()
   123  		scope := rootScope.Fork()
   124  		scope.SetVariable("test", values.NewString("value"))
   125  		fn()
   126  
   127  		value, err := s.Exec(context.Background(), scope)
   128  		So(err, ShouldBeNil)
   129  		So(value, ShouldHaveSameTypeAs, values.None)
   130  	})
   131  
   132  	Convey("Should stop an execution when context is cancelled", t, func() {
   133  		s := expressions.NewBodyExpression(1)
   134  		sourceMap := core.NewSourceMap("test", 1, 1)
   135  		exp, _ := expressions.NewVariableExpression(sourceMap, "test")
   136  		s.Add(exp)
   137  
   138  		rootScope, _ := core.NewRootScope()
   139  		scope := rootScope.Fork()
   140  		scope.SetVariable("test", values.NewString("value"))
   141  
   142  		ctx, cancel := context.WithCancel(context.Background())
   143  		cancel()
   144  
   145  		_, err := s.Exec(ctx, scope)
   146  		So(err, ShouldEqual, core.ErrTerminated)
   147  	})
   148  }