TrivialConsoleReporter refactor & fixes. No longer limitied to 50 columns of dots.

This commit is contained in:
Davis W. Frank 2011-05-16 08:08:12 -07:00
parent b02aa9840a
commit 1d65d56a92
4 changed files with 407 additions and 384 deletions

View File

@ -43,7 +43,7 @@ jasmine.executeSpecs = function(specs, done){
} }
var jasmineEnv = jasmine.getEnv(); var jasmineEnv = jasmine.getEnv();
jasmineEnv.reporter = new jasmine.TrivialConsoleReporter(sys.print, done) jasmineEnv.reporter = new jasmine.TrivialConsoleReporter(sys.print, done);
jasmineEnv.execute(); jasmineEnv.execute();
}; };

View File

@ -1,23 +1,54 @@
describe("TrivialConsoleReporter", function() { describe("TrivialConsoleReporter", function() {
//keep these literal. otherwise the test loses value as a test. //keep these literal. otherwise the test loses value as a test.
function green(str) { return '\033[32m' + str + '\033[0m'; } function green(str) {
function red(str) { return '\033[31m' + str + '\033[0m'; } return '\033[32m' + str + '\033[0m';
function yellow(str) { return '\033[33m' + str + '\033[0m'; } }
function prefixGreen(str) { return '\033[32m' + str; } function red(str) {
function prefixRed(str) { return '\033[31m' + str; } return '\033[31m' + str + '\033[0m';
}
function yellow(str) {
return '\033[33m' + str + '\033[0m';
}
function prefixGreen(str) {
return '\033[32m' + str;
}
function prefixRed(str) {
return '\033[31m' + str;
}
var newline = "\n"; var newline = "\n";
var passingSpec = { results: function(){ return {passed: function(){return true;}}; } }, var passingSpec = {
failingSpec = { results: function(){ return {passed: function(){return false;}}; } }, results: function() {
skippedSpec = { results: function(){ return {skipped: true}; } }, return {
passingRun = { results: function(){ return {failedCount: 0, items_: [null, null, null]}; } }, passed: function() {
failingRun = { results: function(){ return {failedCount: 7, items_: [null, null, null]}; } }; return true;
}
};
}
},
failingSpec = { results: function() {
return {passed: function() {
return false;
}};
} },
skippedSpec = { results: function() {
return {skipped: true};
} },
passingRun = { results: function() {
return {failedCount: 0, items_: [null, null, null]};
} },
failingRun = { results: function() {
return {failedCount: 7, items_: [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);
}
function repeat(thing, times) { function repeat(thing, times) {
var arr = []; var arr = [];
@ -25,39 +56,49 @@ describe("TrivialConsoleReporter", function() {
return arr; return arr;
} }
var fiftyRedFs = repeat(red("F"), 50).join(""),
fiftyGreenDots = repeat(green("."), 50).join("");
function simulateRun(reporter, specResults, suiteResults, finalRunner, startTime, endTime) { function simulateRun(reporter, specResults, suiteResults, finalRunner, startTime, endTime) {
reporter.reportRunnerStarting(); reporter.reportRunnerStarting();
for(var i=0; i<specResults.length; i++) reporter.reportSpecResults(specResults[i]); for (var i = 0; i < specResults.length; i++) {
for(i=0; i<suiteResults.length; i++) reporter.reportSuiteResults(suiteResults[i]); reporter.reportSpecResults(specResults[i]);
}
for (i = 0; i < suiteResults.length; i++) {
reporter.reportSuiteResults(suiteResults[i]);
}
reporter.runnerStartTime = startTime; reporter.runnerStartTime = startTime;
reporter.now = function(){return endTime;}; reporter.now = function() {
return endTime;
};
reporter.reportRunnerResults(finalRunner); reporter.reportRunnerResults(finalRunner);
} }
var reporter, out, done;
beforeEach(function() { beforeEach(function() {
this.out = (function(){ out = (function() {
var output = ""; var output = "";
return { return {
print:function(str) {output += str;}, print:function(str) {
getOutput:function(){return output;}, output += str;
clear: function(){output = "";} },
getOutput:function() {
return output;
},
clear: function() {
output = "";
}
}; };
})(); })();
this.done = false done = false;
var self = this reporter = new jasmine.TrivialConsoleReporter(out.print, function(runner) {
this.reporter = new jasmine.TrivialConsoleReporter(this.out.print, function(runner){ done = true
self.done = true
}); });
}); });
describe('Integration', function() { describe('Integration', function() {
it("prints the proper output under a pass scenario. small numbers.", function() { it("prints the proper output under a pass scenario. small numbers.", function() {
simulateRun(this.reporter, simulateRun(reporter,
repeat(passingSpec, 3), repeat(passingSpec, 3),
[], [],
{ {
@ -70,9 +111,10 @@ describe("TrivialConsoleReporter", function() {
} }
}, },
1000, 1000,
1777); 1777
);
expect(this.out.getOutput()).toEqual( expect(out.getOutput()).toEqual(
[ [
"Started", "Started",
green(".") + green(".") + green("."), green(".") + green(".") + green("."),
@ -85,7 +127,7 @@ describe("TrivialConsoleReporter", function() {
}); });
it("prints the proper output under a pass scenario. large numbers.", function() { it("prints the proper output under a pass scenario. large numbers.", function() {
simulateRun(this.reporter, simulateRun(reporter,
repeat(passingSpec, 57), repeat(passingSpec, 57),
[], [],
{ {
@ -100,7 +142,7 @@ describe("TrivialConsoleReporter", function() {
1000, 1000,
1777); 1777);
expect(this.out.getOutput()).toEqual( expect(out.getOutput()).toEqual(
[ [
"Started", "Started",
@ -113,7 +155,7 @@ describe("TrivialConsoleReporter", function() {
green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") +
green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") +
green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") + green(".") +
green(".") + green(".") + green(".") + green(".") + green(".") + newline + green(".") + green(".") + green(".") + green(".") + green(".") +
green(".") + green(".") + green(".") + green(".") + green(".") + //7 green dots green(".") + green(".") + green(".") + green(".") + green(".") + //7 green dots
green(".") + green("."), green(".") + green("."),
@ -128,9 +170,10 @@ describe("TrivialConsoleReporter", function() {
it("prints the proper output under a failure scenario.", function() { it("prints the proper output under a failure scenario.", function() {
simulateRun(this.reporter, simulateRun(reporter,
[failingSpec, passingSpec, failingSpec], [failingSpec, passingSpec, failingSpec],
[{description:"The oven", [
{description:"The oven",
results:function() { results:function() {
return { return {
items_:[ items_:[
@ -168,7 +211,7 @@ describe("TrivialConsoleReporter", function() {
1000, 1000,
1777); 1777);
expect(this.out.getOutput()).toEqual( expect(out.getOutput()).toEqual(
[ [
"Started", "Started",
red("F") + green(".") + red("F"), red("F") + green(".") + red("F"),
@ -187,98 +230,76 @@ describe("TrivialConsoleReporter", function() {
].join("\n") + "\n" ].join("\n") + "\n"
); );
}); });
}); });
describe('A Test Run', function(){ describe('When a Jasmine environment executes', function() {
beforeEach(function() {
describe('Starts', function(){ reporter.reportRunnerStarting();
it("prints Started", function(){ });
this.reporter.reportRunnerStarting();
it("should print 'Started' to the console", function() {
expect(this.out.getOutput()).toEqual( expect(out.getOutput()).toEqual("Started" + newline);
"Started" + newline });
);
}); describe('when a spec reports', function() {
beforeEach(function() {
out.clear();
}); });
describe('A spec runs', function(){
it("prints a green dot if the spec passes", function() { it("prints a green dot if the spec passes", function() {
this.reporter.reportSpecResults(passingSpec); reporter.reportSpecResults(passingSpec);
expect(this.out.getOutput()).toEqual( expect(out.getOutput()).toEqual(green("."));
green(".")
);
}); });
it("prints a red dot if the spec fails", function() { it("prints a red dot if the spec fails", function() {
this.reporter.reportSpecResults(failingSpec); reporter.reportSpecResults(failingSpec);
expect(this.out.getOutput()).toEqual( expect(out.getOutput()).toEqual(red("F"));
red("F")
);
}); });
it("prints a yellow star if the spec was skipped", function() { it("prints a yellow star if the spec was skipped", function() {
this.reporter.reportSpecResults(skippedSpec); reporter.reportSpecResults(skippedSpec);
expect(this.out.getOutput()).toEqual( expect(out.getOutput()).toEqual(yellow("*"));
yellow("*")
);
}); });
}); });
describe('when a suite reports', function() {
describe('Many specs run', function(){ var emptyResults;
it("starts a new line every 50 specs", function(){ beforeEach(function() {
var self = this; emptyResults = function() {
repeatedlyInvoke(function(){self.reporter.reportSpecResults(failingSpec);}, 49); return {
items_:[]
expect(this.out.getOutput()). };
toEqual(repeat(red("F"), 49).join("")); };
repeatedlyInvoke(function(){self.reporter.reportSpecResults(failingSpec);}, 3);
expect(this.out.getOutput()).
toEqual(fiftyRedFs + newline +
red("F") + red("F"));
repeatedlyInvoke(function(){self.reporter.reportSpecResults(failingSpec);}, 48);
repeatedlyInvoke(function(){self.reporter.reportSpecResults(passingSpec);}, 2);
expect(this.out.getOutput()).
toEqual(fiftyRedFs + newline +
fiftyRedFs + newline +
green(".") + green("."));
});
}); });
describe('A suite runs', function(){
it("remembers suite results", function() { it("remembers suite results", function() {
var emptyResults = function(){return {items_:[]};}; reporter.reportSuiteResults({description: "Oven", results: emptyResults});
this.reporter.reportSuiteResults({description:"Oven", results:emptyResults}); reporter.reportSuiteResults({description: "Mixer", results: emptyResults});
this.reporter.reportSuiteResults({description:"Mixer", results:emptyResults});
var self = this; expect(reporter.suiteResults[0].description).toEqual('Oven');
var descriptions = []; expect(reporter.suiteResults[1].description).toEqual('Mixer');
for(var i=0; i<self.reporter.suiteResults.length; i++)
descriptions.push(self.reporter.suiteResults[i].description);
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 grandparentSuite = {
var grandparentSuite = {description:"My house", results:emptyResults}; description: "My house",
var parentSuite = {description:"kitchen", parentSuite: grandparentSuite, results:emptyResults}; results: emptyResults
this.reporter.reportSuiteResults({description:"oven", parentSuite: parentSuite, results:emptyResults}); };
var parentSuite = {
description: "kitchen",
parentSuite: grandparentSuite,
results: emptyResults
};
reporter.reportSuiteResults({ description: "oven", parentSuite: parentSuite, results: emptyResults });
expect(this.reporter.suiteResults[0].description).toEqual("My house kitchen oven"); expect(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() {
this.reporter.reportSuiteResults({description:"Oven", reporter.reportSuiteResults({description:"Oven",
results: function() { results: function() {
return { return {
items_:[ items_:[
@ -291,7 +312,7 @@ describe("TrivialConsoleReporter", function() {
}; };
}}); }});
expect(this.reporter.suiteResults[0].failedSpecResults). expect(reporter.suiteResults[0].failedSpecResults).
toEqual([ toEqual([
{ failedCount: 99, description: "specTwo" }, { failedCount: 99, description: "specTwo" },
{ failedCount: 88, description: "specFour" } { failedCount: 88, description: "specFour" }
@ -300,12 +321,12 @@ describe("TrivialConsoleReporter", function() {
}); });
describe('Finishes', function(){ describe('and finishes', function() {
describe('Spec failure information', function(){ describe('when reporting spec failure information', function() {
it("prints suite and spec descriptions together as a sentence", function() { it("prints suite and spec descriptions together as a sentence", function() {
this.reporter.suiteResults = [ reporter.suiteResults = [
{description:"The oven", failedSpecResults:[ {description:"The oven", failedSpecResults:[
{description:"heats up", items_:[]}, {description:"heats up", items_:[]},
{description:"cleans itself", items_:[]} {description:"cleans itself", items_:[]}
@ -315,15 +336,15 @@ describe("TrivialConsoleReporter", function() {
]} ]}
]; ];
this.reporter.reportRunnerResults(failingRun); reporter.reportRunnerResults(failingRun);
expect(this.out.getOutput()).toContain("The oven heats up"); expect(out.getOutput()).toContain("The oven heats up");
expect(this.out.getOutput()).toContain("The oven cleans itself"); expect(out.getOutput()).toContain("The oven cleans itself");
expect(this.out.getOutput()).toContain("The mixer blends things together"); expect(out.getOutput()).toContain("The mixer blends things together");
}); });
it("prints stack trace of spec failure", function() { it("prints stack trace of spec failure", function() {
this.reporter.suiteResults = [ reporter.suiteResults = [
{description:"The oven", failedSpecResults:[ {description:"The oven", failedSpecResults:[
{description:"heats up", {description:"heats up",
items_:[ items_:[
@ -333,97 +354,99 @@ describe("TrivialConsoleReporter", function() {
]} ]}
]; ];
this.reporter.reportRunnerResults(failingRun); reporter.reportRunnerResults(failingRun);
expect(this.out.getOutput()).toContain("The oven heats up"); expect(out.getOutput()).toContain("The oven heats up");
expect(this.out.getOutput()).toContain("stack trace one"); expect(out.getOutput()).toContain("stack trace one");
expect(this.out.getOutput()).toContain("stack trace two"); expect(out.getOutput()).toContain("stack trace two");
}); });
}); });
describe('Finished line', function(){ describe('when reporting the execution time', function() {
it("prints the elapsed time in the summary message", function(){ it("prints the full finished message", function() {
this.reporter.now = function(){return 1000;}; reporter.now = function() {
this.reporter.reportRunnerStarting(); return 1000;
this.reporter.now = function(){return 1777;}; };
this.reporter.reportRunnerResults(passingRun); reporter.reportRunnerStarting();
expect(this.out.getOutput()).toContain("0.777 seconds"); reporter.now = function() {
return 1777;
};
reporter.reportRunnerResults(failingRun);
expect(out.getOutput()).toContain("Finished in 0.777 seconds");
}); });
it("prints round time numbers correctly", function() { it("prints round time numbers correctly", function() {
var self = this;
function run(startTime, endTime) { function run(startTime, endTime) {
self.out.clear(); out.clear();
self.reporter.runnerStartTime = startTime; reporter.runnerStartTime = startTime;
self.reporter.now = function(){return endTime;}; reporter.now = function() {
self.reporter.reportRunnerResults(passingRun); return endTime;
};
reporter.reportRunnerResults(passingRun);
} }
run(1000, 11000); run(1000, 11000);
expect(this.out.getOutput()).toContain("10 seconds"); expect(out.getOutput()).toContain("10 seconds");
run(1000, 2000); run(1000, 2000);
expect(this.out.getOutput()).toContain("1 seconds"); expect(out.getOutput()).toContain("1 seconds");
run(1000, 1100); run(1000, 1100);
expect(this.out.getOutput()).toContain("0.1 seconds"); expect(out.getOutput()).toContain("0.1 seconds");
run(1000, 1010); run(1000, 1010);
expect(this.out.getOutput()).toContain("0.01 seconds"); expect(out.getOutput()).toContain("0.01 seconds");
run(1000, 1001); run(1000, 1001);
expect(this.out.getOutput()).toContain("0.001 seconds"); expect(out.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.out.getOutput()).toContain("Finished in 0.777 seconds");
}); });
}); });
describe("specs/expectations/failures summary", function(){ describe("when reporting the results summary", function() {
it("prints statistics in green if there were no failures", function() { it("prints statistics in green if there were no failures", function() {
this.reporter.reportRunnerResults({ reporter.reportRunnerResults({
results:function(){return {items_: [null, null, null], totalCount: 7, failedCount: 0};} results:function() {
return {items_: [null, null, null], totalCount: 7, failedCount: 0};
}
}); });
expect(this.out.getOutput()). expect(out.getOutput()).
toContain("3 specs, 7 expectations, 0 failures"); toContain("3 specs, 7 expectations, 0 failures");
}); });
it("prints statistics in red if there was a failure", function() { it("prints statistics in red if there was a failure", function() {
this.reporter.reportRunnerResults({ reporter.reportRunnerResults({
results:function(){return {items_: [null, null, null], totalCount: 7, failedCount: 3};} results:function() {
return {items_: [null, null, null], totalCount: 7, failedCount: 3};
}
}); });
expect(this.out.getOutput()). expect(out.getOutput()).
toContain("3 specs, 7 expectations, 3 failures"); toContain("3 specs, 7 expectations, 3 failures");
}); });
it("handles pluralization with 1's ones appropriately", function() { it("handles pluralization with 1's ones appropriately", function() {
this.reporter.reportRunnerResults({ reporter.reportRunnerResults({
results:function(){return {items_: [null], totalCount: 1, failedCount: 1};} results:function() {
return {items_: [null], totalCount: 1, failedCount: 1};
}
}); });
expect(this.out.getOutput()). expect(out.getOutput()).
toContain("1 spec, 1 expectation, 1 failure"); toContain("1 spec, 1 expectation, 1 failure");
}); });
}); });
describe("done callback", function() { describe("done callback", function() {
it("calls back when done", function() { it("calls back when done", function() {
expect(this.done).toBeFalsy(); expect(done).toBeFalsy();
this.reporter.reportRunnerResults({ reporter.reportRunnerResults({
results:function(){return {items_: [null, null, null], totalCount: 7, failedCount: 0};} results:function() {
return {items_: [null, null, null], totalCount: 7, failedCount: 0};
}
}); });
expect(this.done).toBeTruthy(); expect(done).toBeTruthy();
}); });
}); });
}); });
}); });
}); });

View File

@ -29,7 +29,7 @@ jasmine.TrivialConsoleReporter = function(print, doneCallback) {
return arr; return arr;
} }
function indent(str, spaces) { var lines = str.split("\n"); function indent(str, spaces) { var lines = (str || '').split("\n");
var newArr = []; var newArr = [];
for(var i = 0; i < lines.length; i++) { for(var i = 0; i < lines.length; i++) {
newArr.push(repeat(" ", spaces).join("") + lines[i]); newArr.push(repeat(" ", spaces).join("") + lines[i]);
@ -97,7 +97,7 @@ jasmine.TrivialConsoleReporter = function(print, doneCallback) {
} else { } else {
redF(); redF();
} }
startNewLineIfNecessary(); // startNewLineIfNecessary();
}; };
this.suiteResults = []; this.suiteResults = [];