Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Callbacks tests have this passed through and add test context as extr…

…a argument
  • Loading branch information...
commit 1db40d489bedc649524a84781bfa72403aa1e18e 1 parent 349437b
@leei authored
Showing with 28 additions and 4 deletions.
  1. +2 −3 lib/vows.js
  2. +7 −1 lib/vows/context.js
  3. +19 −0 test/vows-test.js
View
5 lib/vows.js
@@ -83,8 +83,6 @@ function addVow(vow) {
});
function runTest(args) {
- var topic, status;
-
if (vow.callback instanceof String) {
return output('pending');
}
@@ -92,7 +90,8 @@ 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);
+ 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/)) {
View
8 lib/vows/context.js
@@ -1,4 +1,4 @@
-
+var sys = require('sys');
this.Context = function (vow, ctx, env) {
var that = this;
@@ -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.
View
19 test/vows-test.js
@@ -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,
".": {
Please sign in to comment.
Something went wrong with that request. Please try again.