Refactor Spec block execution into Queue
This commit is contained in:
parent
d5489a3e0d
commit
f73fd8ae95
|
@ -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() {
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
<script type="text/javascript" src="../src/Reporters.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/Queue.js"></script>
|
||||
<script type="text/javascript" src="../src/Suite.js"></script>
|
||||
<script type="text/javascript" src="../src/mock-timeout.js"></script>
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
}
|
||||
};
|
||||
|
54
src/Spec.js
54
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() {
|
||||
|
|
Loading…
Reference in New Issue