Comparing version 1.3.3 to 1.3.4
@@ -1,106 +0,171 @@ | ||
var _puts = require('sys').puts, | ||
colors = require('colors'); | ||
var colors = require('colors'); | ||
function kick(options,callback){ | ||
var module = options.module, | ||
title = options.name ? (options.name).bold+' | '.grey : ''; | ||
function logger(testsuite){ | ||
return function log(msg){ | ||
if(testsuite.options.silent) return; | ||
var tests = Object.keys(module) | ||
.filter(function(el){ return el.substring(0,4)=='test' }) | ||
.map(function(el){ return module[el] }); | ||
process.stdout.write(testsuite.title); | ||
process.stdout.write(msg); | ||
process.stdout.write('\n'); | ||
}; | ||
} | ||
var ctr = 0, | ||
len = tests.length, | ||
fail = 0; | ||
function ok(test, testsuite){ | ||
testsuite.printTestResult(test, 'OK'.green); | ||
} | ||
function mark(test,error){ | ||
ctr++; | ||
error && fail++; | ||
function fail(test, testsuite, error){ | ||
testsuite.fail++; | ||
testsuite.printTestResult(test, 'FAIL'.red); | ||
testsuite.log('ERROR: '.bold.red+(error.message ? error.message.bold : '')+'\n'+error.stack+')'); | ||
} | ||
if(!options.silent){ | ||
process.stdout.write(title); | ||
process.stdout.write(error ? 'FAIL '.bold.red : 'OK '.green.bold); | ||
_puts(test.name.bold.yellow + ' ('.grey+(ctr+'/'+len).bold+', '.grey + (( (new Date).getTime() - test.startTS)/1000+'s').white.bold+') '.grey); | ||
if(error){ | ||
_puts('ERROR: '.bold.red+(error.message ? error.message.bold : '')+'\n'+error.stack+')'); | ||
} | ||
} | ||
function filterTestFunctions(el){ | ||
return el.substring(0,4) == 'test'; | ||
} | ||
if(ctr>=len){ | ||
puts('* All tests have called back.'.grey); | ||
puts('Ran '+String(len).bold+' tests ' + ( fail ? 'with ' + String(fail).bold.red + ' fail.' : 'without any error.') ); | ||
callback(undefined, { 'len':len, 'fail':fail }); | ||
} | ||
} | ||
function genFinish(testsuite){ | ||
return function finish(){ | ||
var fail = testsuite.fail, | ||
len = testsuite.len; | ||
function puts(){ | ||
if(options.silent) return; | ||
_puts(title + Array.prototype.join.call(arguments, ', ')); | ||
} | ||
testsuite.log('* Done.'.grey); | ||
testsuite.log('Ran '+String(len).bold+' tests ' + ( fail ? 'with ' + String(fail).bold.red + ' fail.' : 'without any error.') ); | ||
testsuite.callback(undefined, { 'len':len, 'fail':fail }); | ||
}; | ||
} | ||
function callTestFn(test, args, callback){ | ||
puts('Running '+test.name+' ...'); | ||
function genTestCallback(testsuite, test, callback){ | ||
return function testCallback(error){ | ||
(error ? fail : ok)(test, testsuite, error); | ||
callback(); | ||
}; | ||
} | ||
test.startTS = (new Date).getTime(); | ||
function genTestCaller(testsuite, test){ | ||
function run(args){ | ||
testsuite.log('Running '+test.name+' ...'); | ||
var cb = args[ args.length - 1 ]; | ||
test.startTS = +(new Date); | ||
try { | ||
test.apply(undefined,args); | ||
test.apply(undefined, args); | ||
} catch(error) { | ||
cb(error); | ||
} | ||
} | ||
function callInit(callback){ | ||
if(typeof testsuite.options.module.init != 'function'){ | ||
callback(); | ||
} catch(error){ | ||
callback(error); | ||
return; | ||
} | ||
testsuite.options.module.init(testsuite.options, callback); | ||
} | ||
(function iter(i, error){ | ||
return function callTest(callback){ | ||
if(error){ | ||
return mark(tests[i-1], error); | ||
} | ||
var cb = genTestCallback(testsuite, test, function(error){ | ||
callback && callback(error); | ||
}); | ||
if(i>=len){ | ||
puts('* All tests has been fired.'.grey); | ||
if(tests.length==0) callback(undefined, { 'len':0, 'fail':0 }); | ||
var args = [cb]; | ||
callInit(function(error/* parameters to pass tests */){ | ||
if (error) { | ||
return callback(error); | ||
} | ||
var mergeArgs = [0, 0]; | ||
Array.prototype.push.apply(mergeArgs, Array.prototype.slice.call(arguments, 1)); | ||
args.splice.apply(args, mergeArgs); | ||
run(args); | ||
}); | ||
} | ||
} | ||
function genPrintTestResult(testsuite){ | ||
var line = 0; | ||
return function printTestResult(test, caption){ | ||
if(test.line) { | ||
testsuite.log('WARN '.red.bold + test.name.bold.yellow + ' has run callback more than once.' ); | ||
return; | ||
} | ||
var test = tests[i], | ||
marker = mark.bind(undefined,test), | ||
testCallback; | ||
var duration = (+(new Date) - test.startTS)/1000; | ||
function next(error){ | ||
if(!error && options.ordered){ | ||
return; | ||
} | ||
test.line = ++line; | ||
iter(i+1, error); | ||
} | ||
testsuite.log(caption.bold | ||
+ ' ' | ||
+ test.name.bold.yellow | ||
+ ' ('.grey | ||
+ (test.line + '/' + testsuite.len).bold | ||
+ ', '.grey | ||
+ ( duration + 's').white.bold | ||
+ ') '.grey); | ||
if(options.ordered){ | ||
testCallback = function(error){ | ||
marker.apply(null, arguments); | ||
iter(i+1, error); | ||
} | ||
} else { | ||
testCallback = marker; | ||
if(test.line == testsuite.len){ | ||
testsuite.finish(); | ||
} | ||
}; | ||
}; | ||
var args = [testCallback]; | ||
function kick(options, callback){ | ||
typeof module.init != 'function' ? callTestFn(test, args, next) : module.init(options, function(error/* args to pass test functions */){ | ||
if(error) { | ||
return callback(error); | ||
var testsuite = { | ||
'options': options | ||
}; | ||
testsuite.log = logger(testsuite); | ||
testsuite.title = options.name ? (options.name).bold+' | '.grey : ''; | ||
testsuite.printTestResult = genPrintTestResult(testsuite); | ||
testsuite.finish = genFinish(testsuite); | ||
testsuite.callback = callback; | ||
testsuite.tests = Object.keys(options.module).filter(filterTestFunctions).map(function(el){ return options.module[el]; }); | ||
testsuite.len = testsuite.tests.length; | ||
testsuite.fail = 0; | ||
(function iter(i){ | ||
if(i >= testsuite.len){ | ||
testsuite.log('* All tests has been fired.'.grey); | ||
if(testsuite.tests.length == 0) { | ||
testsuite.finish(); | ||
} | ||
var mergeArgs = [0, 0]; | ||
Array.prototype.push.apply(mergeArgs, Array.prototype.slice.call(arguments, 1)); | ||
args.splice.apply(args, mergeArgs); | ||
return; | ||
} | ||
callTestFn(test, args, next); | ||
}); | ||
var next = iter.bind(undefined, i+1), | ||
test = testsuite.tests[i], | ||
call = genTestCaller(testsuite, test); | ||
if(options.ordered){ | ||
call(next); | ||
} else { | ||
call(); | ||
next(); | ||
} | ||
})(0); | ||
} | ||
module.exports = kick; |
{ | ||
"name":"highkick", | ||
"version":"1.3.3", | ||
"version":"1.3.4", | ||
"description":"Asynchronous, no-style, super simple testing tool.", | ||
@@ -5,0 +5,0 @@ "author":"Azer Koculu <azer@kodfabrik.com>", |
@@ -1,2 +0,2 @@ | ||
module.exports.testError = function fail(callback){ | ||
module.exports.testError = function testError(callback){ | ||
callback(new Error('error')); | ||
@@ -3,0 +3,0 @@ } |
@@ -6,6 +6,24 @@ var assert = require('assert'), | ||
function cleanRequireCache(){ | ||
var filename; | ||
for(filename in require.cache){ | ||
delete require.cache[filename]; | ||
} | ||
} | ||
function init(options, callback){ | ||
cleanRequireCache(); | ||
callback(null, 3.14); | ||
} | ||
function testSimple(pi, callback){ | ||
callback(); | ||
} | ||
function testSimpleAsync(pi, callback){ | ||
setTimeout(function(){ | ||
callback(); | ||
}, 100); | ||
} | ||
function testFail(pi, callback){ | ||
@@ -38,4 +56,8 @@ highkick({ module:require('./fail'), 'silent':true, 'name':'fail' }, function(error, result){ | ||
setTimeout(function(){ | ||
assert.equal(++counter, 6); | ||
callback(); | ||
try { | ||
assert.equal(++counter, 6); | ||
callback(); | ||
} catch(err){ | ||
callback(err); | ||
} | ||
}, 50); | ||
@@ -61,3 +83,3 @@ } | ||
assert.equal(++counter, 5); | ||
highkick({ module:require('./ordered'), 'silent':false, 'ordered':true, 'name':'ordered' },function(error,result){ | ||
highkick({ module:require('./ordered'), 'silent':true, 'ordered':true, 'name':'ordered' },function(error,result){ | ||
!error && result.len == 0 && (error = new Error('Missing test functions.')); | ||
@@ -70,4 +92,6 @@ if(error) return callback(error); | ||
module.exports = { | ||
'init':init, | ||
'testAsync1':testAsync1, | ||
'init': init, | ||
'testSimple': testSimple, | ||
'testSimpleAsync': testSimpleAsync, | ||
'testAsync1': testAsync1, | ||
'testAsync2':testAsync2, | ||
@@ -74,0 +98,0 @@ 'testSync': testSync, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
264
0
11216
9