Skip to content

Commit

Permalink
A few small changes, white-space, organization, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Thomas committed Sep 1, 2010
1 parent e8f034e commit a840c19
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 73 deletions.
155 changes: 84 additions & 71 deletions lib/async_testing.js
Expand Up @@ -4,7 +4,7 @@ var assert = require('assert')
;

// include the default test runner as the "run" function on this module
exports.run = require('./runners').def;
exports.run = require('./default-runner').run;

// adds the assertion functions to a test, but binds them to that particular
// test so assertions are properly associated with the right test.
Expand Down Expand Up @@ -38,9 +38,9 @@ function addAssertionFunctions(test) {
});
}

/* Runs a object of tests. Each property in the object should be a
/* Runs a object of tests. Each property in the object should be a
* test. A test is just a method.
*
*
* Available configuration options:
*
* + parallel: boolean, for whether or not the tests should be run in parallel
Expand Down Expand Up @@ -181,8 +181,8 @@ exports.runSuite = function(obj, options) {
// if they specified the number of assertions, make sure they match up
if (this.obj.numAssertions && this.obj.numAssertions != this.numAssertions) {
this.failure = new assert.AssertionError(
{ message: 'Wrong number of assertions: ' + this.obj.numAssertions +
' expected, ' + this.numAssertions + ' fired'
{ message: 'Wrong number of assertions: ' + this.numAssertions +
' != ' + this.obj.numAssertions
, actual: this.numAssertions
, expected: this.obj.numAssertions
});
Expand Down Expand Up @@ -418,10 +418,10 @@ exports.runSuite = function(obj, options) {
}
}

/* This isn't as efficient as it could be. Basically, this is where we
/* This isn't as efficient as it could be. Basically, this is where we
* analyze all the tests that finished in error and have multiple test
* candidates. Then we generate reports for those errors.
* Right now it is really dumb, it just lumps all of these tests into one
* candidates. Then we generate reports for those errors.
* Right now it is really dumb, it just lumps all of these tests into one
* multierror report. However it could be smarter than that.
*
* TODO make this smarter!
Expand Down Expand Up @@ -483,34 +483,101 @@ exports.runSuites = function(list, options) {
// make sure options exist
options = options || {};

var suites
, startTime
, allResults = []
, index = 0
;

exports.loadSuites(list, options.suiteName, startSuites);

function startSuites(loaded) {
suites = loaded;

if (options.onStart) {
options.onStart(suites.length);
}

startTime = new Date();
runNextSuite();
}

function runNextSuite() {
var item = suites[index];
index++;

if (!item) {
if (options.onDone) {
options.onDone(allResults, new Date()-startTime);
}
return;
}

name = item.name;
suite = item.suite;

var itemOpts =
{ parallel: options.parallel
, testName: options.testName
, name: name
, onSuiteStart: options.onSuiteStart
, onSuiteDone: function(results) {
allResults.push(results);

if (options.onSuiteDone) {
options.onSuiteDone(results);
}

process.nextTick(function() {
runNextSuite();
});
}
, onTestStart: options.onTestStart
, onTestDone: options.onTestDone
, onPrematureExit: options.onPrematureExit
}

exports.runSuite(suite, itemOpts);
}
}

exports.loadSuites = function(list, suiteName, cb) {
if (typeof cb === 'undefined') {
cb = suiteName;
suiteName = null;
}

if (list.constructor != Array) {
list = [list];
}

var index = 0
, allResults = []
, suites = []
var suites = []
, explicit = []
, startTime
;

for(var i = 0; i < list.length; i++) {
explicit.push(list[i]);
}


processNextItem();

function foundSuite(suite) {
if (!suiteName || suite.name == suiteName) {
suite.index = suites.length;
suites.push(suite);
}
}

function processNextItem() {
if( list.length == 0 ) {
return startSuites();
return cb(suites);
}

var item = list.shift();

// not a file name, but a javascript object
if (typeof item !== 'string') {
suites.push({suite: item});
foundSuite({suite: item});
return processNextItem();
}

Expand All @@ -533,11 +600,12 @@ exports.runSuites = function(list, options) {
}

if (stat.isFile()) {
// if they explicitly requested this file make sure to grab it, otherwise
// when recursing into directories only grab files that start with
// "test-" and end with ".js"
if (explicit.indexOf(item) >= 0 || path.basename(file).match(/^test-.*\.js$/)) {
var mod = require(path.join(path.dirname(file), path.basename(file, path.extname(file))));
suites.push({suite: mod, name: item});
foundSuite({suite: mod, name: item});
}
processNextItem();
}
Expand All @@ -557,61 +625,6 @@ exports.runSuites = function(list, options) {
}
});
}

function startSuites() {
if (typeof options.suiteName == 'string') {
for(var i = 0; i < suites.length; i++) {
if (options.suiteName != suites[i].name) {
suites.splice(i,1);
i--;
}
}
}
if (options.onStart) {
options.onStart(suites.length);
}

startTime = new Date();
runNextSuite();
}

function runNextSuite() {
var item = suites[index];
index++;

if (!item) {
if (options.onDone) {
options.onDone(allResults, new Date()-startTime);
}
return;
}

name = item.name;
suite = item.suite;

var itemOpts =
{ parallel: options.parallel
, testName: options.testName
, name: name
, onSuiteStart: options.onSuiteStart
, onSuiteDone: function(results) {
allResults.push(results);

if (options.onSuiteDone) {
options.onSuiteDone(results);
}

process.nextTick(function() {
runNextSuite();
});
}
, onTestStart: options.onTestStart
, onTestDone: options.onTestDone
, onPrematureExit: options.onPrematureExit
}

exports.runSuite(suite, itemOpts);
}
}

// convenience function for wrapping tests in a suite for setup or teardown
Expand Down
6 changes: 4 additions & 2 deletions lib/runners.js → lib/default-runner.js
Expand Up @@ -14,7 +14,8 @@ var testing = require('./async_testing');
* args: command line arguments to override/augment the options
* callback: a function to be called then the suites are finished
*/
exports.def = function(list, options, args, callback) {
exports.run = function(list, options, args, callback) {

var red = function(str){return "\033[31m" + str + "\033[39m"}
, yellow = function(str){return "\033[33m" + str + "\033[39m"}
, green = function(str){return "\033[32m" + str + "\033[39m"}
Expand Down Expand Up @@ -427,11 +428,12 @@ function createHelpMessage(options) {
}

helpMessage +=
+ '\n'
'\n'
+ '\n'
+ '\nAny additional arguments will be interpreted as file names for test suites that'
+ '\nshould be ran.'
;

return helpMessage;
}

0 comments on commit a840c19

Please sign in to comment.