Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Jul 29, 2011
0 parents commit c1c9175
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
node_modules/*
npm_debug.log
102 changes: 102 additions & 0 deletions 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 changes: 7 additions & 0 deletions 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 changes: 16 additions & 0 deletions 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" }
}
Empty file added readme.markdown
Empty file.
1 change: 1 addition & 0 deletions 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.