Skip to content

Commit

Permalink
support for this.callback.call({}, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Feb 21, 2011
1 parent 7874f54 commit 7b20446
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/vows.js
Expand Up @@ -70,19 +70,19 @@ function addVow(vow) {
if (vow.callback.length >= 2 && batch.suite.options.error) {
args.unshift(null);
}
runTest(args);
runTest(args, this.ctx);
vows.tryEnd(batch);

}).on("error", function (err) {
if (vow.callback.length >= 2 || !batch.suite.options.error) {
runTest([err]);
runTest([err], this.ctx);
} else {
output('errored', { type: 'promise', error: err.stack || err.message || JSON.stringify(err) });
}
vows.tryEnd(batch);
});

function runTest(args) {
function runTest(args, ctx) {
var topic, status;

if (vow.callback instanceof String) {
Expand All @@ -92,7 +92,7 @@ function addVow(vow) {
// Run the test, and try to catch `AssertionError`s and other exceptions;
// increment counters accordingly.
try {
vow.callback.apply(vow.binding || null, args);
vow.callback.apply(ctx || vow.binding || null, args);
output('honored');
} catch (e) {
if (e.name && e.name.match(/AssertionError/)) {
Expand Down
8 changes: 6 additions & 2 deletions lib/vows/context.js
Expand Up @@ -9,28 +9,32 @@ this.Context = function (vow, ctx, env) {
this.env.context = this;

this.env.callback = function (/* arguments */) {
var ctx = this;
var args = Array.prototype.slice.call(arguments);

var emit = (function (args) {
//
// Convert callback-style results into events.
//
if (vow.batch.suite.options.error) {
return function () {
var e = args.shift();
that.emitter.ctx = ctx;
// We handle a special case, where the first argument is a
// boolean, in which case we treat it as a result, and not
// an error. This is useful for `path.exists` and other
// functions like it, which only pass a single boolean
// parameter instead of the more common (error, result) pair.
if (typeof(e) === 'boolean' && args.length === 0) {
that.emitter.emit('success', e);
that.emitter.emit.call(that.emitter, 'success', e);
} else {
if (e) { that.emitter.emit('error', e) }
if (e) { that.emitter.emit.call(that.emitter, 'error', e) }
else { that.emitter.emit.apply(that.emitter, ['success'].concat(args)) }
}
};
} else {
return function () {
that.emitter.ctx = ctx;
that.emitter.emit.apply(that.emitter, ['success'].concat(args));
};
}
Expand Down
8 changes: 8 additions & 0 deletions test/vows-test.js
Expand Up @@ -244,6 +244,14 @@ vows.describe("Vows").addBatch({
assert.equal(res, 'hello');
}
},
"using this.callback with a user context": {
topic: function () {
this.callback.call({ boo: true }, null, 'hello');
},
"should work the same as returning a value": function (res) {
assert.isTrue(this.boo);
}
},
"with multiple arguments": {
topic: function () {
this.callback(null, 1, 2, 3);
Expand Down

0 comments on commit 7b20446

Please sign in to comment.