Skip to content

Commit

Permalink
Added example test and updated label output
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebayes committed Feb 15, 2011
1 parent 213c485 commit 8c6bb16
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 10 deletions.
11 changes: 11 additions & 0 deletions 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();

15 changes: 15 additions & 0 deletions 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;
32 changes: 32 additions & 0 deletions 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);
});
});
});
1 change: 1 addition & 0 deletions src/node_should.js
Expand Up @@ -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;
11 changes: 5 additions & 6 deletions src/node_should/context.js
Expand Up @@ -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?
Expand Down Expand Up @@ -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] = '';
}
}

Expand Down Expand Up @@ -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 != '') {
Expand Down
6 changes: 3 additions & 3 deletions src/node_should/printer.js
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/printer_test.js
Expand Up @@ -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();

Expand Down

0 comments on commit 8c6bb16

Please sign in to comment.