From f73fd8ae95d62bacea3e33075398fe84699e3be4 Mon Sep 17 00:00:00 2001 From: ragaskar Date: Sat, 1 Aug 2009 14:56:29 -0700 Subject: [PATCH] Refactor Spec block execution into Queue --- lib/jasmine.js | 83 ++++++++++++++++++---------------- spec/runner.html | 1 + spec/suites/SpecRunningTest.js | 64 ++++---------------------- src/Queue.js | 29 ++++++++++++ src/Spec.js | 54 ++++++---------------- 5 files changed, 97 insertions(+), 134 deletions(-) create mode 100644 src/Queue.js diff --git a/lib/jasmine.js b/lib/jasmine.js index 0b880547..f3f009fe 100644 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -1462,6 +1462,35 @@ jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) { jasmine.StringPrettyPrinter.prototype.append = function(value) { this.string += value; }; +jasmine.Queue = function() { + this.blocks = []; +}; + +jasmine.Queue.prototype.add = function(block) { + this.setNextOnLastInQueue(block); + this.blocks.push(block); +}; + +jasmine.Queue.prototype.start = function(onComplete) { + if (this.blocks[0]) { + this.blocks[0].execute(); + } else { + onComplete(); + } +}; + +/** + * @private + */ +jasmine.Queue.prototype.setNextOnLastInQueue = function (block) { + if (this.blocks.length > 0) { + var previousBlock = this.blocks[this.blocks.length - 1]; + previousBlock.next = function() { + block.execute(); + }; + } +}; + /* JasmineReporters.reporter * Base object that will get called whenever a Spec, Suite, or Runner is done. It is up to * descendants of this object to do something with the results (see json_reporter.js) @@ -1539,7 +1568,7 @@ jasmine.Spec = function(env, suite, description) { this.env = env; this.suite = suite; this.description = description; - this.queue = []; + this.queue = new jasmine.Queue(); this.finished = false; this.afterCallbacks = []; this.spies_ = []; @@ -1559,15 +1588,10 @@ jasmine.Spec.prototype.getResults = function() { jasmine.Spec.prototype.runs = function (func) { var block = new jasmine.Block(this.env, func, this); - this.addToQueue(block); + this.queue.add(block); return this; }; -jasmine.Spec.prototype.addToQueue = function(block) { - this.setNextOnLastInQueue(block); - this.queue.push(block); -}; - /** * @private * @deprecated @@ -1583,33 +1607,15 @@ 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) { var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); - this.addToQueue(waitsFunc); + this.queue.add(waitsFunc); return this; }; -/** - * @private - */ jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); - this.addToQueue(waitsForFunc); + this.queue.add(waitsForFunc); return this; }; @@ -1650,24 +1656,21 @@ jasmine.Spec.prototype.after = function(doAfter) { }; jasmine.Spec.prototype.execute = function() { - if (!this.env.specFilter(this)) { - this.results.skipped = true; - this.finishCallback(); - this.finished = true; + var spec = this; + if (!spec.env.specFilter(spec)) { + spec.results.skipped = true; + spec.finishCallback(); + spec.finished = true; return; } - this.env.currentSpec = this; - this.env.currentlyRunningTests = true; + spec.env.currentSpec = spec; + spec.env.currentlyRunningTests = true; - this.safeExecuteBefores(); + spec.safeExecuteBefores(); - if (this.queue[0]) { - this.queue[0].execute(); - } else { - this.finish(); - } - this.env.currentlyRunningTests = false; + spec.queue.start(function () { spec.finish(); }); + spec.env.currentlyRunningTests = false; }; jasmine.Spec.prototype.safeExecuteBefores = function() { diff --git a/spec/runner.html b/spec/runner.html index 0fef41dd..595daaea 100644 --- a/spec/runner.html +++ b/spec/runner.html @@ -18,6 +18,7 @@ + diff --git a/spec/suites/SpecRunningTest.js b/spec/suites/SpecRunningTest.js index d0b7a386..6736d7c1 100644 --- a/spec/suites/SpecRunningTest.js +++ b/spec/suites/SpecRunningTest.js @@ -96,51 +96,36 @@ describe("jasmine spec running", function () { it('should queue waits and runs that it encounters while executing specs', function() { var specWithRunsAndWaits; + var foo = 0; env.describe('test async spec', function() { specWithRunsAndWaits = env.it('spec w/ queued statments', function () { this.runs(function () { - + foo++; }); this.waits(500); this.runs(function () { - + foo++; }); this.waits(500); this.runs(function () { + foo++; }); }); }); - expect(specWithRunsAndWaits.queue.length).toEqual(1); - + expect(foo).toEqual(0); specWithRunsAndWaits.execute(); - - expect(specWithRunsAndWaits.queue.length).toEqual(6); + expect(foo).toEqual(1); + fakeTimer.tick(500); + expect(foo).toEqual(2); + fakeTimer.tick(500); + expect(foo).toEqual(3); }); it("should run asynchronous tests", function () { var foo = 0; var a_spec; - env.describe('test async spec', function() { - a_spec = env.it('simple queue test', function () { - this.runs(function () { - foo++; - }); - this.runs(function () { - this.expect(foo).toEqual(1); - }); - }); - }); - - expect(a_spec.queue.length).toEqual(1, - 'Expected spec queue length to be 1, was ' + a_spec.queue.length); - - a_spec.execute(); - expect(a_spec.queue.length).toEqual(3, - 'Expected spec queue length to be 3, was ' + a_spec.queue.length); - - foo = 0; env.describe('test async spec', function() { a_spec = env.it('spec w/ queued statments', function () { this.runs(function () { @@ -206,15 +191,12 @@ describe("jasmine spec running", function () { }); }); - expect(another_spec.queue.length).toEqual(1); another_spec.execute(); fakeTimer.tick(1000); - expect(another_spec.queue.length).toEqual(6); expect(another_spec.results.getItems().length).toEqual(1); - expect(another_spec.results.getItems()[0].passed).toEqual(true); var baz = 0; @@ -237,7 +219,6 @@ describe("jasmine spec running", function () { yet_another_spec.execute(); fakeTimer.tick(250); - expect(yet_another_spec.queue.length).toEqual(4); expect(yet_another_spec.results.getItems().length).toEqual(1); expect(yet_another_spec.results.getItems()[0].passed).toEqual(false); }); @@ -267,7 +248,6 @@ describe("jasmine spec running", function () { another_spec.execute(); fakeTimer.tick(2000); - expect(another_spec.queue.length).toEqual(6); expect(another_spec.results.getItems().length).toEqual(1); expect(another_spec.results.getItems()[0].passed).toEqual(true); }); @@ -385,28 +365,6 @@ describe("jasmine spec running", function () { expect(suite.description).toEqual('one suite description'); }); - it('should add tests to suites declared by the passed function', function() { - suite = env.describe('one suite description', function () { - env.it('should be a test'); - }); - - expect(suite.specs.length).toEqual(1); - expect(suite.specs[0].queue.length).toEqual(0); - }); - - it('should enqueue functions for multipart tests', function() { - suite = env.describe('one suite description', function () { - env.it('should be a test with queuedFunctions', function() { - this.runs(function() { - var foo = 0; - foo++; - }); - }); - }); - - expect(suite.specs[0].queue.length).toEqual(1); - }); - it('should enqueue functions for multipart tests and support waits, and run any ready runs() blocks', function() { var foo = 0; var bar = 0; @@ -423,9 +381,7 @@ describe("jasmine spec running", function () { }); }); - expect(suite.specs[0].queue.length).toEqual(1); suite.execute(); - expect(suite.specs[0].queue.length).toEqual(4); expect(foo).toEqual(1); expect(bar).toEqual(0); diff --git a/src/Queue.js b/src/Queue.js new file mode 100644 index 00000000..26165f60 --- /dev/null +++ b/src/Queue.js @@ -0,0 +1,29 @@ +jasmine.Queue = function() { + this.blocks = []; +}; + +jasmine.Queue.prototype.add = function(block) { + this.setNextOnLastInQueue(block); + this.blocks.push(block); +}; + +jasmine.Queue.prototype.start = function(onComplete) { + if (this.blocks[0]) { + this.blocks[0].execute(); + } else { + onComplete(); + } +}; + +/** + * @private + */ +jasmine.Queue.prototype.setNextOnLastInQueue = function (block) { + if (this.blocks.length > 0) { + var previousBlock = this.blocks[this.blocks.length - 1]; + previousBlock.next = function() { + block.execute(); + }; + } +}; + diff --git a/src/Spec.js b/src/Spec.js index 16335e57..026c3458 100644 --- a/src/Spec.js +++ b/src/Spec.js @@ -11,7 +11,7 @@ jasmine.Spec = function(env, suite, description) { this.env = env; this.suite = suite; this.description = description; - this.queue = []; + this.queue = new jasmine.Queue(); this.finished = false; this.afterCallbacks = []; this.spies_ = []; @@ -31,15 +31,10 @@ jasmine.Spec.prototype.getResults = function() { jasmine.Spec.prototype.runs = function (func) { var block = new jasmine.Block(this.env, func, this); - this.addToQueue(block); + this.queue.add(block); return this; }; -jasmine.Spec.prototype.addToQueue = function(block) { - this.setNextOnLastInQueue(block); - this.queue.push(block); -}; - /** * @private * @deprecated @@ -55,33 +50,15 @@ 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) { var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); - this.addToQueue(waitsFunc); + this.queue.add(waitsFunc); return this; }; -/** - * @private - */ jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); - this.addToQueue(waitsForFunc); + this.queue.add(waitsForFunc); return this; }; @@ -122,24 +99,21 @@ jasmine.Spec.prototype.after = function(doAfter) { }; jasmine.Spec.prototype.execute = function() { - if (!this.env.specFilter(this)) { - this.results.skipped = true; - this.finishCallback(); - this.finished = true; + var spec = this; + if (!spec.env.specFilter(spec)) { + spec.results.skipped = true; + spec.finishCallback(); + spec.finished = true; return; } - this.env.currentSpec = this; - this.env.currentlyRunningTests = true; + spec.env.currentSpec = spec; + spec.env.currentlyRunningTests = true; - this.safeExecuteBefores(); + spec.safeExecuteBefores(); - if (this.queue[0]) { - this.queue[0].execute(); - } else { - this.finish(); - } - this.env.currentlyRunningTests = false; + spec.queue.start(function () { spec.finish(); }); + spec.env.currentlyRunningTests = false; }; jasmine.Spec.prototype.safeExecuteBefores = function() {