writable_test.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var Writable = require('../lib/lazystream').Writable;
  2. var DummyWritable = require('./helper').DummyWritable;
  3. exports.writable = {
  4. options: function(test) {
  5. test.expect(3);
  6. var writable = new Writable(function(options) {
  7. test.ok(this instanceof Writable, "Writable should bind itself to callback's this");
  8. test.equal(options.encoding, "utf-8", "Writable should make options accessible to callback");
  9. this.ok = true;
  10. return new DummyWritable([]);
  11. }, {encoding: "utf-8"});
  12. writable.write("test");
  13. test.ok(writable.ok);
  14. test.done();
  15. },
  16. dummy: function(test) {
  17. var expected = [ 'line1\n', 'line2\n' ];
  18. var actual = [];
  19. test.expect(0);
  20. var dummy = new DummyWritable(actual);
  21. expected.forEach(function(item) {
  22. dummy.write(new Buffer(item));
  23. });
  24. test.done();
  25. },
  26. streams2: function(test) {
  27. var expected = [ 'line1\n', 'line2\n' ];
  28. var actual = [];
  29. var instantiated = false;
  30. test.expect(2);
  31. var writable = new Writable(function() {
  32. instantiated = true;
  33. return new DummyWritable(actual);
  34. });
  35. test.equal(instantiated, false, 'DummyWritable should only be instantiated when it is needed');
  36. writable.on('end', function() {
  37. test.equal(actual.join(''), expected.join(''), 'Writable should not change the data of the underlying stream');
  38. test.done();
  39. });
  40. expected.forEach(function(item) {
  41. writable.write(new Buffer(item));
  42. });
  43. writable.end();
  44. }
  45. };