Refactor Spec block execution into Queue

This commit is contained in:
ragaskar 2009-08-01 14:56:29 -07:00
parent d5489a3e0d
commit f73fd8ae95
5 changed files with 97 additions and 134 deletions

View File

@ -1462,6 +1462,35 @@ jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) {
jasmine.StringPrettyPrinter.prototype.append = function(value) { jasmine.StringPrettyPrinter.prototype.append = function(value) {
this.string += 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 /* JasmineReporters.reporter
* Base object that will get called whenever a Spec, Suite, or Runner is done. It is up to * 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) * 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.env = env;
this.suite = suite; this.suite = suite;
this.description = description; this.description = description;
this.queue = []; this.queue = new jasmine.Queue();
this.finished = false; this.finished = false;
this.afterCallbacks = []; this.afterCallbacks = [];
this.spies_ = []; this.spies_ = [];
@ -1559,15 +1588,10 @@ jasmine.Spec.prototype.getResults = function() {
jasmine.Spec.prototype.runs = function (func) { jasmine.Spec.prototype.runs = function (func) {
var block = new jasmine.Block(this.env, func, this); var block = new jasmine.Block(this.env, func, this);
this.addToQueue(block); this.queue.add(block);
return this; return this;
}; };
jasmine.Spec.prototype.addToQueue = function(block) {
this.setNextOnLastInQueue(block);
this.queue.push(block);
};
/** /**
* @private * @private
* @deprecated * @deprecated
@ -1583,33 +1607,15 @@ jasmine.Spec.prototype.expect = function(actual) {
return new (this.getMatchersClass_())(this.env, actual, this.results); 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) { jasmine.Spec.prototype.waits = function(timeout) {
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
this.addToQueue(waitsFunc); this.queue.add(waitsFunc);
return this; return this;
}; };
/**
* @private
*/
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
this.addToQueue(waitsForFunc); this.queue.add(waitsForFunc);
return this; return this;
}; };
@ -1650,24 +1656,21 @@ jasmine.Spec.prototype.after = function(doAfter) {
}; };
jasmine.Spec.prototype.execute = function() { jasmine.Spec.prototype.execute = function() {
if (!this.env.specFilter(this)) { var spec = this;
this.results.skipped = true; if (!spec.env.specFilter(spec)) {
this.finishCallback(); spec.results.skipped = true;
this.finished = true; spec.finishCallback();
spec.finished = true;
return; return;
} }
this.env.currentSpec = this; spec.env.currentSpec = spec;
this.env.currentlyRunningTests = true; spec.env.currentlyRunningTests = true;
this.safeExecuteBefores(); spec.safeExecuteBefores();
if (this.queue[0]) { spec.queue.start(function () { spec.finish(); });
this.queue[0].execute(); spec.env.currentlyRunningTests = false;
} else {
this.finish();
}
this.env.currentlyRunningTests = false;
}; };
jasmine.Spec.prototype.safeExecuteBefores = function() { jasmine.Spec.prototype.safeExecuteBefores = function() {

View File

@ -18,6 +18,7 @@
<script type="text/javascript" src="../src/Reporters.js"></script> <script type="text/javascript" src="../src/Reporters.js"></script>
<script type="text/javascript" src="../src/Runner.js"></script> <script type="text/javascript" src="../src/Runner.js"></script>
<script type="text/javascript" src="../src/Spec.js"></script> <script type="text/javascript" src="../src/Spec.js"></script>
<script type="text/javascript" src="../src/Queue.js"></script>
<script type="text/javascript" src="../src/Suite.js"></script> <script type="text/javascript" src="../src/Suite.js"></script>
<script type="text/javascript" src="../src/mock-timeout.js"></script> <script type="text/javascript" src="../src/mock-timeout.js"></script>

View File

@ -96,51 +96,36 @@ describe("jasmine spec running", function () {
it('should queue waits and runs that it encounters while executing specs', function() { it('should queue waits and runs that it encounters while executing specs', function() {
var specWithRunsAndWaits; var specWithRunsAndWaits;
var foo = 0;
env.describe('test async spec', function() { env.describe('test async spec', function() {
specWithRunsAndWaits = env.it('spec w/ queued statments', function () { specWithRunsAndWaits = env.it('spec w/ queued statments', function () {
this.runs(function () { this.runs(function () {
foo++;
}); });
this.waits(500); this.waits(500);
this.runs(function () { this.runs(function () {
foo++;
}); });
this.waits(500); this.waits(500);
this.runs(function () { this.runs(function () {
foo++;
}); });
}); });
}); });
expect(specWithRunsAndWaits.queue.length).toEqual(1); expect(foo).toEqual(0);
specWithRunsAndWaits.execute(); specWithRunsAndWaits.execute();
expect(foo).toEqual(1);
expect(specWithRunsAndWaits.queue.length).toEqual(6); fakeTimer.tick(500);
expect(foo).toEqual(2);
fakeTimer.tick(500);
expect(foo).toEqual(3);
}); });
it("should run asynchronous tests", function () { it("should run asynchronous tests", function () {
var foo = 0; var foo = 0;
var a_spec; 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() { env.describe('test async spec', function() {
a_spec = env.it('spec w/ queued statments', function () { a_spec = env.it('spec w/ queued statments', function () {
this.runs(function () { this.runs(function () {
@ -206,15 +191,12 @@ describe("jasmine spec running", function () {
}); });
}); });
expect(another_spec.queue.length).toEqual(1);
another_spec.execute(); another_spec.execute();
fakeTimer.tick(1000); fakeTimer.tick(1000);
expect(another_spec.queue.length).toEqual(6);
expect(another_spec.results.getItems().length).toEqual(1); expect(another_spec.results.getItems().length).toEqual(1);
expect(another_spec.results.getItems()[0].passed).toEqual(true); expect(another_spec.results.getItems()[0].passed).toEqual(true);
var baz = 0; var baz = 0;
@ -237,7 +219,6 @@ describe("jasmine spec running", function () {
yet_another_spec.execute(); yet_another_spec.execute();
fakeTimer.tick(250); 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().length).toEqual(1);
expect(yet_another_spec.results.getItems()[0].passed).toEqual(false); expect(yet_another_spec.results.getItems()[0].passed).toEqual(false);
}); });
@ -267,7 +248,6 @@ describe("jasmine spec running", function () {
another_spec.execute(); another_spec.execute();
fakeTimer.tick(2000); fakeTimer.tick(2000);
expect(another_spec.queue.length).toEqual(6);
expect(another_spec.results.getItems().length).toEqual(1); expect(another_spec.results.getItems().length).toEqual(1);
expect(another_spec.results.getItems()[0].passed).toEqual(true); 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'); 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() { it('should enqueue functions for multipart tests and support waits, and run any ready runs() blocks', function() {
var foo = 0; var foo = 0;
var bar = 0; var bar = 0;
@ -423,9 +381,7 @@ describe("jasmine spec running", function () {
}); });
}); });
expect(suite.specs[0].queue.length).toEqual(1);
suite.execute(); suite.execute();
expect(suite.specs[0].queue.length).toEqual(4);
expect(foo).toEqual(1); expect(foo).toEqual(1);
expect(bar).toEqual(0); expect(bar).toEqual(0);

29
src/Queue.js Normal file
View File

@ -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();
};
}
};

View File

@ -11,7 +11,7 @@ jasmine.Spec = function(env, suite, description) {
this.env = env; this.env = env;
this.suite = suite; this.suite = suite;
this.description = description; this.description = description;
this.queue = []; this.queue = new jasmine.Queue();
this.finished = false; this.finished = false;
this.afterCallbacks = []; this.afterCallbacks = [];
this.spies_ = []; this.spies_ = [];
@ -31,15 +31,10 @@ jasmine.Spec.prototype.getResults = function() {
jasmine.Spec.prototype.runs = function (func) { jasmine.Spec.prototype.runs = function (func) {
var block = new jasmine.Block(this.env, func, this); var block = new jasmine.Block(this.env, func, this);
this.addToQueue(block); this.queue.add(block);
return this; return this;
}; };
jasmine.Spec.prototype.addToQueue = function(block) {
this.setNextOnLastInQueue(block);
this.queue.push(block);
};
/** /**
* @private * @private
* @deprecated * @deprecated
@ -55,33 +50,15 @@ jasmine.Spec.prototype.expect = function(actual) {
return new (this.getMatchersClass_())(this.env, actual, this.results); 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) { jasmine.Spec.prototype.waits = function(timeout) {
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
this.addToQueue(waitsFunc); this.queue.add(waitsFunc);
return this; return this;
}; };
/**
* @private
*/
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
this.addToQueue(waitsForFunc); this.queue.add(waitsForFunc);
return this; return this;
}; };
@ -122,24 +99,21 @@ jasmine.Spec.prototype.after = function(doAfter) {
}; };
jasmine.Spec.prototype.execute = function() { jasmine.Spec.prototype.execute = function() {
if (!this.env.specFilter(this)) { var spec = this;
this.results.skipped = true; if (!spec.env.specFilter(spec)) {
this.finishCallback(); spec.results.skipped = true;
this.finished = true; spec.finishCallback();
spec.finished = true;
return; return;
} }
this.env.currentSpec = this; spec.env.currentSpec = spec;
this.env.currentlyRunningTests = true; spec.env.currentlyRunningTests = true;
this.safeExecuteBefores(); spec.safeExecuteBefores();
if (this.queue[0]) { spec.queue.start(function () { spec.finish(); });
this.queue[0].execute(); spec.env.currentlyRunningTests = false;
} else {
this.finish();
}
this.env.currentlyRunningTests = false;
}; };
jasmine.Spec.prototype.safeExecuteBefores = function() { jasmine.Spec.prototype.safeExecuteBefores = function() {