You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.3 KiB

  1. var common = require('../common');
  2. var WriteStreamStub = GENTLY.stub('fs', 'WriteStream');
  3. var File = require(common.lib + '/file'),
  4. EventEmitter = require('events').EventEmitter,
  5. file,
  6. gently;
  7. function test(test) {
  8. gently = new Gently();
  9. file = new File();
  10. test();
  11. gently.verify(test.name);
  12. }
  13. test(function constructor() {
  14. assert.ok(file instanceof EventEmitter);
  15. assert.strictEqual(file.size, 0);
  16. assert.strictEqual(file.path, null);
  17. assert.strictEqual(file.name, null);
  18. assert.strictEqual(file.type, null);
  19. assert.strictEqual(file.lastModifiedDate, null);
  20. assert.strictEqual(file._writeStream, null);
  21. (function testSetProperties() {
  22. var file2 = new File({foo: 'bar'});
  23. assert.equal(file2.foo, 'bar');
  24. })();
  25. });
  26. test(function open() {
  27. var WRITE_STREAM;
  28. file.path = '/foo';
  29. gently.expect(WriteStreamStub, 'new', function (path) {
  30. WRITE_STREAM = this;
  31. assert.strictEqual(path, file.path);
  32. });
  33. file.open();
  34. assert.strictEqual(file._writeStream, WRITE_STREAM);
  35. });
  36. test(function write() {
  37. var BUFFER = {length: 10},
  38. CB_STUB,
  39. CB = function() {
  40. CB_STUB.apply(this, arguments);
  41. };
  42. file._writeStream = {};
  43. gently.expect(file._writeStream, 'write', function (buffer, cb) {
  44. assert.strictEqual(buffer, BUFFER);
  45. gently.expect(file, 'emit', function (event, bytesWritten) {
  46. assert.ok(file.lastModifiedDate instanceof Date);
  47. assert.equal(event, 'progress');
  48. assert.equal(bytesWritten, file.size);
  49. });
  50. CB_STUB = gently.expect(function writeCb() {
  51. assert.equal(file.size, 10);
  52. });
  53. cb();
  54. gently.expect(file, 'emit', function (event, bytesWritten) {
  55. assert.equal(event, 'progress');
  56. assert.equal(bytesWritten, file.size);
  57. });
  58. CB_STUB = gently.expect(function writeCb() {
  59. assert.equal(file.size, 20);
  60. });
  61. cb();
  62. });
  63. file.write(BUFFER, CB);
  64. });
  65. test(function end() {
  66. var CB_STUB,
  67. CB = function() {
  68. CB_STUB.apply(this, arguments);
  69. };
  70. file._writeStream = {};
  71. gently.expect(file._writeStream, 'end', function (cb) {
  72. gently.expect(file, 'emit', function (event) {
  73. assert.equal(event, 'end');
  74. });
  75. CB_STUB = gently.expect(function endCb() {
  76. });
  77. cb();
  78. });
  79. file.end(CB);
  80. });