Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 4 commits into from

2 participants

@leei

The conversation we had regarding passing this through the callback and adding context as an extra argument are now implemented and tested.

User visible changes:

  • On this.callback triggered vows, the this variable is now bound to the target of the callback invocation in the code being tested. In other words, if the user code responds to the asynchronous call with callback.call(foo, err, status) then this will be bound to foo in the vow's function.
  • If the test code needs to examine the vow's context (e.g. to examine the topics array) then this context is available as an extra argument after all of those sent from the user code.
@indexzero
Owner

This seems reasonable. The purpose would be to allow this.context to be available inside of individual vow functions correct?

@indexzero
Owner

Closing for lack of follow-up.

@indexzero indexzero closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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,
".": {
Something went wrong with that request. Please try again.