Permalink
Browse files

event order for 'on'

now if you have a nested 'on' declaration I will enforce the nested order.
  • Loading branch information...
1 parent d9fe353 commit 3943fecdb0d1a5c9c41565a4a296646aad97ee76 @seebees seebees committed with indexzero Nov 20, 2011
Showing with 33 additions and 7 deletions.
  1. +19 −4 lib/vows.js
  2. +8 −0 lib/vows/context.js
  3. +6 −3 test/vows-test.js
View
@@ -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);
@@ -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) &&
@@ -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) {
@@ -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 {
@@ -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
@@ -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);
View
@@ -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';
View
@@ -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
+ }
}
}
}

0 comments on commit 3943fec

Please sign in to comment.