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

@@ -88,5 +88,5 @@ jasmine.ActionCollection.prototype.waitForDone = function(action) {
self.env.clearInterval(id);
afterExecute();
}
}, 150);
}, 15000);
};

View File

@@ -12,7 +12,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
};
@@ -23,7 +23,7 @@ jasmine.Block.prototype.execute = function() {
} catch (e) {
this.fail(e);
}
this.next();
this._next();
};
jasmine.Block.prototype.fail = function(e) {

View File

@@ -45,7 +45,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);
}
@@ -76,7 +76,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) {

View File

@@ -75,6 +75,6 @@ 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;
});
}

View File

@@ -1,5 +1,9 @@
jasmine.Queue = function() {
jasmine.Queue = function(onComplete) {
this.blocks = [];
this.onComplete = function () {
onComplete();
};
this.index = 0;
};
jasmine.Queue.prototype.add = function(block) {
@@ -7,11 +11,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();
}
};
@@ -19,11 +30,28 @@ 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;
};

View File

@@ -25,7 +25,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;
};

View File

@@ -7,18 +7,22 @@
* @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() {
@@ -44,7 +48,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);
@@ -87,17 +91,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)) {
@@ -112,7 +122,7 @@ jasmine.Spec.prototype.execute = function() {
spec.safeExecuteBefores();
spec.queue.start(function () { spec.finish(); });
spec.queue.start();
spec.env.currentlyRunningTests = false;
};
@@ -121,7 +131,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]);
}
}
@@ -135,7 +145,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) {

View File

@@ -8,17 +8,16 @@
* @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;
@@ -28,8 +27,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;
if (this.parentSuite) {
this.parentSuite.next();
}
};
jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) {
@@ -43,13 +46,25 @@ 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._next = function () {
this.next();
};
jasmine.Suite.prototype.next = function() {
if (this.queue.isComplete()) {
this.finish();
} else {
this.queue._next();
}
};

View File

@@ -9,6 +9,6 @@ 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);
};

View File

@@ -18,19 +18,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);