-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomposite_test.go
239 lines (233 loc) · 3.66 KB
/
composite_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package typ
import (
"reflect"
"testing"
)
type (
TypedArray [6]interface{}
TypedSlice []interface{}
StructType struct{}
)
var (
MapData map[string]interface{}
ArrayData [6]interface{}
SliceData []interface{}
TypedArrayData TypedArray
TypedSliceData TypedSlice
StructTypeData StructType
)
func init() {
// Test Data
ArrayData = [6]interface{}{
1, 2, nil,
}
SliceData = []interface{}{
1, 2, nil,
}
TypedArrayData = TypedArray{1, 2, nil}
TypedSliceData = TypedSlice{1, 2, nil}
StructTypeData = StructType{}
MapData = map[string]interface{}{
"one": map[string]string{
"sub_one": "1",
"2": "2",
},
"array": ArrayData,
"slice": SliceData,
"typed_array": TypedArrayData,
"typed_slice": TypedSliceData,
"struct": StructTypeData,
}
// Converters
}
func TestCompositeGet(t *testing.T) {
testData := [][]interface{}{
{
// exists
MapData,
[]interface{}{"one", "sub_one"},
true,
"1",
nil,
},
{
// invalid, same type
MapData,
[]interface{}{"one", "invalid"},
false,
nil,
ErrOutOfBounds,
},
{
// invalid, wrong type
MapData,
[]interface{}{"one", 2},
false,
nil,
ErrInvalidArgument,
},
{
// empty args
MapData,
[]interface{}{},
false,
nil,
ErrInvalidArgument,
},
{
// invalid data
nil,
[]interface{}{},
false,
nil,
ErrInvalidArgument,
},
{
// exists
ArrayData,
[]interface{}{1},
true,
2,
nil,
},
{
// invalid, nil Value
ArrayData,
[]interface{}{3},
false,
nil,
ErrOutOfBounds,
},
{
// invalid, wrong type
ArrayData,
[]interface{}{"1"},
false,
nil,
ErrInvalidArgument,
}, {
// exists
TypedArrayData,
[]interface{}{1},
true,
2,
nil,
},
{
// invalid, nil Value
TypedArrayData,
[]interface{}{3},
false,
nil,
ErrOutOfBounds,
},
{
// invalid, wrong type
TypedArrayData,
[]interface{}{"1"},
false,
nil,
ErrInvalidArgument,
},
{
// exists
SliceData,
[]interface{}{1},
true,
2,
nil,
},
{
// invalid, out of range
SliceData,
[]interface{}{3},
false,
nil,
ErrOutOfRange,
},
{
// invalid, wrong type
SliceData,
[]interface{}{"1"},
false,
nil,
ErrInvalidArgument,
},
{
// invalid, nil Value given
SliceData,
[]interface{}{2},
false,
nil,
ErrOutOfBounds,
},
{
// exists
TypedSliceData,
[]interface{}{1},
true,
2,
"",
},
{
// invalid, out of range
TypedSliceData,
[]interface{}{3},
false,
nil,
ErrOutOfRange,
},
{
// invalid, wrong type
TypedSliceData,
[]interface{}{"1"},
false,
nil,
ErrInvalidArgument,
},
{
// invalid, nil Value given
TypedSliceData,
[]interface{}{2},
false,
nil,
ErrOutOfBounds,
},
{
// struct, wrong type data
StructTypeData,
[]interface{}{1},
false,
nil,
ErrUnexpectedValue,
},
}
var (
test bool
expects interface{}
)
for _, v := range testData {
data := v[0]
args := v[1].([]interface{})
test = v[2].(bool)
expects = v[3]
err, _ := v[4].(error)
typ := Of(data).Get(args...)
value := typ.Interface()
testValid := value.Valid() != test
testEquals := test && !reflect.DeepEqual(value.V(), expects)
testError := !test && (value.Err() != err)
if testValid || testEquals || testError {
t.Errorf("Of(%v[%[1]T]).Get(%v), %s", data, args, errNull{
expects, value.Valid(), err,
value.V(), value.Valid(), value.Err(),
})
}
}
}
// BenchmarkOfGet-8 3000000 407 ns/op
func BenchmarkOfGet(b *testing.B) {
for i := 0; i < b.N; i++ {
Of(MapData).Get("one", "sub_one")
}
}