| 40 | } |
| 41 | |
| 42 | func TestKeyBy(t *testing.T) { |
| 43 | t.Parallel() |
| 44 | |
| 45 | type foo struct { |
| 46 | baz string |
| 47 | bar int |
| 48 | } |
| 49 | transform := func(f *foo) (string, int) { |
| 50 | return f.baz, f.bar |
| 51 | } |
| 52 | testCases := []struct { |
| 53 | in []*foo |
| 54 | expect map[string]int |
| 55 | }{ |
| 56 | { |
| 57 | in: []*foo{{baz: "apple", bar: 1}}, |
| 58 | expect: map[string]int{"apple": 1}, |
| 59 | }, |
| 60 | { |
| 61 | in: []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}}, |
| 62 | expect: map[string]int{"apple": 1, "banana": 2}, |
| 63 | }, |
| 64 | { |
| 65 | in: []*foo{{baz: "apple", bar: 1}, {baz: "apple", bar: 2}}, |
| 66 | expect: map[string]int{"apple": 2}, |
| 67 | }, |
| 68 | } |
| 69 | for i, tt := range testCases { |
| 70 | t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) { |
| 71 | t.Parallel() |
| 72 | |
| 73 | require.Equal(t, KeyBy(tt.in, transform), tt.expect) |
| 74 | }) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestMap(t *testing.T) { |
| 79 | t.Parallel() |