From c1c917580eba41f150dbe9cd779d97b64312fb72 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Fri, 29 Jul 2011 19:19:23 +1000 Subject: [PATCH] initial --- .gitignore | 3 ++ asynct.js | 102 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd.js | 7 ++++ package.json | 16 ++++++++ readme.markdown | 0 x.report | 1 + 6 files changed, 129 insertions(+) create mode 100644 .gitignore create mode 100644 asynct.js create mode 100644 cmd.js create mode 100644 package.json create mode 100644 readme.markdown create mode 100644 x.report diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13abef4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +node_modules/* +npm_debug.log diff --git a/asynct.js b/asynct.js new file mode 100644 index 0000000..af19729 --- /dev/null +++ b/asynct.js @@ -0,0 +1,102 @@ +//asynct.js + +//simple async test adapter + +var assert = require('assert') + , ctrl = require('ctrlflow') + , isSetup = /^__?setup$/ + , isTeardown = /^__?teardown$/ + , isSetupTeardown = /^__?(setup|teardown)$/ + +exports.run = run + +function run (tests,reporter,callback){ + var status = {} + var setup = [], teardown = [] + var names = Object.keys(tests).filter(function (name){ + if(isSetup(name)) + setup.push(name) + else if (isTeardown(name)) + teardown.push(name) + else return true + }) + + names = setup.concat(names).concat(teardown) + + var tests = names.map(function (name){ + var test = tests[name] + status[name] = 'not started' + return function (){ + var __next = this.next + , finishAlready = false + + if(!isSetupTeardown(name)) //don't report setup and teardown unless there is an error. + reporter.test(name) + + function next (){ + status[name] = 'finished' + if(finishAlready) + return reporter.test(name,new Error('test \'' + name + '\' called finish twice')) + finishAlready = true + __next() + } + + function error(err){ + reporter.test(name,err) + next() + } + + var tester = new Tester(name,next,function (err){reporter.test(name,err)}) + + function handle(err){ + var catcher = tester.catch || tester.uncaughtExceptionHandler + if('function' == typeof catcher){ + try{ + catcher(err) + } catch (err){ + error(err) + } + } else { + error(err) + } + } + + process.removeAllListeners('uncaughtException') + process.on('uncaughtException',handle) + + try{ + status[name] = 'started' + test.call(null,tester) + if(isTeardown(name)) + next()//teardown is sync! .. + //on second thoughts this is probably not a good idea. + //maybe, by hooking process.nextTick and the events you could have an + //event loop drain event, so that you know when the process is about to exit. + } catch (err) {handle(err)} + } + }) + + ctrl.seq(tests,function (err){throw err}).done(callback).go() + + return function (){ + process.removeAllListeners('uncaughtException') + + names.forEach(function (name){ + if(status[name] != 'finished') + reporter.test(name, "did not finish, state was: " + status[name]) + }) + } + +} + +Tester.prototype = assert + +function Tester (name,next,handler){ + this.done = next + this.finish = next + this.name = name + this.catch = null + this.failure = handler + this.error = handler + this.uncaughtExceptionHandler = null +} diff --git a/cmd.js b/cmd.js new file mode 100644 index 0000000..684bc57 --- /dev/null +++ b/cmd.js @@ -0,0 +1,7 @@ +#! /usr/bin/env node + +var cmd = require('test-cmd') + , asynct = require('./asynct') + +if(!module.parent) + cmd.go(asynct) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..4099935 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ "name": "asynct" +, "version": "1.0.0" +, "description": "" +, "homepage": "http://github.com/dominictarr/asynct" +, "repository": + { "type": "git" + , "url": "https://github.com/dominictarr/asynct.git" } +, "dependencies": { + "ctrlflow": ">=0.0.3" + , "test-cmd": "1" + } +, "main": "./asynct.js" +, "author": "Dominic Tarr (http://bit.ly/dominictarr)" +, "scripts": { "test": "meta-test test/*.js" } +, "bin": { "asynct": "./cmd.js" } +} \ No newline at end of file diff --git a/readme.markdown b/readme.markdown new file mode 100644 index 0000000..e69de29 diff --git a/x.report b/x.report new file mode 100644 index 0000000..4bea48c --- /dev/null +++ b/x.report @@ -0,0 +1 @@ +{"name":"/home/dominic/source/dev/asynct","failures":[],"status":"success","filename":"/home/dominic/source/dev/asynct","os":"linux","version":"v0.4.8","meta":{},"tests":[{"name":"../map-reduce/test/errors.asynct.js","failures":[],"status":"success","filename":"../map-reduce/test/errors.asynct.js","os":"linux","version":"v0.4.8","meta":{},"tests":[{"name":"emit.error","failures":[],"status":"success"},{"name":"throw Error","failures":[],"status":"success"},{"name":"throw Error async","failures":[],"status":"success"},{"name":"dont catch in callback","failures":[],"status":"success"}]},{"name":"../map-reduce/test/map-reduce.asynct.js","failures":[],"status":"success","filename":"../map-reduce/test/map-reduce.asynct.js","os":"linux","version":"v0.4.8","meta":{},"tests":[{"name":"map","failures":[],"status":"success"},{"name":"map default to identity","failures":[],"status":"success"},{"name":"map default to identity with initial set","failures":[],"status":"success"},{"name":"reduce defaults to collect","failures":[],"status":"success"},{"name":"reduce stop early","failures":[],"status":"success"},{"name":"max","failures":[],"status":"success"},{"name":"min","failures":[],"status":"success"}]}]} \ No newline at end of file