github.com/MontFerret/ferret@v0.18.0/pkg/runtime/collections/unique_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/collections"
    10  	"github.com/MontFerret/ferret/pkg/runtime/core"
    11  	"github.com/MontFerret/ferret/pkg/runtime/values"
    12  )
    13  
    14  func TestUniqueIterator(t *testing.T) {
    15  	Convey("Should return only unique items", t, func() {
    16  		arr := []core.Value{
    17  			values.NewInt(1),
    18  			values.NewInt(2),
    19  			values.NewInt(2),
    20  			values.NewInt(3),
    21  			values.NewInt(4),
    22  			values.NewInt(3),
    23  			values.NewInt(5),
    24  			values.NewInt(6),
    25  			values.NewInt(5),
    26  			values.NewInt(6),
    27  		}
    28  
    29  		iter, err := collections.NewUniqueIterator(
    30  			sliceIterator(arr),
    31  			collections.DefaultValueVar,
    32  		)
    33  
    34  		So(err, ShouldBeNil)
    35  		ctx := context.Background()
    36  		scope, _ := core.NewRootScope()
    37  
    38  		sets, err := collections.ToSlice(ctx, scope, iter)
    39  
    40  		So(err, ShouldBeNil)
    41  
    42  		res := toArrayOfValues(sets)
    43  
    44  		So(res.String(), ShouldEqual, `[1,2,3,4,5,6]`)
    45  	})
    46  
    47  	Convey("Should return only unique items 2", t, func() {
    48  		arr := []core.Value{
    49  			values.NewInt(1),
    50  			values.NewInt(1),
    51  			values.NewInt(1),
    52  			values.NewInt(1),
    53  			values.NewInt(1),
    54  			values.NewInt(1),
    55  		}
    56  
    57  		iter, err := collections.NewUniqueIterator(
    58  			sliceIterator(arr),
    59  			collections.DefaultValueVar,
    60  		)
    61  
    62  		So(err, ShouldBeNil)
    63  
    64  		ctx := context.Background()
    65  		scope, _ := core.NewRootScope()
    66  
    67  		sets, err := collections.ToSlice(ctx, scope, iter)
    68  
    69  		So(err, ShouldBeNil)
    70  
    71  		res := toArrayOfValues(sets)
    72  
    73  		So(res.String(), ShouldEqual, `[1]`)
    74  	})
    75  
    76  	Convey("Should return only unique items 3", t, func() {
    77  		arr := []core.Value{
    78  			values.NewString("a"),
    79  			values.NewString("b"),
    80  			values.NewString("c"),
    81  			values.NewString("d"),
    82  			values.NewString("e"),
    83  			values.NewString("a"),
    84  			values.NewString("b"),
    85  			values.NewString("f"),
    86  			values.NewString("d"),
    87  			values.NewString("e"),
    88  			values.NewString("f"),
    89  		}
    90  
    91  		iter, err := collections.NewUniqueIterator(
    92  			sliceIterator(arr),
    93  			collections.DefaultValueVar,
    94  		)
    95  
    96  		So(err, ShouldBeNil)
    97  
    98  		ctx := context.Background()
    99  		scope, _ := core.NewRootScope()
   100  
   101  		sets, err := collections.ToSlice(ctx, scope, iter)
   102  
   103  		So(err, ShouldBeNil)
   104  
   105  		res := toArrayOfValues(sets)
   106  
   107  		So(res.String(), ShouldEqual, `["a","b","c","d","e","f"]`)
   108  	})
   109  }