github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/slice/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/slice"
    10  	"github.com/m4gshm/gollections/slice/group"
    11  )
    12  
    13  func Test_group_odd_even(t *testing.T) {
    14  	var (
    15  		even   = func(v int) bool { return v%2 == 0 }
    16  		groups = group.Of(slice.Of(1, 1, 2, 4, 3, 1), even, as.Is[int])
    17  	)
    18  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    19  }
    20  
    21  func Test_ByMultiple(t *testing.T) {
    22  	var (
    23  		even   = func(v int) bool { return v%2 == 0 }
    24  		groups = group.ByMultiple(slice.Of(1, 1, 2, 4, 3, 1), func(i int) []bool { return slice.Of(even(i)) }, func(i int) []int { return slice.Of(i) })
    25  	)
    26  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    27  }
    28  
    29  func Test_ByMultipleKeys(t *testing.T) {
    30  	var (
    31  		even   = func(v int) bool { return v%2 == 0 }
    32  		groups = group.ByMultipleKeys(slice.Of(1, 1, 2, 4, 3, 1), func(i int) []bool { return slice.Of(even(i)) }, as.Is[int])
    33  	)
    34  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    35  }
    36  
    37  func Test_ByMultipleValues(t *testing.T) {
    38  	var (
    39  		even   = func(v int) bool { return v%2 == 0 }
    40  		groups = group.ByMultipleValues(slice.Of(1, 1, 2, 4, 3, 1), even, func(i int) []int { return slice.Of(i) })
    41  	)
    42  	assert.Equal(t, map[bool][]int{false: {1, 1, 3, 1}, true: {2, 4}}, groups)
    43  }