github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/collections/reverse_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/values"
    10  	"github.com/MontFerret/ferret/pkg/stdlib/collections"
    11  )
    12  
    13  func TestReverse(t *testing.T) {
    14  	Convey("When args are not passed", t, func() {
    15  		Convey("It should return an error", func() {
    16  			var err error
    17  			_, err = collections.Reverse(context.Background())
    18  
    19  			So(err, ShouldBeError)
    20  		})
    21  	})
    22  
    23  	Convey("Should reverse a text with right encoding", t, func() {
    24  		out, _ := collections.Reverse(
    25  			context.Background(),
    26  			values.NewString("The quick brown 狐 jumped over the lazy 犬"),
    27  		)
    28  
    29  		So(out, ShouldEqual, "犬 yzal eht revo depmuj 狐 nworb kciuq ehT")
    30  	})
    31  
    32  	Convey("Should return a copy of an array with reversed elements", t, func() {
    33  		arr := values.NewArrayWith(
    34  			values.NewInt(1),
    35  			values.NewInt(2),
    36  			values.NewInt(3),
    37  			values.NewInt(4),
    38  			values.NewInt(5),
    39  			values.NewInt(6),
    40  		)
    41  
    42  		out, err := collections.Reverse(
    43  			context.Background(),
    44  			arr,
    45  		)
    46  
    47  		So(err, ShouldBeNil)
    48  		So(out.String(), ShouldEqual, "[6,5,4,3,2,1]")
    49  	})
    50  
    51  	Convey("Should return an empty array when there no elements in a source one", t, func() {
    52  		arr := values.NewArray(0)
    53  
    54  		out, err := collections.Reverse(
    55  			context.Background(),
    56  			arr,
    57  		)
    58  
    59  		So(err, ShouldBeNil)
    60  		So(out.String(), ShouldEqual, "[]")
    61  	})
    62  }