Skip to content
Browse files

Added example test and updated label output

  • Loading branch information...
1 parent 213c485 commit 8c6bb1689b4bfefbf7a881a3b86258ea18c80f97 @lukebayes committed Feb 15, 2011
View
11 example/runner.js
@@ -0,0 +1,11 @@
+
+require.paths.unshift('../src');
+
+require.paths.unshift('src');
+require.paths.unshift('test');
+
+var Runner = require('node_should').Runner;
+
+var runner = new Runner();
+runner.run();
+
View
15 example/src/some_class.js
@@ -0,0 +1,15 @@
+
+var SomeClass = function() {
+ this.isDone = false;
+ this.name = 'default';
+}
+
+SomeClass.prototype.doSomeLongAsyncTask = function(duration, callback) {
+ var self = this;
+ setTimeout(function() {
+ self.isDone = true;
+ callback();
+ }, duration);
+}
+
+exports.SomeClass = SomeClass;
View
32 example/test/unit/some_class_test.js
@@ -0,0 +1,32 @@
+
+var SomeClass = require('some_class').SomeClass;
+
+context('SomeClass', function() {
+
+ setup(function() {
+ this.instance1 = new SomeClass();
+ });
+
+ should('be instantiable', function() {
+ assert.ok(this.instance1 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');
+ }));
+ });
+
+ context('with a custom name', function() {
+
+ setup(function() {
+ this.instance = new SomeClass();
+ this.instance.name = 'foo';
+ });
+
+ should('accept setter', function() {
+ assert.equal('foo', this.instance.name);
+ });
+ });
+});
View
1 src/node_should.js
@@ -3,6 +3,7 @@ exports.Context = require('node_should/context').Context;
exports.Printer = require('node_should/printer').Printer;
exports.Runner = require('node_should/runner').Runner;
exports.Stack = require('node_should/stack').Stack;
+
exports.ArrayIterator = require('node_should/array_iterator').ArrayIterator;
exports.CompositeIterator = require('node_should/composite_iterator').CompositeIterator;
exports.Composite = require('node_should/composite').Composite;
View
11 src/node_should/context.js
@@ -37,6 +37,9 @@ Context.prototype.addTeardownHandler = function(handler) {
*/
Context.prototype.addTestHandler = function(label, handler) {
this._cleanHandlerArguments(arguments);
+ if (this.getLabel() != '') {
+ label = [this.getLabel(), 'should', label].join(' ');
+ }
this._testHandlers.push({ handler: handler, label: label });
// TODO(lukebayes) Probably shouldn't set label here, it's doing work that
// isn't necessary, and will only be used in cases of failure?
@@ -74,17 +77,13 @@ Context.prototype.addTestHandler = function(label, handler) {
* # b: A
*/
Context.prototype._cleanHandlerArguments = function(args) {
- var label = this.getLabel();
var handler = null;
if (args.length == 0) throw 'The handler provided to Context.addTestHandler (or addSetupHandler, addTeardownHandler) must not be null';
if (args.length == 1) {
if (typeof(args[0]) != 'function') throw 'A handler must be sent to the add___ method'
args[1] = args[0];
- args[0] = label;
- } else if (args.length == 2) {
- args[0] = [label, 'should', args[0]].join(' ');
- return;
+ args[0] = '';
}
}
@@ -267,7 +266,7 @@ Context.prototype.getLabel = function() {
if (this.parent) {
var parentLabel = this.parent.getLabel();
if (parentLabel != '') {
- labelParts.push(parentLabel);
+ labelParts.unshift(parentLabel);
}
}
if (this._label != '') {
View
6 src/node_should/printer.js
@@ -169,9 +169,9 @@ Printer.prototype._printDuration = function() {
Printer.prototype._printFailureDetails = function() {
var self = this;
- this.failed.forEach(function(failure) {
- if (failure) {
- self._printFailure(failure.stack);
+ this.failed.forEach(function(test) {
+ if (test && test.failure) {
+ self._printFailure(test.failure.stack);
self._printFailure('\n');
self._printFailure('\n');
}
View
2 test/unit/printer_test.js
@@ -83,7 +83,7 @@ var FakePrinter = require('fake_printer').FakePrinter;
try {
assert.fail(null, 'fake actual value', 'fake expected value', '!=', 'foo');
} catch (failure) {
- p._testFailureHandler(failure);
+ p._testFailureHandler({failure: failure});
}
p.finish();

0 comments on commit 8c6bb16

Please sign in to comment.
Something went wrong with that request. Please try again.