github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/unshift_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 TestUnshift(t *testing.T) {
    14  	Convey("Should return a copy of an array", 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  		)
    22  
    23  		out, err := arrays.Unshift(context.Background(), arr, values.NewInt(0))
    24  
    25  		So(err, ShouldBeNil)
    26  		So(out, ShouldNotEqual, arr)
    27  		So(out.String(), ShouldEqual, "[0,1,2,3,4,5]")
    28  	})
    29  
    30  	Convey("Should ignore non-unique items", 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  		)
    38  
    39  		out, err := arrays.Unshift(
    40  			context.Background(),
    41  			arr,
    42  			values.NewInt(0),
    43  			values.True,
    44  		)
    45  
    46  		So(err, ShouldBeNil)
    47  		So(out, ShouldNotEqual, arr)
    48  		So(out.String(), ShouldEqual, "[0,1,2,3,4,5]")
    49  
    50  		out2, err := arrays.Unshift(
    51  			context.Background(),
    52  			arr,
    53  			values.NewInt(0),
    54  			values.True,
    55  		)
    56  
    57  		So(err, ShouldBeNil)
    58  		So(out2, ShouldNotEqual, arr)
    59  		So(out.String(), ShouldEqual, "[0,1,2,3,4,5]")
    60  	})
    61  }