diff --git a/lib/jasmine.js b/lib/jasmine.js index 9d61bf2..60bdf1d 100644 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -21,11 +21,16 @@ jasmine.unimplementedMethod_ = function() { jasmine.undefined = jasmine.___undefined___; /** - * Default interval for event loop yields. Small values here may result in slow test running. Zero means no updates until all tests have completed. + * Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed. * */ jasmine.DEFAULT_UPDATE_INTERVAL = 250; +/** + * Default timeout interval in milliseconds for waitsFor() blocks. + */ +jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; + jasmine.getGlobal = function() { function getGlobal() { return this; @@ -490,22 +495,24 @@ var runs = function(func) { }; /** - * Waits for a timeout before moving to the next runs()-defined block. - * @param {Number} timeout + * Waits a fixed time period before moving to the next block. + * + * @deprecated Use waitsFor() instead + * @param {Number} timeout milliseconds to wait */ var waits = function(timeout) { jasmine.getEnv().currentSpec.waits(timeout); }; /** - * Waits for the latchFunction to return true before proceeding to the next runs()-defined block. + * Waits for the latchFunction to return true before proceeding to the next block. * - * @param {Number} timeout * @param {Function} latchFunction - * @param {String} message + * @param {String} optional_timeoutMessage + * @param {Number} optional_timeout */ -var waitsFor = function(timeout, latchFunction, message) { - jasmine.getEnv().currentSpec.waitsFor(timeout, latchFunction, message); +var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { + jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments); }; /** @@ -660,6 +667,7 @@ jasmine.Env = function() { this.reporter = new jasmine.MultiReporter(); this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL; + this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; this.lastUpdate = 0; this.specFilter = function() { return true; @@ -1660,6 +1668,7 @@ jasmine.Queue = function(env) { this.running = false; this.index = 0; this.offset = 0; + this.abort = false; }; jasmine.Queue.prototype.addBefore = function(block) { @@ -1694,7 +1703,7 @@ jasmine.Queue.prototype.next_ = function() { while (goAgain) { goAgain = false; - if (self.index < self.blocks.length) { + if (self.index < self.blocks.length && !this.abort) { var calledSynchronously = true; var completedSynchronously = false; @@ -1704,6 +1713,10 @@ jasmine.Queue.prototype.next_ = function() { return; } + if (self.blocks[self.index].abort) { + self.abort = true; + } + self.offset = 0; self.index++; @@ -1900,14 +1913,46 @@ jasmine.Spec.prototype.expect = function(actual) { return positive; }; +/** + * Waits a fixed time period before moving to the next block. + * + * @deprecated Use waitsFor() instead + * @param {Number} timeout milliseconds to wait + */ jasmine.Spec.prototype.waits = function(timeout) { var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); this.addToQueue(waitsFunc); return this; }; -jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { - var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); +/** + * Waits for the latchFunction to return true before proceeding to the next block. + * + * @param {Function} latchFunction + * @param {String} optional_timeoutMessage + * @param {Number} optional_timeout + */ +jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { + var latchFunction_ = null; + var optional_timeoutMessage_ = null; + var optional_timeout_ = null; + + for (var i = 0; i < arguments.length; i++) { + var arg = arguments[i]; + switch (typeof arg) { + case 'function': + latchFunction_ = arg; + break; + case 'string': + optional_timeoutMessage_ = arg; + break; + case 'number': + optional_timeout_ = arg; + break; + } + } + + var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this); this.addToQueue(waitsForFunc); return this; }; @@ -2142,7 +2187,7 @@ jasmine.WaitsBlock.prototype.execute = function (onComplete) { * @param {jasmine.Spec} spec The Jasmine spec. */ jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { - this.timeout = timeout; + this.timeout = timeout || env.defaultTimeoutInterval; this.latchFunction = latchFunction; this.message = message; this.totalTimeSpentWaitingForLatch = 0; @@ -2171,7 +2216,9 @@ jasmine.WaitsForBlock.prototype.execute = function(onComplete) { name: 'timeout', message: message }); - // todo: need to prevent additional blocks in this spec from running... [xw 20100819] + + this.abort = true; + onComplete(); } else { this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT; var self = this; @@ -2368,5 +2415,5 @@ jasmine.version_= { "major": 0, "minor": 11, "build": 1, - "revision": 1282768601 + "revision": 1282784791 };