github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/slice_test.go (about)

     1  package arrays_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"github.com/MontFerret/ferret/pkg/runtime/values"
    10  	"github.com/MontFerret/ferret/pkg/stdlib/arrays"
    11  )
    12  
    13  func TestSlice(t *testing.T) {
    14  	Convey("Should return a sliced array with a given start position ", t, func() {
    15  		arr := values.NewArrayWith(
    16  			values.NewInt(1),
    17  			values.NewInt(2),
    18  			values.NewInt(3),
    19  			values.NewInt(4),
    20  			values.NewInt(5),
    21  			values.NewInt(6),
    22  		)
    23  
    24  		out, err := arrays.Slice(context.Background(), arr, values.NewInt(3))
    25  
    26  		So(err, ShouldBeNil)
    27  		So(out.String(), ShouldEqual, "[4,5,6]")
    28  	})
    29  
    30  	Convey("Should return an empty array when start position is out of bounds", t, func() {
    31  		arr := values.NewArrayWith(
    32  			values.NewInt(1),
    33  			values.NewInt(2),
    34  			values.NewInt(3),
    35  			values.NewInt(4),
    36  			values.NewInt(5),
    37  			values.NewInt(6),
    38  		)
    39  
    40  		out, err := arrays.Slice(context.Background(), arr, values.NewInt(6))
    41  
    42  		So(err, ShouldBeNil)
    43  		So(out.String(), ShouldEqual, "[]")
    44  	})
    45  
    46  	Convey("Should return a sliced array with a given start position and length", t, func() {
    47  		arr := values.NewArrayWith(
    48  			values.NewInt(1),
    49  			values.NewInt(2),
    50  			values.NewInt(3),
    51  			values.NewInt(4),
    52  			values.NewInt(5),
    53  			values.NewInt(6),
    54  		)
    55  
    56  		out, err := arrays.Slice(
    57  			context.Background(),
    58  			arr,
    59  			values.NewInt(2),
    60  			values.NewInt(2),
    61  		)
    62  
    63  		So(err, ShouldBeNil)
    64  		So(out.String(), ShouldEqual, "[3,4]")
    65  	})
    66  
    67  	Convey("Should return an empty array when length is out of bounds", t, func() {
    68  		arr := values.NewArrayWith(
    69  			values.NewInt(1),
    70  			values.NewInt(2),
    71  			values.NewInt(3),
    72  			values.NewInt(4),
    73  			values.NewInt(5),
    74  			values.NewInt(6),
    75  		)
    76  
    77  		out, err := arrays.Slice(context.Background(), arr, values.NewInt(2), values.NewInt(10))
    78  
    79  		So(err, ShouldBeNil)
    80  		So(out.String(), ShouldEqual, "[3,4,5,6]")
    81  	})
    82  }