Skip to content

Commit 3943fec

Browse files
seebeesindexzero
authored andcommitted
event order for 'on'
now if you have a nested 'on' declaration I will enforce the nested order.
1 parent d9fe353 commit 3943fec

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

lib/vows.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ var Suite = require('./vows/suite').Suite;
5757
//
5858
function addVow(vow) {
5959
var batch = vow.batch,
60-
event = vow.binding.context.event || 'success';
60+
event = vow.binding.context.event || 'success'
61+
self = this;
6162

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

118+
if (vow.binding.context.isEvent && vow.binding.context.after) {
119+
var after = vow.binding.context.after;
120+
// only need to check order. I won't get here if the after event
121+
// has never been emitted
122+
if (self._vowsEmitedEventsOrder.indexOf(after) >
123+
self._vowsEmitedEventsOrder.indexOf(event)) {
124+
output('broken', event + ' emitted before ' + after);
125+
return;
126+
}
127+
}
128+
118129
// Run the test, and try to catch `AssertionError`s and other exceptions;
119130
// increment counters accordingly.
120131
try {
@@ -210,6 +221,7 @@ vows.describe = function (subject) {
210221
// just in case someone emit's before I get to it
211222
this.options.Emitter.prototype.emit = function (event) {
212223
this._vowsEmitedEvents = this._vowsEmitedEvents || {};
224+
this._vowsEmitedEventsOrder = this._vowsEmitedEventsOrder || [];
213225
// slice off the event
214226
var args = Array.prototype.slice.call(arguments, 1);
215227
// if multiple events are fired, add or push
@@ -218,6 +230,9 @@ vows.describe = function (subject) {
218230
} else {
219231
this._vowsEmitedEvents[event] = [args];
220232
}
233+
234+
// push the event onto a stack so I have an order
235+
this._vowsEmitedEventsOrder.push(event);
221236
return oldEmit.apply(this, arguments);
222237
}
223238
this.suites.push(suite);

lib/vows/context.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,17 @@ this.Context = function (vow, ctx, env) {
5252
this.name = vow.description = 'on';
5353
}
5454

55+
// if this is a sub-event context AND it's context was an event,
56+
// then I must enforce event order.
57+
// this will not do a good job of handling pin-pong events
58+
if (this.name === 'on' && ctx.isEvent) {
59+
this.after = ctx.name;
60+
}
61+
5562
if (ctx.name === 'on') {
5663
this.isEvent = true;
5764
this.event = this.name;
65+
this.after = ctx.after;
5866
} else {
5967
this.isEvent = false;
6068
this.event = 'success';

test/vows-test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,12 @@ vows.describe("Vows with sub events").addBatch({
407407
assert.strictEqual(ret, 'request_data');
408408
},
409409
on: {
410-
"end": {
411-
"will catch end, even if it is in empty nested in 'request'": function (ret) {
412-
assert.strictEqual(ret, 'end_data')
410+
on: {
411+
"end": {
412+
"will require that 'end' is emitted after 'request'": function (ret) {
413+
assert.strictEqual(ret, 'end_data');
414+
// TODO need a test that fails to prove this works
415+
}
413416
}
414417
}
415418
}

0 commit comments

Comments
 (0)