test.js
2.61 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
var test = require('tape')
var path = require('path')
var from = require('./')
var fs = require('fs')
var tmp = path.resolve(
__dirname, 'tmp.txt'
)
function fromString(string) {
return from(function(size, next) {
if (string.length <= 0) return next(null, null)
var chunk = string.slice(0, size)
string = string.slice(size)
next(null, chunk)
})
}
test('from2', function(t) {
var contents = fs.readFileSync(__filename, 'utf8')
var stream = fromString(contents)
stream
.pipe(fs.createWriteStream(tmp))
.on('close', function() {
t.equal(fs.readFileSync(tmp, 'utf8'), contents)
fs.unlinkSync(tmp)
t.end()
})
})
test('old mode', function(t) {
var contents = fs.readFileSync(__filename, 'utf8')
var stream = fromString(contents)
var buffer = ''
stream.on('data', function(data) {
buffer += data
}).on('end', function() {
t.equal(buffer, contents)
t.end()
})
})
test('destroy', function(t) {
var stream = from(function(size, next) {
process.nextTick(function() {
next(null, 'no')
})
})
stream.on('data', function(data) {
t.ok(false)
}).on('close', function() {
t.ok(true)
t.end()
})
stream.destroy()
})
test('arrays', function (t) {
var input = ['a', 'b', 'c']
var stream = from(input)
var output = []
stream.on('data', function (letter) {
output.push(letter.toString())
})
stream.on('end', function () {
t.deepEqual(input, output)
t.end()
})
})
test('obj arrays', function (t) {
var input = [{foo:'a'}, {foo:'b'}, {foo:'c'}]
var stream = from.obj(input)
var output = []
stream.on('data', function (letter) {
output.push(letter)
})
stream.on('end', function () {
t.deepEqual(input, output)
t.end()
})
})
test('arrays can emit errors', function (t) {
var input = ['a', 'b', new Error('ooops'), 'c']
var stream = from(input)
var output = []
stream.on('data', function (letter) {
output.push(letter.toString())
})
stream.on('error', function(e){
t.deepEqual(['a', 'b'], output)
t.equal('ooops', e.message)
t.end()
})
stream.on('end', function () {
t.fail('the stream should have errored')
})
})
test('obj arrays can emit errors', function (t) {
var input = [{foo:'a'}, {foo:'b'}, new Error('ooops'), {foo:'c'}]
var stream = from.obj(input)
var output = []
stream.on('data', function (letter) {
output.push(letter)
})
stream.on('error', function(e){
t.deepEqual([{foo:'a'}, {foo:'b'}], output)
t.equal('ooops', e.message)
t.end()
})
stream.on('end', function () {
t.fail('the stream should have errored')
})
})