github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/push_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 TestPush(t *testing.T) {
    14  	Convey("Should create a new array with a new element in the end", 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.Push(context.Background(), arr, values.NewInt(6))
    24  
    25  		So(err, ShouldBeNil)
    26  		So(out.String(), ShouldEqual, "[1,2,3,4,5,6]")
    27  	})
    28  
    29  	Convey("Should not add a new element if not unique when uniqueness check is enabled", t, func() {
    30  		arr := values.NewArrayWith(
    31  			values.NewInt(1),
    32  			values.NewInt(2),
    33  			values.NewInt(3),
    34  			values.NewInt(4),
    35  			values.NewInt(5),
    36  		)
    37  
    38  		out, err := arrays.Push(
    39  			context.Background(),
    40  			arr,
    41  			values.NewInt(6),
    42  			values.True,
    43  		)
    44  
    45  		So(err, ShouldBeNil)
    46  		So(out.String(), ShouldEqual, "[1,2,3,4,5,6]")
    47  
    48  		out2, err := arrays.Push(
    49  			context.Background(),
    50  			arr,
    51  			values.NewInt(6),
    52  			values.True,
    53  		)
    54  
    55  		So(err, ShouldBeNil)
    56  		So(out2.String(), ShouldEqual, "[1,2,3,4,5,6]")
    57  	})
    58  }