Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Trying to integrated updated printer scheme

  • Loading branch information...
commit 8ce2a73825ab4beebaef265f2b719fe350fc2076 1 parent e6a55a0
@lukebayes authored
View
2  example/test/unit/some_class_test.js
@@ -5,7 +5,7 @@ var SomeClass = require('some_class').SomeClass;
// This is the outermost context in the file, but
// there is no reason a single file can't contain
// multiple contexts.
-context('SomeClass', function() {
+context('CustomClass', function() {
// This is an example setup method. Any number of
// setup methods can be declared and will be called
View
27 src/node_should/context.js
@@ -162,21 +162,24 @@ Context.prototype._createTestHandlerIterator = function(completeHandler) {
testHandlerList.push(new ArrayIterator(handlers));
});
- // Add a custom handler to trigger complete handler
- // after all tests have finished:
- handlers.push(function() {
- if (completeHandler) {
- // break out of the try..catch
- // that wraps test methods:
- setTimeout(function() {
- completeHandler();
- });
- }
- });
-
+ handlers.push(this._getContextCompleteHandler(completeHandler));
return new ArrayIterator(testHandlerList);
}
+Context.prototype._getContextCompleteHandler = function(externalCompleteHandler) {
+ var self = this;
+ return {
+ handler: function() {
+ self.emit('complete', self);
+ if (externalCompleteHandler && typeof(externalCompleteHandler) == "function") {
+ setTimeout(function() {
+ externalCompleteHandler(this);
+ });
+ }
+ }
+ }
+}
+
Context.prototype._getTestExecutionOptions = function(iterator) {
var self = this;
var options = {};
View
50 src/node_should/printer.js
@@ -2,6 +2,7 @@ var sys = require('sys');
var style = require('node_should/colored');
var Printer = function() {
+ this.finishTimeoutId = null;
this.colorized = true;
this.contexts = [];
this.errored = [];
@@ -23,23 +24,31 @@ var Printer = function() {
Printer.prototype.addContext = function(context) {
this.contexts.push(context);
var self = this;
- context.addListener('success', function(test) {
+
+ var successHandler = function(test) {
self._testSuccessHandler(test);
- });
- context.addListener('failure', function(test) {
+ };
+
+ var failureHandler = function(test) {
self._testFailureHandler(test);
- });
- context.addListener('error', function(test) {
+ };
+
+ var errorHandler = function(test) {
self._testErrorHandler(test);
- });
-}
+ };
-Printer.prototype.removeContext = function(context) {
- var index = this.contexts.indexOf(context);
- this.contexts.splice(index, 1);
- if (this.contexts.length == 0) {
- this.finish();
- }
+ var completeHandler = function(context) {
+ context.removeListener('success', successHandler);
+ context.removeListener('failure', failureHandler);
+ context.removeListener('error', errorHandler);
+ context.removeListener('complete', completeHandler);
+ self._contextCompleteHandler(context);
+ };
+
+ context.addListener('success', successHandler);
+ context.addListener('failure', failureHandler);
+ context.addListener('error', errorHandler);
+ context.addListener('complete', completeHandler);
}
Printer.prototype.start = function() {
@@ -68,6 +77,21 @@ Printer.prototype._testErrorHandler = function(error) {
this._addError(error);
}
+Printer.prototype._contextCompleteHandler = function(context) {
+ var index = this.contexts.indexOf(context);
+ this.contexts.splice(index, 1);
+
+ var self = this;
+ // Give the application one execution frame to add another
+ // context before auto-finishing.
+ clearTimeout(this.finishTimeoutId);
+ this.finishTimeoutId = setTimeout(function() {
+ if (self.contexts.length == 0) {
+ self.finish();
+ }
+ }, 0);
+}
+
Printer.prototype.testIgnoreHandler = function(test, message) {
this._addIgnore(test, message);
}
View
13 src/node_should/runner.js
@@ -71,9 +71,7 @@ Runner.prototype._createScope = function(file, printers, completeHandler) {
context = c;
}
c.addExecutionHandler.apply(c, arguments);
- c.execute(function() {
- self._removeContextFromPrinters(printers, c, completeHandler);
- });
+ c.execute();
if (c.parent) {
context = c.parent;
}
@@ -118,14 +116,5 @@ Runner.prototype._addContextToPrinters = function(printers, context) {
});
}
-Runner.prototype._removeContextFromPrinters = function(printers, context, completeHandler) {
- printers.forEach(function(printer) {
- printer.removeContext(context);
- if (completeHandler && printer.finished) {
- completeHandler.call(this);
- }
- });
-}
-
exports.Runner = Runner;
View
5 test/fixtures/first.js
@@ -1,5 +1,5 @@
-context('SomeClass', function() {
+context('AnotherClass', function() {
setup(function() {
this.name = 'abcd';
@@ -12,10 +12,7 @@ context('SomeClass', function() {
should('throw exception', function() {
assert.throws(function() {
throw new Error('hello world');
- // This passes:
});
- // This fails:
- //}, /hello/);
});
});
View
32 test/unit/context_test.js
@@ -27,27 +27,27 @@ context('A new Context', function() {
});
should('build from composite', function() {
- var root = new Context('SomeClass');
+ var root = new Context('SomeClass 6');
var child1 = new Context('with child');
root.addChild(child1);
- assert.equal('SomeClass with child', child1.getLabel());
+ assert.equal('SomeClass 6 with child', child1.getLabel());
});
should('build from composite, ignoring null child labels', function() {
- var root = new Context('SomeClass');
+ var root = new Context('SomeClass 8');
var child1 = new Context();
root.addChild(child1);
- assert.equal('SomeClass', child1.getLabel());
+ assert.equal('SomeClass 8', child1.getLabel());
});
should('build from composite, ignoring null root label', function() {
var root = new Context();
- var child1 = new Context('SomeClass');
+ var child1 = new Context('SomeClass 9');
root.addChild(child1);
- assert.equal('SomeClass', child1.getLabel());
+ assert.equal('SomeClass 9', child1.getLabel());
});
});
@@ -139,7 +139,7 @@ context('A new Context', function() {
should('run setup, then tests, then teardowns', function() {
var executed = [];
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 10');
c.addSetupHandler(function() {
executed.push('setup');
});
@@ -159,7 +159,7 @@ context('A new Context', function() {
});
should('run handlers in shared scope', function() {
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 11');
c.addSetupHandler(function() {
this.foo = 'bar';
});
@@ -171,7 +171,7 @@ context('A new Context', function() {
should('call setups for each test method', function() {
var executed = [];
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 12');
c.addSetupHandler(function() { executed.push('setup1'); });
c.addSetupHandler(function() { executed.push('setup2'); });
c.addTestHandler(function() { executed.push('test1') });
@@ -251,7 +251,7 @@ context('A new Context', function() {
should('capture runtime errors', function() {
var error = null;
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 13');
c.addListener('error', function(test) {
error = test.error;
});
@@ -259,7 +259,7 @@ context('A new Context', function() {
throw 'unknown application error';
});
c.execute();
- assert.match(/SomeClass should do something/, error.toString());
+ assert.match(/SomeClass 13 should do something/, error.toString());
assert.match(/unknown application error/, error.toString());
});
@@ -345,29 +345,29 @@ context('A new Context', function() {
context('failure', function() {
should('display helpful message', function() {
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 3');
c.addTestHandler('accept argument', function() {
assert.ok(false);
});
c.addListener('failure', function(test) {
- assert.match(/SomeClass should accept argument/, test.failure.message);
+ assert.match(/SomeClass 3 should accept argument/, test.failure.message);
});
c.execute();
});
should('create default label', function() {
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 4');
c.addTestHandler(function() {
assert.ok(false);
});
c.addListener('failure', function(test) {
- assert.match(/SomeClass/, test.failure.stack);
+ assert.match(/SomeClass 4/, test.failure.stack);
});
c.execute();
});
should('work with throws assertion failures', function() {
- var composite = new Context('SomeClass');
+ var composite = new Context('SomeClass 5');
composite.addTestHandler(function() {
assert.throws(function() {
throw 'hello world';
View
12 test/unit/printer_test.js
@@ -255,7 +255,7 @@ context('printer', function() {
});
should('accept failing context', function() {
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 1');
c.addTestHandler('should do something', function() {
assert.ok(false, 'expected failure!');
});
@@ -263,14 +263,14 @@ context('printer', function() {
var p = new FakePrinter();
p.start();
p.addContext(c);
- c.execute();
- p.finish();
- var message = p.out.message;
- assert.match(/Test Count: 1, OK: 0, Failures: 1, Errors: 0, Ignored: 0/, message);
+ c.execute(async(function() {
+ var message = p.out.message;
+ assert.match(/Test Count: 1, OK: 0, Failures: 1, Errors: 0, Ignored: 0/, message);
+ }));
});
should('accept passing context', function() {
- var c = new Context('SomeClass');
+ var c = new Context('SomeClass 2');
c.addTestHandler('should do something', function() {
assert.ok(true);
});
View
1  test/unit/runner_test.js
@@ -30,6 +30,5 @@ context('A new Runner', function() {
}, 20));
});
});
-
*/
});
Please sign in to comment.
Something went wrong with that request. Please try again.