-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
173 lines (142 loc) · 4.42 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
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
var concat = require('concat-stream')
var test = require('tape')
var File = require('vinyl')
var map = require('./')
var fs = require('fs')
test('null streams are just passed on', function(t) {
var file = new File({
contents: null
})
var stream = map(function(string) {
t.fail('should not get called')
})
stream.once('end', function() {
t.end()
}).end(file)
})
test('buffer streams are passed a buffer', function(t) {
var contents = fs.readFileSync(__filename)
var file = new File({
contents: contents
})
var stream = map(function(src) {
t.ok(Buffer.isBuffer(src), 'Buffer.isBuffer(contents)')
t.equal(String(contents), String(src), 'Buffer contents are correct')
})
stream.once('end', function() {
t.end()
}).end(file)
})
test('modifying buffer streams', function(t) {
t.plan(5)
var contents = fs.readFileSync(__filename)
var file = new File({
contents: contents
})
var stream = map(function(src) {
t.ok(Buffer.isBuffer(src), 'Buffer.isBuffer(contents)')
t.equal(String(contents), String(src), 'Buffer contents are correct')
return String(src).toUpperCase()
}).on('data', function(file) {
t.ok(Buffer.isBuffer(file.contents), 'output contents are a buffer')
t.equal(String(file.contents), String(contents).toUpperCase())
})
stream.once('end', function() {
t.pass('Reached "end" event')
}).end(file)
})
test('multiple buffers in a pipeline', function(t) {
t.plan(9)
var contents = fs.readFileSync(__filename)
var file = new File({
contents: contents
})
var stream = createStream()
function createStream() {
return map(function(src) {
t.ok(Buffer.isBuffer(src), 'Buffer.isBuffer(contents)')
t.equal(String(contents), String(src), 'Buffer contents are correct')
})
}
stream
.pipe(createStream())
.pipe(createStream())
.pipe(createStream())
.once('end', function() {
t.pass('reached stream "end" event')
})
stream.end(file)
})
test('stream streams are passed a stream', function(t) {
t.plan(2)
var fileStream = fs.createReadStream(__filename)
var contents = fs.readFileSync(__filename)
var file = new File({
contents: fileStream
})
var stream = map(function(src) {
t.ok(Buffer.isBuffer(src), 'Buffer.isBuffer(contents)')
t.equal(String(contents), String(src), 'Buffer contents are correct')
})
stream.once('end', function() {
t.end()
}).end(file)
})
test('stream streams are passed a stream, modifying output stream', function(t) {
t.plan(5)
var fileStream = fs.createReadStream(__filename)
var contents = fs.readFileSync(__filename)
var file = new File({
contents: fileStream
})
var stream = map(function(src) {
t.ok(Buffer.isBuffer(src), 'Buffer.isBuffer(contents)')
t.equal(String(contents), String(src), 'Buffer contents are correct')
return String(src).toUpperCase()
}).on('data', function(file) {
t.ok(file.contents.pipe, 'output contents are a stream')
file.contents.pipe(concat(function(upper) {
t.equal(String(upper), String(contents).toUpperCase())
}))
})
stream.once('end', function() {
t.pass('reached "end" event')
}).end(file)
})
test('multiple stream streams in a pipeline passing streams', function(t) {
t.plan(17)
var contents = fs.readFileSync(__filename)
var fileStream = fs.createReadStream(__filename)
var file = new File({ contents: fileStream })
var stream = createStream()
function createStream() {
return map(function(src) {
t.ok(Buffer.isBuffer(src), 'Buffer.isBuffer(contents)')
t.equal(String(contents), String(src), 'Buffer contents are correct')
}).once('data', function(file) {
t.ok(file.contents.pipe, 'output contents are a stream')
t.ok(!Buffer.isBuffer(file), 'output contents are not a buffer')
})
}
stream
.pipe(createStream())
.pipe(createStream())
.pipe(createStream())
.once('end', function() {
t.pass('reached stream "end" event')
})
stream.end(file)
})
test('first thrown error in sync mapper gets emitted as an error', function(t) {
t.plan(1)
var stream = map(function(src) {
throw new Error('should be caught')
}).on('error', function(err) {
t.ok(err, 'error was caught and emitted')
}).on('data', function() {
t.fail('should not get emitted as "data"')
})
stream.write(new File({ contents: new Buffer(' ') }))
stream.write(new File({ contents: new Buffer(' ') }))
stream.end()
})