Re-refactored Queue to use straightforward callbacks

This commit is contained in:
ragaskar 2009-08-03 22:22:13 -07:00
parent 9475de28b3
commit 0d6c6c2a35
8 changed files with 47 additions and 72 deletions

View File

@ -479,24 +479,20 @@ describe("jasmine spec running", function () {
var nested = env.describe('suite', function () {
env.describe('nested', function () {
env.it('should run nested suites', function () {
console.log('first')
foo++;
});
env.it('should run nested suites', function () {
console.log('second')
bar++;
});
});
env.describe('nested 2', function () {
env.it('should run suites following nested suites', function () {
console.log('third')
baz++;
});
});
env.it('should run tests following nested suites', function () {
console.log('fourth')
quux++;
});
});

View File

@ -88,5 +88,5 @@ jasmine.ActionCollection.prototype.waitForDone = function(action) {
self.env.clearInterval(id);
afterExecute();
}
}, 15000);
}, 150);
};

View File

@ -12,18 +12,14 @@ jasmine.Block = function(env, func, spec) {
this.spec = spec;
};
jasmine.Block.prototype._next = function() {
this.spec.finish(); // default value is to be done after one function
};
jasmine.Block.prototype.execute = function() {
jasmine.Block.prototype.execute = function(onComplete) {
this.env.reporter.log('>> Jasmine Running ' + this.spec.suite.description + ' ' + this.spec.description + '...');
try {
this.func.apply(this.spec);
} catch (e) {
this.fail(e);
}
this._next();
onComplete();
};
jasmine.Block.prototype.fail = function(e) {

View File

@ -1,49 +1,44 @@
jasmine.Queue = function(onComplete) {
jasmine.Queue = function() {
this.blocks = [];
this.onComplete = function () {
onComplete();
};
this.running = false;
this.index = 0;
};
jasmine.Queue.prototype.add = function(block) {
this.setNextOnLastInQueue(block);
this.blocks.push(block);
};
jasmine.Queue.prototype.start = function() {
if (this.blocks[0]) {
this.blocks[0].execute();
jasmine.Queue.prototype.start = function(onComplete) {
var self = this;
self.onComplete = onComplete;
if (self.blocks[0]) {
self.blocks[0].execute(function () {
self._next();
});
} else {
this.onComplete();
self.finish();
}
};
jasmine.Queue.prototype.isRunning = function () {
return this.running;
};
jasmine.Queue.prototype._next = function () {
this.index++;
if (this.index < this.blocks.length) {
this.blocks[this.index].execute();
}
};
/**
* @private
*/
jasmine.Queue.prototype.setNextOnLastInQueue = function (block) {
var self = this;
block._next = function () {
self.onComplete();
};
if (self.blocks.length > 0) {
var previousBlock = self.blocks[self.blocks.length - 1];
previousBlock._next = function() {
block.execute();
};
self.index++;
if (self.index < self.blocks.length) {
self.blocks[self.index].execute(function () {self._next();});
} else {
self.finish();
}
};
jasmine.Queue.prototype.isComplete = function () {
return this.index >= (this.blocks.length - 1);
jasmine.Queue.prototype.finish = function () {
if (this.onComplete) {
this.onComplete();
}
this.running = false;
};
jasmine.Queue.prototype.getResults = function () {

View File

@ -90,16 +90,15 @@ jasmine.Spec.prototype.finishCallback = function() {
this.env.reporter.reportSpecResults(this);
};
jasmine.Spec.prototype.finish = function() {
jasmine.Spec.prototype.finish = function(onComplete) {
for (var i = 0; i < this.afterCallbacks.length; i++) {
this.afterCallbacks[i]();
}
this.safeExecuteAfters();
this.removeAllSpies();
this.finishCallback();
this.finished = true;
if (this.suite.next) {
this.suite.next();
if (onComplete) {
onComplete();
}
};
@ -108,7 +107,7 @@ jasmine.Spec.prototype.after = function(doAfter) {
};
jasmine.Spec.prototype.execute = function() {
jasmine.Spec.prototype.execute = function(onComplete) {
var spec = this;
if (!spec.env.specFilter(spec)) {
spec.results.skipped = true;
@ -122,7 +121,7 @@ jasmine.Spec.prototype.execute = function() {
spec.safeExecuteBefores();
spec.queue.start();
spec.queue.start(function () { spec.finish(onComplete); });
spec.env.currentlyRunningTests = false;
};

View File

@ -27,11 +27,11 @@ jasmine.Suite.prototype.getFullName = function() {
return fullName;
};
jasmine.Suite.prototype.finish = function() {
jasmine.Suite.prototype.finish = function(onComplete) {
this.env.reporter.reportSuiteResults(this);
this.finished = true;
if (this.parentSuite) {
this.parentSuite.next();
if (typeof(onComplete) == 'function') {
onComplete();
}
};
@ -53,18 +53,8 @@ jasmine.Suite.prototype.add = function(block) {
this.queue.add(block);
};
jasmine.Suite.prototype.execute = function() {
this.queue.start();
jasmine.Suite.prototype.execute = function(onComplete) {
var self = this;
this.queue.start(function () { self.finish(onComplete); });
};
jasmine.Suite.prototype._next = function () {
this.next();
};
jasmine.Suite.prototype.next = function() {
if (this.queue.isComplete()) {
this.finish();
} else {
this.queue._next();
}
};

View File

@ -5,10 +5,9 @@ jasmine.WaitsBlock = function(env, timeout, spec) {
jasmine.util.inherit(jasmine.WaitsBlock, jasmine.Block);
jasmine.WaitsBlock.prototype.execute = function () {
var self = this;
self.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...');
self.env.setTimeout(function () {
self._next();
}, self.timeout);
jasmine.WaitsBlock.prototype.execute = function (onComplete) {
this.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...');
this.env.setTimeout(function () {
onComplete();
}, this.timeout);
};

View File

@ -10,7 +10,7 @@ jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block);
jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 100;
jasmine.WaitsForBlock.prototype.execute = function () {
jasmine.WaitsForBlock.prototype.execute = function (onComplete) {
var self = this;
self.env.reporter.log('>> Jasmine waiting for ' + (self.message || 'something to happen'));
var latchFunctionResult;
@ -18,12 +18,12 @@ jasmine.WaitsForBlock.prototype.execute = function () {
latchFunctionResult = self.latchFunction.apply(self.spec);
} catch (e) {
self.fail(e);
self._next();
onComplete();
return;
}
if (latchFunctionResult) {
self._next();
onComplete();
} else if (self.totalTimeSpentWaitingForLatch >= self.timeout) {
var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.message || 'something to happen');
self.fail({
@ -33,6 +33,6 @@ jasmine.WaitsForBlock.prototype.execute = function () {
self.spec._next();
} else {
self.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
self.env.setTimeout(function () { self.execute(); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
self.env.setTimeout(function () { self.execute(onComplete); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
}
};