Skip to content

Commit

Permalink
Allow for registration of multiple Reporter with Jasmine.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Williams committed Jul 9, 2009
1 parent 521f839 commit a6aa9c6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
44 changes: 41 additions & 3 deletions lib/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
};
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script type="text/javascript" src="../src/Env.js"></script>
<script type="text/javascript" src="../src/ActionCollection.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/QueuedFunction.js"></script>
Expand All @@ -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);
Expand Down Expand Up @@ -66,9 +68,8 @@


var jasmineEnv = jasmine.getEnv();
jasmineEnv.reporter = new jasmine.TrivialReporter();
jasmineEnv.addReporter(new jasmine.TrivialReporter());
jasmineEnv.execute();
console.log('env', jasmineEnv);
</script>

</body>
Expand Down
13 changes: 13 additions & 0 deletions spec/suites/MultiReporterTest.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
14 changes: 14 additions & 0 deletions src/Env.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();
};
Expand Down
24 changes: 24 additions & 0 deletions src/MultiReporter.js
Original file line number Diff line number Diff line change
@@ -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);
}
})();
4 changes: 2 additions & 2 deletions src/Spec.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Suite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* For storing & executing a Jasmine suite.
* Internal representation of a Jasmine suite.
*
* @constructor
* @param {jasmine.Env} env
Expand Down

0 comments on commit a6aa9c6

Please sign in to comment.