github.com/MontFerret/ferret@v0.18.0/pkg/runtime/collections/limit_test.go (about)

     1  package collections_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"github.com/MontFerret/ferret/pkg/runtime/collections"
    10  	"github.com/MontFerret/ferret/pkg/runtime/core"
    11  	"github.com/MontFerret/ferret/pkg/runtime/values"
    12  )
    13  
    14  func TestLimit(t *testing.T) {
    15  	Convey("Should limit iteration", t, func() {
    16  		arr := []core.Value{
    17  			values.NewInt(1),
    18  			values.NewInt(2),
    19  			values.NewInt(3),
    20  			values.NewInt(4),
    21  			values.NewInt(5),
    22  		}
    23  
    24  		iter, err := collections.NewLimitIterator(
    25  			sliceIterator(arr),
    26  			1,
    27  			0,
    28  		)
    29  
    30  		So(err, ShouldBeNil)
    31  
    32  		ctx := context.Background()
    33  		scope, _ := core.NewRootScope()
    34  
    35  		res, err := collections.ToSlice(ctx, scope, iter)
    36  
    37  		So(err, ShouldBeNil)
    38  		So(len(res), ShouldEqual, 1)
    39  	})
    40  
    41  	Convey("Should limit iteration (2)", t, func() {
    42  		arr := []core.Value{
    43  			values.NewInt(1),
    44  			values.NewInt(2),
    45  			values.NewInt(3),
    46  			values.NewInt(4),
    47  			values.NewInt(5),
    48  		}
    49  
    50  		iter, err := collections.NewLimitIterator(
    51  			sliceIterator(arr),
    52  			2,
    53  			0,
    54  		)
    55  
    56  		So(err, ShouldBeNil)
    57  
    58  		ctx := context.Background()
    59  		scope, _ := core.NewRootScope()
    60  
    61  		res, err := collections.ToSlice(ctx, scope, iter)
    62  
    63  		So(err, ShouldBeNil)
    64  		So(len(res), ShouldEqual, 2)
    65  	})
    66  
    67  	Convey("Should limit iteration with offset", t, func() {
    68  		arr := []core.Value{
    69  			values.NewInt(1),
    70  			values.NewInt(2),
    71  			values.NewInt(3),
    72  			values.NewInt(4),
    73  			values.NewInt(5),
    74  		}
    75  
    76  		offset := 2
    77  		iter, err := collections.NewLimitIterator(
    78  			sliceIterator(arr),
    79  			2,
    80  			offset,
    81  		)
    82  
    83  		So(err, ShouldBeNil)
    84  
    85  		ctx := context.Background()
    86  		scope, _ := core.NewRootScope()
    87  
    88  		res, err := collections.ToSlice(ctx, scope, iter)
    89  
    90  		So(err, ShouldBeNil)
    91  		So(len(res), ShouldEqual, 2)
    92  
    93  		for idx, nextScope := range res {
    94  			expected := arr[idx+offset]
    95  			current := nextScope.MustGetVariable(collections.DefaultValueVar)
    96  
    97  			So(expected, ShouldEqual, current)
    98  		}
    99  	})
   100  
   101  	Convey("Should limit iteration with offset at the end", t, func() {
   102  		arr := []core.Value{
   103  			values.NewInt(1),
   104  			values.NewInt(2),
   105  			values.NewInt(3),
   106  			values.NewInt(4),
   107  			values.NewInt(5),
   108  		}
   109  
   110  		offset := 3
   111  
   112  		iter, err := collections.NewLimitIterator(
   113  			sliceIterator(arr),
   114  			2,
   115  			offset,
   116  		)
   117  
   118  		So(err, ShouldBeNil)
   119  
   120  		ctx := context.Background()
   121  		scope, _ := core.NewRootScope()
   122  
   123  		res, err := collections.ToSlice(ctx, scope, iter)
   124  
   125  		So(err, ShouldBeNil)
   126  		So(len(res), ShouldEqual, 2)
   127  
   128  		for idx, nextScope := range res {
   129  			expected := arr[idx+offset]
   130  			current := nextScope.MustGetVariable(collections.DefaultValueVar)
   131  
   132  			So(expected, ShouldEqual, current)
   133  		}
   134  	})
   135  
   136  	Convey("Should limit iteration with offset with going out of bounds", t, func() {
   137  		arr := []core.Value{
   138  			values.NewInt(1),
   139  			values.NewInt(2),
   140  			values.NewInt(3),
   141  			values.NewInt(4),
   142  			values.NewInt(5),
   143  		}
   144  
   145  		offset := 4
   146  
   147  		iter, err := collections.NewLimitIterator(
   148  			sliceIterator(arr),
   149  			2,
   150  			offset,
   151  		)
   152  
   153  		So(err, ShouldBeNil)
   154  
   155  		ctx := context.Background()
   156  		scope, _ := core.NewRootScope()
   157  
   158  		res, err := collections.ToSlice(ctx, scope, iter)
   159  
   160  		So(err, ShouldBeNil)
   161  		So(len(res), ShouldEqual, 1)
   162  	})
   163  }