Comparing version 0.0.11 to 0.0.12
@@ -16,3 +16,5 @@ 'use strict'; | ||
var errors = 0; | ||
var next; | ||
// constants | ||
@@ -53,2 +55,3 @@ var GREEN = '\u001b[32m'; | ||
{ | ||
errors += 1; | ||
// only report the first failure | ||
@@ -196,8 +199,13 @@ if (!self.failure) | ||
var series = clone(param); | ||
var testResult = new CompositeResult('success'); | ||
runOne(series, testResult, function(error, result) | ||
// uncaught exceptions | ||
process.on('uncaughtException', function(error) | ||
{ | ||
if (testResult.failure) | ||
uncaughtException(error); | ||
}); | ||
var runner = new Runner('success', series); | ||
runner.runAll(function(error, result) | ||
{ | ||
if (result.failure) | ||
{ | ||
testResult.key = 'failure'; | ||
result.key = errors + ' failures'; | ||
} | ||
@@ -209,65 +217,107 @@ callback(error, result); | ||
/** | ||
* Run one function in the series, go to the next. | ||
* Process an uncaught exception. | ||
*/ | ||
function runOne(series, testResult, callback) | ||
function uncaughtException(error) | ||
{ | ||
if (isEmpty(series)) | ||
if (!next) | ||
{ | ||
return callback(null, testResult); | ||
return log.error('Exception without next text'); | ||
} | ||
for (var key in series) | ||
next('Uncaught exception: ' + error.stack); | ||
} | ||
/** | ||
* A runner for a series of tests. | ||
*/ | ||
var Runner = function(key, series) | ||
{ | ||
// self-reference | ||
var self = this; | ||
// attributes | ||
self.result = new CompositeResult(key); | ||
var finished = false; | ||
/** | ||
* Run all functions in the series, one by one. | ||
*/ | ||
self.runAll = function(callback) | ||
{ | ||
var value = series[key]; | ||
if (typeof value == 'object') | ||
runOne(function(error, result) | ||
{ | ||
var subResult = new CompositeResult(key); | ||
runOne(value, subResult, function(error, result) | ||
if (finished) | ||
{ | ||
if (error) | ||
{ | ||
log.error('Could not run all functions'); | ||
return; | ||
} | ||
testResult.add(subResult); | ||
deleteAndRunNext(key, series, testResult, callback); | ||
}); | ||
return; | ||
} | ||
finished = true; | ||
callback(error, result); | ||
}); | ||
} | ||
/** | ||
* Run one function in the given series, go to the next. | ||
*/ | ||
function runOne(callback) | ||
{ | ||
if (isEmpty(series)) | ||
{ | ||
return callback(null, self.result); | ||
} | ||
else if (typeof value == 'function') | ||
for (var key in series) | ||
{ | ||
var subResult = new TestResult(key); | ||
// it is a function to run | ||
value(function(error, result) | ||
var value = series[key]; | ||
if (typeof value == 'object') | ||
{ | ||
subResult.callback(error, result); | ||
testResult.add(subResult); | ||
deleteAndRunNext(key, series, testResult, callback); | ||
}); | ||
var runner = new Runner(key, value); | ||
runner.runAll(function(error, result) | ||
{ | ||
if (error) | ||
{ | ||
log.error('Could not run all functions'); | ||
return; | ||
} | ||
self.result.add(runner.result); | ||
deleteAndRunNext(key, callback); | ||
}); | ||
} | ||
else if (typeof value == 'function') | ||
{ | ||
var subResult = new TestResult(key); | ||
// it is a function to run | ||
next = function(error, result) | ||
{ | ||
subResult.callback(error, result); | ||
self.result.add(subResult); | ||
deleteAndRunNext(key, callback); | ||
}; | ||
value(next); | ||
} | ||
else | ||
{ | ||
log.error('Invalid value %s', value); | ||
self.result.callback('Key %s has an invalid value %s'); | ||
deleteAndRunNext(key, callback); | ||
} | ||
// only the first element in series is used; | ||
// the rest are called by recursion in deleteAndRunNext() | ||
return; | ||
} | ||
else | ||
{ | ||
log.error('Invalid value %s', value); | ||
testResult.callback('Key %s has an invalid value %s'); | ||
deleteAndRunNext(key, series, testResult, callback); | ||
} | ||
// only the first element in series is used; | ||
// the rest are called by recursion in deleteAndRunNext() | ||
return; | ||
} | ||
} | ||
/** | ||
* Delete the current function, run the next. | ||
*/ | ||
function deleteAndRunNext(key, series, testResult, callback) | ||
{ | ||
if (!(key in series)) | ||
/** | ||
* Delete the current function, run the next. | ||
*/ | ||
function deleteAndRunNext(key, callback) | ||
{ | ||
// already run | ||
return; | ||
if (!(key in series)) | ||
{ | ||
// already run | ||
return; | ||
} | ||
delete series[key]; | ||
return process.nextTick(function() | ||
{ | ||
runOne(callback); | ||
}); | ||
} | ||
delete series[key]; | ||
return process.nextTick(function() | ||
{ | ||
runOne(series, testResult, callback); | ||
}); | ||
} | ||
@@ -291,2 +341,5 @@ | ||
}, | ||
f: function(callback) { | ||
throw new Error('exception'); | ||
}, | ||
}, | ||
@@ -308,3 +361,3 @@ }; | ||
console.assert(result.results.b.results.e.message == 'e', 'Should have a e for b.e'); | ||
log.info('Test run successful: %s', result); | ||
log.info('Test run successful with 2 failures: %s', result); | ||
}); | ||
@@ -311,0 +364,0 @@ } |
{ | ||
"name": "testing", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Simple testing framework.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/alexfernandez/testing", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23313
699