Permalink
Browse files

Bugfix: fs.ReadStream.setEncoding

Improve test-fs-read-stream test.
  • Loading branch information...
1 parent 31854c7 commit 8840ce340de2d22ee3f88698e907d4c8028a0345 @ry ry committed May 27, 2010
Showing with 36 additions and 22 deletions.
  1. +1 −1 lib/fs.js
  2. +1 −0 test/fixtures/elipses.txt
  3. +34 −21 test/simple/{test-file-read-stream.js → test-fs-read-stream.js}
View
@@ -635,7 +635,7 @@ fs.FileReadStream = fs.ReadStream; // support the legacy name
ReadStream.prototype.setEncoding = function (encoding) {
var Utf8Decoder = require("utf8decoder").Utf8Decoder; // lazy load
var self = this;
- this._encoding = enc.toLowerCase();
+ this._encoding = encoding.toLowerCase();
if (this._encoding == 'utf-8' || this._encoding == 'utf8') {
this._decoder = new Utf8Decoder();
this._decoder.onString = function(str) {
Oops, something went wrong.
@@ -7,38 +7,27 @@ require('../common');
Buffer = require('buffer').Buffer;
path = require('path');
fs = require('fs');
-fn = path.join(fixturesDir, 'test_ca.pem');
+fn = path.join(fixturesDir, 'elipses.txt');
-file = fs.createReadStream(fn);
-
-callbacks = {
- open: -1,
- end: -1,
- data: -1,
- close: -1,
- destroy: -1
-};
+callbacks = { open: 0, end: 0, close: 0, destroy: 0 };
paused = false;
-fileContent = '';
+file = fs.createReadStream(fn);
file.addListener('open', function(fd) {
+ file.length = 0;
callbacks.open++;
assert.equal('number', typeof fd);
assert.ok(file.readable);
});
-file.addListener('error', function(err) {
- throw err;
-});
file.addListener('data', function(data) {
- callbacks.data++;
assert.ok(data instanceof Buffer);
assert.ok(!paused);
- fileContent += data;
-
+ file.length += data.length;
+
paused = true;
file.pause();
assert.ok(file.paused);
@@ -50,15 +39,17 @@ file.addListener('data', function(data) {
}, 10);
});
+
file.addListener('end', function(chunk) {
callbacks.end++;
});
+
file.addListener('close', function() {
callbacks.close++;
assert.ok(!file.readable);
- assert.equal(fs.readFileSync(fn), fileContent);
+ //assert.equal(fs.readFileSync(fn), fileContent);
});
var file2 = fs.createReadStream(fn);
@@ -67,8 +58,30 @@ file2.destroy(function(err) {
callbacks.destroy++;
});
-process.addListener('exit', function() {
- for (var k in callbacks) {
- assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
+var file3 = fs.createReadStream(fn);
+file3.length = 0;
+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);
+});

0 comments on commit 8840ce3

Please sign in to comment.