Skip to content

Commit

Permalink
allow nested contexts with no topics
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed May 10, 2010
1 parent 0b891d6 commit fa51949
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/vows.js
Expand Up @@ -274,20 +274,21 @@ vows.tell = function (subject, tests) {
// If it encounters a `topic` function, it waits for the returned
// promise to emit (the topic), at which point it runs the functions under it,
// passing the topic as an argument.
(function run(ctx) {
(function run(ctx, lastTopic) {
var ctxAdded = false;

if ('topic' in ctx.tests) {

topic = lastTopic = ('topic' in ctx.tests) ? ctx.tests.topic
: lastTopic;
if (topic) {
// Topic isn't a function, wrap it into one.
if (typeof(ctx.tests.topic) !== 'function') {
ctx.tests.topic = (function (topic) {
if (typeof(topic) !== 'function') {
topic = (function (topic) {
return function () { return topic };
})(ctx.tests.topic);
})(topic);
}

// Run the topic, passing the previous context topics
topic = ctx.tests.topic.apply(ctx.env, ctx.topics);
topic = topic.apply(ctx.env, ctx.topics);

// If the topic 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 @@ -334,11 +335,11 @@ vows.tell = function (subject, tests) {
if (topic) {
topic.addListener("success", function (vow, ctx) {
return function (val) {
return run(new(Context)(vow, ctx, env));
return run(new(Context)(vow, ctx, env), lastTopic);
};
}(vow, ctx));
} else {
run(new(Context)(vow, ctx, env));
run(new(Context)(vow, ctx, env), lastTopic);
}
}
});
Expand Down
10 changes: 10 additions & 0 deletions test/vows-test.js
Expand Up @@ -97,6 +97,16 @@ vows.describe("Vows", {
}
}
},
"Nested contexts with no topics": {
topic: 45,
"should": {
"pass": {
"the value down": function (topic) {
assert.equal(topic, 45);
}
}
}
},
"Non-promise return value": {
topic: function () { return 1 },
"should be converted to a promise": function (val) {
Expand Down

0 comments on commit fa51949

Please sign in to comment.