Browse files

initial

  • Loading branch information...
0 parents commit c1c917580eba41f150dbe9cd779d97b64312fb72 @dominictarr committed Jul 29, 2011
Showing with 129 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +102 −0 asynct.js
  3. +7 −0 cmd.js
  4. +16 −0 package.json
  5. 0 readme.markdown
  6. +1 −0 x.report
3 .gitignore
@@ -0,0 +1,3 @@
+node_modules
+node_modules/*
+npm_debug.log
102 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
+}
7 cmd.js
@@ -0,0 +1,7 @@
+#! /usr/bin/env node
+
+var cmd = require('test-cmd')
+ , asynct = require('./asynct')
+
+if(!module.parent)
+ cmd.go(asynct)
16 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 <dominic.tarr@gmail.com> (http://bit.ly/dominictarr)"
+, "scripts": { "test": "meta-test test/*.js" }
+, "bin": { "asynct": "./cmd.js" }
+}
0 readme.markdown
No changes.
1 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"}]}]}

0 comments on commit c1c9175

Please sign in to comment.