forked from max-mapper/csv-write-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
111 lines (84 loc) · 2.7 KB
/
test.js
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
var test = require('tape')
var csv = require('./')
var concat = require('concat-stream')
test('encode from basic array', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,bar\n', data.toString())
t.end()
}))
writer.write(["world", "bar"])
writer.end()
})
test('encode from array w/ escaped quotes in header row', function(t) {
var writer = csv({ headers: ['why "hello" there', "foo"]})
writer.pipe(concat(function(data) {
t.equal('"why ""hello"" there",foo\nworld,bar\n', data.toString())
t.end()
}))
writer.write(["world", "bar"])
writer.end()
})
test('encode from array w/ escaped quotes in cells', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,"this is an ""escaped"" cell"\n', data.toString())
t.end()
}))
writer.write(["world", 'this is an "escaped" cell'])
writer.end()
})
test('encode from array w/ escaped newline in cells', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,"this is a\nmultiline cell"\n', data.toString())
t.end()
}))
writer.write(["world", 'this is a\nmultiline cell'])
writer.end()
})
test('encode from array w/ escaped comma in cells', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,"this is a cell with, commas, in it"\n', data.toString())
t.end()
}))
writer.write(["world", 'this is a cell with, commas, in it'])
writer.end()
})
test('encode from object w/ headers specified', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,bar\n', data.toString())
t.end()
}))
writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})
test('encode from object w/ auto headers', function(t) {
var writer = csv()
writer.pipe(concat(function(data) {
t.equal('hello,foo,baz\nworld,bar,taco\n', data.toString())
t.end()
}))
writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})
test('no headers specified', function(t) {
var writer = csv()
writer.on('error', function(err) {
t.equal(err.message, 'no headers specified')
t.end()
})
writer.write(['foo', 'bar'])
writer.end()
})
test('no headers displayed', function(t) {
var writer = csv({sendHeaders: false})
writer.pipe(concat(function(data) {
t.equal('world,bar,taco\n', data.toString())
t.end()
}))
writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})