github.com/m4gshm/gollections@v0.0.10/loop/test/api_group_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/m4gshm/gollections/convert/as"
     9  	"github.com/m4gshm/gollections/loop/group"
    10  	"github.com/m4gshm/gollections/op"
    11  	"github.com/m4gshm/gollections/slice"
    12  )
    13  
    14  func Test_group_odd_even(t *testing.T) {
    15  	var (
    16  		even   = func(v int) bool { return v%2 == 0 }
    17  		groups = group.Of(slice.NewIter(slice.Of(1, 1, 2, 4, 3, 1)).Next, even, as.Is[int])
    18  	)
    19  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    20  }
    21  
    22  func Test_ByMultiple(t *testing.T) {
    23  	var (
    24  		even   = func(v int) bool { return v%2 == 0 }
    25  		groups = group.ByMultiple(slice.NewIter(slice.Of(1, 1, 2, 4, 3, 1)).Next, func(i int) []bool { return slice.Of(even(i)) }, as.Slice[int])
    26  	)
    27  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    28  }
    29  
    30  func Test_ByMultipleEmptyKey(t *testing.T) {
    31  	var (
    32  		even   = func(v int) bool { return v%2 == 0 }
    33  		groups = group.ByMultiple(slice.NewIter(slice.Of(1, 1, 2, 4, 3, 1)).Next, func(i int) []bool { return op.IfElse(even(i), slice.Of(true), nil) }, as.Slice[int])
    34  	)
    35  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    36  }
    37  
    38  func Test_ByMultipleEmptyVal(t *testing.T) {
    39  	var (
    40  		even   = func(v int) bool { return v%2 == 0 }
    41  		groups = group.ByMultiple(slice.NewIter(slice.Of(1, 1, 2, 4, 3, 1)).Next,
    42  			func(i int) []bool { return slice.Of(even(i)) },
    43  			func(i int) []int { return op.IfElse(even(i), nil, slice.Of(i)) },
    44  		)
    45  	)
    46  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {0, 0}}, groups)
    47  }
    48  
    49  func Test_ByMultipleKeys(t *testing.T) {
    50  	var (
    51  		even   = func(v int) bool { return v%2 == 0 }
    52  		groups = group.ByMultipleKeys(slice.NewIter(slice.Of(1, 1, 2, 4, 3, 1)).Next, func(i int) []bool { return slice.Of(even(i)) }, as.Is[int])
    53  	)
    54  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    55  }
    56  
    57  func Test_ByMultipleValues(t *testing.T) {
    58  
    59  	var (
    60  		even   = func(v int) bool { return v%2 == 0 }
    61  		groups = group.ByMultipleValues(slice.NewIter(slice.Of(1, 1, 2, 4, 3, 1)).Next, even, as.Slice[int])
    62  	)
    63  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    64  }