github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/operators/range_test.go (about)

     1  package operators_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	. "github.com/smartystreets/goconvey/convey"
     9  
    10  	"github.com/MontFerret/ferret/pkg/runtime/core"
    11  	"github.com/MontFerret/ferret/pkg/runtime/expressions/operators"
    12  	"github.com/MontFerret/ferret/pkg/runtime/values"
    13  )
    14  
    15  type MockExpressionInt int
    16  
    17  func (e MockExpressionInt) Exec(_ context.Context, _ *core.Scope) (core.Value, error) {
    18  	return values.NewInt(int(e)), nil
    19  }
    20  
    21  type MockExpressionFloat float64
    22  
    23  func (e MockExpressionFloat) Exec(_ context.Context, _ *core.Scope) (core.Value, error) {
    24  	return values.NewFloat(float64(e)), nil
    25  }
    26  
    27  type MockExpressionString string
    28  
    29  func (e MockExpressionString) Exec(_ context.Context, _ *core.Scope) (core.Value, error) {
    30  	return values.NewString(string(e)), nil
    31  }
    32  
    33  type FailingMockExpression int
    34  
    35  func (e FailingMockExpression) Exec(_ context.Context, _ *core.Scope) (core.Value, error) {
    36  	return nil, errors.New("MockError")
    37  }
    38  
    39  var sourceMap = core.NewSourceMap("hello", 2, 3)
    40  var rootScope, _ = core.NewRootScope()
    41  
    42  func TestRangeOperator(t *testing.T) {
    43  	var correctEx MockExpressionInt = 10
    44  	var failEx FailingMockExpression = 0
    45  	Convey("NewRangeOperator", t, func() {
    46  		Convey("Should return *RangeOperator, given correct arguments", func() {
    47  			res, _ := operators.NewRangeOperator(sourceMap, correctEx, correctEx)
    48  			So(res, ShouldHaveSameTypeAs, &operators.RangeOperator{})
    49  		})
    50  		Convey("Should return error if no left operator is given", func() {
    51  			_, err := operators.NewRangeOperator(sourceMap, nil, correctEx)
    52  			So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument)
    53  		})
    54  		Convey("Should return error if no right operator is given", func() {
    55  			_, err := operators.NewRangeOperator(sourceMap, correctEx, nil)
    56  			So(err, ShouldHaveSameTypeAs, core.ErrMissedArgument)
    57  		})
    58  	})
    59  	Convey(".Exec", t, func() {
    60  		Convey("Should return error if left expression fails", func() {
    61  			rangeOp, _ := operators.NewRangeOperator(sourceMap, failEx, correctEx)
    62  			_, err := rangeOp.Exec(context.TODO(), rootScope)
    63  			So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{})
    64  		})
    65  		Convey("Should return error if right expression fails", func() {
    66  			rangeOp, _ := operators.NewRangeOperator(sourceMap, correctEx, failEx)
    67  			_, err := rangeOp.Exec(context.TODO(), rootScope)
    68  			So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{})
    69  		})
    70  	})
    71  	Convey(".Eval", t, func() {
    72  		var stringEx MockExpressionString = "noInt/noFloat"
    73  		Convey("Should return error if given non int/float left Expression", func() {
    74  			rangeOp, _ := operators.NewRangeOperator(sourceMap, stringEx, correctEx)
    75  			_, err := rangeOp.Exec(context.TODO(), rootScope)
    76  			So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{})
    77  		})
    78  		Convey("Should return error if given non int/float right Expression", func() {
    79  			rangeOp, _ := operators.NewRangeOperator(sourceMap, correctEx, stringEx)
    80  			_, err := rangeOp.Exec(context.TODO(), rootScope)
    81  			So(err, ShouldHaveSameTypeAs, &core.SourceErrorDetail{})
    82  		})
    83  		Convey("Should return Array with range [start,...end] given ints", func() {
    84  			var start MockExpressionInt = 1
    85  			var end MockExpressionInt = 14
    86  
    87  			rangeOp, _ := operators.NewRangeOperator(sourceMap, start, end)
    88  			arr, err := rangeOp.Exec(context.TODO(), rootScope)
    89  			So(err, ShouldHaveSameTypeAs, nil)
    90  			So(arr, ShouldHaveSameTypeAs, &values.Array{})
    91  			So(arr.String(), ShouldEqual, "[1,2,3,4,5,6,7,8,9,10,11,12,13,14]")
    92  		})
    93  		Convey("Should return Array with range [start,...end] given floats", func() {
    94  			var start MockExpressionFloat = -2.2
    95  			var end MockExpressionFloat = 12.4
    96  
    97  			rangeOp, _ := operators.NewRangeOperator(sourceMap, start, end)
    98  			arr, err := rangeOp.Exec(context.TODO(), rootScope)
    99  			So(err, ShouldHaveSameTypeAs, nil)
   100  			So(arr, ShouldHaveSameTypeAs, &values.Array{})
   101  			So(arr.String(), ShouldEqual, "[-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12]")
   102  		})
   103  	})
   104  }