-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_slice_test.go
212 lines (198 loc) · 4.33 KB
/
decoder_slice_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
package csvdecoder
import (
"reflect"
"strings"
"testing"
)
func TestIntSlice(t *testing.T) {
type TestRow struct {
Field []int
}
for _, tc := range []struct {
name string
RowStruct TestRow
data string
expected []int
}{
{
name: "should work for an empty array",
RowStruct: TestRow{},
data: "[]\n",
expected: []int{},
},
{
name: "should work for a single-valued array",
RowStruct: TestRow{},
data: "[1]\n",
expected: []int{1},
},
{
name: "should work for an array with positive values",
RowStruct: TestRow{},
data: "[1, 2, 3]\n",
expected: []int{1, 2, 3},
},
{
name: "should work for an array with negative values",
RowStruct: TestRow{},
data: "[-1, -2, -3]\n",
expected: []int{-1, -2, -3},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
d, err := NewWithConfig(strings.NewReader(tc.data), Config{IgnoreHeaders: false, Comma: '\t'})
if err != nil {
t.Fatalf("could not create d: %s", err)
}
for d.Next() {
err := d.Scan(&tc.RowStruct.Field)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(tc.RowStruct.Field, tc.expected) {
t.Errorf("expected value '%v' got '%v'", tc.expected, tc.RowStruct.Field)
}
}
if d.Err() != nil {
t.Errorf("d error: %w", err)
}
})
}
}
func TestMultiLevelIntSlice(t *testing.T) {
type TestRow struct {
Field [][][]int
}
for _, tc := range []struct {
name string
RowStruct TestRow
data string
expected [][][]int
}{
{
name: "should work for an empty array",
RowStruct: TestRow{},
data: "[]\n",
expected: [][][]int{},
},
{
name: "should work for an empty array in an array",
RowStruct: TestRow{},
data: "[[]]\n",
expected: [][][]int{{}},
},
{
name: "should work for items with same level",
RowStruct: TestRow{},
data: "[[], [[1, 2],[3, 4]]]\n",
expected: [][][]int{{}, {{1, 2}, {3, 4}}},
},
{
name: "should work for items with different levels",
RowStruct: TestRow{},
data: "[[], [[1, 2]]]\n",
expected: [][][]int{{}, {{1, 2}}},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
d, err := NewWithConfig(strings.NewReader(tc.data), Config{IgnoreHeaders: false, Comma: '\t'})
if err != nil {
t.Fatalf("could not create d: %s", err)
}
for d.Next() {
err := d.Scan(&tc.RowStruct.Field)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(tc.RowStruct.Field, tc.expected) {
t.Errorf("expected value '%v' got '%v'", tc.expected, tc.RowStruct.Field)
}
}
if d.Err() != nil {
t.Errorf("d error: %w", err)
}
})
}
}
func TestStructSlice(t *testing.T) {
type MyStruct struct {
A int `json:"a"`
B int32 `json:"b"`
C int64 `json:"c"`
D string `json:"d"`
E bool `json:"e"`
}
type TestRow struct {
Field []MyStruct
}
for _, tc := range []struct {
name string
RowStruct TestRow
data string
expected []MyStruct
}{
{
name: "should work for an empty array",
RowStruct: TestRow{},
data: "[]\n",
expected: []MyStruct{},
},
{
name: "should work for an array with one value",
RowStruct: TestRow{},
data: `[{"a":1, "b":2, "c": 3, "d":"value1", "e": true}]`,
expected: []MyStruct{
{
A: 1,
B: 2,
C: 3,
D: "value1",
E: true,
},
},
},
{
name: "should work for an array with multiple values",
RowStruct: TestRow{},
data: `[{"a":1, "b":2, "c": 3, "d":"value1", "e": true}, {"a":4, "b":5, "c": 6, "d":"value2", "e": false}]`,
expected: []MyStruct{
{
A: 1,
B: 2,
C: 3,
D: "value1",
E: true,
},
{
A: 4,
B: 5,
C: 6,
D: "value2",
E: false,
},
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
d, err := NewWithConfig(strings.NewReader(tc.data), Config{IgnoreHeaders: false, Comma: '\t'})
if err != nil {
t.Fatalf("could not create d: %s", err)
}
for d.Next() {
err := d.Scan(&tc.RowStruct.Field)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(tc.RowStruct.Field, tc.expected) {
t.Errorf("expected value '%v' got '%v'", tc.expected, tc.RowStruct.Field)
}
}
if d.Err() != nil {
t.Errorf("d error: %w", err)
}
})
}
}