-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_test.go
More file actions
283 lines (254 loc) · 6.83 KB
/
parse_test.go
File metadata and controls
283 lines (254 loc) · 6.83 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//go:build goexperiment.simd && amd64
package simdcsv
import (
"encoding/csv"
"io"
"reflect"
"strings"
"testing"
)
// =============================================================================
// ParseBytes Tests
// =============================================================================
// TestParseBytes_Basic tests the ParseBytes function with various inputs.
func TestParseBytes_Basic(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "simple CSV",
input: "a,b,c\n1,2,3\n",
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}},
},
{
name: "empty input",
input: "",
want: nil,
},
{
name: "single field",
input: "hello\n",
want: [][]string{{"hello"}},
},
{
name: "quoted fields",
input: `"a","b,c","d"` + "\n",
want: [][]string{{"a", "b,c", "d"}},
},
{
name: "double quotes",
input: `"he said ""hello"""` + "\n",
want: [][]string{{`he said "hello"`}},
},
{
name: "no trailing newline",
input: "a,b,c",
want: [][]string{{"a", "b", "c"}},
},
{
name: "multiline field",
input: "\"hello\nworld\",b\n",
want: [][]string{{"hello\nworld", "b"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseBytes([]byte(tt.input), ',')
if err != nil {
t.Fatalf("ParseBytes error: %v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseBytes mismatch:\ngot=%v\nwant=%v", got, tt.want)
}
// Also compare with encoding/csv
stdReader := csv.NewReader(strings.NewReader(tt.input))
stdReader.FieldsPerRecord = -1
stdRecords, stdErr := stdReader.ReadAll()
if stdErr != nil {
t.Fatalf("encoding/csv error: %v", stdErr)
}
if !reflect.DeepEqual(got, stdRecords) {
t.Errorf("ParseBytes vs encoding/csv mismatch:\nParseBytes=%v\nencoding/csv=%v", got, stdRecords)
}
})
}
}
// TestParseBytes_CustomSeparator tests ParseBytes with custom separators.
func TestParseBytes_CustomSeparator(t *testing.T) {
tests := []struct {
name string
input string
comma rune
want [][]string
}{
{
name: "tab separator",
input: "a\tb\tc\n",
comma: '\t',
want: [][]string{{"a", "b", "c"}},
},
{
name: "semicolon separator",
input: "a;b;c\n",
comma: ';',
want: [][]string{{"a", "b", "c"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseBytes([]byte(tt.input), tt.comma)
if err != nil {
t.Fatalf("ParseBytes error: %v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseBytes mismatch:\ngot=%v\nwant=%v", got, tt.want)
}
})
}
}
// =============================================================================
// ParseBytesStreaming Tests
// =============================================================================
// TestParseBytesStreaming_Basic tests the ParseBytesStreaming function.
func TestParseBytesStreaming_Basic(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "simple CSV",
input: "a,b,c\n1,2,3\n",
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}},
},
{
name: "empty input",
input: "",
want: nil,
},
{
name: "single field",
input: "hello\n",
want: [][]string{{"hello"}},
},
{
name: "quoted fields",
input: `"a","b,c","d"` + "\n",
want: [][]string{{"a", "b,c", "d"}},
},
{
name: "multiline field",
input: "\"hello\nworld\",b\n",
want: [][]string{{"hello\nworld", "b"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got [][]string
err := ParseBytesStreaming([]byte(tt.input), ',', func(record []string) error {
// Make a copy to avoid slice reuse issues
recordCopy := make([]string, len(record))
copy(recordCopy, record)
got = append(got, recordCopy)
return nil
})
if err != nil {
t.Fatalf("ParseBytesStreaming error: %v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseBytesStreaming mismatch:\ngot=%v\nwant=%v", got, tt.want)
}
// Compare with ParseBytes
pbResult, pbErr := ParseBytes([]byte(tt.input), ',')
if pbErr != nil {
t.Fatalf("ParseBytes error: %v", pbErr)
}
if !reflect.DeepEqual(got, pbResult) {
t.Errorf("ParseBytesStreaming vs ParseBytes mismatch:\nStreaming=%v\nParseBytes=%v", got, pbResult)
}
})
}
}
// TestParseBytesStreaming_CallbackError tests that callback errors are propagated.
func TestParseBytesStreaming_CallbackError(t *testing.T) {
input := "a,b\nc,d\ne,f\n"
expectedErr := io.EOF // Use a recognizable error
callCount := 0
err := ParseBytesStreaming([]byte(input), ',', func(record []string) error {
callCount++
if callCount == 2 {
return expectedErr
}
return nil
})
if err != expectedErr {
t.Errorf("Expected error %v, got %v", expectedErr, err)
}
if callCount != 2 {
t.Errorf("Expected callback to be called 2 times, got %d", callCount)
}
}
// TestParseBytesStreaming_CustomSeparator tests with custom separators.
func TestParseBytesStreaming_CustomSeparator(t *testing.T) {
input := "a\tb\tc\n1\t2\t3\n"
want := [][]string{{"a", "b", "c"}, {"1", "2", "3"}}
var got [][]string
err := ParseBytesStreaming([]byte(input), '\t', func(record []string) error {
recordCopy := make([]string, len(record))
copy(recordCopy, record)
got = append(got, recordCopy)
return nil
})
if err != nil {
t.Fatalf("ParseBytesStreaming error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("ParseBytesStreaming mismatch:\ngot=%v\nwant=%v", got, want)
}
}
// =============================================================================
// buildRecords Tests
// =============================================================================
func TestBuildRecords_Nil(t *testing.T) {
result := buildRecords(nil, nil, false)
if result != nil {
t.Errorf("buildRecords(nil, nil) = %v, want nil", result)
}
}
func TestBuildRecords_EmptyRows(t *testing.T) {
pr := &parseResult{
fields: []fieldInfo{},
rows: []rowInfo{},
}
result := buildRecords([]byte(""), pr, false)
if result != nil {
t.Errorf("buildRecords with empty rows = %v, want nil", result)
}
}
// =============================================================================
// buildRecord Tests
// =============================================================================
func TestBuildRecord(t *testing.T) {
buf := []byte("hello,world\n")
pr := &parseResult{
fields: []fieldInfo{
{start: 0, length: 5},
{start: 6, length: 5},
},
rows: []rowInfo{
{firstField: 0, fieldCount: 2, lineNum: 1},
},
}
record := buildRecord(buf, pr, pr.rows[0], false)
if len(record) != 2 {
t.Fatalf("expected 2 fields, got %d", len(record))
}
if record[0] != "hello" {
t.Errorf("field 0 = %q, want %q", record[0], "hello")
}
if record[1] != "world" {
t.Errorf("field 1 = %q, want %q", record[1], "world")
}
}