github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/minus_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/core"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values"
    11  	"github.com/MontFerret/ferret/pkg/stdlib/arrays"
    12  )
    13  
    14  func TestMinus(t *testing.T) {
    15  	Convey("Should find differences between 2 arrays", t, func() {
    16  		arr1 := values.NewArrayWith(
    17  			values.NewInt(1),
    18  			values.NewInt(2),
    19  			values.NewInt(3),
    20  			values.NewInt(4),
    21  		)
    22  
    23  		arr2 := values.NewArrayWith(
    24  			values.NewInt(3),
    25  			values.NewInt(4),
    26  			values.NewInt(5),
    27  			values.NewInt(6),
    28  		)
    29  
    30  		out, err := arrays.Minus(context.Background(), arr1, arr2)
    31  
    32  		check := map[int]bool{
    33  			1: true,
    34  			2: true,
    35  		}
    36  
    37  		So(err, ShouldBeNil)
    38  
    39  		arr := out.(*values.Array)
    40  
    41  		So(arr.Length(), ShouldEqual, 2)
    42  
    43  		arr.ForEach(func(value core.Value, idx int) bool {
    44  			_, exists := check[int(value.(values.Int))]
    45  
    46  			So(exists, ShouldBeTrue)
    47  
    48  			return true
    49  		})
    50  	})
    51  
    52  	Convey("Should find differences between more than 2 arrays", t, func() {
    53  		arr1 := values.NewArrayWith(
    54  			values.NewInt(1),
    55  			values.NewInt(2),
    56  			values.NewInt(3),
    57  			values.NewInt(4),
    58  		)
    59  
    60  		arr2 := values.NewArrayWith(
    61  			values.NewInt(3),
    62  			values.NewInt(9),
    63  			values.NewInt(5),
    64  			values.NewInt(6),
    65  		)
    66  
    67  		arr3 := values.NewArrayWith(
    68  			values.NewInt(4),
    69  			values.NewInt(5),
    70  			values.NewInt(6),
    71  			values.NewInt(7),
    72  			values.NewInt(8),
    73  		)
    74  
    75  		out, err := arrays.Minus(context.Background(), arr1, arr2, arr3)
    76  
    77  		check := map[int]bool{
    78  			1: true,
    79  			2: true,
    80  		}
    81  
    82  		So(err, ShouldBeNil)
    83  
    84  		arr := out.(*values.Array)
    85  
    86  		So(arr.Length(), ShouldEqual, 2)
    87  
    88  		arr.ForEach(func(value core.Value, idx int) bool {
    89  			_, exists := check[int(value.(values.Int))]
    90  
    91  			So(exists, ShouldBeTrue)
    92  
    93  			return true
    94  		})
    95  	})
    96  }