diff --git a/lib/jasmine.js b/lib/jasmine.js index ae6ec96..913a777 100644 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -844,7 +844,7 @@ jasmine.ActionCollection.prototype.waitForDone = function(action) { self.env.clearInterval(id); afterExecute(); } - }, 15000); + }, 150); }; /** No-op base class for Jasmine reporters. * @@ -887,18 +887,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) { @@ -1462,52 +1458,59 @@ jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) { jasmine.StringPrettyPrinter.prototype.append = function(value) { this.string += value; }; -jasmine.Queue = function(onComplete) { +jasmine.Queue = function() { this.blocks = []; - this.onComplete = function () { - onComplete(); - }; + 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.setNextOnLastInQueue(block); this.blocks.push(block); }; -jasmine.Queue.prototype.start = function() { - if (this.blocks[0]) { - this.blocks[0].execute(); +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 () { + 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.offset = 0; + 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 () { + this.running = false; + if (this.onComplete) { + this.onComplete(); + } }; jasmine.Queue.prototype.getResults = function () { @@ -1598,9 +1601,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 = []; @@ -1621,10 +1622,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 @@ -1642,13 +1651,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; }; @@ -1676,16 +1685,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(); } }; @@ -1694,7 +1702,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; @@ -1708,22 +1716,19 @@ jasmine.Spec.prototype.execute = function() { spec.safeExecuteBefores(); - spec.queue.start(); + 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() { @@ -1813,11 +1818,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; - for (var suite = this; suite; suite = suite.parentSuite) { - suite.finished = true; + if (typeof(onComplete) == 'function') { + onComplete(); } }; @@ -1839,23 +1844,11 @@ 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.finished = function () { - //for (var suite = this; suite; suite = suite.parentSuite) { - // suite.finished = true; - //} -}; - -jasmine.Suite.prototype.next = function() { - if (this.queue.isComplete()) { - this.finish(); - } else { - this.queue._next(); - } -}; jasmine.WaitsBlock = function(env, timeout, spec) { this.timeout = timeout; jasmine.Block.call(this, env, null, spec); @@ -1863,12 +1856,11 @@ 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); }; jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { this.timeout = timeout; @@ -1882,7 +1874,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; @@ -1890,12 +1882,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({ @@ -1905,7 +1897,7 @@ 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); } }; // Mock setTimeout, clearTimeout diff --git a/spec/suites/SpecRunningTest.js b/spec/suites/SpecRunningTest.js index 12c1e15..2a3b583 100644 --- a/spec/suites/SpecRunningTest.js +++ b/spec/suites/SpecRunningTest.js @@ -593,14 +593,12 @@ describe("jasmine spec running", function () { }); }); - xit("#beforeEach should be able to eval runs and waits blocks", function () { + it("#beforeEach should be able to eval runs and waits blocks", function () { var foo = 0; var bar = 0; var suiteWithBefore = env.describe('one suite with a before', function () { this.beforeEach(function () { - console.error('in beforeEach') this.runs(function () { - console.error('in beforeEach runs') foo++; }); this.waits(500); @@ -611,7 +609,6 @@ describe("jasmine spec running", function () { }); env.it('should be a spec', function () { - console.error('in spec ') bar = 1; foo++; }); @@ -621,12 +618,10 @@ describe("jasmine spec running", function () { expect(foo).toEqual(0); expect(bar).toEqual(0); suiteWithBefore.execute(); - console.error('before tick'); expect(bar).toEqual(0); expect(foo).toEqual(1); fakeTimer.tick(500); - console.error('after tick') expect(bar).toEqual(0); expect(foo).toEqual(2); @@ -765,6 +760,7 @@ describe("jasmine spec running", function () { env.execute(); + var expected = [ "outer beforeEach", "outer it 1", diff --git a/src/Queue.js b/src/Queue.js index 9df3531..0751436 100644 --- a/src/Queue.js +++ b/src/Queue.js @@ -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 () { diff --git a/src/Spec.js b/src/Spec.js index e700d58..4f0ac94 100644 --- a/src/Spec.js +++ b/src/Spec.js @@ -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() {