github.com/seeker-insurance/kit@v0.0.13/maputil/maputil_test.go (about) 1 package maputil 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 var m = map[string]interface{}{"a": 1, "b": 2, "c": "bar"} 11 12 func TestSortedKeys(t *testing.T) { 13 want := []string{"a", "b", "c"} 14 got := SortedKeys(m) 15 assert.Equal(t, want, got) 16 } 17 18 func TestVals(t *testing.T) { 19 want := []interface{}{1, 2, "bar"} 20 got := Vals(m) 21 sort.Sort(sorter(got)) 22 assert.Equal(t, want, got) 23 } 24 25 //sorter considers ints to be smaller than strings 26 type sorter []interface{} 27 28 func (s sorter) Less(i, j int) bool { 29 if a, isInt := s[i].(int); isInt { 30 if b, isInt := s[j].(int); isInt { 31 return a < b 32 } 33 return true 34 } 35 if a, isStr := s[i].(string); isStr { 36 if b, isStr := s[j].(string); isStr { 37 return a < b 38 } 39 return false 40 } 41 panic("this shouldn't happen") 42 } 43 func (s sorter) Len() int { return len(s) } 44 func (s sorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 45 46 func TestCopy(t *testing.T) { 47 start := map[string]interface{}{"1": 1} 48 got := Copy(start) 49 assert.Equal(t, start, got) 50 start["2"] = 2 51 assert.NotEqual(t, start, got) // no leak 52 }