Refactor Runner to use Queue; remove ActionCollection

This commit is contained in:
ragaskar 2009-08-12 22:12:28 -07:00
parent 0061054aaa
commit 7b63960db0
8 changed files with 106 additions and 275 deletions

View File

@ -2,7 +2,7 @@ desc 'Builds lib/jasmine from source'
task :build do
# these files must be loaded first
sources = ["src/base.js", "src/util.js", "src/Env.js", "src/ActionCollection.js", "src/Reporter.js", "src/Block.js"]
sources = ["src/base.js", "src/util.js", "src/Env.js", "src/Reporter.js", "src/Block.js"]
sources += Dir.glob('src/*.js').reject{|f| sources.include?(f)}.sort

View File

@ -616,7 +616,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
if (parentSuite) {
parentSuite.add(suite);
} else {
this.currentRunner.suites.push(suite);
this.currentRunner.add(suite);
}
this.currentSuite = suite;
@ -754,98 +754,6 @@ jasmine.Env.prototype.contains_ = function(haystack, needle) {
jasmine.Env.prototype.addEqualityTester = function(equalityTester) {
this.equalityTesters_.push(equalityTester);
};
/**
* base for Runner & Suite: allows for a queue of functions to get executed, allowing for
* any one action to complete, including asynchronous calls, before going to the next
* action.
*
* @constructor
* @param {jasmine.Env} env
*/
jasmine.ActionCollection = function(env) {
this.env = env;
this.actions = [];
this.index = 0;
this.finished = false;
};
/**
* Marks the collection as done & calls the finish callback, if there is one
*/
jasmine.ActionCollection.prototype.finish = function() {
if (this.finishCallback) {
this.finishCallback();
}
this.finished = true;
};
/**
* Starts executing the queue of functions/actions.
*/
jasmine.ActionCollection.prototype.execute = function() {
if (this.actions.length > 0) {
this.next();
}
};
/**
* Gets the current action.
*/
jasmine.ActionCollection.prototype.getCurrentAction = function() {
return this.actions[this.index];
};
/**
* Executes the next queued function/action. If there are no more in the queue, calls #finish.
*/
jasmine.ActionCollection.prototype.next = function() {
if (this.index >= this.actions.length) {
this.finish();
return;
}
var currentAction = this.getCurrentAction();
currentAction.execute(this);
if (currentAction.afterCallbacks) {
for (var i = 0; i < currentAction.afterCallbacks.length; i++) {
try {
currentAction.afterCallbacks[i]();
} catch (e) {
alert(e);
}
}
}
this.waitForDone(currentAction);
};
jasmine.ActionCollection.prototype.waitForDone = function(action) {
var self = this;
var afterExecute = function afterExecute() {
self.index++;
self.next();
};
if (action.finished) {
var now = new Date().getTime();
if (this.env.updateInterval && now - this.env.lastUpdate > this.env.updateInterval) {
this.env.lastUpdate = now;
this.env.setTimeout(afterExecute, 0);
} else {
afterExecute();
}
return;
}
var id = this.env.setInterval(function() {
if (action.finished) {
self.env.clearInterval(id);
afterExecute();
}
}, 150);
};
/** No-op base class for Jasmine reporters.
*
* @constructor
@ -1562,27 +1470,35 @@ jasmine.Reporters.reporter = function(callbacks) {
* @param {jasmine.Env} env
*/
jasmine.Runner = function(env) {
jasmine.ActionCollection.call(this, env);
this.suites = this.actions;
var self = this;
self.env = env;
self.queue = new jasmine.Queue();
};
jasmine.util.inherit(jasmine.Runner, jasmine.ActionCollection);
jasmine.Runner.prototype.execute = function() {
if (this.env.reporter.reportRunnerStarting) {
this.env.reporter.reportRunnerStarting(this);
var self = this;
if (self.env.reporter.reportRunnerStarting) {
self.env.reporter.reportRunnerStarting(this);
}
jasmine.ActionCollection.prototype.execute.call(this);
self.queue.start(function () { self.finishCallback(); });
};
jasmine.Runner.prototype.finishCallback = function() {
this.env.reporter.reportRunnerResults(this);
};
jasmine.Runner.prototype.add = function(block) {
this.queue.add(block);
};
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()[0]);
var runnerResults = this.queue.getResults();
for (var i=0; i < runnerResults.length; i++) {
var suiteResults = runnerResults[i];
for (var j=0; j < suiteResults.length; j++) {
results.rollupCounts(suiteResults[j]);
}
}
return results;
};
@ -1789,7 +1705,7 @@ jasmine.Suite = function(env, description, specDefinitions, parentSuite) {
var self = this;
self.id = env.nextSuiteId_++;
self.description = description;
self.queue = new jasmine.Queue(function () { self.finish(); });
self.queue = new jasmine.Queue();
self.parentSuite = parentSuite;
self.env = env;
self.beforeQueue = [];

View File

@ -3,26 +3,25 @@
<html>
<head>
<title>Jasmine Test Runner</title>
<script type="text/javascript" src="../src/base.js"></script>
<script type="text/javascript" src="../src/util.js"></script>
<script type="text/javascript" src="../src/Env.js"></script>
<script type="text/javascript" src="../src/ActionCollection.js"></script>
<script type="text/javascript" src="../src/Reporter.js"></script>
<script type="text/javascript" src="../src/Matchers.js"></script>
<script type="text/javascript" src="../src/MultiReporter.js"></script>
<script type="text/javascript" src="../src/NestedResults.js"></script>
<script type="text/javascript" src="../src/PrettyPrinter.js"></script>
<script type="text/javascript" src="../src/Block.js"></script>
<script type="text/javascript" src="../src/WaitsBlock.js"></script>
<script type="text/javascript" src="../src/WaitsForBlock.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/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>
<script type="text/javascript" src="../src/base.js"></script>
<script type="text/javascript" src="../src/util.js"></script>
<script type="text/javascript" src="../src/Env.js"></script>
<script type="text/javascript" src="../src/Reporter.js"></script>
<script type="text/javascript" src="../src/Matchers.js"></script>
<script type="text/javascript" src="../src/MultiReporter.js"></script>
<script type="text/javascript" src="../src/NestedResults.js"></script>
<script type="text/javascript" src="../src/PrettyPrinter.js"></script>
<script type="text/javascript" src="../src/Block.js"></script>
<script type="text/javascript" src="../src/WaitsBlock.js"></script>
<script type="text/javascript" src="../src/WaitsForBlock.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/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>
<script type="text/javascript" src="../lib/TrivialReporter.js"></script>
<script type="text/javascript" src="../lib/TrivialReporter.js"></script>
<script type="text/javascript">

View File

@ -1,24 +1,15 @@
describe('RunnerTest', function() {
var fakeTimer;
var env;
beforeEach(function () {
beforeEach(function() {
env = new jasmine.Env();
});
it('should be able to add a suite', function() {
env.describe('one suite description', function () {
env.it('should be a test');
});
expect(env.currentRunner.suites.length).toEqual(1); // "Runner expected one suite, got " + env.currentRunner.suites.length);
});
it('should be able to push multiple suites', function() {
env.describe('one suite description', function () {
env.it('should be a test');
});
env.describe('another suite description', function () {
env.it('should be a test');
});
expect(env.currentRunner.suites.length).toEqual(2); //"Runner expected two suites, but got " + env.currentRunner.suites.length);
fakeTimer = new jasmine.FakeTimer();
env.setTimeout = fakeTimer.setTimeout;
env.clearTimeout = fakeTimer.clearTimeout;
env.setInterval = fakeTimer.setInterval;
env.clearInterval = fakeTimer.clearInterval;
});
it('should run child suites and specs and generate results when execute is called', function() {
@ -41,11 +32,9 @@ describe('RunnerTest', function() {
env.currentRunner.execute();
var runnerResults = env.currentRunner.getResults();
console.error('runnerResults', runnerResults)
expect(runnerResults.totalCount).toEqual(2);
expect(runnerResults.passedCount).toEqual(1);
expect(runnerResults.failedCount).toEqual(1);
});
@ -68,9 +57,10 @@ describe('RunnerTest', function() {
env.currentRunner.execute();
expect(env.currentRunner.suites.length).toEqual(1); // "Runner expected 1 suite, got " + env.currentRunner.suites.length);
expect(env.currentRunner.suites[0].getResults()[0].passed()).toEqual(false); // "Runner should have run specs in first suite");
expect(env.currentRunner.suites[1]).toEqual(undefined); // "Second suite should be undefined, but was " + reporter.toJSON(env.currentRunner.suites[1]));
var runnerResults = env.currentRunner.getResults();
expect(runnerResults.totalCount).toEqual(1);
expect(runnerResults.passedCount).toEqual(0);
expect(runnerResults.failedCount).toEqual(1);
});
it('should roll up results from all specs', function() {
@ -99,32 +89,42 @@ describe('RunnerTest', function() {
expect(results.failedCount).toEqual(1);
});
it('should set the finished flag when #finished is called', function(){
env.currentRunner.finish();
expect(env.currentRunner.finished).toEqual(true);
describe('reporting', function () {
var fakeReporter;
beforeEach(function () {
fakeReporter = jasmine.createSpyObj("fakeReporter", ["log", "reportRunnerStarting", "reportRunnerResults"]);
env.addReporter(fakeReporter);
});
it('should call the finish callback when the runner is finished', function() {
var foo = 0;
it('should report runner results when the runner has completed running', function() {
env.describe('one suite description', function () {
env.it('should be a test', function() {
this.runs(function () {
this.expect(true).toEqual(true);
});
});
});
env.currentRunner.finishCallback = function() {
foo++;
};
env.describe('another suite description', function () {
env.it('should be another test', function() {
this.waits(200);
this.runs(function () {
this.expect(true).toEqual(false);
});
});
});
env.currentRunner.finish();
expect(env.currentRunner.finished).toEqual(true);
expect(foo).toEqual(1);
env.currentRunner.execute();
expect(fakeReporter.reportRunnerResults).wasNotCalled();
fakeTimer.tick(200);
expect(fakeReporter.reportRunnerResults).wasCalledWith(env.currentRunner);
});
it("should report when the tests start running", function() {
var fakeReporter = jasmine.createSpyObj("fakeReporter", ["log", "reportRunnerStarting"]);
env.addReporter(fakeReporter);
var runner = new jasmine.Runner(env);
runner.execute();
expect(fakeReporter.reportRunnerStarting).wasNotCalled();
env.currentRunner.execute();
expect(fakeReporter.reportRunnerStarting).wasCalledWith(env.currentRunner);
});
});
});

View File

@ -1,92 +0,0 @@
/**
* base for Runner & Suite: allows for a queue of functions to get executed, allowing for
* any one action to complete, including asynchronous calls, before going to the next
* action.
*
* @constructor
* @param {jasmine.Env} env
*/
jasmine.ActionCollection = function(env) {
this.env = env;
this.actions = [];
this.index = 0;
this.finished = false;
};
/**
* Marks the collection as done & calls the finish callback, if there is one
*/
jasmine.ActionCollection.prototype.finish = function() {
if (this.finishCallback) {
this.finishCallback();
}
this.finished = true;
};
/**
* Starts executing the queue of functions/actions.
*/
jasmine.ActionCollection.prototype.execute = function() {
if (this.actions.length > 0) {
this.next();
}
};
/**
* Gets the current action.
*/
jasmine.ActionCollection.prototype.getCurrentAction = function() {
return this.actions[this.index];
};
/**
* Executes the next queued function/action. If there are no more in the queue, calls #finish.
*/
jasmine.ActionCollection.prototype.next = function() {
if (this.index >= this.actions.length) {
this.finish();
return;
}
var currentAction = this.getCurrentAction();
currentAction.execute(this);
if (currentAction.afterCallbacks) {
for (var i = 0; i < currentAction.afterCallbacks.length; i++) {
try {
currentAction.afterCallbacks[i]();
} catch (e) {
alert(e);
}
}
}
this.waitForDone(currentAction);
};
jasmine.ActionCollection.prototype.waitForDone = function(action) {
var self = this;
var afterExecute = function afterExecute() {
self.index++;
self.next();
};
if (action.finished) {
var now = new Date().getTime();
if (this.env.updateInterval && now - this.env.lastUpdate > this.env.updateInterval) {
this.env.lastUpdate = now;
this.env.setTimeout(afterExecute, 0);
} else {
afterExecute();
}
return;
}
var id = this.env.setInterval(function() {
if (action.finished) {
self.env.clearInterval(id);
afterExecute();
}
}, 150);
};

View File

@ -47,7 +47,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
if (parentSuite) {
parentSuite.add(suite);
} else {
this.currentRunner.suites.push(suite);
this.currentRunner.add(suite);
}
this.currentSuite = suite;

View File

@ -5,27 +5,35 @@
* @param {jasmine.Env} env
*/
jasmine.Runner = function(env) {
jasmine.ActionCollection.call(this, env);
this.suites = this.actions;
var self = this;
self.env = env;
self.queue = new jasmine.Queue();
};
jasmine.util.inherit(jasmine.Runner, jasmine.ActionCollection);
jasmine.Runner.prototype.execute = function() {
if (this.env.reporter.reportRunnerStarting) {
this.env.reporter.reportRunnerStarting(this);
var self = this;
if (self.env.reporter.reportRunnerStarting) {
self.env.reporter.reportRunnerStarting(this);
}
jasmine.ActionCollection.prototype.execute.call(this);
self.queue.start(function () { self.finishCallback(); });
};
jasmine.Runner.prototype.finishCallback = function() {
this.env.reporter.reportRunnerResults(this);
};
jasmine.Runner.prototype.add = function(block) {
this.queue.add(block);
};
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()[0]);
var runnerResults = this.queue.getResults();
for (var i=0; i < runnerResults.length; i++) {
var suiteResults = runnerResults[i];
for (var j=0; j < suiteResults.length; j++) {
results.rollupCounts(suiteResults[j]);
}
}
return results;
};

View File

@ -11,7 +11,7 @@ jasmine.Suite = function(env, description, specDefinitions, parentSuite) {
var self = this;
self.id = env.nextSuiteId_++;
self.description = description;
self.queue = new jasmine.Queue(function () { self.finish(); });
self.queue = new jasmine.Queue();
self.parentSuite = parentSuite;
self.env = env;
self.beforeQueue = [];