runner-wide beforeEach, afterEach support

This commit is contained in:
ragaskar
2009-09-28 16:23:21 -07:00
parent 2588368231
commit a394b564f4
26 changed files with 1659 additions and 1436 deletions

View File

@@ -6,7 +6,7 @@
jasmine.Env = function() {
this.currentSpec = null;
this.currentSuite = null;
this.currentRunner = new jasmine.Runner(this);
this.currentRunner_ = new jasmine.Runner(this);
this.currentlyRunningTests = false;
this.reporter = new jasmine.MultiReporter();
@@ -64,7 +64,7 @@ jasmine.Env.prototype.addReporter = function(reporter) {
};
jasmine.Env.prototype.execute = function() {
this.currentRunner.execute();
this.currentRunner_.execute();
};
jasmine.Env.prototype.describe = function(description, specDefinitions) {
@@ -74,7 +74,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
if (parentSuite) {
parentSuite.add(suite);
} else {
this.currentRunner.add(suite);
this.currentRunner_.add(suite);
}
this.currentSuite = suite;
@@ -87,11 +87,24 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
};
jasmine.Env.prototype.beforeEach = function(beforeEachFunction) {
this.currentSuite.beforeEach(beforeEachFunction);
if (this.currentSuite) {
this.currentSuite.beforeEach(beforeEachFunction);
} else {
this.currentRunner_.beforeEach(beforeEachFunction);
}
};
jasmine.Env.prototype.currentRunner = function () {
return this.currentRunner_;
};
jasmine.Env.prototype.afterEach = function(afterEachFunction) {
this.currentSuite.afterEach(afterEachFunction);
if (this.currentSuite) {
this.currentSuite.afterEach(afterEachFunction);
} else {
this.currentRunner_.afterEach(afterEachFunction);
}
};
jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) {

View File

@@ -8,6 +8,8 @@ jasmine.Runner = function(env) {
var self = this;
self.env = env;
self.queue = new jasmine.Queue(env);
self.before_ = [];
self.after_ = [];
self.suites_ = [];
};
@@ -21,6 +23,17 @@ jasmine.Runner.prototype.execute = function() {
});
};
jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) {
beforeEachFunction.typeName = 'beforeEach';
this.before_.push(beforeEachFunction);
};
jasmine.Runner.prototype.afterEach = function(afterEachFunction) {
afterEachFunction.typeName = 'afterEach';
this.after_.push(afterEachFunction);
};
jasmine.Runner.prototype.finishCallback = function() {
this.env.reporter.reportRunnerResults(this);
};

View File

@@ -9,10 +9,12 @@
jasmine.Spec = function(env, suite, description) {
if (!env) {
throw new Error('jasmine.Env() required');
};
}
;
if (!suite) {
throw new Error('jasmine.Suite() required');
};
}
;
var spec = this;
spec.id = env.nextSpecId ? env.nextSpecId() : null;
spec.env = env;
@@ -118,10 +120,10 @@ jasmine.Spec.prototype.finish = function(onComplete) {
jasmine.Spec.prototype.after = function(doAfter, test) {
if (this.queue.isRunning()) {
if (this.queue.isRunning()) {
this.queue.add(new jasmine.Block(this.env, doAfter, this));
} else {
this.afterCallbacks.unshift(doAfter);
this.afterCallbacks.unshift(doAfter);
}
};
@@ -146,21 +148,26 @@ jasmine.Spec.prototype.execute = function(onComplete) {
};
jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
var runner = this.env.currentRunner();
for (var suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.beforeQueue) {
for (var i = 0; i < suite.beforeQueue.length; i++)
this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this));
for (var i = 0; i < suite.before_.length; i++) {
this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
}
}
for (var i = 0; i < runner.before_.length; i++) {
this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
}
for (i = 0; i < this.afterCallbacks.length; i++) {
this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
}
for (suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.afterQueue) {
for (var j = 0; j < suite.afterQueue.length; j++)
this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this));
for (var i = 0; i < suite.after_.length; i++) {
this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
}
}
for (var i = 0; i < runner.after_.length; i++) {
this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
}
};
jasmine.Spec.prototype.explodes = function() {

View File

@@ -14,8 +14,8 @@ jasmine.Suite = function(env, description, specDefinitions, parentSuite) {
self.queue = new jasmine.Queue(env);
self.parentSuite = parentSuite;
self.env = env;
self.beforeQueue = [];
self.afterQueue = [];
self.before_ = [];
self.after_ = [];
self.specs_ = [];
};
@@ -37,12 +37,12 @@ jasmine.Suite.prototype.finish = function(onComplete) {
jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) {
beforeEachFunction.typeName = 'beforeEach';
this.beforeQueue.push(beforeEachFunction);
this.before_.push(beforeEachFunction);
};
jasmine.Suite.prototype.afterEach = function(afterEachFunction) {
afterEachFunction.typeName = 'afterEach';
this.afterQueue.push(afterEachFunction);
this.after_.push(afterEachFunction);
};
/** @deprecated */
@@ -56,7 +56,7 @@ jasmine.Suite.prototype.results = function() {
jasmine.Suite.prototype.add = function(block) {
if (block instanceof jasmine.Suite) {
this.env.currentRunner.addSuite(block);
this.env.currentRunner().addSuite(block);
} else {
this.specs_.push(block);
}