Visually group specs by suite.

This commit is contained in:
Aaron Peckham & Christian Williams 2009-08-10 17:50:03 -07:00
parent b1a007dfb5
commit 4b244612c1
6 changed files with 109 additions and 11 deletions

View File

@ -1,4 +1,6 @@
jasmine.TrivialReporter = function() {
jasmine.TrivialReporter = function(doc) {
this.document = doc || document;
this.suiteDivs = {};
};
jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) {
@ -25,6 +27,24 @@ jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarA
return el;
};
jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
var suites = runner.getAllSuites();
for (var i = 0; i < suites.length; i++) {
var suite = suites[i];
var suiteDiv = this.createDom('div', { className: 'suite' },
this.createDom('a', { className: 'runSpec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"),
suite.description);
console.log(suite);
console.log(suite.getFullName());
this.suiteDivs[suite.getFullName()] = suiteDiv;
var parentDiv = this.document.body;
if (suite.parentSuite) {
parentDiv = this.suiteDivs[suite.parentSuite.getFullName()];
}
parentDiv.appendChild(suiteDiv);
}
};
jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) {
};
@ -52,7 +72,7 @@ jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
}
}
document.body.appendChild(specDiv);
this.suiteDivs[spec.suite.getFullName()].appendChild(specDiv);
};
jasmine.TrivialReporter.prototype.log = function() {
@ -60,7 +80,7 @@ jasmine.TrivialReporter.prototype.log = function() {
};
jasmine.TrivialReporter.prototype.getLocation = function() {
return document.location;
return this.document.location;
};
jasmine.TrivialReporter.prototype.specFilter = function(spec) {
@ -72,7 +92,7 @@ jasmine.TrivialReporter.prototype.specFilter = function(spec) {
}
if (!paramMap["spec"]) return true;
return spec.getFullName().indexOf(paramMap["spec"]) > -1;
return spec.getFullName().indexOf(paramMap["spec"]) == 0;
};
//protect against console.log incidents

View File

@ -24,6 +24,12 @@ p {
color: red;
}
.suite {
border: 1px outset gray;
margin: 2px;
padding-left: 1em;
}
.spec {
margin: 5px;
clear: both;

View File

@ -1724,6 +1724,28 @@ jasmine.Runner.prototype.finishCallback = function() {
this.env.reporter.reportRunnerResults(this);
};
jasmine.Runner.prototype.getAllSuites = function() {
var suitesToReturn = [];
function addSuite(suite) {
suitesToReturn.push(suite);
for (var j = 0; j < suite.specs.length; j++) {
var spec = suite.specs[j];
if (spec instanceof jasmine.Suite) {
addSuite(spec);
}
}
}
for (var i = 0; i < this.suites.length; i++) {
var suite = this.suites[i];
addSuite(suite);
}
return suitesToReturn;
};
jasmine.Runner.prototype.getResults = function() {
var results = new jasmine.NestedResults();
for (var i = 0; i < this.suites.length; i++) {

View File

@ -123,4 +123,17 @@ describe('RunnerTest', function() {
expect(fakeReporter.reportRunnerStarting).wasCalledWith(env.currentRunner);
});
it("should return a flat array of all suites, including nested suites", function() {
var suite1, suite2;
suite1 = env.describe("spec 1", function() {
suite2 = env.describe("nested spec", function() {});
});
console.log("runner:", env.currentRunner);
document.runner = env.currentRunner;
var suites = env.currentRunner.getAllSuites();
expect(suites).toEqual([suite1, suite2]);
});
});

View File

@ -1,19 +1,34 @@
describe("TrivialReporter", function() {
var trivialReporter;
var body;
beforeEach(function() {
body = document.createElement("body");
});
function fakeSpec(name) {
return {
getFullName: function() { return name; }
};
}
it("should allow for focused spec running", function() {
var trivialReporter = new jasmine.TrivialReporter();
spyOn(trivialReporter, 'getLocation').andReturn({search: "?spec=run%20this"});
it("should run only specs beginning with spec parameter", function() {
trivialReporter = new jasmine.TrivialReporter({ location: {search: "?spec=run%20this"} });
expect(trivialReporter.specFilter(fakeSpec("run this"))).toBeTruthy();
expect(trivialReporter.specFilter(fakeSpec("not the right spec"))).toBeFalsy();
expect(trivialReporter.specFilter(fakeSpec("not run this"))).toBeFalsy();
});
it("should not run specs that don't match the filter", function() {
var trivialReporter = new jasmine.TrivialReporter();
spyOn(trivialReporter, 'getLocation').andReturn({search: "?spec=run%20this"});
expect(trivialReporter.specFilter(fakeSpec("not the right spec"))).toBeFalsy();
it("should display empty divs for every suite when the runner is starting", function() {
trivialReporter = new jasmine.TrivialReporter({ body: body });
trivialReporter.reportRunnerStarting({
getAllSuites: function() {
return [ new jasmine.Suite(null, "suite 1", null, null) ];
}
});
var divs = body.getElementsByTagName("div");
expect(divs.length).toEqual(1);
expect(divs[0].innerHTML).toContain("suite 1");
});
});

View File

@ -22,6 +22,28 @@ jasmine.Runner.prototype.finishCallback = function() {
this.env.reporter.reportRunnerResults(this);
};
jasmine.Runner.prototype.getAllSuites = function() {
var suitesToReturn = [];
function addSuite(suite) {
suitesToReturn.push(suite);
for (var j = 0; j < suite.specs.length; j++) {
var spec = suite.specs[j];
if (spec instanceof jasmine.Suite) {
addSuite(spec);
}
}
}
for (var i = 0; i < this.suites.length; i++) {
var suite = this.suites[i];
addSuite(suite);
}
return suitesToReturn;
};
jasmine.Runner.prototype.getResults = function() {
var results = new jasmine.NestedResults();
for (var i = 0; i < this.suites.length; i++) {