|
| 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 | + |
| 38 | + t.Run("support structs", func(t *testing.T) { |
| 39 | + type person struct { |
| 40 | + name string |
| 41 | + age int |
| 42 | + } |
| 43 | + |
| 44 | + in := []person{ |
| 45 | + {name: "john", age: 20}, |
| 46 | + {name: "doe", age: 23}, |
| 47 | + } |
| 48 | + out := 0 |
| 49 | + expected := 43 |
| 50 | + |
| 51 | + err := godash.Reduce(in, &out, func(acc int, p person) int { |
| 52 | + return acc + p.age |
| 53 | + }) |
| 54 | + |
| 55 | + assert.NoError(t, err) |
| 56 | + assert.Equal(t, expected, out) |
| 57 | + }) |
| 58 | + |
| 59 | + add := func(acc, element int) int { |
| 60 | + return acc + element |
| 61 | + } |
| 62 | + |
| 63 | + t.Run("should not panic if output is nil", func(t *testing.T) { |
| 64 | + in := []int{1, 2, 3} |
| 65 | + { |
| 66 | + var out int |
| 67 | + |
| 68 | + err := godash.Reduce(in, out, add) |
| 69 | + |
| 70 | + assert.EqualError(t, err, "cannot set out. Pass a reference to set output") |
| 71 | + } |
| 72 | + |
| 73 | + { |
| 74 | + err := godash.Reduce(in, nil, add) |
| 75 | + |
| 76 | + assert.EqualError(t, err, "output is nil. Pass a reference to set output") |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("should not accept reducer function that are not functions", func(t *testing.T) { |
| 81 | + in := []int{1, 2, 3} |
| 82 | + var out int |
| 83 | + |
| 84 | + err := godash.Reduce(in, &out, 7) |
| 85 | + |
| 86 | + assert.EqualError(t, err, "reduceFn has to be a function") |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("should not accept reducer function that do not take exactly two argument", func(t *testing.T) { |
| 90 | + in := []int{1, 2, 3} |
| 91 | + var out int |
| 92 | + |
| 93 | + { |
| 94 | + err := godash.Reduce(in, &out, func() int { return 0 }) |
| 95 | + assert.EqualError(t, err, "reducer function has to take exactly two argument") |
| 96 | + } |
| 97 | + |
| 98 | + { |
| 99 | + err := godash.Reduce(in, &out, func(int) int { return 0 }) |
| 100 | + assert.EqualError(t, err, "reducer function has to take exactly two argument") |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("should not accept reducer function that do not return exactly one value", func(t *testing.T) { |
| 105 | + in := []int{1, 2, 3} |
| 106 | + var out int |
| 107 | + |
| 108 | + { |
| 109 | + err := godash.Reduce(in, &out, func(int, int) {}) |
| 110 | + assert.EqualError(t, err, "reducer function should return only one return value") |
| 111 | + } |
| 112 | + |
| 113 | + { |
| 114 | + err := godash.Reduce(in, &out, func(int, int) (int, int) { return 0, 0 }) |
| 115 | + assert.EqualError(t, err, "reducer function should return only one return value") |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run("should accept reducer function whose first argument's kind should be output's kind", func(t *testing.T) { |
| 120 | + in := []int{1, 2, 3} |
| 121 | + var out int |
| 122 | + |
| 123 | + { |
| 124 | + err := godash.Reduce(in, &out, func(string, int) int { return 0 }) |
| 125 | + assert.EqualError(t, err, "reducer function's first argument has to be the type of out") |
| 126 | + } |
| 127 | + |
| 128 | + { |
| 129 | + err := godash.Reduce(in, &out, func(int, int) int { return 0 }) |
| 130 | + assert.NoError(t, err) |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("should accept reducer function whose second argument's kind should be input slice's element kind", func(t *testing.T) { |
| 135 | + in := []int{1, 2, 3} |
| 136 | + var out string |
| 137 | + |
| 138 | + { |
| 139 | + err := godash.Reduce(in, &out, func(string, string) string { return "" }) |
| 140 | + assert.EqualError(t, err, "reducer function's second argument has to be the type of element of input slice") |
| 141 | + } |
| 142 | + |
| 143 | + { |
| 144 | + err := godash.Reduce(in, &out, func(string, int) string { return "" }) |
| 145 | + assert.NoError(t, err) |
| 146 | + } |
| 147 | + }) |
| 148 | + |
| 149 | + t.Run("should accept reducer function whose return kind should be output's kind", func(t *testing.T) { |
| 150 | + in := []int{1, 2, 3} |
| 151 | + var out string |
| 152 | + |
| 153 | + { |
| 154 | + err := godash.Reduce(in, &out, func(string, int) int { return 0 }) |
| 155 | + assert.EqualError(t, err, "reducer function's return type has to be the type of out") |
| 156 | + } |
| 157 | + |
| 158 | + { |
| 159 | + err := godash.Reduce(in, &out, func(string, int) string { return "" }) |
| 160 | + assert.NoError(t, err) |
| 161 | + } |
| 162 | + }) |
| 163 | +} |
0 commit comments