Queue/Spec refactor

This commit is contained in:
ragaskar
2009-08-01 15:28:39 -07:00
parent b6a41c85e1
commit 9475de28b3
16 changed files with 340 additions and 174 deletions

View File

@@ -35,7 +35,7 @@ jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) {
jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
var specDiv = this.createDom('div', {
className: spec.getResults().passed ? 'spec passed' : 'spec failed'
className: spec.getResults().passed() ? 'spec passed' : 'spec failed'
}, spec.getFullName());
var resultItems = spec.getResults().getItems();

View File

@@ -614,7 +614,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
var parentSuite = this.currentSuite;
if (parentSuite) {
parentSuite.specs.push(suite);
parentSuite.add(suite);
} else {
this.currentRunner.suites.push(suite);
}
@@ -645,7 +645,7 @@ jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) {
jasmine.Env.prototype.it = function(description, func) {
var spec = new jasmine.Spec(this, this.currentSuite, description);
this.currentSuite.specs.push(spec);
this.currentSuite.add(spec);
this.currentSpec = spec;
if (func) {
@@ -844,7 +844,7 @@ jasmine.ActionCollection.prototype.waitForDone = function(action) {
self.env.clearInterval(id);
afterExecute();
}
}, 150);
}, 15000);
};
/** No-op base class for Jasmine reporters.
*
@@ -887,7 +887,7 @@ jasmine.Block = function(env, func, spec) {
this.spec = spec;
};
jasmine.Block.prototype.next = function() {
jasmine.Block.prototype._next = function() {
this.spec.finish(); // default value is to be done after one function
};
@@ -898,7 +898,7 @@ jasmine.Block.prototype.execute = function() {
} catch (e) {
this.fail(e);
}
this.next();
this._next();
};
jasmine.Block.prototype.fail = function(e) {
@@ -1339,9 +1339,9 @@ jasmine.NestedResults.prototype.addResult = function(result) {
/**
* @returns {Boolean} True if <b>everything</b> below passed
*/
jasmine.NestedResults.prototype.__defineGetter__('passed', function() {
jasmine.NestedResults.prototype.passed = function() {
return this.passedCount === this.totalCount;
});
}
/**
* Base class for pretty printing for expectation results.
*/
@@ -1462,8 +1462,12 @@ jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) {
jasmine.StringPrettyPrinter.prototype.append = function(value) {
this.string += value;
};
jasmine.Queue = function() {
jasmine.Queue = function(onComplete) {
this.blocks = [];
this.onComplete = function () {
onComplete();
};
this.index = 0;
};
jasmine.Queue.prototype.add = function(block) {
@@ -1471,11 +1475,18 @@ jasmine.Queue.prototype.add = function(block) {
this.blocks.push(block);
};
jasmine.Queue.prototype.start = function(onComplete) {
jasmine.Queue.prototype.start = function() {
if (this.blocks[0]) {
this.blocks[0].execute();
} else {
onComplete();
this.onComplete();
}
};
jasmine.Queue.prototype._next = function () {
this.index++;
if (this.index < this.blocks.length) {
this.blocks[this.index].execute();
}
};
@@ -1483,14 +1494,31 @@ jasmine.Queue.prototype.start = function(onComplete) {
* @private
*/
jasmine.Queue.prototype.setNextOnLastInQueue = function (block) {
if (this.blocks.length > 0) {
var previousBlock = this.blocks[this.blocks.length - 1];
previousBlock.next = function() {
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();
};
}
};
jasmine.Queue.prototype.isComplete = function () {
return this.index >= (this.blocks.length - 1);
};
jasmine.Queue.prototype.getResults = function () {
var results = [];
for (var i = 0; i < this.blocks.length; i++) {
results.push(this.blocks[i].getResults());
}
return results;
};
/* 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)
@@ -1551,7 +1579,8 @@ jasmine.Runner.prototype.finishCallback = function() {
jasmine.Runner.prototype.getResults = function() {
var results = new jasmine.NestedResults();
for (var i = 0; i < this.suites.length; i++) {
results.rollupCounts(this.suites[i].getResults());
//TODO: FIX
results.rollupCounts(this.suites[i].getResults()[0]);
}
return results;
};
@@ -1564,18 +1593,22 @@ jasmine.Runner.prototype.getResults = function() {
* @param {String} description
*/
jasmine.Spec = function(env, suite, description) {
this.id = env.nextSpecId_++;
this.env = env;
this.suite = suite;
this.description = description;
this.queue = new jasmine.Queue();
this.finished = false;
this.afterCallbacks = [];
this.spies_ = [];
var spec = this;
spec.id = env.nextSpecId_++;
spec.env = env;
spec.suite = suite;
spec.description = description;
spec.queue = new jasmine.Queue(function () {
spec.finish();
});
this.results = new jasmine.NestedResults();
this.results.description = description;
this.matchersClass = null;
spec.finished = false;
spec.afterCallbacks = [];
spec.spies_ = [];
spec.results = new jasmine.NestedResults();
spec.results.description = description;
spec.matchersClass = null;
};
jasmine.Spec.prototype.getFullName = function() {
@@ -1601,7 +1634,7 @@ jasmine.Spec.prototype.expects_that = function(actual) {
};
/**
* @private
* @private
*/
jasmine.Spec.prototype.expect = function(actual) {
return new (this.getMatchersClass_())(this.env, actual, this.results);
@@ -1644,17 +1677,23 @@ jasmine.Spec.prototype.finishCallback = function() {
};
jasmine.Spec.prototype.finish = function() {
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();
}
};
jasmine.Spec.prototype.after = function(doAfter) {
this.afterCallbacks.unshift(doAfter);
};
jasmine.Spec.prototype.execute = function() {
var spec = this;
if (!spec.env.specFilter(spec)) {
@@ -1669,7 +1708,7 @@ jasmine.Spec.prototype.execute = function() {
spec.safeExecuteBefores();
spec.queue.start(function () { spec.finish(); });
spec.queue.start();
spec.env.currentlyRunningTests = false;
};
@@ -1678,7 +1717,7 @@ jasmine.Spec.prototype.safeExecuteBefores = function() {
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]);
befores.push(suite.beforeQueue[i]);
}
}
@@ -1692,7 +1731,7 @@ jasmine.Spec.prototype.safeExecuteAfters = function() {
for (var suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.afterQueue) {
for (var i = 0; i < suite.afterQueue.length; i++)
afters.unshift(suite.afterQueue[i]);
afters.unshift(suite.afterQueue[i]);
}
}
while (afters.length) {
@@ -1755,17 +1794,16 @@ jasmine.Spec.prototype.removeAllSpies = function() {
* @param {jasmine.Suite} parentSuite
*/
jasmine.Suite = function(env, description, specDefinitions, parentSuite) {
jasmine.ActionCollection.call(this, env);
this.id = env.nextSuiteId_++;
this.description = description;
this.specs = this.actions;
this.parentSuite = parentSuite;
this.beforeQueue = [];
this.afterQueue = [];
var self = this;
self.id = env.nextSuiteId_++;
self.description = description;
self.queue = new jasmine.Queue(function () { self.finish(); });
self.parentSuite = parentSuite;
self.env = env;
self.beforeQueue = [];
self.afterQueue = [];
};
jasmine.util.inherit(jasmine.Suite, jasmine.ActionCollection);
jasmine.Suite.prototype.getFullName = function() {
var fullName = this.description;
@@ -1775,8 +1813,12 @@ jasmine.Suite.prototype.getFullName = function() {
return fullName;
};
jasmine.Suite.prototype.finishCallback = function() {
jasmine.Suite.prototype.finish = function() {
this.env.reporter.reportSuiteResults(this);
this.finished = true;
for (var suite = this; suite; suite = suite.parentSuite) {
suite.finished = true;
}
};
jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) {
@@ -1790,16 +1832,30 @@ jasmine.Suite.prototype.afterEach = function(afterEachFunction) {
};
jasmine.Suite.prototype.getResults = function() {
var results = new jasmine.NestedResults();
results.description = this.description;
results.id = this.id;
for (var i = 0; i < this.specs.length; i++) {
results.rollupCounts(this.specs[i].getResults());
}
return results;
return this.queue.getResults();
};
jasmine.Suite.prototype.add = function(block) {
this.queue.add(block);
};
jasmine.Suite.prototype.execute = function() {
this.queue.start();
};
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);
@@ -1811,7 +1867,7 @@ 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._next();
}, self.timeout);
};
jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) {
@@ -1834,19 +1890,19 @@ jasmine.WaitsForBlock.prototype.execute = function () {
latchFunctionResult = self.latchFunction.apply(self.spec);
} catch (e) {
self.fail(e);
self.next();
self._next();
return;
}
if (latchFunctionResult) {
self.next();
self._next();
} else if (self.totalTimeSpentWaitingForLatch >= self.timeout) {
var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.message || 'something to happen');
self.fail({
name: 'timeout',
message: message
});
self.spec.next();
self.spec._next();
} else {
self.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
self.env.setTimeout(function () { self.execute(); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);