|
| 1 | +package godash_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/assert" |
| 5 | + "github.com/thecasualcoder/godash" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestReduce(t *testing.T) { |
| 11 | + t.Run("support primitive types", func(t *testing.T) { |
| 12 | + { |
| 13 | + in := []int{1, 2, 3} |
| 14 | + var out int |
| 15 | + |
| 16 | + err := godash.Reduce(in, &out, func(acc, element int) int { |
| 17 | + return acc + element |
| 18 | + }) |
| 19 | + |
| 20 | + expected := 6 |
| 21 | + assert.NoError(t, err) |
| 22 | + assert.Equal(t, expected, out) |
| 23 | + } |
| 24 | + { |
| 25 | + in := []int{1, 2, 3} |
| 26 | + var out string |
| 27 | + |
| 28 | + err := godash.Reduce(in, &out, func(acc string, element int) string { |
| 29 | + return acc + strconv.Itoa(element) |
| 30 | + }) |
| 31 | + |
| 32 | + expected := "123" |
| 33 | + assert.NoError(t, err) |
| 34 | + assert.Equal(t, expected, out) |
| 35 | + } |
| 36 | + { |
| 37 | + in := []string{"one", "two", "two", "three", "three", "three"} |
| 38 | + out := map[string]int{} |
| 39 | + |
| 40 | + err := godash.Reduce(in, &out, func(acc map[string]int, element string) map[string]int { |
| 41 | + if _, present := acc[element]; present { |
| 42 | + acc[element] = acc[element] + 1 |
| 43 | + } else { |
| 44 | + acc[element] = 1 |
| 45 | + } |
| 46 | + return acc |
| 47 | + }) |
| 48 | + |
| 49 | + expected := map[string]int{"one": 1, "two": 2, "three": 3} |
| 50 | + assert.NoError(t, err) |
| 51 | + assert.Equal(t, expected, out) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("support structs", func(t *testing.T) { |
| 56 | + type person struct { |
| 57 | + name string |
| 58 | + age int |
| 59 | + } |
| 60 | + |
| 61 | + in := []person{ |
| 62 | + {name: "john", age: 20}, |
| 63 | + {name: "doe", age: 23}, |
| 64 | + } |
| 65 | + out := 0 |
| 66 | + expected := 43 |
| 67 | + |
| 68 | + err := godash.Reduce(in, &out, func(acc int, p person) int { |
| 69 | + return acc + p.age |
| 70 | + }) |
| 71 | + |
| 72 | + assert.NoError(t, err) |
| 73 | + assert.Equal(t, expected, out) |
| 74 | + }) |
| 75 | + |
| 76 | + add := func(acc, element int) int { |
| 77 | + return acc + element |
| 78 | + } |
| 79 | + |
| 80 | + t.Run("should not panic if output is nil", func(t *testing.T) { |
| 81 | + in := []int{1, 2, 3} |
| 82 | + { |
| 83 | + var out int |
| 84 | + |
| 85 | + err := godash.Reduce(in, out, add) |
| 86 | + |
| 87 | + assert.EqualError(t, err, "cannot set out. Pass a reference to set output") |
| 88 | + } |
| 89 | + |
| 90 | + { |
| 91 | + err := godash.Reduce(in, nil, add) |
| 92 | + |
| 93 | + assert.EqualError(t, err, "output is nil. Pass a reference to set output") |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("should not accept reducer function that are not functions", func(t *testing.T) { |
| 98 | + in := []int{1, 2, 3} |
| 99 | + var out int |
| 100 | + |
| 101 | + err := godash.Reduce(in, &out, 7) |
| 102 | + |
| 103 | + assert.EqualError(t, err, "reduceFn has to be a (func) and not (int)") |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("should not accept reducer function that do not take exactly two argument", func(t *testing.T) { |
| 107 | + in := []int{1, 2, 3} |
| 108 | + var out int |
| 109 | + |
| 110 | + { |
| 111 | + err := godash.Reduce(in, &out, func() int { return 0 }) |
| 112 | + assert.EqualError(t, err, "reduceFn has to take exactly 2 arguments and not 0 argument(s)") |
| 113 | + } |
| 114 | + |
| 115 | + { |
| 116 | + err := godash.Reduce(in, &out, func(int) int { return 0 }) |
| 117 | + assert.EqualError(t, err, "reduceFn has to take exactly 2 arguments and not 1 argument(s)") |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + t.Run("should not accept reducer function that do not return exactly one value", func(t *testing.T) { |
| 122 | + in := []int{1, 2, 3} |
| 123 | + var out int |
| 124 | + |
| 125 | + { |
| 126 | + err := godash.Reduce(in, &out, func(int, int) {}) |
| 127 | + assert.EqualError(t, err, "reduceFn should have only one return value and not 0 return type(s)") |
| 128 | + } |
| 129 | + |
| 130 | + { |
| 131 | + err := godash.Reduce(in, &out, func(int, int) (int, int) { return 0, 0 }) |
| 132 | + assert.EqualError(t, err, "reduceFn should have only one return value and not 2 return type(s)") |
| 133 | + } |
| 134 | + }) |
| 135 | + |
| 136 | + t.Run("should accept reducer function whose first argument's kind should be output's kind", func(t *testing.T) { |
| 137 | + in := []int{1, 2, 3} |
| 138 | + var out int |
| 139 | + |
| 140 | + { |
| 141 | + err := godash.Reduce(in, &out, func(string, int) int { return 0 }) |
| 142 | + assert.EqualError(t, err, "reduceFn's first argument's type(string) has to be the type of out(int)") |
| 143 | + } |
| 144 | + |
| 145 | + { |
| 146 | + err := godash.Reduce(in, &out, func(int, int) int { return 0 }) |
| 147 | + assert.NoError(t, err) |
| 148 | + } |
| 149 | + }) |
| 150 | + |
| 151 | + t.Run("should accept reducer function whose second argument's kind should be input slice's element kind", func(t *testing.T) { |
| 152 | + in := []int{1, 2, 3} |
| 153 | + var out string |
| 154 | + |
| 155 | + { |
| 156 | + err := godash.Reduce(in, &out, func(string, string) string { return "" }) |
| 157 | + assert.EqualError(t, err, "reduceFn's second argument's type(string) has to be the type of element of input slice(int)") |
| 158 | + } |
| 159 | + |
| 160 | + { |
| 161 | + err := godash.Reduce(in, &out, func(string, int) string { return "" }) |
| 162 | + assert.NoError(t, err) |
| 163 | + } |
| 164 | + }) |
| 165 | + |
| 166 | + t.Run("should accept reducer function whose return kind should be output's kind", func(t *testing.T) { |
| 167 | + in := []int{1, 2, 3} |
| 168 | + var out string |
| 169 | + |
| 170 | + { |
| 171 | + err := godash.Reduce(in, &out, func(string, int) int { return 0 }) |
| 172 | + assert.EqualError(t, err, "reduceFn's return type(int) has to be the type of out(string)") |
| 173 | + } |
| 174 | + |
| 175 | + { |
| 176 | + err := godash.Reduce(in, &out, func(string, int) string { return "" }) |
| 177 | + assert.NoError(t, err) |
| 178 | + } |
| 179 | + }) |
| 180 | +} |
0 commit comments