Skip to content

Commit

Permalink
evaluate everything within an 'environment', which is passed down
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed May 2, 2010
1 parent 531d4bf commit 1198d46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/vows.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function addVow(/* description & callback */) {
// Run the test, and try to catch `AssertionError`s and other exceptions;
// increment counters accordingly.
try {
vow.callback.apply(null, arguments);
vow.callback.apply(args[0].binding || null, arguments);
title += stylize(vow.description, 'green');
honored++;
} catch (e) {
Expand Down Expand Up @@ -211,16 +211,18 @@ vows.tell = function (topic, tests) {

this.promise = new(events.EventEmitter);

function Context(vow, ctx) {
function Context(vow, ctx, env) {
this.tests = vow.callback;
this.topics = (ctx.topics || []).slice(0);
this.env = env || {};
this.env.context = this;
this.name = (ctx.name ? ctx.name + ' ' : '') +
(vow.description || '');
}

// We run the tests asynchronously, for added flexibility
process.nextTick(function () {
var setup, vow;
var setup, vow, env;

if (typeof(topic) === 'string' && tests) {
if (!vows.options.brief) {
Expand Down Expand Up @@ -266,7 +268,7 @@ vows.tell = function (topic, tests) {
var ctxAdded = false;
if (typeof(ctx.tests["setup"]) === 'function') {
// Run the setup, passing the previous context topics
setup = ctx.tests.setup.apply(ctx, ctx.topics);
setup = ctx.tests.setup.apply(ctx.env, ctx.topics);

// If the setup doesn't return an event emitter (such as a promise),
// we create it ourselves, and emit the value on the next tick.
Expand Down Expand Up @@ -294,8 +296,10 @@ vows.tell = function (topic, tests) {
vow = Object.create({
callback: ctx.tests[item],
context: ctx.name,
description: item
description: item,
binding: ctx.env
});
env = Object.create(ctx.env);

// If we encounter a function, add it to the callbacks
// of the `setup` function, so it'll get called once the
Expand All @@ -311,11 +315,11 @@ vows.tell = function (topic, tests) {
if (setup) {
setup.addListener("success", function (vow, ctx) {
return function (val) {
return run(new(Context)(vow, ctx));
return run(new(Context)(vow, ctx, env));
};
}(vow, ctx));
} else {
run(new(Context)(vow, ctx));
run(new(Context)(vow, ctx, env));
}
}
});
Expand Down
9 changes: 8 additions & 1 deletion test/vows-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ vows.tell("Vows", {
"testing equality": function (it) {
assert.equal(it, "hello world");
},
"has access to the environment": function () {
assert.equal(this.state, 42);
},
"a sub context": {
setup: function () {
return this.state;
},
"has access to the parent context object": function (r) {
"has access to the parent environment": function (r) {
assert.equal(r, 42);
assert.equal(this.state, 42);
},
"has access to the parent context object": function (r) {
assert.ok(Array.isArray(this.context.topics));
assert.include(this.context.topics, "hello world");
}
}
}
Expand Down

0 comments on commit 1198d46

Please sign in to comment.