github.com/go-playground/pkg/v5@v5.29.1/map/map_test.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 package mapext 5 6 import ( 7 . "github.com/go-playground/assert/v2" 8 "sort" 9 "testing" 10 ) 11 12 func TestRetain(t *testing.T) { 13 m := map[string]int{ 14 "0": 0, 15 "1": 1, 16 "2": 2, 17 "3": 3, 18 } 19 Retain(m, func(key string, value int) bool { 20 return value < 1 || value > 2 21 }) 22 Equal(t, len(m), 2) 23 Equal(t, m["0"], 0) 24 Equal(t, m["3"], 3) 25 } 26 27 func TestMap(t *testing.T) { 28 // Test Map to slice 29 m := map[string]int{ 30 "0": 0, 31 "1": 1, 32 } 33 slice := Map(m, make([]int, 0, len(m)), func(accum []int, key string, value int) []int { 34 return append(accum, value) 35 }) 36 sort.SliceStable(slice, func(i, j int) bool { 37 return i < j 38 }) 39 Equal(t, len(slice), 2) 40 41 // Test Map to Map of different type 42 inverted := Map(m, make(map[int]string, len(m)), func(accum map[int]string, key string, value int) map[int]string { 43 accum[value] = key 44 return accum 45 }) 46 Equal(t, len(inverted), 2) 47 Equal(t, inverted[0], "0") 48 Equal(t, inverted[1], "1") 49 }