| 60 | } |
| 61 | |
| 62 | func TestMap(t *testing.T) { |
| 63 | tests := []struct { |
| 64 | name string |
| 65 | slice []string |
| 66 | transform func(string) int |
| 67 | expected []int |
| 68 | }{ |
| 69 | { |
| 70 | name: "nil slice returns empty slice", |
| 71 | slice: nil, |
| 72 | transform: func(s string) int { return len(s) }, |
| 73 | expected: []int{}, |
| 74 | }, |
| 75 | { |
| 76 | name: "empty slice returns empty slice", |
| 77 | slice: []string{}, |
| 78 | transform: func(s string) int { return len(s) }, |
| 79 | expected: []int{}, |
| 80 | }, |
| 81 | { |
| 82 | name: "transforms each element", |
| 83 | slice: []string{"apple", "fig", "banana"}, |
| 84 | transform: func(s string) int { return len(s) }, |
| 85 | expected: []int{5, 3, 6}, |
| 86 | }, |
| 87 | { |
| 88 | name: "single element", |
| 89 | slice: []string{"hello"}, |
| 90 | transform: func(s string) int { return len(s) }, |
| 91 | expected: []int{5}, |
| 92 | }, |
| 93 | } |
| 94 | |
| 95 | for _, tt := range tests { |
| 96 | t.Run(tt.name, func(t *testing.T) { |
| 97 | result := Map(tt.slice, tt.transform) |
| 98 | assert.Equal(t, tt.expected, result, |
| 99 | "Map should transform all elements in slice %v", tt.slice) |
| 100 | }) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestDeduplicate(t *testing.T) { |
| 105 | tests := []struct { |