Skip to content

Commit

Permalink
'setup' is now called 'topic'
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed May 7, 2010
1 parent a7b5857 commit 8a03c2a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
54 changes: 27 additions & 27 deletions lib/vows.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ vows.options = {
// Run all vows/tests.
// It can take either a function as `tests`,
// or an object literal.
vows.tell = function (topic, tests) {
vows.tell = function (subject, tests) {
this.options.Emitter.prototype.addVow = addVow;
this.tests = tests;
this.remaining = 0;
Expand All @@ -222,14 +222,14 @@ vows.tell = function (topic, tests) {

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

if (typeof(topic) === 'string' && tests) {
if (typeof(subject) === 'string' && tests) {
if (!vows.options.brief) {
puts('\n' + stylize(topic, 'underline') + '\n');
}
} else if (topic instanceof Object || topic instanceof Function) {
vows.tests = topic;
} else if (subject instanceof Object || subject instanceof Function) {
vows.tests = subject;
} else { throw "tell() takes a topic and an Object" }

start = new(Date);
Expand Down Expand Up @@ -257,49 +257,49 @@ vows.tell = function (topic, tests) {
})(vows.tests);

// The test runner, it calls itself recursively, passing the
// previous context to the inner contexts. This is so the `setup`
// previous context to the inner contexts. This is so the `topic`
// functions have access to all the previous context topics in their
// arguments list.
// It is defined and invoked at the same time.
// If it encounters a `setup` function, it waits for the returned
// 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) {
var ctxAdded = false;

if ('setup' in ctx.tests) {
if ('topic' in ctx.tests) {

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

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

// If the setup doesn't return an event emitter (such as a promise),
// 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.
if (! (setup instanceof vows.options.Emitter)) {
if (! (topic instanceof vows.options.Emitter)) {
var emitter = new(vows.options.Emitter);

process.nextTick(function (val) {
return function () { emitter.emit("success", val) };
}(setup)); setup = emitter;
}(topic)); topic = emitter;
}

setup.addListener('success', function (val) {
// Once the setup fires, add the return value
topic.addListener('success', function (val) {
// Once the topic fires, add the return value
// to the beginning of the topics list, so it
// becomes the first argument for the next setup.
// becomes the first argument for the next topic.
ctx.topics.unshift(val);
});
} else { setup = null }
} else { topic = null }

// Now run the tests, or sub-contexts
Object.keys(ctx.tests).filter(function (k) {
return ctx.tests[k] && k !== 'setup';
return ctx.tests[k] && k !== 'topic';
}).forEach(function (item) {
// Holds the current test or context
vow = Object.create({
Expand All @@ -311,18 +311,18 @@ vows.tell = function (topic, tests) {
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
// setup fires.
// of the `topic` function, so it'll get called once the
// topic fires.
// If we encounter an object literal, we recurse, sending it
// our current context.
if (typeof(vow.callback) === 'function') {
setup.addVow(vow);
topic.addVow(vow);
} else if (typeof(vow.callback) === 'object' && ! Array.isArray(vow.callback)) {
// If there's a setup stage, we have to wait for it to fire,
// before calling the inner context. Else, just run the inner context
// synchronously.
if (setup) {
setup.addListener("success", function (vow, ctx) {
if (topic) {
topic.addListener("success", function (vow, ctx) {
return function (val) {
return run(new(Context)(vow, ctx, env));
};
Expand Down
26 changes: 13 additions & 13 deletions test/vows-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var promiser = function (val) {
}
};

vows.tell("Vows", {
vows.describe("Vows", {
"A context": {
setup: promiser("hello world"),
topic: promiser("hello world"),

"testing equality": function (it) {
assert.equal(it, "hello world");
Expand All @@ -45,7 +45,7 @@ vows.tell("Vows", {
assert.isEmpty("");
},
"with a nested context": {
setup: function (parent) {
topic: function (parent) {
this.state = 42;
return promiser(parent)();
},
Expand All @@ -56,7 +56,7 @@ vows.tell("Vows", {
assert.equal(this.state, 42);
},
"a sub context": {
setup: function () {
topic: function () {
return this.state;
},
"has access to the parent environment": function (r) {
Expand All @@ -71,24 +71,24 @@ vows.tell("Vows", {
}
},
"Nested contexts": {
setup: promiser(1),
topic: promiser(1),

"have": {
setup: function (a) { return promiser(2)() },
topic: function (a) { return promiser(2)() },

"access": {
setup: function (b, a) { return promiser(3)() },
topic: function (b, a) { return promiser(3)() },

"to": {
setup: function (c, b, a) { return promiser([4, c, b, a])() },
topic: function (c, b, a) { return promiser([4, c, b, a])() },

"the parent topics": function (topics) {
assert.equal(topics.join(), [4, 3, 2, 1].join());
}
},

"from": {
setup: function (c, b, a) { return promiser([4, c, b, a])() },
topic: function (c, b, a) { return promiser([4, c, b, a])() },

"the parent topics": function(topics) {
assert.equal(topics.join(), [4, 3, 2, 1].join());
Expand All @@ -98,27 +98,27 @@ vows.tell("Vows", {
}
},
"Non-promise return value": {
setup: function () { return 1 },
topic: function () { return 1 },
"should be converted to a promise": function (val) {
assert.equal(val, 1);
}
},
"A 'prepared' interface": {
"with a wrapped function": {
setup: function () { return api.get(42) },
topic: function () { return api.get(42) },
"should work as expected": function (val) {
assert.equal(val, 42);
}
},
"with a non-wrapped function": {
setup: function () { return api.version() },
topic: function () { return api.version() },
"should work as expected": function (val) {
assert.equal(val, '1.0');
}
}
},
"Non-functions as subjects": {
setup: 45,
topic: 45,

"should work as expected": function (subject) {
assert.equal(subject, 45);
Expand Down

0 comments on commit 8a03c2a

Please sign in to comment.