Major refactoring of Spec. Moved QueuedFunction to Block, WaitsBlock and WaitsForBlock. Waits and WaitsFor blocks now sequentially stackable

This commit is contained in:
ragaskar
2009-07-29 22:27:11 -07:00
parent 708d148d31
commit a1a278ee69
10 changed files with 912 additions and 754 deletions

31
src/Block.js Normal file
View File

@@ -0,0 +1,31 @@
/**
* Blocks are functions with executable code that make up a spec.
*
* @constructor
* @param {jasmine.Env} env
* @param {Function} func
* @param {jasmine.Spec} spec
*/
jasmine.Block = function(env, func, spec) {
this.env = env;
this.func = func;
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() {
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();
};
jasmine.Block.prototype.fail = function(e) {
this.spec.results.addResult(new jasmine.ExpectationResult(false, jasmine.util.formatException(e), null));
};

View File

@@ -1,84 +0,0 @@
/**
* QueuedFunction is how ActionCollections' actions are implemented
*
* @constructor
* @param {jasmine.Env} env
* @param {Function} func
* @param {Number} timeout
* @param {Function} latchFunction
* @param {jasmine.Spec} spec
*/
jasmine.QueuedFunction = function(env, func, timeout, latchFunction, spec) {
this.env = env;
this.func = func;
this.timeout = timeout;
this.latchFunction = latchFunction;
this.spec = spec;
this.totalTimeSpentWaitingForLatch = 0;
this.latchTimeoutIncrement = 100;
};
jasmine.QueuedFunction.prototype.next = function() {
this.spec.finish(); // default value is to be done after one function
};
jasmine.QueuedFunction.prototype.safeExecute = function() {
this.env.reporter.log('>> Jasmine Running ' + this.spec.suite.description + ' ' + this.spec.description + '...');
try {
this.func.apply(this.spec);
} catch (e) {
this.fail(e);
}
};
jasmine.QueuedFunction.prototype.execute = function() {
var self = this;
var executeNow = function() {
self.safeExecute();
self.next();
};
var executeLater = function() {
self.env.setTimeout(executeNow, self.timeout);
};
var executeNowOrLater = function() {
var latchFunctionResult;
try {
latchFunctionResult = self.latchFunction.apply(self.spec);
} catch (e) {
self.fail(e);
self.next();
return;
}
if (latchFunctionResult) {
executeNow();
} else if (self.totalTimeSpentWaitingForLatch >= self.timeout) {
var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.latchFunction.description || 'something to happen');
self.fail({
name: 'timeout',
message: message
});
self.next();
} else {
self.totalTimeSpentWaitingForLatch += self.latchTimeoutIncrement;
self.env.setTimeout(executeNowOrLater, self.latchTimeoutIncrement);
}
};
if (this.latchFunction !== undefined) {
executeNowOrLater();
} else if (this.timeout > 0) {
executeLater();
} else {
executeNow();
}
};
jasmine.QueuedFunction.prototype.fail = function(e) {
this.spec.results.addResult(new jasmine.ExpectationResult(false, jasmine.util.formatException(e), null));
};

View File

@@ -12,8 +12,6 @@ jasmine.Spec = function(env, suite, description) {
this.suite = suite;
this.description = description;
this.queue = [];
this.currentTimeout = 0;
this.currentLatchFunction = undefined;
this.finished = false;
this.afterCallbacks = [];
this.spies_ = [];
@@ -33,17 +31,12 @@ jasmine.Spec.prototype.getResults = function() {
};
jasmine.Spec.prototype.addToQueue = function(func) {
var queuedFunction = new jasmine.QueuedFunction(this.env, func, this.currentTimeout, this.currentLatchFunction, this);
this.queue.push(queuedFunction);
var block = new jasmine.Block(this.env, func, this);
if (this.queue.length > 1) {
var previousQueuedFunction = this.queue[this.queue.length - 2];
previousQueuedFunction.next = function() {
queuedFunction.execute();
};
}
this.setNextOnLastInQueue(block);
this.queue.push(block);
this.resetTimeout();
return this;
};
@@ -56,31 +49,48 @@ jasmine.Spec.prototype.expects_that = function(actual) {
};
/**
* @private
* @private
*/
jasmine.Spec.prototype.expect = function(actual) {
return new (this.getMatchersClass_())(this.env, actual, this.results);
};
/**
* @private
*/
jasmine.Spec.prototype.setNextOnLastInQueue = function (block) {
if (this.queue.length > 0) {
var previousBlock = this.queue[this.queue.length - 1];
previousBlock.next = function() {
block.execute();
};
}
};
/**
* @private
*/
jasmine.Spec.prototype.waits = function(timeout) {
this.currentTimeout = timeout;
this.currentLatchFunction = undefined;
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
this.setNextOnLastInQueue(waitsFunc);
this.queue.push(waitsFunc);
return this;
};
/**
* @private
*/
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, message) {
this.currentTimeout = timeout;
this.currentLatchFunction = latchFunction;
this.currentLatchFunction.description = message;
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
this.setNextOnLastInQueue(waitsForFunc);
this.queue.push(waitsForFunc);
return this;
};
jasmine.Spec.prototype.failWithException = function (e) {
this.results.addResult(new jasmine.ExpectationResult(false, jasmine.util.formatException(e), null));
};
jasmine.Spec.prototype.getMatchersClass_ = function() {
return this.matchersClass || jasmine.Matchers;
};
@@ -97,11 +107,6 @@ jasmine.Spec.prototype.addMatchers = function(matchersPrototype) {
this.matchersClass = newMatchersClass;
};
jasmine.Spec.prototype.resetTimeout = function() {
this.currentTimeout = 0;
this.currentLatchFunction = undefined;
};
jasmine.Spec.prototype.finishCallback = function() {
this.env.reporter.reportSpecResults(this);
};

14
src/WaitsBlock.js Normal file
View File

@@ -0,0 +1,14 @@
jasmine.WaitsBlock = function(env, timeout, spec) {
this.timeout = timeout;
jasmine.Block.call(this, env, null, 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);
};

38
src/WaitsForBlock.js Normal file
View File

@@ -0,0 +1,38 @@
jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) {
this.timeout = timeout;
this.latchFunction = latchFunction;
this.message = message;
this.totalTimeSpentWaitingForLatch = 0;
jasmine.Block.call(this, env, null, spec);
};
jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block);
jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 100;
jasmine.WaitsForBlock.prototype.execute = function () {
var self = this;
self.env.reporter.log('>> Jasmine waiting for ' + (self.message || 'something to happen'));
var latchFunctionResult;
try {
latchFunctionResult = self.latchFunction.apply(self.spec);
} catch (e) {
self.fail(e);
self.next();
return;
}
if (latchFunctionResult) {
self.next();
} else if (self.totalTimeSpentWaitingForLatch >= self.timeout) {
var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.message || 'something to happen');
self.fail({
name: 'timeout',
message: message
});
self.spec.next();
} else {
self.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
self.env.setTimeout(function () { self.execute(); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
}
};