Permalink
Browse files

teardown support

  • Loading branch information...
5long committed Nov 5, 2010
1 parent e92082c commit b1fc8951b90363ffb31a696f37fd79588e02da77
Showing with 63 additions and 11 deletions.
  1. +4 −0 doc/api.mkd
  2. +8 −0 example/all_in_one.js
  3. +4 −0 src/suite_monad.js
  4. +28 −10 src/test_suite.js
  5. +6 −0 src/util.js
  6. +13 −1 test/test_suite.js
View
@@ -88,6 +88,10 @@ Example:
})
})
+### suiteMonad.teardown(action)
+
+Needless to say.
+
### suiteMonad.test(desc, action)
Add a test to the suite. Tests always run after all the setups finish.
View
@@ -7,6 +7,10 @@ reut.suite("A suite to contain 'em all")
fixture.answer = 42
done()
})
+.teardown(function(fixture, done) {
+ delete fixture.answer
+ done()
+})
.test("Native assert methods", function(test, fixture) {
test.ok(!test.fail, "Not provided")
test.ok(!test.ifError, "Others are ok to use")
@@ -43,6 +47,10 @@ reut.suite("A suite to contain 'em all")
done()
})
})
+.teardown(function(fixture, done) {
+ delete fixture.stat
+ done()
+})
.test("Something async", function(test, fixture) {
var boy = new EE()
test.instanceOf(fixture.stat, fs.Stats)
View
@@ -14,6 +14,10 @@ util.merge(SuiteMonad.prototype, {
this._suite.add(new Test(desc, action))
return this
}
+, teardown: function(fn) {
+ this._suite.addTeardown(fn)
+ return this
+ }
})
module.exports = SuiteMonad
View
@@ -5,6 +5,7 @@ var util = require('./util')
function TestSuite(desc) {
this.desc = desc
this._setupQueue = []
+ this._teardownQueue = []
this._tests = []
}
util.inherits(TestSuite, EventEmitter)
@@ -16,30 +17,47 @@ util.merge(TestSuite.prototype, {
cb = conf
conf = {fixture: {}}
}
- this._doSetup(conf.fixture, function(err) {
- if (err) throw err
- this.emit("start")
-
- async.map(this._tests, function(t) {
- thisSuite.emit("yield", t)
- t.run(conf, this)
- }, function(err, results) {
+ async.chain(
+ function setup() {
+ thisSuite._doSetup(conf.fixture, this)
+ }
+ , function test(err) {
+ if (err) throw err
+ thisSuite.emit("start")
+ async.map(thisSuite._tests, function(t) {
+ thisSuite.emit("yield", t)
+ t.run(conf, this)
+ }, this)
+ }
+ , function teardown(err, results) {
+ if (err) return this(err, [])
+ thisSuite._doTeardown(conf.fixture, this)
+ }
+ , function end(err, dummy, results) {
thisSuite.emit("end")
cb(err, results)
- })
- }.bind(this))
+ }
+ )
}
, _doSetup: function(fixture, cb) {
async.map(this._setupQueue, function(fn) {
fn(fixture, this)
}, cb)
}
+, _doTeardown: function(fixture, cb) {
+ async.map(this._teardownQueue, function(fn) {
+ fn(fixture, this)
+ }, cb)
+ }
, add: function(test) {
this._tests.push(test)
}
, addSetup: function(fn) {
this._setupQueue.push(fn)
}
+, addTeardown: function(fn) {
+ this._teardownQueue.push(fn)
+ }
, reportTo: function(reporter) {
reporter.watch(this)
}
View
@@ -45,6 +45,12 @@ util.async = {
var array = array.slice()
defer(mapIter, [array, action, [], cb])
}
+, chain: function() {
+ var actions = makeArray(arguments)
+ process.nextTick(function() {
+ chainIter(actions, [])
+ })
+ }
}
function chainIter(actions, initial) {
View
@@ -2,7 +2,7 @@ var reut = require('../src')
, TestSuite = reut.TestSuite
, Test = reut.Test
, assert = require('assert')
- , remainingCallbacks = 5
+ , remainingCallbacks = 7
, testDesc = "a test"
, spyReporter = {
watch: function() {
@@ -28,6 +28,18 @@ theSuite.addSetup(function(fixture, done) {
done()
})
+theSuite.addTeardown(function(fixture, done) {
+ remainingCallbacks--
+ delete fixture.foo
+ done()
+})
+
+theSuite.addTeardown(function(fixture, done) {
+ remainingCallbacks--
+ assert.ok(!fixture.foo)
+ done()
+})
+
theSuite.on("start", function() {
remainingCallbacks--
})

0 comments on commit b1fc895

Please sign in to comment.