-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv.test.ts
More file actions
63 lines (57 loc) · 1.94 KB
/
csv.test.ts
File metadata and controls
63 lines (57 loc) · 1.94 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
import { describe, expect, it, vi } from 'vitest'
import { parseCsv } from '../../src/index.js'
describe('parseCsv', () => {
it('parses simple CSV', () => {
const csv = 'Name,Age,Occupation\nAlice,30,Engineer\nBob,25,Designer'
const expected = [
['Name', 'Age', 'Occupation'],
['Alice', '30', 'Engineer'],
['Bob', '25', 'Designer'],
]
expect(parseCsv(csv)).toEqual(expected)
})
it('ignores empty last line', () => {
const csv = 'Name,Age,Occupation\nAlice,30,Engineer\n'
const expected = [
['Name', 'Age', 'Occupation'],
['Alice', '30', 'Engineer'],
]
expect(parseCsv(csv)).toEqual(expected)
})
it('handles quoted fields', () => {
const csv = 'Name,Age,Occupation\n"Alice, PhD",30,Engineer\nBob,25,"Designer, Senior"'
const expected = [
['Name', 'Age', 'Occupation'],
['Alice, PhD', '30', 'Engineer'],
['Bob', '25', 'Designer, Senior'],
]
expect(parseCsv(csv)).toEqual(expected)
})
it('handles escaped quotes', () => {
const csv = 'Name,Quote\nAlice,"She said, ""Hello world"""\nBob,"This is ""an example"" of quotes"'
const expected = [
['Name', 'Quote'],
['Alice', 'She said, "Hello world"'],
['Bob', 'This is "an example" of quotes'],
]
expect(parseCsv(csv)).toEqual(expected)
})
it('handles newlines within quoted fields', () => {
const csv = 'Name,Address\nAlice,"123 Main St.\nAnytown, USA"'
const expected = [
['Name', 'Address'],
['Alice', '123 Main St.\nAnytown, USA'],
]
expect(parseCsv(csv)).toEqual(expected)
})
it('handles unterminated quotes', () => {
const csv = 'Name,Quote\nAlice,"This is an unterminated quote\n'
const expected = [
['Name', 'Quote'],
['Alice', 'This is an unterminated quote\n'],
]
vi.spyOn(console, 'error')
expect(parseCsv(csv)).toEqual(expected)
expect(console.error).toHaveBeenCalledWith('csv unterminated quote')
})
})