|
1
|
|
|
package generic |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"fmt" |
|
5
|
|
|
"reflect" |
|
6
|
|
|
"testing" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/stretchr/testify/assert" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
func TestNewIterable_WhenNotIterable_ExpectNotIterableError(t *testing.T) { |
|
12
|
|
|
iterable, err := NewIterable(0) |
|
13
|
|
|
|
|
14
|
|
|
assert.Nil(t, iterable) |
|
15
|
|
|
assert.EqualError(t, err, "value of type int is not iterable") |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
func TestNewIterable_WhenNilSlice_ExpectIsNil(t *testing.T) { |
|
19
|
|
|
var slice []string |
|
20
|
|
|
|
|
21
|
|
|
iterable, err := NewIterable(slice) |
|
22
|
|
|
|
|
23
|
|
|
if assert.NoError(t, err) { |
|
24
|
|
|
assert.True(t, iterable.IsNil()) |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
func TestNewIterable_WhenNilMap_ExpectIsNil(t *testing.T) { |
|
29
|
|
|
var value map[string]string |
|
30
|
|
|
|
|
31
|
|
|
iterable, err := NewIterable(value) |
|
32
|
|
|
|
|
33
|
|
|
if assert.NoError(t, err) { |
|
34
|
|
|
assert.True(t, iterable.IsNil()) |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
func TestNewIterable_WhenSliceElementImplementsInterface_ExpectTrue(t *testing.T) { |
|
39
|
|
|
var value []testInterface |
|
40
|
|
|
interfaceType := reflect.TypeOf((*testInterface)(nil)).Elem() |
|
41
|
|
|
|
|
42
|
|
|
iterable, err := NewIterable(value) |
|
43
|
|
|
|
|
44
|
|
|
if assert.NoError(t, err) { |
|
45
|
|
|
implements := iterable.IsElementImplements(interfaceType) |
|
46
|
|
|
assert.True(t, implements) |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
func TestNewIterable_WhenMapElementImplementsInterface_ExpectTrue(t *testing.T) { |
|
51
|
|
|
var value map[string]testInterface |
|
52
|
|
|
interfaceType := reflect.TypeOf((*testInterface)(nil)).Elem() |
|
53
|
|
|
|
|
54
|
|
|
iterable, err := NewIterable(value) |
|
55
|
|
|
|
|
56
|
|
|
if assert.NoError(t, err) { |
|
57
|
|
|
implements := iterable.IsElementImplements(interfaceType) |
|
58
|
|
|
assert.True(t, implements) |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
func TestIterableArray_Iterate_WhenBreakAtFirstElement_ExpectCountIsOne(t *testing.T) { |
|
63
|
|
|
count := 0 |
|
64
|
|
|
iterable, err := NewIterable([]string{"a", "b", "c"}) |
|
65
|
|
|
if err != nil { |
|
66
|
|
|
t.Fatal("failed to initialize iterable from slice") |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
err = iterable.Iterate(func(key Key, value interface{}) error { |
|
70
|
|
|
count++ |
|
71
|
|
|
return fmt.Errorf("error") |
|
72
|
|
|
}) |
|
73
|
|
|
|
|
74
|
|
|
assert.EqualError(t, err, "error") |
|
75
|
|
|
assert.Equal(t, 1, count) |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
func TestIterableMap_Iterate_WhenBreakAtFirstElement_ExpectCountIsOne(t *testing.T) { |
|
79
|
|
|
count := 0 |
|
80
|
|
|
iterable, err := NewIterable(map[string]string{"a": "a", "b": "b", "c": "c"}) |
|
81
|
|
|
if err != nil { |
|
82
|
|
|
t.Fatal("failed to initialize iterable from map") |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
err = iterable.Iterate(func(key Key, value interface{}) error { |
|
86
|
|
|
count++ |
|
87
|
|
|
return fmt.Errorf("error") |
|
88
|
|
|
}) |
|
89
|
|
|
|
|
90
|
|
|
assert.EqualError(t, err, "error") |
|
91
|
|
|
assert.Equal(t, 1, count) |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
func TestNewIterable(t *testing.T) { |
|
95
|
|
|
tests := []struct { |
|
96
|
|
|
name string |
|
97
|
|
|
value interface{} |
|
98
|
|
|
expectedKey string |
|
99
|
|
|
expectedIsIndex bool |
|
100
|
|
|
expectedIndex int |
|
101
|
|
|
expectedValue interface{} |
|
102
|
|
|
}{ |
|
103
|
|
|
{ |
|
104
|
|
|
name: "array of strings", |
|
105
|
|
|
value: [...]string{"value"}, |
|
106
|
|
|
expectedKey: "0", |
|
107
|
|
|
expectedIsIndex: true, |
|
108
|
|
|
expectedIndex: 0, |
|
109
|
|
|
expectedValue: "value", |
|
110
|
|
|
}, |
|
111
|
|
|
{ |
|
112
|
|
|
name: "array of string pointers", |
|
113
|
|
|
value: [...]*string{stringPointer("value")}, |
|
114
|
|
|
expectedKey: "0", |
|
115
|
|
|
expectedIsIndex: true, |
|
116
|
|
|
expectedIndex: 0, |
|
117
|
|
|
expectedValue: "value", |
|
118
|
|
|
}, |
|
119
|
|
|
{ |
|
120
|
|
|
name: "array of string pointers with nil", |
|
121
|
|
|
value: [...]*string{nil}, |
|
122
|
|
|
expectedKey: "0", |
|
123
|
|
|
expectedIsIndex: true, |
|
124
|
|
|
expectedIndex: 0, |
|
125
|
|
|
expectedValue: nil, |
|
126
|
|
|
}, |
|
127
|
|
|
{ |
|
128
|
|
|
name: "slice of strings", |
|
129
|
|
|
value: []string{"value"}, |
|
130
|
|
|
expectedKey: "0", |
|
131
|
|
|
expectedIsIndex: true, |
|
132
|
|
|
expectedIndex: 0, |
|
133
|
|
|
expectedValue: "value", |
|
134
|
|
|
}, |
|
135
|
|
|
{ |
|
136
|
|
|
name: "slice of string pointers", |
|
137
|
|
|
value: []*string{stringPointer("value")}, |
|
138
|
|
|
expectedKey: "0", |
|
139
|
|
|
expectedIsIndex: true, |
|
140
|
|
|
expectedIndex: 0, |
|
141
|
|
|
expectedValue: "value", |
|
142
|
|
|
}, |
|
143
|
|
|
{ |
|
144
|
|
|
name: "slice of string pointers with nil", |
|
145
|
|
|
value: []*string{nil}, |
|
146
|
|
|
expectedKey: "0", |
|
147
|
|
|
expectedIsIndex: true, |
|
148
|
|
|
expectedIndex: 0, |
|
149
|
|
|
expectedValue: nil, |
|
150
|
|
|
}, |
|
151
|
|
|
{ |
|
152
|
|
|
name: "map of strings", |
|
153
|
|
|
value: map[string]string{"key": "value"}, |
|
154
|
|
|
expectedKey: "key", |
|
155
|
|
|
expectedIsIndex: false, |
|
156
|
|
|
expectedIndex: -1, |
|
157
|
|
|
expectedValue: "value", |
|
158
|
|
|
}, |
|
159
|
|
|
{ |
|
160
|
|
|
name: "map of string pointers", |
|
161
|
|
|
value: map[string]*string{"key": stringPointer("value")}, |
|
162
|
|
|
expectedKey: "key", |
|
163
|
|
|
expectedIsIndex: false, |
|
164
|
|
|
expectedIndex: -1, |
|
165
|
|
|
expectedValue: "value", |
|
166
|
|
|
}, |
|
167
|
|
|
{ |
|
168
|
|
|
name: "map of string pointers with nil", |
|
169
|
|
|
value: map[string]*string{"key": nil}, |
|
170
|
|
|
expectedKey: "key", |
|
171
|
|
|
expectedIsIndex: false, |
|
172
|
|
|
expectedIndex: -1, |
|
173
|
|
|
expectedValue: nil, |
|
174
|
|
|
}, |
|
175
|
|
|
{ |
|
176
|
|
|
name: "map of string pointers with pointer key", |
|
177
|
|
|
value: map[*string]*string{stringPointer("key"): nil}, |
|
178
|
|
|
expectedKey: "key", |
|
179
|
|
|
expectedIsIndex: false, |
|
180
|
|
|
expectedIndex: -1, |
|
181
|
|
|
expectedValue: nil, |
|
182
|
|
|
}, |
|
183
|
|
|
{ |
|
184
|
|
|
name: "map of string pointers with nil pointer key", |
|
185
|
|
|
value: map[*string]*string{nil: nil}, |
|
186
|
|
|
expectedKey: "", |
|
187
|
|
|
expectedIsIndex: false, |
|
188
|
|
|
expectedIndex: -1, |
|
189
|
|
|
expectedValue: nil, |
|
190
|
|
|
}, |
|
191
|
|
|
{ |
|
192
|
|
|
name: "map of string pointers with struct key", |
|
193
|
|
|
value: map[mapKey]string{{Key: "key"}: "value"}, |
|
194
|
|
|
expectedKey: "<generic.mapKey Value>", |
|
195
|
|
|
expectedIsIndex: false, |
|
196
|
|
|
expectedIndex: -1, |
|
197
|
|
|
expectedValue: "value", |
|
198
|
|
|
}, |
|
199
|
|
|
} |
|
200
|
|
|
for _, test := range tests { |
|
201
|
|
|
t.Run(test.name, func(t *testing.T) { |
|
202
|
|
|
iterable, err := NewIterable(test.value) |
|
203
|
|
|
|
|
204
|
|
|
if assert.NoError(t, err) { |
|
205
|
|
|
assert.Equal(t, 1, iterable.Count()) |
|
206
|
|
|
assert.False(t, iterable.IsNil()) |
|
207
|
|
|
i := 0 |
|
208
|
|
|
err = iterable.Iterate(func(key Key, value interface{}) error { |
|
209
|
|
|
assert.Equal(t, test.expectedKey, key.String()) |
|
210
|
|
|
assert.Equal(t, test.expectedIsIndex, key.IsIndex()) |
|
211
|
|
|
assert.Equal(t, test.expectedIndex, key.Index()) |
|
212
|
|
|
assert.Equal(t, test.expectedValue, value) |
|
213
|
|
|
i++ |
|
214
|
|
|
|
|
215
|
|
|
return nil |
|
216
|
|
|
}) |
|
217
|
|
|
assert.NoError(t, err) |
|
218
|
|
|
assert.Equal(t, 1, i) |
|
219
|
|
|
} |
|
220
|
|
|
}) |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|