beforeEach now supports waits and Runs blocks

This commit is contained in:
ragaskar
2009-08-03 23:09:50 -07:00
parent 0d6c6c2a35
commit b55399bd4b
4 changed files with 105 additions and 102 deletions

View File

@@ -2,14 +2,25 @@ jasmine.Queue = function() {
this.blocks = [];
this.running = false;
this.index = 0;
this.offset = 0;
};
jasmine.Queue.prototype.addBefore = function (block) {
this.blocks.unshift(block);
};
jasmine.Queue.prototype.add = function(block) {
this.blocks.push(block);
};
jasmine.Queue.prototype.insertNext = function (block) {
this.blocks.splice((this.index + this.offset + 1), 0, block);
this.offset++;
};
jasmine.Queue.prototype.start = function(onComplete) {
var self = this;
self.running = true;
self.onComplete = onComplete;
if (self.blocks[0]) {
self.blocks[0].execute(function () {
@@ -26,6 +37,7 @@ jasmine.Queue.prototype.isRunning = function () {
jasmine.Queue.prototype._next = function () {
var self = this;
self.offset = 0;
self.index++;
if (self.index < self.blocks.length) {
self.blocks[self.index].execute(function () {self._next();});
@@ -35,10 +47,10 @@ jasmine.Queue.prototype._next = function () {
};
jasmine.Queue.prototype.finish = function () {
this.running = false;
if (this.onComplete) {
this.onComplete();
}
this.running = false;
};
jasmine.Queue.prototype.getResults = function () {

View File

@@ -12,9 +12,7 @@ jasmine.Spec = function(env, suite, description) {
spec.env = env;
spec.suite = suite;
spec.description = description;
spec.queue = new jasmine.Queue(function () {
spec.finish();
});
spec.queue = new jasmine.Queue();
spec.finished = false;
spec.afterCallbacks = [];
@@ -35,10 +33,18 @@ jasmine.Spec.prototype.getResults = function() {
jasmine.Spec.prototype.runs = function (func) {
var block = new jasmine.Block(this.env, func, this);
this.queue.add(block);
this.addToQueue(block);
return this;
};
jasmine.Spec.prototype.addToQueue = function (block) {
if (this.queue.isRunning()) {
this.queue.insertNext(block);
} else {
this.queue.add(block);
}
};
/**
* @private
* @deprecated
@@ -56,13 +62,13 @@ jasmine.Spec.prototype.expect = function(actual) {
jasmine.Spec.prototype.waits = function(timeout) {
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
this.queue.add(waitsFunc);
this.addToQueue(waitsFunc);
return this;
};
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
this.queue.add(waitsForFunc);
this.addToQueue(waitsForFunc);
return this;
};
@@ -121,22 +127,19 @@ jasmine.Spec.prototype.execute = function(onComplete) {
spec.safeExecuteBefores();
spec.queue.start(function () { spec.finish(onComplete); });
spec.queue.start(function () {
spec.finish(onComplete);
});
spec.env.currentlyRunningTests = false;
};
jasmine.Spec.prototype.safeExecuteBefores = function() {
var befores = [];
for (var suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.beforeQueue) {
for (var i = 0; i < suite.beforeQueue.length; i++)
befores.push(suite.beforeQueue[i]);
this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this));
}
}
while (befores.length) {
this.safeExecuteBeforeOrAfter(befores.pop());
}
};
jasmine.Spec.prototype.safeExecuteAfters = function() {