Skip to content

Commit

Permalink
Fix: Clone with constructor (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
darsain authored and phated committed Jun 7, 2016
1 parent e19855d commit 7fc1aca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ File.prototype.clone = function(opt) {
contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
}

var file = new File({
var file = new this.constructor({
cwd: this.cwd,
base: this.base,
stat: (this.stat ? cloneStats(this.stat) : null),
Expand Down
24 changes: 24 additions & 0 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,30 @@ describe('File', function() {

done();
});

it('should work with extended files', function(done) {
function ExtendedFile() {
File.apply(this, arguments);
}
ExtendedFile.prototype = Object.create(File.prototype);
ExtendedFile.prototype.constructor = ExtendedFile;
// Object.setPrototypeOf(ExtendedFile, File);
// Just copy static stuff since Object.setPrototypeOf is node >=0.12
Object.keys(File).forEach(function(key) {
ExtendedFile[key] = File[key];
});

var file = new ExtendedFile();
var file2 = file.clone();

file2.should.not.equal(file, 'refs should be different');
file2.constructor.should.equal(ExtendedFile);
(file2 instanceof ExtendedFile).should.equal(true);
(file2 instanceof File).should.equal(true);
ExtendedFile.prototype.isPrototypeOf(file2).should.equal(true);
File.prototype.isPrototypeOf(file2).should.equal(true);
done();
});
});

describe('pipe()', function() {
Expand Down

0 comments on commit 7fc1aca

Please sign in to comment.