Permalink
Browse files

Fixed sort order of setup routines in composite structure

  • Loading branch information...
1 parent 8c6bb16 commit 8ba73c3a61ec4ff2d1685aa67944796ace7f86c4 @lukebayes committed Feb 16, 2011
Showing with 61 additions and 37 deletions.
  1. +6 −6 example/test/unit/some_class_test.js
  2. +47 −31 src/node_should/context.js
  3. +8 −0 test/unit/context_test.js
@@ -4,28 +4,28 @@ var SomeClass = require('some_class').SomeClass;
context('SomeClass', function() {
setup(function() {
- this.instance1 = new SomeClass();
+ this.instance = new SomeClass();
});
should('be instantiable', function() {
- assert.ok(this.instance1 instanceof SomeClass);
+ assert.ok(this.instance instanceof SomeClass);
});
should('do some long task', function() {
var self = this;
- this.instance1.doSomeLongAsyncTask(10, this.async(function() {
- assert.ok(self.instance1.isDone, 'long async task completed');
+ this.instance.doSomeLongAsyncTask(10, this.async(function() {
+ assert.ok(self.instance.isDone, 'long async task completed');
}));
});
context('with a custom name', function() {
setup(function() {
- this.instance = new SomeClass();
+ assert.ok(this.instance);
this.instance.name = 'foo';
});
- should('accept setter', function() {
+ should('accept parent setter', function() {
assert.equal('foo', this.instance.name);
});
});
View
@@ -101,27 +101,6 @@ Context.prototype._executeAllTests = function(testIterator) {
}
}
-Context.prototype._getTestExecutionOptions = function(iterator) {
- var self = this;
- var options = {
- asyncHandlers : 0,
- iterator : iterator,
- scope : {
- async : function(callback) {
- options.asyncHandlers++;
- return function() {
- options.asyncHandlers--;
- self._callHandler(callback, options.scope);
- if (options.asyncHandlers == 0) {
- self._executeNextSetupOrTestOrTeardown(options);
- }
- }
- }
- }
- }
- return options;
-}
-
Context.prototype._createTestHandlerIterator = function(completeHandler) {
var testHandlers = this._testHandlers;
@@ -156,6 +135,30 @@ Context.prototype._createTestHandlerIterator = function(completeHandler) {
return new ArrayIterator(testHandlerList);
}
+Context.prototype._getTestExecutionOptions = function(iterator) {
+ var self = this;
+ var options = {};
+
+ var asyncHandler = function(callback) {
+ options.asyncHandlers++;
+ return function() {
+ options.asyncHandlers--;
+ self._callHandler(callback, options);
+ if (options.asyncHandlers == 0) {
+ self._executeNextSetupOrTestOrTeardown(options);
+ }
+ }
+ }
+
+ options.asyncHandlers = 0;
+ options.iterator = iterator;
+ options.scope = {
+ async : asyncHandler
+ }
+
+ return options;
+}
+
Context.prototype._getTestStartedHandler = function(testHandlerData) {
return function() {
testHandlerData.startedAt = new Date();
@@ -178,15 +181,21 @@ Context.prototype._onSuccess = function(testHandlerData) {
this.emit('success', testHandlerData);
}
-Context.prototype._onFailure = function(failure) {
- this.emit('failure', failure);
+Context.prototype._onFailure = function(testHandlerData) {
+ if (this.listeners('failure').length == 0) {
+ throw testHandlerData.failure;
+ }
+ this.emit('failure', testHandlerData);
}
-Context.prototype._onError = function(error) {
- this.emit('error', error);
+Context.prototype._onError = function(testHandlerData) {
+ if (this.listeners('error').length == 0) {
+ throw testHandlerData.error;
+ }
+ this.emit('error', testHandlerData);
}
-Context.prototype._callHandler = function(handlerData, scope) {
+Context.prototype._callHandler = function(handlerData, options) {
var handler = null;
var failureLabel = null;
if (typeof(handlerData) == 'function') {
@@ -195,21 +204,24 @@ Context.prototype._callHandler = function(handlerData, scope) {
handler = handlerData.handler;
failureLabel = handlerData.label;
}
+
try {
- handler.call(scope);
+ handler.call(options.scope);
} catch (e) {
if (e instanceof AssertionError) {
if (failureLabel) {
e.message = failureLabel + '\n' + e.message;
}
handlerData.failure = e;
this._onFailure(handlerData);
- } else {
+ } else if (e) {
if (failureLabel) {
e = new e.constructor(failureLabel + '\n' + e.toString());
}
- handlerData.error = e;
+ handlerData.error = e
this._onError(handlerData);
+ } else {
+ throw 'Caught handler exception with no exception? (' + e +')';
}
}
}
@@ -218,18 +230,20 @@ Context.prototype._executeNextSetupOrTestOrTeardown = function(options) {
var itr = options.iterator;
if (itr.hasNext()) {
- this._callHandler(itr.next(), options.scope);
+ this._callHandler(itr.next(), options);
if(options.asyncHandlers == 0) {
this._executeNextSetupOrTestOrTeardown(options);
}
}
}
Context.prototype.getAllSetupHandlers = function() {
- var handlers = this._setupHandlers.slice(0);
+ var handlers = []
if (this.parent) {
handlers = handlers.concat(this.parent.getAllSetupHandlers());
}
+ handlers = handlers.concat(this._setupHandlers.slice(0));
+
return handlers;
}
@@ -246,6 +260,8 @@ Context.prototype._getSetupHandlersForTest = function(label) {
return this.getAllSetupHandlers()
.slice(0)
.map(function(handlerData) {
+ // NOTE: This add .label to shared handlerData
+ // by reference - (slice is shallow clone).
handlerData.label = label;
return handlerData;
});
@@ -318,19 +318,22 @@ require('../common');
var parent = new Context('SomeParent');
parent.addSetupHandler(function() {
executed.push('parentsetup');
+ this.param = {};
});
parent.addTeardownHandler(function() {
executed.push('parentteardwon');
});
var child = new Context('with a child');
child.addSetupHandler(function() {
executed.push('childsetup');
+ this.param.name = 'childvalue';
});
child.addTeardownHandler(function() {
executed.push('childteardown');
});
child.addTestHandler('do something', function() {
executed.push('testmethod1');
+ assert.equal(this.param.name, 'childvalue');
});
child.addTestHandler('do something else', function() {
executed.push('testmethod2');
@@ -339,4 +342,9 @@ require('../common');
parent.addChild(child);
child.execute();
assert.equal(10, executed.length);
+ assert.equal('parentsetup', executed[0]);
+})();
+
+(function contextShouldStopIfASetupFails() {
+ //assert.fail('todo');
})();

0 comments on commit 8ba73c3

Please sign in to comment.