Bugfix: fs.ReadStream.setEncoding

Improve test-fs-read-stream test.
This commit is contained in:
Ryan Dahl 2010-05-26 18:32:56 -07:00
parent 31854c7990
commit 8840ce340d
3 changed files with 36 additions and 22 deletions

View File

@ -635,7 +635,7 @@ fs.FileReadStream = fs.ReadStream; // support the legacy name
ReadStream.prototype.setEncoding = function (encoding) { ReadStream.prototype.setEncoding = function (encoding) {
var Utf8Decoder = require("utf8decoder").Utf8Decoder; // lazy load var Utf8Decoder = require("utf8decoder").Utf8Decoder; // lazy load
var self = this; var self = this;
this._encoding = enc.toLowerCase(); this._encoding = encoding.toLowerCase();
if (this._encoding == 'utf-8' || this._encoding == 'utf8') { if (this._encoding == 'utf-8' || this._encoding == 'utf8') {
this._decoder = new Utf8Decoder(); this._decoder = new Utf8Decoder();
this._decoder.onString = function(str) { this._decoder.onString = function(str) {

1
test/fixtures/elipses.txt vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -7,37 +7,26 @@ require('../common');
Buffer = require('buffer').Buffer; Buffer = require('buffer').Buffer;
path = require('path'); path = require('path');
fs = require('fs'); fs = require('fs');
fn = path.join(fixturesDir, 'test_ca.pem'); fn = path.join(fixturesDir, 'elipses.txt');
file = fs.createReadStream(fn); callbacks = { open: 0, end: 0, close: 0, destroy: 0 };
callbacks = {
open: -1,
end: -1,
data: -1,
close: -1,
destroy: -1
};
paused = false; paused = false;
fileContent = ''; file = fs.createReadStream(fn);
file.addListener('open', function(fd) { file.addListener('open', function(fd) {
file.length = 0;
callbacks.open++; callbacks.open++;
assert.equal('number', typeof fd); assert.equal('number', typeof fd);
assert.ok(file.readable); assert.ok(file.readable);
}); });
file.addListener('error', function(err) {
throw err;
});
file.addListener('data', function(data) { file.addListener('data', function(data) {
callbacks.data++;
assert.ok(data instanceof Buffer); assert.ok(data instanceof Buffer);
assert.ok(!paused); assert.ok(!paused);
fileContent += data; file.length += data.length;
paused = true; paused = true;
file.pause(); file.pause();
@ -50,15 +39,17 @@ file.addListener('data', function(data) {
}, 10); }, 10);
}); });
file.addListener('end', function(chunk) { file.addListener('end', function(chunk) {
callbacks.end++; callbacks.end++;
}); });
file.addListener('close', function() { file.addListener('close', function() {
callbacks.close++; callbacks.close++;
assert.ok(!file.readable); assert.ok(!file.readable);
assert.equal(fs.readFileSync(fn), fileContent); //assert.equal(fs.readFileSync(fn), fileContent);
}); });
var file2 = fs.createReadStream(fn); var file2 = fs.createReadStream(fn);
@ -67,8 +58,30 @@ file2.destroy(function(err) {
callbacks.destroy++; callbacks.destroy++;
}); });
process.addListener('exit', function() { var file3 = fs.createReadStream(fn);
for (var k in callbacks) { file3.length = 0;
assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]); file3.setEncoding('utf8');
file3.addListener('data', function(data) {
assert.equal("string", typeof(data));
file3.length += data.length;
for (var i = 0; i < data.length; i++) {
// http://www.fileformat.info/info/unicode/char/2026/index.htm
assert.equal("\u2026", data[i]);
} }
}); });
file3.addListener('close', function () {
callbacks.close++;
});
process.addListener('exit', function() {
assert.equal(1, callbacks.open);
assert.equal(1, callbacks.end);
assert.equal(1, callbacks.destroy);
assert.equal(2, callbacks.close);
assert.equal(30000, file.length);
assert.equal(10000, file3.length);
});