github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/sliceexamples/slice_ToMapResolv_test.go (about)

     1  package sliceexamples
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/m4gshm/gollections/map_/resolv"
     9  	"github.com/m4gshm/gollections/op"
    10  	"github.com/m4gshm/gollections/slice"
    11  )
    12  
    13  func Test_Slice_ToMapResolv(t *testing.T) {
    14  
    15  	var ageGroupedSortedNames map[string][]string
    16  
    17  	ageGroupedSortedNames = slice.ToMapResolv(users, func(u User) string {
    18  		return op.IfElse(u.age <= 30, "<=30", ">30")
    19  	}, User.Name, resolv.SortedSlice)
    20  
    21  	//map[<=30:[Bob Tom] >30:[Alice Chris]]
    22  
    23  	assert.Equal(t, slice.Of("Bob", "Tom"), ageGroupedSortedNames["<=30"])
    24  	assert.Equal(t, slice.Of("Alice", "Chris"), ageGroupedSortedNames[">30"])
    25  }