index.mocha.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. var assert = require('assert')
  2. , StreamTest = require('streamtest')
  3. , BufferStream = require('../src')
  4. ;
  5. // Helpers
  6. function syncBufferPrefixer(headerText) {
  7. return new BufferStream({
  8. objectMode: headerText instanceof Object
  9. }, function(err, buf, cb) {
  10. assert.equal(err, null);
  11. if(null === buf) {
  12. cb(null, Buffer(headerText));
  13. } else if(buf instanceof Array) {
  14. buf.unshift(headerText);
  15. cb(null, buf);
  16. } else {
  17. cb(null, Buffer.concat([Buffer(headerText), buf]));
  18. }
  19. });
  20. }
  21. function asyncBufferPrefixer(headerText) {
  22. return new BufferStream({
  23. objectMode: headerText instanceof Object
  24. }, function(err, buf, cb) {
  25. assert.equal(err, null);
  26. if(null === buf) {
  27. setTimeout(function() {
  28. cb(null, Buffer(headerText));
  29. }, 0);
  30. } else if(buf instanceof Array) {
  31. setTimeout(function() {
  32. buff.push(headerText);
  33. cb(null, buf);
  34. }, 0);
  35. } else {
  36. setTimeout(function() {
  37. cb(null, Buffer.concat([Buffer(headerText), buf]));
  38. }, 0);
  39. }
  40. });
  41. }
  42. // Tests
  43. describe('bufferstreams', function() {
  44. it('should fail when callback is not a function', function() {
  45. assert.throws(function() {
  46. new BufferStream();
  47. });
  48. });
  49. // Iterating through versions
  50. StreamTest.versions.forEach(function(version) {
  51. describe('for ' + version + ' streams', function() {
  52. describe('in buffer mode', function() {
  53. describe('synchonously', function() {
  54. it('should work with one pipe', function(done) {
  55. StreamTest[version].fromChunks(['te', 'st'])
  56. .pipe(syncBufferPrefixer('plop'))
  57. .pipe(StreamTest[version].toText(function(err, data) {
  58. if(err) {
  59. return done(err);
  60. }
  61. assert.equal(data, 'ploptest');
  62. done();
  63. }));
  64. });
  65. it('should work when returning a null buffer', function(done) {
  66. StreamTest[version].fromChunks(['te', 'st'])
  67. .pipe(new BufferStream(function(err, buf, cb){
  68. cb(null, null);
  69. }))
  70. .pipe(StreamTest[version].toText(function(err, data) {
  71. if(err) {
  72. return done(err);
  73. }
  74. assert.equal(data, '');
  75. done();
  76. }));
  77. });
  78. it('should work with multiple pipes', function(done) {
  79. StreamTest[version].fromChunks(['te', 'st'])
  80. .pipe(syncBufferPrefixer('plop'))
  81. .pipe(syncBufferPrefixer('plip'))
  82. .pipe(syncBufferPrefixer('plap'))
  83. .pipe(StreamTest[version].toText(function(err, data) {
  84. if(err) {
  85. return done(err);
  86. }
  87. assert.equal(data, 'plapplipploptest');
  88. done();
  89. }));
  90. });
  91. });
  92. describe('asynchonously', function() {
  93. it('should work with one pipe', function(done) {
  94. StreamTest[version].fromChunks(['te', 'st'])
  95. .pipe(asyncBufferPrefixer('plop'))
  96. .pipe(StreamTest[version].toText(function(err, data) {
  97. if(err) {
  98. return done(err);
  99. }
  100. assert.equal(data, 'ploptest');
  101. done();
  102. }));
  103. });
  104. it('should work when returning a null buffer', function(done) {
  105. StreamTest[version].fromChunks(['te', 'st'])
  106. .pipe(BufferStream(function(err, buf, cb){
  107. cb(null, null);
  108. }))
  109. .pipe(StreamTest[version].toText(function(err, data) {
  110. if(err) {
  111. return done(err);
  112. }
  113. assert.equal(data, '');
  114. done();
  115. }));
  116. });
  117. it('should work with multiple pipes', function(done) {
  118. StreamTest[version].fromChunks(['te', 'st'])
  119. .pipe(asyncBufferPrefixer('plop'))
  120. .pipe(asyncBufferPrefixer('plip'))
  121. .pipe(asyncBufferPrefixer('plap'))
  122. .pipe(StreamTest[version].toText(function(err, data) {
  123. if(err) {
  124. return done(err);
  125. }
  126. assert.equal(data, 'plapplipploptest');
  127. done();
  128. }));
  129. });
  130. });
  131. });
  132. describe('in object mode', function() {
  133. var object1 = {txt: 'te'};
  134. var object2 = {txt: 'st'};
  135. var object3 = {txt: 'e'};
  136. var object4 = {txt: 'd'};
  137. var object5 = {txt: 'u'};
  138. var object6 = {txt: 'ni'};
  139. var object7 = {txt: 't'};
  140. describe('synchonously', function() {
  141. it('should work with one pipe', function(done) {
  142. StreamTest[version].fromObjects([object1, object2])
  143. .pipe(syncBufferPrefixer(object4))
  144. .pipe(StreamTest[version].toObjects(function(err, objs) {
  145. if(err) {
  146. return done(err);
  147. }
  148. assert.deepEqual(objs, [object4, object1, object2]);
  149. done();
  150. }));
  151. });
  152. it('should work when returning a null buffer', function(done) {
  153. StreamTest[version].fromObjects([object1, object2])
  154. .pipe(new BufferStream({
  155. objectMode: true
  156. }, function(err, buf, cb){
  157. cb(null, null);
  158. }))
  159. .pipe(StreamTest[version].toObjects(function(err, objs) {
  160. if(err) {
  161. return done(err);
  162. }
  163. assert.equal(objs.length, 0);
  164. done();
  165. }));
  166. });
  167. it('should work with multiple pipes', function(done) {
  168. StreamTest[version].fromObjects([object1, object2])
  169. .pipe(syncBufferPrefixer(object4))
  170. .pipe(syncBufferPrefixer(object5))
  171. .pipe(syncBufferPrefixer(object6))
  172. .pipe(StreamTest[version].toObjects(function(err, objs) {
  173. if(err) {
  174. return done(err);
  175. }
  176. assert.deepEqual(objs, [object6, object5, object4, object1, object2]);
  177. done();
  178. }));
  179. });
  180. });
  181. describe('asynchonously', function() {
  182. it('should work with one pipe', function(done) {
  183. StreamTest[version].fromObjects([object1, object2])
  184. .pipe(syncBufferPrefixer(object4))
  185. .pipe(StreamTest[version].toObjects(function(err, objs) {
  186. if(err) {
  187. return done(err);
  188. }
  189. assert.deepEqual(objs, [object4, object1, object2]);
  190. done();
  191. }));
  192. });
  193. it('should work when returning a null buffer', function(done) {
  194. StreamTest[version].fromObjects([object1, object2])
  195. .pipe(BufferStream({
  196. objectMode: true
  197. }, function(err, buf, cb){
  198. cb(null, null);
  199. }))
  200. .pipe(StreamTest[version].toObjects(function(err, objs) {
  201. if(err) {
  202. return done(err);
  203. }
  204. assert.equal(objs.length, 0);
  205. done();
  206. }));
  207. });
  208. it('should work with multiple pipes', function(done) {
  209. StreamTest[version].fromObjects([object1, object2])
  210. .pipe(syncBufferPrefixer(object4))
  211. .pipe(syncBufferPrefixer(object5))
  212. .pipe(syncBufferPrefixer(object6))
  213. .pipe(StreamTest[version].toObjects(function(err, objs) {
  214. if(err) {
  215. return done(err);
  216. }
  217. assert.deepEqual(objs, [object6, object5, object4, object1, object2]);
  218. done();
  219. }));
  220. });
  221. });
  222. });
  223. });
  224. });
  225. });