-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathaggregate_test.go
More file actions
84 lines (69 loc) · 1.82 KB
/
Copy pathaggregate_test.go
File metadata and controls
84 lines (69 loc) · 1.82 KB
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
package linq
import (
"strings"
"testing"
)
func TestAggregate(t *testing.T) {
input := []string{"apple", "mango", "orange", "passionfruit", "grape"}
r, ok := FromSlice(input).Aggregate(func(r string, i string) string {
if len(r) > len(i) {
return r
}
return i
})
if !ok || r != "passionfruit" {
t.Errorf("FromSlice(%v).Aggregate()=%v,%v expected passionfruit,true", input, r, ok)
}
}
func TestAggregate_Empty(t *testing.T) {
r, ok := FromSlice([]string{}).Aggregate(func(r string, i string) string {
return r
})
if ok || r != "" {
t.Errorf("FromSlice([]).Aggregate()=%v,%v expected \"\",false", r, ok)
}
}
func TestAggregateWithSeed(t *testing.T) {
input := []string{"apple", "mango", "orange", "banana", "grape"}
want := "passionfruit"
r := FromSlice(input).AggregateWithSeed(want,
func(r string, i string) string {
if len(r) > len(i) {
return r
}
return i
})
if r != want {
t.Errorf("FromSlice(%v).AggregateWithSeed()=%v expected %v", input, r, want)
}
}
func TestAggregateWithSeed_TypeChanging(t *testing.T) {
// The accumulator type (int) differs from the element type (string).
input := []string{"apple", "mango", "orange"}
want := 16
r := FromSlice(input).AggregateWithSeed(0,
func(acc int, i string) int {
return acc + len(i)
})
if r != want {
t.Errorf("FromSlice(%v).AggregateWithSeed()=%v expected %v", input, r, want)
}
}
func TestAggregateWithSeedBy(t *testing.T) {
input := []string{"apple", "mango", "orange", "passionfruit", "grape"}
want := "PASSIONFRUIT"
r := FromSlice(input).AggregateWithSeedBy("banana",
func(r string, i string) string {
if len(r) > len(i) {
return r
}
return i
},
func(r string) string {
return strings.ToUpper(r)
},
)
if r != want {
t.Errorf("FromSlice(%v).AggregateWithSeedBy()=%v expected %v", input, r, want)
}
}