Skip to content

Commit

Permalink
fix double-piping bug in the output
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 25, 2012
1 parent 463afd2 commit ddd2535
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
51 changes: 51 additions & 0 deletions example/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var falafel = require('falafel');
var test = require('../');

test('nested array test', function (t) {
t.plan(5);

var src = '(' + function () {
var xs = [ 1, 2, [ 3, 4 ] ];
var ys = [ 5, 6 ];
g([ xs, ys ]);
} + ')()';

var output = falafel(src, function (node) {
if (node.type === 'ArrayExpression') {
node.update('fn(' + node.source() + ')');
}
});

t.test(function (q) {
q.plan(2);
q.ok(true);

setTimeout(function () {
q.ok(true);
}, 3000);
});

var arrays = [
[ 3, 4 ],
[ 1, 2, [ 3, 4 ] ],
[ 5, 6 ],
[ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ],
];

Function(['fn','g'], output)(
function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
t.same(xs, [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]);
}
);
});

test('another', function (t) {
t.plan(1);
setTimeout(function () {
t.ok(true);
}, 100);
});
16 changes: 5 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exports.Test = Test;
function createHarness () {
var pending = [];
var running = false;

var began = false;
var out = new Render();

return function (name, conf, cb) {
Expand All @@ -18,19 +20,11 @@ function createHarness () {
}
var t = new Test;
t.name = name;
var piped = false;

t.pipe = function () {
piped = true;
};

t.once('pipe', function () {
piped = true;
});

process.nextTick(function () {
if (!piped) out.pipe(createDefaultStream());
out.begin();
if (!out.piped) out.pipe(createDefaultStream());
if (!began) out.begin();
began = true;

var run = function () {
running = true;
Expand Down
9 changes: 7 additions & 2 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ function Render () {

Render.prototype = new Stream;

Render.prototype.pipe = function () {
this.piped = true;
return Stream.prototype.pipe.apply(this, arguments);
};

Render.prototype.begin = function () {
this.emit('data', 'TAP version 13\n');
};

Render.prototype.push = function (t) {
var self = this;
this.emit('data', '# ' + t.name + '\n');
this.emit('data', Array(t.indent + 1).join(' ') + '# ' + t.name + '\n');

t.on('result', function (res) {
self.emit('data', encodeResult(res, self.count + 1));
Expand All @@ -44,7 +49,7 @@ Render.prototype.close = function () {
};

function encodeResult (res, count) {
var output = '';
var output = Array(res.indent + 1).join(' ');
output += (res.ok ? 'ok ' : 'not ok ') + count;
output += res.name ? ' ' + res.name.replace(/\s+/g, ' ') : '';

Expand Down
25 changes: 23 additions & 2 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,39 @@ module.exports = Test;

Test.prototype = new EventEmitter;

function Test (name) {
function Test (name, opts) {
if (!opts) opts = {};
EventEmitter.call(this);

this.name = name || '(anonymous)';
this.assertCount = 0;
this.indent = opts.indent || 0;
}

Test.prototype.test = function (name, cb) {
var self = this;

if (typeof name === 'function') {
cb = name;
name = '(anonymous)';
}

/*
var t = new Test(name);
t.on('plan', function (n) {
self._plan += n;
});
*/
};

Test.prototype.comment = function (msg) {
this.result('\n' + msg.trim());
};

Test.prototype.plan = function (n) {
this._plan = n;
this.emit('plan', n);
};

Test.prototype.end = function () {
Expand All @@ -32,6 +52,7 @@ Test.prototype._assert = function assert (ok, opts) {

var res = {
id : self.assertCount ++,
indent : self.indent,
ok : Boolean(ok),
skip : defined(extra.skip, opts.skip),
name : defined(extra.message, opts.message, '(unnamed assert)'),
Expand Down Expand Up @@ -81,7 +102,7 @@ Test.prototype.skip = function (msg, extra) {
Test.prototype.ok
= Test.prototype.true
= Test.prototype.assert
= function (value, msg) {
= function (value, msg, extra) {
this._assert(value, {
message : msg,
operator : 'ok',
Expand Down

0 comments on commit ddd2535

Please sign in to comment.