readable_test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var Readable = require('../lib/lazystream').Readable;
  2. var DummyReadable = require('./helper').DummyReadable;
  3. exports.readable = {
  4. dummy: function(test) {
  5. var expected = [ 'line1\n', 'line2\n' ];
  6. var actual = [];
  7. test.expect(1);
  8. new DummyReadable([].concat(expected))
  9. .on('data', function(chunk) {
  10. actual.push(chunk.toString());
  11. })
  12. .on('end', function() {
  13. test.equal(actual.join(''), expected.join(''), 'DummyReadable should produce the data it was created with');
  14. test.done();
  15. });
  16. },
  17. options: function(test) {
  18. test.expect(3);
  19. var readable = new Readable(function(options) {
  20. test.ok(this instanceof Readable, "Readable should bind itself to callback's this");
  21. test.equal(options.encoding, "utf-8", "Readable should make options accessible to callback");
  22. this.ok = true;
  23. return new DummyReadable(["test"]);
  24. }, {encoding: "utf-8"});
  25. readable.read(4);
  26. test.ok(readable.ok);
  27. test.done();
  28. },
  29. streams2: function(test) {
  30. var expected = [ 'line1\n', 'line2\n' ];
  31. var actual = [];
  32. var instantiated = false;
  33. test.expect(2);
  34. var readable = new Readable(function() {
  35. instantiated = true;
  36. return new DummyReadable([].concat(expected));
  37. });
  38. test.equal(instantiated, false, 'DummyReadable should only be instantiated when it is needed');
  39. readable.on('readable', function() {
  40. var chunk;
  41. while ((chunk = readable.read())) {
  42. actual.push(chunk.toString());
  43. }
  44. });
  45. readable.on('end', function() {
  46. test.equal(actual.join(''), expected.join(''), 'Readable should not change the data of the underlying stream');
  47. test.done();
  48. });
  49. readable.read(0);
  50. },
  51. resume: function(test) {
  52. var expected = [ 'line1\n', 'line2\n' ];
  53. var actual = [];
  54. var instantiated = false;
  55. test.expect(2);
  56. var readable = new Readable(function() {
  57. instantiated = true;
  58. return new DummyReadable([].concat(expected));
  59. });
  60. readable.pause();
  61. readable.on('data', function(chunk) {
  62. actual.push(chunk.toString());
  63. });
  64. readable.on('end', function() {
  65. test.equal(actual.join(''), expected.join(''), 'Readable should not change the data of the underlying stream');
  66. test.done();
  67. });
  68. test.equal(instantiated, false, 'DummyReadable should only be instantiated when it is needed');
  69. readable.resume();
  70. }
  71. };