Skip to content

Commit

Permalink
took out lots of complexity, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 28, 2013
1 parent 78c7b77 commit 827b59d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 140 deletions.
144 changes: 16 additions & 128 deletions index.js
@@ -1,20 +1,14 @@
var createDefaultStream = require('./lib/default_stream');
var Render = require('./lib/render');
var Test = require('./lib/test');
var through = require('through');

var canEmitExit = typeof process !== 'undefined' && process
&& typeof process.on === 'function'
;
var canExit = typeof process !== 'undefined' && process
&& typeof process.exit === 'function'
;
var onexit = (function () {
var stack = [];
if (canEmitExit) process.on('exit', function (code) {
for (var i = 0; i < stack.length; i++) stack[i](code);
});
return function (cb) { stack.push(cb) };
})();

var nextTick = typeof setImmediate !== 'undefined'
? setImmediate
Expand All @@ -28,138 +22,32 @@ exports.Test = Test;
var exitInterval;

function createHarness (conf_) {
var pending = [];
var running = false;
var count = 0;

var began = false;
var only = false;
var closed = false;
var out = new Render();
if (!conf_) conf_ = {};

var tests = [];
if (conf_.exit === false && exitInterval) clearInterval(exitInterval);

exitInterval = !exitInterval && conf_.exit !== false && canEmitExit
&& typeof process._getActiveHandles === 'function'
&& setInterval(function () {
if (/^v0\.8\./.test(process.version)
&& process._getActiveHandles().length === 1) {
tests.forEach(function (t) { t._exit() });
}
}, 200);

var exitCode = 0;
var exit = function (c) { exitCode = c };

out.on('end', function () {
nextTick(function () {
clearInterval(exitInterval);
if (canExit && conf_.exit !== false) process.exit(exitCode);
});
var output = through(null, function () {
if (--count === 0 && !closed) {
closed = true
out.close();
}
});
output.pause();
nextTick(function () { output.resume() });

var test = function (name, conf, cb) {
count++;
var t = new Test(name, conf, cb);
tests.push(t);
if (!conf || typeof conf !== 'object') conf = conf_;

if (conf.exit !== false) {
onexit(function (code) {
t._exit();
if (!closed) {
closed = true
out.close();
}
if (!code && !t._ok && (!only || name === only)) {
exit(1);
}
});
}

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

var run = function () {
running = true;
out.push(t);
t.run();
};

if (only && name !== only) {
count--;
return;
}

if (running || pending.length) {
pending.push(run);
}
else run();
});

t.on('test', function sub (st) {
count++;
st.on('test', sub);
st.on('end', onend);
console.log('SUBTEST');
});
t.on('result', function (r) {
console.dir(r);
if (!r.ok) exitCode = 1
});
nextTick(function () {
t.run();
});
t.on('result', function (r) { if (!r.ok) exitCode = 1 });

t.on('end', onend);

return t;

function onend () {
count--;
if (this._progeny.length) {
var unshifts = map(this._progeny, function (st) {
return function () {
running = true;
out.push(st);
st.run();
};
});
pending.unshift.apply(pending, unshifts);
}

process.nextTick(function () {
running = false;
if (pending.length) return pending.shift()();
if (count === 0 && !closed) {
closed = true
out.close();
}
if (conf.exit !== false && canExit && !t._ok) {
exit(1);
}
});
}
};

test.only = function (name) {
if (only) {
throw new Error("there can only be one only test");
}

only = name;

return test.apply(null, arguments);
};

test.stream = out;
return test;
}

function map (xs, f) {
if (xs.map) return xs.map(f);
var res = [];
for (var i = 0; i < xs.length; i++) {
res.push(f(xs[i]));
}
return res;
}

// vim: set softtabstop=4 shiftwidth=4:
20 changes: 13 additions & 7 deletions lib/render.js
@@ -1,7 +1,18 @@
var Stream = require('stream');
var json = typeof JSON === 'object' ? JSON : require('jsonify');
var through = require('through');

module.exports = Render;
module.exports = function (test) {
var out = through();
out.pause();
var r = new Render;
r.pipe(out);
r.push(test);

out.begin = function () { r.begin() };
out.close = function () { r.close() };
return out;
};

function Render () {
Stream.call(this);
Expand All @@ -13,11 +24,6 @@ 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');
};
Expand All @@ -31,7 +37,7 @@ Render.prototype.push = function (t) {
self.emit('data', '# ' + res + '\n');
return;
}

self.emit('data', encodeResult(res, self.count + 1));
self.count ++;

Expand Down
18 changes: 13 additions & 5 deletions lib/test.js
@@ -1,7 +1,9 @@
var EventEmitter = require('events').EventEmitter;
var Stream = require('stream');
var deepEqual = require('deep-equal');
var defined = require('defined');
var path = require('path');
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;

module.exports = Test;

Expand All @@ -10,9 +12,10 @@ var nextTick = typeof setImmediate !== 'undefined'
: process.nextTick
;

Test.prototype = new EventEmitter;
inherits(Test, EventEmitter);

function Test (name_, opts_, cb_) {
var self = this;
var name = '(anonymous)';
var opts = {};
var cb;
Expand All @@ -30,8 +33,7 @@ function Test (name_, opts_, cb_) {
}
}

EventEmitter.call(this);

this.readable = true;
this.name = name || '(anonymous)';
this.assertCount = 0;
this._skip = opts.skip || false;
Expand Down Expand Up @@ -70,6 +72,13 @@ Test.prototype.plan = function (n) {
};

Test.prototype.end = function () {
var self = this;
if (this._progeny.length) {
var t = this._progeny.shift();
t.on('end', function () { self.end() });
return;
}

if (!this.ended) this.emit('end');
if (this._plan !== undefined &&
!this._planError && this.assertCount !== this._plan) {
Expand All @@ -94,7 +103,6 @@ Test.prototype._exit = function () {
else if (!this.ended) {
this.fail('test exited without ending');
}

};

Test.prototype._assert = function assert (ok, opts) {
Expand Down

0 comments on commit 827b59d

Please sign in to comment.