1 |
|
---|
2 | var test = require('tape')
|
---|
3 | var spec = require('stream-spec')
|
---|
4 | var through = require('../')
|
---|
5 |
|
---|
6 | /*
|
---|
7 | I'm using these two functions, and not streams and pipe
|
---|
8 | so there is less to break. if this test fails it must be
|
---|
9 | the implementation of _through_
|
---|
10 | */
|
---|
11 |
|
---|
12 | function write(array, stream) {
|
---|
13 | array = array.slice()
|
---|
14 | function next() {
|
---|
15 | while(array.length)
|
---|
16 | if(stream.write(array.shift()) === false)
|
---|
17 | return stream.once('drain', next)
|
---|
18 |
|
---|
19 | stream.end()
|
---|
20 | }
|
---|
21 |
|
---|
22 | next()
|
---|
23 | }
|
---|
24 |
|
---|
25 | function read(stream, callback) {
|
---|
26 | var actual = []
|
---|
27 | stream.on('data', function (data) {
|
---|
28 | actual.push(data)
|
---|
29 | })
|
---|
30 | stream.once('end', function () {
|
---|
31 | callback(null, actual)
|
---|
32 | })
|
---|
33 | stream.once('error', function (err) {
|
---|
34 | callback(err)
|
---|
35 | })
|
---|
36 | }
|
---|
37 |
|
---|
38 | test('simple defaults', function(assert) {
|
---|
39 |
|
---|
40 | var l = 1000
|
---|
41 | , expected = []
|
---|
42 |
|
---|
43 | while(l--) expected.push(l * Math.random())
|
---|
44 |
|
---|
45 | var t = through()
|
---|
46 | var s = spec(t).through().pausable()
|
---|
47 |
|
---|
48 | read(t, function (err, actual) {
|
---|
49 | assert.ifError(err)
|
---|
50 | assert.deepEqual(actual, expected)
|
---|
51 | assert.end()
|
---|
52 | })
|
---|
53 |
|
---|
54 | t.on('close', s.validate)
|
---|
55 |
|
---|
56 | write(expected, t)
|
---|
57 | });
|
---|
58 |
|
---|
59 | test('simple functions', function(assert) {
|
---|
60 |
|
---|
61 | var l = 1000
|
---|
62 | , expected = []
|
---|
63 |
|
---|
64 | while(l--) expected.push(l * Math.random())
|
---|
65 |
|
---|
66 | var t = through(function (data) {
|
---|
67 | this.emit('data', data*2)
|
---|
68 | })
|
---|
69 | var s = spec(t).through().pausable()
|
---|
70 |
|
---|
71 |
|
---|
72 | read(t, function (err, actual) {
|
---|
73 | assert.ifError(err)
|
---|
74 | assert.deepEqual(actual, expected.map(function (data) {
|
---|
75 | return data*2
|
---|
76 | }))
|
---|
77 | assert.end()
|
---|
78 | })
|
---|
79 |
|
---|
80 | t.on('close', s.validate)
|
---|
81 |
|
---|
82 | write(expected, t)
|
---|
83 | })
|
---|
84 |
|
---|
85 | test('pauses', function(assert) {
|
---|
86 |
|
---|
87 | var l = 1000
|
---|
88 | , expected = []
|
---|
89 |
|
---|
90 | while(l--) expected.push(l) //Math.random())
|
---|
91 |
|
---|
92 | var t = through()
|
---|
93 |
|
---|
94 | var s = spec(t)
|
---|
95 | .through()
|
---|
96 | .pausable()
|
---|
97 |
|
---|
98 | t.on('data', function () {
|
---|
99 | if(Math.random() > 0.1) return
|
---|
100 | t.pause()
|
---|
101 | process.nextTick(function () {
|
---|
102 | t.resume()
|
---|
103 | })
|
---|
104 | })
|
---|
105 |
|
---|
106 | read(t, function (err, actual) {
|
---|
107 | assert.ifError(err)
|
---|
108 | assert.deepEqual(actual, expected)
|
---|
109 | })
|
---|
110 |
|
---|
111 | t.on('close', function () {
|
---|
112 | s.validate()
|
---|
113 | assert.end()
|
---|
114 | })
|
---|
115 |
|
---|
116 | write(expected, t)
|
---|
117 | })
|
---|
118 |
|
---|
119 | test('does not soft-end on `undefined`', function(assert) {
|
---|
120 | var stream = through()
|
---|
121 | , count = 0
|
---|
122 |
|
---|
123 | stream.on('data', function (data) {
|
---|
124 | count++
|
---|
125 | })
|
---|
126 |
|
---|
127 | stream.write(undefined)
|
---|
128 | stream.write(undefined)
|
---|
129 |
|
---|
130 | assert.equal(count, 2)
|
---|
131 |
|
---|
132 | assert.end()
|
---|
133 | })
|
---|