From a6aa9c652b299e14ac569969c64eab8f9e5a66a9 Mon Sep 17 00:00:00 2001 From: Christian Williams Date: Wed, 8 Jul 2009 17:55:25 -0700 Subject: [PATCH] Allow for registration of multiple Reporter with Jasmine. --- lib/jasmine.js | 44 +++++++++++++++++++++++++++++--- spec/runner.html | 5 ++-- spec/suites/MultiReporterTest.js | 13 ++++++++++ src/Env.js | 14 ++++++++++ src/MultiReporter.js | 24 +++++++++++++++++ src/Spec.js | 4 +-- src/Suite.js | 2 +- 7 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 spec/suites/MultiReporterTest.js create mode 100644 src/MultiReporter.js diff --git a/lib/jasmine.js b/lib/jasmine.js index 79be747..899340f 100644 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -567,12 +567,18 @@ jasmine.util.argsToArray = function(args) { return arrayOfArgs; }; +/** + * Environment for Jasmine + * @ + */ jasmine.Env = function() { this.currentSpec = null; this.currentSuite = null; this.currentRunner = new jasmine.Runner(this); this.currentlyRunningTests = false; + this.reporter = new jasmine.MultiReporter(); + this.updateInterval = 0; this.lastUpdate = 0; this.specFilter = function() { @@ -589,6 +595,14 @@ jasmine.Env.prototype.clearTimeout = jasmine.clearTimeout; jasmine.Env.prototype.setInterval = jasmine.setInterval; jasmine.Env.prototype.clearInterval = jasmine.clearInterval; +/** + * Register a reporter to receive status updates from Jasmine. + * @param {Object} reporter An object which will receive status updates. + */ +jasmine.Env.prototype.addReporter = function(reporter) { + this.reporter.addReporter(reporter); +}; + jasmine.Env.prototype.execute = function() { this.currentRunner.execute(); }; @@ -1260,6 +1274,30 @@ window.clearInterval = function(timeoutKey) { return jasmine.Clock.installed.clearInterval.apply(this, arguments); }; +/** + * @constructor + */ +jasmine.MultiReporter = function() { + this.subReporters_ = []; +}; + +jasmine.MultiReporter.prototype.addReporter = function(reporter) { + this.subReporters_.push(reporter); +}; + +(function() { + var functionNames = ["reportRunnerResults", "reportSuiteResults", "reportSpecResults", "log"]; + for (var i = 0; i < functionNames.length; i++) { + var functionName = functionNames[i]; + jasmine.MultiReporter.prototype[functionName] = (function(functionName) { + return function() { + for (var j = 0; j < this.subReporters_.length; j++) { + this.subReporters_[j][functionName].apply(this.subReporters_[j], arguments); + } + }; + })(functionName); + } +})(); /** * Holds results for a set of Jasmine spec. Allows for the results array to hold another jasmine.NestedResults * @@ -1602,8 +1640,8 @@ jasmine.Runner.prototype.getResults = function() { }; /** * Internal representation of a Jasmine specification, or test. - * @private - * @constructs + * + * @constructor * @param {jasmine.Env} env * @param {jasmine.Suite} suite * @param {String} description @@ -1806,7 +1844,7 @@ jasmine.Spec.prototype.removeAllSpies = function() { }; /** - * For storing & executing a Jasmine suite. + * Internal representation of a Jasmine suite. * * @constructor * @param {jasmine.Env} env diff --git a/spec/runner.html b/spec/runner.html index fe4bbd8..e09e0ad 100644 --- a/spec/runner.html +++ b/spec/runner.html @@ -11,6 +11,7 @@ + @@ -28,6 +29,7 @@ jasmine.include('suites/ExceptionsTest.js', true); jasmine.include('suites/JsonReporterTest.js', true); jasmine.include('suites/MatchersTest.js', true); + jasmine.include('suites/MultiReporterTest.js', true); jasmine.include('suites/NestedResultsTest.js', true); jasmine.include('suites/PrettyPrintTest.js', true); jasmine.include('suites/ReporterTest.js', true); @@ -66,9 +68,8 @@ var jasmineEnv = jasmine.getEnv(); - jasmineEnv.reporter = new jasmine.TrivialReporter(); + jasmineEnv.addReporter(new jasmine.TrivialReporter()); jasmineEnv.execute(); - console.log('env', jasmineEnv); diff --git a/spec/suites/MultiReporterTest.js b/spec/suites/MultiReporterTest.js new file mode 100644 index 0000000..cb4174e --- /dev/null +++ b/spec/suites/MultiReporterTest.js @@ -0,0 +1,13 @@ +describe("MultiReporter", function() { + it("should delegate to any and all subreporters", function() { + var multiReporter = new jasmine.MultiReporter(); + var fakeReporter1 = jasmine.createSpyObj("fakeReporter1", ["reportSpecResults"]); + var fakeReporter2 = jasmine.createSpyObj("fakeReporter2", ["reportSpecResults"]); + multiReporter.addReporter(fakeReporter1); + multiReporter.addReporter(fakeReporter2); + + multiReporter.reportSpecResults('blah', 'foo'); + expect(fakeReporter1.reportSpecResults).wasCalledWith('blah', 'foo'); + expect(fakeReporter2.reportSpecResults).wasCalledWith('blah', 'foo'); + }); +}); \ No newline at end of file diff --git a/src/Env.js b/src/Env.js index 0708a8c..77d2742 100644 --- a/src/Env.js +++ b/src/Env.js @@ -1,9 +1,15 @@ +/** + * Environment for Jasmine + * @ + */ jasmine.Env = function() { this.currentSpec = null; this.currentSuite = null; this.currentRunner = new jasmine.Runner(this); this.currentlyRunningTests = false; + this.reporter = new jasmine.MultiReporter(); + this.updateInterval = 0; this.lastUpdate = 0; this.specFilter = function() { @@ -20,6 +26,14 @@ jasmine.Env.prototype.clearTimeout = jasmine.clearTimeout; jasmine.Env.prototype.setInterval = jasmine.setInterval; jasmine.Env.prototype.clearInterval = jasmine.clearInterval; +/** + * Register a reporter to receive status updates from Jasmine. + * @param {Object} reporter An object which will receive status updates. + */ +jasmine.Env.prototype.addReporter = function(reporter) { + this.reporter.addReporter(reporter); +}; + jasmine.Env.prototype.execute = function() { this.currentRunner.execute(); }; diff --git a/src/MultiReporter.js b/src/MultiReporter.js new file mode 100644 index 0000000..0f49401 --- /dev/null +++ b/src/MultiReporter.js @@ -0,0 +1,24 @@ +/** + * @constructor + */ +jasmine.MultiReporter = function() { + this.subReporters_ = []; +}; + +jasmine.MultiReporter.prototype.addReporter = function(reporter) { + this.subReporters_.push(reporter); +}; + +(function() { + var functionNames = ["reportRunnerResults", "reportSuiteResults", "reportSpecResults", "log"]; + for (var i = 0; i < functionNames.length; i++) { + var functionName = functionNames[i]; + jasmine.MultiReporter.prototype[functionName] = (function(functionName) { + return function() { + for (var j = 0; j < this.subReporters_.length; j++) { + this.subReporters_[j][functionName].apply(this.subReporters_[j], arguments); + } + }; + })(functionName); + } +})(); diff --git a/src/Spec.js b/src/Spec.js index 3138da0..d4d0361 100644 --- a/src/Spec.js +++ b/src/Spec.js @@ -1,7 +1,7 @@ /** * Internal representation of a Jasmine specification, or test. - * @private - * @constructs + * + * @constructor * @param {jasmine.Env} env * @param {jasmine.Suite} suite * @param {String} description diff --git a/src/Suite.js b/src/Suite.js index aa206d4..fe43110 100644 --- a/src/Suite.js +++ b/src/Suite.js @@ -1,5 +1,5 @@ /** - * For storing & executing a Jasmine suite. + * Internal representation of a Jasmine suite. * * @constructor * @param {jasmine.Env} env