Initial commit
[yaffs-website] / node_modules / stream-consume / test / tests.js
1 /*jshint node:true */
2 /*global describe:false, it:false */
3 "use strict";
4
5 var consume = require('../');
6 var Stream = require('stream');
7 var Readable = Stream.Readable;
8 var Writable = Stream.Writable;
9 var Duplex = Stream.Duplex;
10 var should = require('should');
11 var through = require('through2');
12 require('mocha');
13
14 describe('stream-consume', function() {
15
16     it('should cause a Readable stream to complete if it\'s not piped anywhere', function(done) {
17         var rs = new Readable({highWaterMark: 2});
18         var a = 0;
19         var ended = false;
20         rs._read = function() {
21             if (a++ < 100) {
22                 rs.push(a + "");
23             } else {
24                 ended = true;
25                 rs.push(null);
26             }
27         };
28
29         rs.on("end", function() {
30             a.should.be.above(99);
31             ended.should.be.true;
32             done();
33         });
34
35         consume(rs);
36     });
37
38     it('should work with Readable streams in objectMode', function(done) {
39         var rs = new Readable({highWaterMark: 2, objectMode: true});
40         var a = 0;
41         var ended = false;
42         rs._read = function() {
43             if (a++ < 100) {
44                 rs.push(a);
45             } else {
46                 ended = true;
47                 rs.push(null);
48             }
49         };
50
51         rs.on("end", function() {
52             a.should.be.above(99);
53             ended.should.be.true;
54             done();
55         });
56
57         consume(rs);
58     });
59
60     it('should not interfere with a Readable stream that is piped somewhere', function(done) {
61         var rs = new Readable({highWaterMark: 2});
62         var a = 0;
63         var ended = false;
64         rs._read = function() {
65             if (a++ < 100) {
66                 rs.push(".");
67             } else {
68                 ended = true;
69                 rs.push(null);
70             }
71         };
72
73         var sizeRead = 0;
74         var ws = new Writable({highWaterMark: 2});
75         ws._write = function(chunk, enc, next) {
76             sizeRead += chunk.length;
77             next();
78         }
79
80         ws.on("finish", function() {
81             a.should.be.above(99);
82             ended.should.be.true;
83             sizeRead.should.equal(100);
84             done();
85         });
86
87         rs.pipe(ws);
88
89         consume(rs);
90     });
91
92     it('should not interfere with a Writable stream', function(done) {
93         var rs = new Readable({highWaterMark: 2});
94         var a = 0;
95         var ended = false;
96         rs._read = function() {
97             if (a++ < 100) {
98                 rs.push(".");
99             } else {
100                 ended = true;
101                 rs.push(null);
102             }
103         };
104
105         var sizeRead = 0;
106         var ws = new Writable({highWaterMark: 2});
107         ws._write = function(chunk, enc, next) {
108             sizeRead += chunk.length;
109             next();
110         }
111
112         ws.on("finish", function() {
113             a.should.be.above(99);
114             ended.should.be.true;
115             sizeRead.should.equal(100);
116             done();
117         });
118
119         rs.pipe(ws);
120
121         consume(ws);
122     });
123
124     it('should handle a Transform stream', function(done) {
125         var rs = new Readable({highWaterMark: 2});
126         var a = 0;
127         var ended = false;
128         rs._read = function() {
129             if (a++ < 100) {
130                 rs.push(".");
131             } else {
132                 ended = true;
133                 rs.push(null);
134             }
135         };
136
137         var sizeRead = 0;
138         var flushed = false;
139         var ts = through({highWaterMark: 2}, function(chunk, enc, cb) {
140             sizeRead += chunk.length;
141             this.push(chunk);
142             cb();
143         }, function(cb) {
144             flushed = true;
145             cb();
146         });
147
148         ts.on("end", function() {
149             a.should.be.above(99);
150             ended.should.be.true;
151             sizeRead.should.equal(100);
152             flushed.should.be.true;
153             done();
154         });
155
156         rs.pipe(ts);
157
158         consume(ts);
159     });
160
161     it('should handle a classic stream', function(done) {
162         var rs = new Stream();
163         var ended = false;
164         var i;
165
166         rs.on("end", function() {
167             ended.should.be.true;
168             done();
169         });
170
171         consume(rs);
172
173         for (i = 0; i < 100; i++) {
174             rs.emit("data", i);
175         }
176         ended = true;
177         rs.emit("end");
178     });
179
180 });