Skip to content

Commit

Permalink
objectMode for stream output
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Mar 4, 2014
1 parent 5d2cd63 commit 26c05e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ exports = module.exports = (function () {
return getHarness.only.apply(this, arguments);
}

lazyLoad.createStream = function () {
lazyLoad.createStream = function (opts) {
if (!opts) opts = {};
if (!harness) {
var output = through();
getHarness({ stream: output });
getHarness({ stream: output, objectMode: opts.objectMode });
return output;
}
return harness.createStream();
return harness.createStream(opts);
};

return lazyLoad
Expand All @@ -51,7 +52,7 @@ function createExitHarness (conf) {
autoclose: defined(conf.autoclose, false)
});

var stream = harness.createStream();
var stream = harness.createStream({ objectMode: conf.objectMode });
var es = stream.pipe(conf.stream || createDefaultStream());
if (canEmitExit) {
es.on('error', function (err) { harness._exitCode = 1 });
Expand Down Expand Up @@ -124,8 +125,8 @@ function createHarness (conf_) {

test._tests = [];

test.createStream = function () {
return results.createStream();
test.createStream = function (opts) {
return results.createStream(opts);
};

var only = false;
Expand Down
43 changes: 38 additions & 5 deletions lib/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,43 @@ function Results () {
this.tests = [];
}

Results.prototype.createStream = function () {
Results.prototype.createStream = function (opts) {
if (!opts) opts = {};
var self = this;
var output = resumer();
output.queue('TAP version 13\n');
var output, testId = 0;
if (opts.objectMode) {
output = through();
self.on('_push', function ontest (t, extra) {
if (!extra) extra = {};
var id = testId++;
var row = {
type: 'test',
name: t.name,
id: id
};
if (extra.parent) {
row.parent = extra.parent;
}
output.queue(row);
t.on('test', function (st) {
ontest(st, { parent: id });
});
t.on('result', function (res) {
res.test = id;
res.type = 'assert';
output.queue(res);
});
t.on('end', function () {
output.queue({ type: 'end', test: id });
});
});
self.on('done', function () { output.queue(null) });
}
else {
output = resumer();
output.queue('TAP version 13\n');
self._stream.pipe(output);
}

nextTick(function next() {
var t;
Expand All @@ -33,7 +66,6 @@ Results.prototype.createStream = function () {
}
self.emit('done');
});
self._stream.pipe(output);

return output;
};
Expand All @@ -42,6 +74,7 @@ Results.prototype.push = function (t) {
var self = this;
self.tests.push(t);
self._watch(t);
self.emit('_push', t);
};

Results.prototype.only = function (name) {
Expand Down Expand Up @@ -154,7 +187,7 @@ function getSerialize () {
};
}

function getNextTest(results) {
function getNextTest (results) {
if (!results._only) {
return results.tests.shift();
}
Expand Down

0 comments on commit 26c05e3

Please sign in to comment.