github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/flatten_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 TestFlatten(t *testing.T) {
    14  	Convey("Should flatten an array with depth 1", t, func() {
    15  		arr := values.NewArrayWith(
    16  			values.NewInt(1),
    17  			values.NewInt(2),
    18  			values.NewArrayWith(
    19  				values.NewInt(3),
    20  				values.NewInt(4),
    21  				values.NewArrayWith(
    22  					values.NewInt(5),
    23  					values.NewInt(6),
    24  				),
    25  			),
    26  			values.NewInt(7),
    27  			values.NewArrayWith(
    28  				values.NewInt(8),
    29  				values.NewArrayWith(
    30  					values.NewInt(9),
    31  					values.NewArrayWith(
    32  						values.NewInt(10),
    33  					),
    34  				),
    35  			),
    36  		)
    37  
    38  		out, err := arrays.Flatten(context.Background(), arr)
    39  
    40  		So(err, ShouldBeNil)
    41  		So(out.String(), ShouldEqual, "[1,2,3,4,[5,6],7,8,[9,[10]]]")
    42  	})
    43  
    44  	Convey("Should flatten an array with depth more than 1", t, func() {
    45  		arr := values.NewArrayWith(
    46  			values.NewInt(1),
    47  			values.NewInt(2),
    48  			values.NewArrayWith(
    49  				values.NewInt(3),
    50  				values.NewInt(4),
    51  				values.NewArrayWith(
    52  					values.NewInt(5),
    53  					values.NewInt(6),
    54  				),
    55  			),
    56  			values.NewInt(7),
    57  			values.NewArrayWith(
    58  				values.NewInt(8),
    59  				values.NewArrayWith(
    60  					values.NewInt(9),
    61  					values.NewArrayWith(
    62  						values.NewInt(10),
    63  					),
    64  				),
    65  			),
    66  		)
    67  
    68  		out, err := arrays.Flatten(context.Background(), arr, values.NewInt(2))
    69  
    70  		So(err, ShouldBeNil)
    71  		So(out.String(), ShouldEqual, "[1,2,3,4,5,6,7,8,9,[10]]")
    72  	})
    73  }