Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Smelled bad, so refactor

  • Loading branch information...
commit b4165788dc7a4259f130f501e0bebff63675d3ed 1 parent a41779e
@5long authored
Showing with 35 additions and 43 deletions.
  1. +11 −17 src/suite_monad.js
  2. +19 −26 src/test_suite.js
  3. +5 −0 src/util.js
View
28 src/suite_monad.js
@@ -5,27 +5,21 @@ function SuiteMonad(suite) {
this._suite = suite
}
util.merge(SuiteMonad.prototype, {
- setup: function(fn) {
- this._suite.addSetup(fn)
- return this
- }
-, test: function(desc, action) {
- if (arguments.length < 2) throw TypeError("Wrong number of arguments")
+ test: function(desc, action) {
this._suite.add(new Test(desc, action))
return this
}
-, teardown: function(fn) {
- this._suite.addTeardown(fn)
- return this
- }
-, startup: function(fn) {
- this._suite.addStartup(fn)
- return this
- }
-, shutdown: function(fn) {
- this._suite.addShutdown(fn)
+})
+
+"setup teardown startup shutdown".split(" ")
+.forEach(defForward, SuiteMonad.prototype)
+
+function defForward(old) {
+ var neo = "add" + util.ucFirst(old)
+ this[old] = function(fn) {
+ this._suite[neo](fn)
return this
}
-})
+}
module.exports = SuiteMonad
View
45 src/test_suite.js
@@ -40,26 +40,6 @@ util.merge(TestSuite.prototype, {
}
)
}
-, _doSetup: function(fixture, cb) {
- async.paraMap(this._setupQueue, function(fn) {
- fn(fixture, this)
- }, cb)
- }
-, _doTeardown: function(fixture, cb) {
- async.paraMap(this._teardownQueue, function(fn) {
- fn(fixture, this)
- }, cb)
- }
-, _doStartup: function(fixture, cb) {
- async.paraMap(this._startupQueue, function(fn) {
- fn(fixture, this)
- }, cb)
- }
-, _doShutdown: function(fixture, cb) {
- async.paraMap(this._startupQueue, function(fn) {
- fn(fixture, this)
- }, cb)
- }
, _runTest: function(t, conf, cb) {
var thisSuite = this
async.chain(
@@ -82,15 +62,28 @@ util.merge(TestSuite.prototype, {
, 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)
}
})
+"setup teardown startup shutdown".split(" ")
+.forEach(boundAsyncQueue, TestSuite.prototype)
+
+function boundAsyncQueue(key) {
+ var ucFirst = util.ucFirst(key)
+ , keyExecute = "_do" + ucFirst
+ , keyQueue = "_" + key + "Queue"
+ , keyAdd = "add" + ucFirst
+
+ this[keyExecute] = function(fixture, cb) {
+ async.paraMap(this[keyQueue], function(fn) {
+ fn(fixture, this)
+ }, cb)
+ }
+ this[keyAdd] = function(fn) {
+ this[keyQueue].push(fn)
+ }
+}
+
module.exports = TestSuite
View
5 src/util.js
@@ -25,6 +25,11 @@ var util = module.exports = {
Object.defineProperty(this, prop, pd)
}, dest)
}
+, ucFirst: function(str) {
+ return str.replace(/^[a-z]/, function(c) {
+ return c.toUpperCase()
+ })
+ }
}
util.async = {
Please sign in to comment.
Something went wrong with that request. Please try again.