-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_test.go
More file actions
159 lines (155 loc) · 3.23 KB
/
array_test.go
File metadata and controls
159 lines (155 loc) · 3.23 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
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
package arrayutil
import (
"reflect"
"testing"
)
func TestContains(t *testing.T) {
type anon struct {
prop string
}
anon1 := anon{prop: "anon 1"}
anon2 := anon{prop: "anon 2"}
type args struct {
arr interface{}
clause interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "String exists",
args: args{
arr: []interface{}{"this", "that"},
clause: "this",
},
want: true,
},
{
name: "Int exists",
args: args{
arr: []interface{}{1, 2},
clause: 2,
},
want: true,
},
{
name: "Referred object exists",
args: args{
arr: []interface{}{anon1, anon2},
clause: anon2,
},
want: true,
},
{
name: "Copy object exists",
args: args{
arr: []interface{}{anon1, anon2},
clause: anon{prop: "anon 1"},
},
want: true,
},
{
name: "Does not exists",
args: args{
arr: []interface{}{anon1, anon2},
clause: anon{prop: "not anon"},
},
want: false,
},
{
name: "Argument is not an array",
args: args{arr: "something", clause: "search"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Contains(tt.args.arr, tt.args.clause); got != tt.want {
t.Errorf("Contains() = %v, want %v", got, tt.want)
}
})
}
}
func TestReduce(t *testing.T) {
type Person struct {
Name string
Birthplace string
}
type PersonGroup map[string][]string
type args struct {
arr interface{}
initialValue interface{}
transform interface{}
}
tests := []struct {
name string
args args
want interface{}
wantErr bool
}{
{
name: "Input must be an array",
args: args{arr: "something"},
wantErr: true,
},
{
name: "Transform must not be nil",
args: args{arr: []int{1, 2, 3}, transform: nil},
wantErr: true,
},
{
name: "Transform must be a function",
args: args{arr: []int{1, 2, 3}, transform: "something"},
wantErr: true,
},
{
name: "Sum of array",
args: args{
arr: []int{1, 2, 3},
initialValue: 0,
transform: func(accumulator, entry, idx int) int {
return accumulator + entry
},
},
wantErr: false,
want: 6,
},
{
name: "Group by person's name",
args: args{
arr: []Person{
Person{"John Doe", "Jakarta"},
Person{"John Doe", "Depok"},
Person{"John Doe", "Medan"},
},
initialValue: make(PersonGroup),
transform: func(accumulator PersonGroup, entry Person, idx int) PersonGroup {
birthplaces, exists := accumulator[entry.Name]
if !exists {
birthplaces = []string{entry.Birthplace}
} else {
birthplaces = append(birthplaces, entry.Birthplace)
}
accumulator[entry.Name] = birthplaces
return accumulator
},
},
wantErr: false,
want: PersonGroup{"John Doe": []string{"Jakarta", "Depok", "Medan"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Reduce(tt.args.arr, tt.args.initialValue, tt.args.transform)
if (err != nil) != tt.wantErr {
t.Errorf("Reduce() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Reduce() = %v, want %v", got, tt.want)
}
})
}
}