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