Browse Source

test: Add transform objectMode test

v0.9.9-release
isaacs 12 years ago
parent
commit
6e05faa3d0
  1. 81
      test/simple/test-stream2-transform.js

81
test/simple/test-stream2-transform.js

@ -44,6 +44,7 @@ function run() {
fn({ fn({
same: assert.deepEqual, same: assert.deepEqual,
equal: assert.equal, equal: assert.equal,
ok: assert,
end: function () { end: function () {
count--; count--;
run(); run();
@ -331,3 +332,83 @@ test('passthrough facaded', function(t) {
}, 10); }, 10);
}, 10); }, 10);
}); });
test('object transform (json parse)', function(t) {
console.error('json parse stream');
var jp = new Transform({ objectMode: true });
jp._transform = function(data, output, cb) {
try {
output(JSON.parse(data));
cb();
} catch (er) {
cb(er);
}
};
// anything except null/undefined is fine.
// those are "magic" in the stream API, because they signal EOF.
var objects = [
{ foo: 'bar' },
100,
"string",
{ nested: { things: [ { foo: 'bar' }, 100, "string" ] } }
];
var ended = false;
jp.on('end', function() {
ended = true;
});
objects.forEach(function(obj) {
jp.write(JSON.stringify(obj));
var res = jp.read();
t.same(res, obj);
});
jp.end();
process.nextTick(function() {
t.ok(ended);
t.end();
})
});
test('object transform (json stringify)', function(t) {
console.error('json parse stream');
var js = new Transform({ objectMode: true });
js._transform = function(data, output, cb) {
try {
output(JSON.stringify(data));
cb();
} catch (er) {
cb(er);
}
};
// anything except null/undefined is fine.
// those are "magic" in the stream API, because they signal EOF.
var objects = [
{ foo: 'bar' },
100,
"string",
{ nested: { things: [ { foo: 'bar' }, 100, "string" ] } }
];
var ended = false;
js.on('end', function() {
ended = true;
});
objects.forEach(function(obj) {
js.write(obj);
var res = js.read();
t.equal(res, JSON.stringify(obj));
});
js.end();
process.nextTick(function() {
t.ok(ended);
t.end();
})
});

Loading…
Cancel
Save