specs/assertions/failures

This commit is contained in:
Steve Conover 2011-02-28 16:50:54 -08:00
parent 7d2b900b48
commit 882c287191
2 changed files with 97 additions and 77 deletions

View File

@ -11,11 +11,11 @@ describe("TrivialNodeReporter", function() {
var newline = "\n"; var newline = "\n";
var passingSpec = { results: function(){ return {passed:function(){return true;}}; } }, var passingSpec = { results: function(){ return {passed: function(){return true;}}; } },
failingSpec = { results: function(){ return {passed:function(){return false;}}; } }, failingSpec = { results: function(){ return {passed: function(){return false;}}; } },
skippedSpec = { results: function(){ return {skipped:true}; } }, skippedSpec = { results: function(){ return {skipped: true}; } },
passingRun = { results: function(){ return {failedCount: 0}; } }, passingRun = { results: function(){ return {failedCount: 0, specs: function(){return [null, null, null];}}; } },
failingRun = { results: function(){ return {failedCount: 7}; } }; failingRun = { results: function(){ return {failedCount: 7, specs: function(){return [null, null, null];}}; } };
function repeatedlyInvoke(f, times) { for(var i=0; i<times; i++) f(times+1); } function repeatedlyInvoke(f, times) { for(var i=0; i<times; i++) f(times+1); }
@ -109,24 +109,24 @@ describe("TrivialNodeReporter", function() {
describe('A suite runs', function(){ describe('A suite runs', function(){
it("remembers suite results", function(){ it("remembers suite results", function(){
var emptyResults = function(){return {items_:[]};}; var emptyResults = function(){return {items_:[]};};
this.reporter.reportSuiteResults({description:"Oven", results:emptyResults}) this.reporter.reportSuiteResults({description:"Oven", results:emptyResults});
this.reporter.reportSuiteResults({description:"Mixer", results:emptyResults}) this.reporter.reportSuiteResults({description:"Mixer", results:emptyResults});
var self = this var self = this;
var descriptions = [] var descriptions = [];
for(var i=0; i<self.reporter.suiteResults.length; i++) for(var i=0; i<self.reporter.suiteResults.length; i++)
descriptions.push(self.reporter.suiteResults[i].description) descriptions.push(self.reporter.suiteResults[i].description);
expect(descriptions).toEqual(["Oven", "Mixer"]) expect(descriptions).toEqual(["Oven", "Mixer"]);
}); });
it("creates a description out of the current suite and any parent suites", function(){ it("creates a description out of the current suite and any parent suites", function(){
var emptyResults = function(){return {items_:[]};}; var emptyResults = function(){return {items_:[]};};
var grandparentSuite = {description:"My house", results:emptyResults} var grandparentSuite = {description:"My house", results:emptyResults};
var parentSuite = {description:"kitchen", parentSuite: grandparentSuite, results:emptyResults} var parentSuite = {description:"kitchen", parentSuite: grandparentSuite, results:emptyResults};
this.reporter.reportSuiteResults({description:"oven", parentSuite: parentSuite, results:emptyResults}) this.reporter.reportSuiteResults({description:"oven", parentSuite: parentSuite, results:emptyResults});
expect(this.reporter.suiteResults[0].description).toEqual("My house kitchen oven") expect(this.reporter.suiteResults[0].description).toEqual("My house kitchen oven");
}); });
it("gathers failing spec results from the suite. the spec must have a description.", function(){ it("gathers failing spec results from the suite. the spec must have a description.", function(){
@ -140,74 +140,87 @@ describe("TrivialNodeReporter", function() {
{failedCount:88, description:"specFour"}, {failedCount:88, description:"specFour"},
{failedCount:3} {failedCount:3}
] ]
} };
}}) }});
expect(this.reporter.suiteResults[0].failedSpecResults). expect(this.reporter.suiteResults[0].failedSpecResults).
toEqual([ toEqual([
{failedCount:99, description:"specTwo"}, {failedCount:99, description:"specTwo"},
{failedCount:88, description:"specFour"} {failedCount:88, description:"specFour"}
]) ]);
}); });
}); });
describe('Finishes', function(){ describe('Finishes', function(){
it("prints Finished in green if the test run succeeded", function(){ describe('Finished line', function(){
this.reporter.reportRunnerResults(passingRun);
expect(this.fakeSys.getOutput()).toContain(
newline + prefixGreen("Finished")
);
});
it("prints Finished in red if the test run failed", function(){
this.reporter.reportRunnerResults(failingRun);
expect(this.fakeSys.getOutput()).toContain(
newline + prefixRed("Finished")
);
});
it("prints the elapsed time in the summary message", function(){ it("prints the elapsed time in the summary message", function(){
this.reporter.now = function(){return 1000;}; this.reporter.now = function(){return 1000;};
this.reporter.reportRunnerStarting(); this.reporter.reportRunnerStarting();
this.reporter.now = function(){return 1777;}; this.reporter.now = function(){return 1777;};
this.reporter.reportRunnerResults(passingRun); this.reporter.reportRunnerResults(passingRun);
expect(this.fakeSys.getOutput()).toContain("0.777 seconds"); expect(this.fakeSys.getOutput()).toContain("0.777 seconds");
}); });
it("prints round time numbers correctly", function(){ it("prints round time numbers correctly", function(){
var self = this; var self = this;
function run(startTime, endTime) { function run(startTime, endTime) {
self.fakeSys.clear(); self.fakeSys.clear();
self.reporter.runnerStartTime = startTime; self.reporter.runnerStartTime = startTime;
self.reporter.now = function(){return endTime;}; self.reporter.now = function(){return endTime;};
self.reporter.reportRunnerResults(passingRun); self.reporter.reportRunnerResults(passingRun);
} }
run(1000, 11000); run(1000, 11000);
expect(this.fakeSys.getOutput()).toContain("10 seconds"); expect(this.fakeSys.getOutput()).toContain("10 seconds");
run(1000, 2000); run(1000, 2000);
expect(this.fakeSys.getOutput()).toContain("1 seconds"); expect(this.fakeSys.getOutput()).toContain("1 seconds");
run(1000, 1100); run(1000, 1100);
expect(this.fakeSys.getOutput()).toContain("0.1 seconds"); expect(this.fakeSys.getOutput()).toContain("0.1 seconds");
run(1000, 1010); run(1000, 1010);
expect(this.fakeSys.getOutput()).toContain("0.01 seconds"); expect(this.fakeSys.getOutput()).toContain("0.01 seconds");
run(1000, 1001); run(1000, 1001);
expect(this.fakeSys.getOutput()).toContain("0.001 seconds"); expect(this.fakeSys.getOutput()).toContain("0.001 seconds");
});
it("prints the full finished message", function(){
this.reporter.now = function(){return 1000;};
this.reporter.reportRunnerStarting();
this.reporter.now = function(){return 1777;};
this.reporter.reportRunnerResults(failingRun);
expect(this.fakeSys.getOutput()).toContain("Finished in 0.777 seconds");
});
}); });
it("altogether now", function(){ describe("specs/assertions/failures summary", function(){
this.reporter.now = function(){return 1000;}; it("prints statistics in green if there were no failures", function() {
this.reporter.reportRunnerStarting(); this.reporter.reportRunnerResults({
this.reporter.now = function(){return 1777;}; results:function(){return {specs: function(){return [null, null, null];}, totalCount: 7, failedCount: 0};}
this.reporter.reportRunnerResults(failingRun); });
expect(this.fakeSys.getOutput()).toContain(red("Finished in 0.777 seconds")); expect(this.fakeSys.getOutput()).
toContain("3 specs, 7 assertions, 0 failures");
});
it("prints statistics in red if there was a failure", function() {
this.reporter.reportRunnerResults({
results:function(){return {specs: function(){return [null, null, null];}, totalCount: 7, failedCount: 3};}
});
expect(this.fakeSys.getOutput()).
toContain("3 specs, 7 assertions, 3 failures");
});
it("handles pluralization with 1's ones appropriately", function() {
this.reporter.reportRunnerResults({
results:function(){return {specs: function(){return [null];}, totalCount: 1, failedCount: 1};}
});
expect(this.fakeSys.getOutput()).
toContain("1 spec, 1 assertion, 1 failure");
});
}); });
}); });

View File

@ -21,10 +21,16 @@ jasmine.TrivialNodeReporter = function(sys) {
function greenDot() { sys.print(greenStr(".")); } function greenDot() { sys.print(greenStr(".")); }
function redF() { sys.print(redStr("F")); } function redF() { sys.print(redStr("F")); }
function yellowStar() { sys.print(yellowStr("*")); } function yellowStar() { sys.print(yellowStr("*")); }
function finished(colorF, elapsed) { newline(); sys.print(colorF("Finished in " + elapsed/1000 + " seconds")); } function plural(str, count) { return count == 1 ? str : str + "s"; }
function greenFinished(elapsed) { finished(greenStr, elapsed); }
function redFinished(elapsed) { finished(redStr, elapsed); } function finished(elapsed) { newline(); sys.print("Finished in " + elapsed/1000 + " seconds"); }
function summary(colorF, specs, assertions, failed) { newline();
colorF(sys.print(specs + " " + plural("spec", specs) + ", " +
assertions + " " + plural("assertion", assertions) + ", " +
failed + " " + plural("failure", failed))); }
function greenSummary(specs, assertions, failed){ summary(greenStr, specs, assertions, failed); }
function redSummary(specs, assertions, failed){ summary(redStr, specs, assertions, failed); }
@ -41,9 +47,9 @@ jasmine.TrivialNodeReporter = function(sys) {
} }
function fullSuiteDescription(suite) { function fullSuiteDescription(suite) {
var fullDescription = suite.description var fullDescription = suite.description;
if (suite.parentSuite) fullDescription = fullSuiteDescription(suite.parentSuite) + " " + fullDescription if (suite.parentSuite) fullDescription = fullSuiteDescription(suite.parentSuite) + " " + fullDescription ;
return fullDescription return fullDescription;
} }
var startNewLineIfNecessary = lineEnder(defaultColumnsPerLine); var startNewLineIfNecessary = lineEnder(defaultColumnsPerLine);
@ -76,19 +82,20 @@ jasmine.TrivialNodeReporter = function(sys) {
}; };
suite.results().items_.forEach(function(spec){ suite.results().items_.forEach(function(spec){
if (spec.failedCount > 0 && spec.description) suiteResult.failedSpecResults.push(spec) if (spec.failedCount > 0 && spec.description) suiteResult.failedSpecResults.push(spec);
}); });
this.suiteResults.push(suiteResult) this.suiteResults.push(suiteResult);
}; };
this.reportRunnerResults = function(runner) { this.reportRunnerResults = function(runner) {
var elapsed = this.now() - this.runnerStartTime; finished(this.now() - this.runnerStartTime);
if (runner.results().failedCount === 0) { var results = runner.results();
greenFinished(elapsed); if (results.failedCount === 0) {
greenSummary(results.specs().length, results.totalCount, results.failedCount);
} else { } else {
redFinished(elapsed); redSummary(results.specs().length, results.totalCount, results.failedCount);
} }
}; };
}; };