Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation and testing of this passthrough and adding context as extra argument. #29

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/vows.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,15 @@ function addVow(vow) {
});

function runTest(args) {
var topic, status;

if (vow.callback instanceof String) {
return output('pending');
}

// Run the test, and try to catch `AssertionError`s and other exceptions;
// increment counters accordingly.
try {
vow.callback.apply(vow.binding || null, args);
var env = vow.binding;
vow.callback.apply((env && env.binding) || vow.binding || null, args);
output('honored');
} catch (e) {
if (e.name && e.name.match(/AssertionError/)) {
Expand Down
8 changes: 7 additions & 1 deletion lib/vows/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

var sys = require('sys');
this.Context = function (vow, ctx, env) {
var that = this;

Expand All @@ -9,7 +9,13 @@ this.Context = function (vow, ctx, env) {
this.env.context = this;

this.env.callback = function (/* arguments */) {
var target = this;
var args = Array.prototype.slice.call(arguments);
// Add context as extra argument
args.push(that);
// Attach a binding to the env...
env.binding = target;

var emit = (function (args) {
//
// Convert callback-style results into events.
Expand Down
19 changes: 19 additions & 0 deletions test/vows-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ vows.describe("Vows").addBatch({
}
}
},
"a callback-style return": {
topic: function() {
var target = {a: 1};
function f(callback) {
callback.call(target, null, 1);
}
f(this.callback);
},
'should pass on the user\'s callback arguments': function(err, status) {
assert.isNull(err);
assert.equal(status, 1);
},
'should set this to the target of the user\'s callback': function(err, status) {
assert.deepEqual(this, {a: 1});
},
'should pass test context as extra parameter': function(err, status, context) {
assert.equal(context.name, "a callback-style return");
}
},
"A nested context with no topics": {
topic: 45,
".": {
Expand Down