|
4 | 4 | "bytes" |
5 | 5 | "encoding/csv" |
6 | 6 | "encoding/hex" |
| 7 | + "errors" |
7 | 8 | "fmt" |
8 | 9 | "io" |
9 | 10 | "strings" |
@@ -218,41 +219,23 @@ func TestEncoding_Len(t *testing.T) { |
218 | 219 | t.Parallel() |
219 | 220 |
|
220 | 221 | testCases := []struct { |
| 222 | + name string |
221 | 223 | enc utfbom.Encoding |
222 | 224 | expected int |
223 | 225 | }{ |
224 | | - { |
225 | | - enc: utfbom.Unknown, |
226 | | - expected: 0, |
227 | | - }, |
228 | | - { |
229 | | - enc: utfbom.UTF8, |
230 | | - expected: 3, |
231 | | - }, |
232 | | - { |
233 | | - enc: utfbom.UTF16BigEndian, |
234 | | - expected: 2, |
235 | | - }, |
236 | | - { |
237 | | - enc: utfbom.UTF16LittleEndian, |
238 | | - expected: 2, |
239 | | - }, |
240 | | - { |
241 | | - enc: utfbom.UTF32BigEndian, |
242 | | - expected: 4, |
243 | | - }, |
244 | | - { |
245 | | - enc: utfbom.UTF32LittleEndian, |
246 | | - expected: 4, |
247 | | - }, |
248 | | - { |
249 | | - enc: 999, |
250 | | - expected: 0, |
251 | | - }, |
| 226 | + {"Unknown", utfbom.Unknown, 0}, |
| 227 | + {"UTF8", utfbom.UTF8, 3}, |
| 228 | + {"UTF16BigEndian", utfbom.UTF16BigEndian, 2}, |
| 229 | + {"UTF16LittleEndian", utfbom.UTF16LittleEndian, 2}, |
| 230 | + {"UTF32BigEndian", utfbom.UTF32BigEndian, 4}, |
| 231 | + {"UTF32LittleEndian", utfbom.UTF32LittleEndian, 4}, |
| 232 | + {"InvalidEncoding", 999, 0}, |
252 | 233 | } |
253 | 234 |
|
254 | 235 | for _, tc := range testCases { |
255 | | - be.Equal(t, tc.enc.Len(), tc.expected) |
| 236 | + t.Run(tc.name, func(t *testing.T) { |
| 237 | + be.Equal(t, tc.enc.Len(), tc.expected) |
| 238 | + }) |
256 | 239 | } |
257 | 240 | } |
258 | 241 |
|
@@ -313,16 +296,6 @@ func TestReader_StringWithoutBOM(t *testing.T) { |
313 | 296 | be.Err(t, iotest.TestReader(rd, []byte(nobomstring)), nil) |
314 | 297 | } |
315 | 298 |
|
316 | | -func TestReader_UsualReader(t *testing.T) { |
317 | | - t.Parallel() |
318 | | - |
319 | | - bomPrefixedStringReader := strings.NewReader(teststring) |
320 | | - |
321 | | - rd := utfbom.NewReader(bomPrefixedStringReader) |
322 | | - |
323 | | - be.Err(t, iotest.TestReader(rd, []byte(teststring[3:])), nil) |
324 | | -} |
325 | | - |
326 | 299 | func TestReader_OneByteReader(t *testing.T) { |
327 | 300 | t.Parallel() |
328 | 301 |
|
@@ -570,6 +543,19 @@ func TestPrepend_TypeAliases(t *testing.T) { |
570 | 543 | }) |
571 | 544 | } |
572 | 545 |
|
| 546 | +// TestReader_UnderlyingReaderError verifies that when the underlying reader |
| 547 | +// returns a non-EOF error during BOM detection, it is wrapped with ErrRead. |
| 548 | +func TestReader_UnderlyingReaderError(t *testing.T) { |
| 549 | + t.Parallel() |
| 550 | + |
| 551 | + rd := utfbom.NewReader(iotest.ErrReader(errors.New("disk failure"))) |
| 552 | + |
| 553 | + buf := make([]byte, 10) |
| 554 | + n, err := rd.Read(buf) |
| 555 | + be.Equal(t, 0, n) |
| 556 | + be.True(t, errors.Is(err, utfbom.ErrRead)) |
| 557 | +} |
| 558 | + |
573 | 559 | func TestNewReader_NilPanics(t *testing.T) { |
574 | 560 | t.Parallel() |
575 | 561 |
|
|
0 commit comments