1 | var tape = require('tape')
|
2 | var through = require('through2')
|
3 | var pumpify = require('./')
|
4 | var stream = require('stream')
|
5 |
|
6 | tape('basic', function(t) {
|
7 | t.plan(3)
|
8 |
|
9 | var pipeline = pumpify(
|
10 | through(function(data, enc, cb) {
|
11 | t.same(data.toString(), 'hello')
|
12 | cb(null, data.toString().toUpperCase())
|
13 | }),
|
14 | through(function(data, enc, cb) {
|
15 | t.same(data.toString(), 'HELLO')
|
16 | cb(null, data.toString().toLowerCase())
|
17 | })
|
18 | )
|
19 |
|
20 | pipeline.write('hello')
|
21 | pipeline.on('data', function(data) {
|
22 | t.same(data.toString(), 'hello')
|
23 | t.end()
|
24 | })
|
25 | })
|
26 |
|
27 | tape('3 times', function(t) {
|
28 | t.plan(4)
|
29 |
|
30 | var pipeline = pumpify(
|
31 | through(function(data, enc, cb) {
|
32 | t.same(data.toString(), 'hello')
|
33 | cb(null, data.toString().toUpperCase())
|
34 | }),
|
35 | through(function(data, enc, cb) {
|
36 | t.same(data.toString(), 'HELLO')
|
37 | cb(null, data.toString().toLowerCase())
|
38 | }),
|
39 | through(function(data, enc, cb) {
|
40 | t.same(data.toString(), 'hello')
|
41 | cb(null, data.toString().toUpperCase())
|
42 | })
|
43 | )
|
44 |
|
45 | pipeline.write('hello')
|
46 | pipeline.on('data', function(data) {
|
47 | t.same(data.toString(), 'HELLO')
|
48 | t.end()
|
49 | })
|
50 | })
|
51 |
|
52 | tape('destroy', function(t) {
|
53 | var test = through()
|
54 | test.destroy = function() {
|
55 | t.ok(true)
|
56 | t.end()
|
57 | }
|
58 |
|
59 | var pipeline = pumpify(through(), test)
|
60 |
|
61 | pipeline.destroy()
|
62 | })
|
63 |
|
64 | tape('close', function(t) {
|
65 | var test = through()
|
66 | var pipeline = pumpify(through(), test)
|
67 |
|
68 | pipeline.on('error', function(err) {
|
69 | t.same(err.message, 'lol')
|
70 | t.end()
|
71 | })
|
72 |
|
73 | test.emit('error', new Error('lol'))
|
74 | })
|
75 |
|
76 | tape('end waits for last one', function(t) {
|
77 | var ran = false
|
78 |
|
79 | var a = through()
|
80 | var b = through()
|
81 | var c = through(function(data, enc, cb) {
|
82 | setTimeout(function() {
|
83 | ran = true
|
84 | cb()
|
85 | }, 100)
|
86 | })
|
87 |
|
88 | var pipeline = pumpify(a, b, c)
|
89 |
|
90 | pipeline.write('foo')
|
91 | pipeline.end(function() {
|
92 | t.ok(ran)
|
93 | t.end()
|
94 | })
|
95 |
|
96 | t.ok(!ran)
|
97 | })
|
98 |
|
99 | tape('always wait for finish', function(t) {
|
100 | var a = new stream.Readable()
|
101 | a._read = function() {}
|
102 | a.push('hello')
|
103 |
|
104 | var pipeline = pumpify(a, through(), through())
|
105 | var ran = false
|
106 |
|
107 | pipeline.on('finish', function() {
|
108 | t.ok(ran)
|
109 | t.end()
|
110 | })
|
111 |
|
112 | setTimeout(function() {
|
113 | ran = true
|
114 | a.push(null)
|
115 | }, 100)
|
116 | })
|
117 |
|
118 | tape('async', function(t) {
|
119 | var pipeline = pumpify()
|
120 |
|
121 | t.plan(4)
|
122 |
|
123 | pipeline.write('hello')
|
124 | pipeline.on('data', function(data) {
|
125 | t.same(data.toString(), 'HELLO')
|
126 | t.end()
|
127 | })
|
128 |
|
129 | setTimeout(function() {
|
130 | pipeline.setPipeline(
|
131 | through(function(data, enc, cb) {
|
132 | t.same(data.toString(), 'hello')
|
133 | cb(null, data.toString().toUpperCase())
|
134 | }),
|
135 | through(function(data, enc, cb) {
|
136 | t.same(data.toString(), 'HELLO')
|
137 | cb(null, data.toString().toLowerCase())
|
138 | }),
|
139 | through(function(data, enc, cb) {
|
140 | t.same(data.toString(), 'hello')
|
141 | cb(null, data.toString().toUpperCase())
|
142 | })
|
143 | )
|
144 | }, 100)
|
145 | }) |
\ | No newline at end of file |