-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation_test.go
More file actions
268 lines (234 loc) · 7.13 KB
/
validation_test.go
File metadata and controls
268 lines (234 loc) · 7.13 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
//go:build goexperiment.simd && amd64
package simdcsv
import (
"errors"
"strings"
"testing"
)
// =============================================================================
// isFieldTerminator Tests
// =============================================================================
func TestIsFieldTerminator(t *testing.T) {
tests := []struct {
name string
b byte
comma rune
want bool
}{
{"comma with default", ',', ',', true},
{"newline", '\n', ',', true},
{"carriage return", '\r', ',', true},
{"semicolon with semicolon comma", ';', ';', true},
{"comma with semicolon comma", ',', ';', true},
{"regular char", 'a', ',', false},
{"space", ' ', ',', false},
{"tab", '\t', ',', false},
{"quote", '"', ',', false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isFieldTerminator(tt.b, tt.comma)
if got != tt.want {
t.Errorf("isFieldTerminator(%q, %q) = %v, want %v", tt.b, tt.comma, got, tt.want)
}
})
}
}
// =============================================================================
// validateFieldQuotes Tests
// =============================================================================
func TestValidateFieldQuotes_Valid(t *testing.T) {
tests := []struct {
name string
input string
}{
{"simple unquoted", "hello"},
{"simple quoted", `"hello"`},
{"quoted with comma", `"hello,world"`},
{"quoted with escaped quote", `"he""llo"`},
{"empty quoted", `""`},
{"quoted at start", `"hello",world`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := newTestReaderWithBuffer([]byte(tt.input))
err := r.validateFieldQuotesWithField(fieldInfo{}, 0, uint64(len(tt.input)), 1)
if err != nil {
t.Errorf("validateFieldQuotes(%q) unexpected error: %v", tt.input, err)
}
})
}
}
func TestValidateFieldQuotes_BareQuote(t *testing.T) {
tests := []struct {
name string
input string
column int
}{
{"bare quote in middle", `hel"lo`, 4},
{"bare quote at start unquoted", `"hello`, 1}, // Actually this is unclosed, not bare
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := newTestReaderWithBuffer([]byte(tt.input))
err := r.validateFieldQuotesWithField(fieldInfo{}, 0, uint64(len(tt.input)), 1)
if err == nil {
t.Errorf("validateFieldQuotes(%q) expected error, got nil", tt.input)
return
}
var parseErr *ParseError
if !errors.As(err, &parseErr) {
t.Errorf("expected ParseError, got %T", err)
return
}
if !errors.Is(parseErr.Err, ErrBareQuote) && !errors.Is(parseErr.Err, ErrQuote) {
t.Errorf("expected ErrBareQuote or ErrQuote, got %v", parseErr.Err)
}
})
}
}
func TestValidateFieldQuotes_UnclosedQuote(t *testing.T) {
input := `"hello`
r := newTestReaderWithBuffer([]byte(input))
err := r.validateFieldQuotesWithField(fieldInfo{}, 0, uint64(len(input)), 1)
if err == nil {
t.Error("expected error for unclosed quote")
return
}
var parseErr *ParseError
if !errors.As(err, &parseErr) {
t.Errorf("expected ParseError, got %T", err)
return
}
if !errors.Is(parseErr.Err, ErrQuote) {
t.Errorf("expected ErrQuote, got %v", parseErr.Err)
}
}
func TestValidateFieldQuotes_TextAfterClosingQuote(t *testing.T) {
input := `"hello"world`
r := newTestReaderWithBuffer([]byte(input))
err := r.validateFieldQuotesWithField(fieldInfo{}, 0, uint64(len(input)), 1)
if err == nil {
t.Error("expected error for text after closing quote")
return
}
var parseErr *ParseError
if !errors.As(err, &parseErr) {
t.Errorf("expected ParseError, got %T", err)
return
}
if !errors.Is(parseErr.Err, ErrQuote) {
t.Errorf("expected ErrQuote, got %v", parseErr.Err)
}
}
func TestValidateFieldQuotes_WithTrimLeadingSpace(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{"space before quoted valid", ` "hello"`, false},
{"space before quoted with comma", ` "hello",`, false},
{"space before unquoted", " hello", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Find the end position (before comma or end of string)
endPos := len(tt.input)
if idx := strings.Index(tt.input, ","); idx >= 0 {
endPos = idx
}
r := newTestReaderWithBuffer([]byte(tt.input))
r.TrimLeadingSpace = true
err := r.validateFieldQuotesWithField(fieldInfo{}, 0, uint64(endPos), 1)
if tt.wantErr && err == nil {
t.Errorf("validateFieldQuotes(%q) expected error, got nil", tt.input)
}
if !tt.wantErr && err != nil {
t.Errorf("validateFieldQuotes(%q) unexpected error: %v", tt.input, err)
}
})
}
}
// newTestReaderWithBuffer creates a Reader with a raw buffer for testing validation.
func newTestReaderWithBuffer(buf []byte) *Reader {
r := &Reader{Comma: ','}
r.state.rawBuffer = buf
return r
}
// =============================================================================
// validateQuotedField Tests
// =============================================================================
func TestValidateQuotedField(t *testing.T) {
tests := []struct {
name string
input []byte
wantErr bool
errType error
}{
{"valid simple", []byte(`"hello"`), false, nil},
{"valid escaped", []byte(`"he""llo"`), false, nil},
{"valid empty", []byte(`""`), false, nil},
{"unclosed", []byte(`"hello`), true, ErrQuote},
{"text after close", []byte(`"hello"x`), true, ErrQuote},
{"valid with comma after", []byte(`"hello",`), false, nil},
{"valid with newline after", []byte("\"hello\"\n"), false, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Reader{Comma: ','}
err := r.validateQuotedField(tt.input, 0, 1)
if tt.wantErr {
if err == nil {
t.Errorf("expected error, got nil")
return
}
var parseErr *ParseError
if errors.As(err, &parseErr) {
if !errors.Is(parseErr.Err, tt.errType) {
t.Errorf("expected %v, got %v", tt.errType, parseErr.Err)
}
}
} else if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}
// =============================================================================
// validateUnquotedField Tests
// =============================================================================
func TestValidateUnquotedField(t *testing.T) {
tests := []struct {
name string
input []byte
wantErr bool
}{
{"valid simple", []byte("hello"), false},
{"valid with spaces", []byte("hello world"), false},
{"valid numbers", []byte("12345"), false},
{"bare quote", []byte(`hel"lo`), true},
{"quote at start", []byte(`"hello`), true},
{"quote at end", []byte(`hello"`), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Reader{Comma: ','}
err := r.validateUnquotedField(tt.input, 0, 1)
if tt.wantErr && err == nil {
t.Errorf("expected error for %q, got nil", tt.input)
}
if !tt.wantErr && err != nil {
t.Errorf("unexpected error for %q: %v", tt.input, err)
}
if tt.wantErr && err != nil {
var parseErr *ParseError
if !errors.As(err, &parseErr) {
t.Errorf("expected ParseError, got %T", err)
} else if !errors.Is(parseErr.Err, ErrBareQuote) {
t.Errorf("expected ErrBareQuote, got %v", parseErr.Err)
}
}
})
}
}