Skip to content

Commit

Permalink
event order for 'on'
Browse files Browse the repository at this point in the history
now if you have a nested 'on' declaration I will enforce the nested order.
  • Loading branch information
seebees authored and indexzero committed Nov 25, 2011
1 parent d9fe353 commit 3943fec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
23 changes: 19 additions & 4 deletions lib/vows.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ var Suite = require('./vows/suite').Suite;
//
function addVow(vow) {
var batch = vow.batch,
event = vow.binding.context.event || 'success';
event = vow.binding.context.event || 'success'
self = this;

batch.total ++;
batch.vows.push(vow);
Expand All @@ -66,8 +67,8 @@ function addVow(vow) {
this.on(event, function () {
var args = Array.prototype.slice.call(arguments);
// If the vow is a sub-event then we know it is an
// emited event. So I don't muck with the arguments
// However the legacey behavior:
// emitted event. So I don't muck with the arguments
// However the legacy behavior:
// If the callback is expecting two or more arguments,
// pass the error as the first (null) and the result after.
if (!(this.ctx && this.ctx.isEvent) &&
Expand Down Expand Up @@ -95,7 +96,6 @@ function addVow(vow) {
this._vowsEmitedEvents.hasOwnProperty(event)) {
// make sure no one is messing with me
if (Array.isArray(this._vowsEmitedEvents[event])) {
var self = this;
// I don't think I need to optimize for one event,
// I think it is more important to make sure I check the vow n times
self._vowsEmitedEvents[event].forEach(function(args) {
Expand All @@ -115,6 +115,17 @@ function addVow(vow) {
return output('pending');
}

if (vow.binding.context.isEvent && vow.binding.context.after) {
var after = vow.binding.context.after;
// only need to check order. I won't get here if the after event
// has never been emitted
if (self._vowsEmitedEventsOrder.indexOf(after) >
self._vowsEmitedEventsOrder.indexOf(event)) {
output('broken', event + ' emitted before ' + after);
return;
}
}

// Run the test, and try to catch `AssertionError`s and other exceptions;
// increment counters accordingly.
try {
Expand Down Expand Up @@ -210,6 +221,7 @@ vows.describe = function (subject) {
// just in case someone emit's before I get to it
this.options.Emitter.prototype.emit = function (event) {
this._vowsEmitedEvents = this._vowsEmitedEvents || {};
this._vowsEmitedEventsOrder = this._vowsEmitedEventsOrder || [];
// slice off the event
var args = Array.prototype.slice.call(arguments, 1);
// if multiple events are fired, add or push
Expand All @@ -218,6 +230,9 @@ vows.describe = function (subject) {
} else {
this._vowsEmitedEvents[event] = [args];
}

// push the event onto a stack so I have an order
this._vowsEmitedEventsOrder.push(event);
return oldEmit.apply(this, arguments);
}
this.suites.push(suite);
Expand Down
8 changes: 8 additions & 0 deletions lib/vows/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ this.Context = function (vow, ctx, env) {
this.name = vow.description = 'on';
}

// if this is a sub-event context AND it's context was an event,
// then I must enforce event order.
// this will not do a good job of handling pin-pong events
if (this.name === 'on' && ctx.isEvent) {
this.after = ctx.name;
}

if (ctx.name === 'on') {
this.isEvent = true;
this.event = this.name;
this.after = ctx.after;
} else {
this.isEvent = false;
this.event = 'success';
Expand Down
9 changes: 6 additions & 3 deletions test/vows-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,12 @@ vows.describe("Vows with sub events").addBatch({
assert.strictEqual(ret, 'request_data');
},
on: {
"end": {
"will catch end, even if it is in empty nested in 'request'": function (ret) {
assert.strictEqual(ret, 'end_data')
on: {
"end": {
"will require that 'end' is emitted after 'request'": function (ret) {
assert.strictEqual(ret, 'end_data');
// TODO need a test that fails to prove this works
}
}
}
}
Expand Down

0 comments on commit 3943fec

Please sign in to comment.