jasmine/spec/suites/TrivialNodeReporterSpec.js

349 lines
13 KiB
JavaScript
Raw Normal View History

describe("TrivialNodeReporter", function() {
2011-02-28 22:51:03 +00:00
//keep these literal. otherwise the test loses value as a test.
function green(str) { return '\033[32m' + str + '\033[0m'; }
function red(str) { return '\033[31m' + str + '\033[0m'; }
function yellow(str) { return '\033[33m' + str + '\033[0m'; }
2011-02-28 23:13:46 +00:00
function prefixGreen(str) { return '\033[32m' + str; }
function prefixRed(str) { return '\033[31m' + str; }
var newline = "\n";
2011-03-01 00:50:54 +00:00
var passingSpec = { results: function(){ return {passed: function(){return true;}}; } },
failingSpec = { results: function(){ return {passed: function(){return false;}}; } },
skippedSpec = { results: function(){ return {skipped: true}; } },
passingRun = { results: function(){ return {failedCount: 0, specs: function(){return [null, null, null];}}; } },
failingRun = { results: function(){ return {failedCount: 7, specs: function(){return [null, null, null];}}; } };
2011-02-28 22:39:59 +00:00
2011-02-28 22:51:03 +00:00
function repeatedlyInvoke(f, times) { for(var i=0; i<times; i++) f(times+1); }
2011-02-28 22:39:59 +00:00
function repeat(thing, times) {
var arr = [];
for(var i=0; i<times; i++) arr.push(thing);
return arr;
}
2011-02-28 22:51:03 +00:00
var fiftyRedFs = repeat(red("F"), 50).join(""),
fiftyGreenDots = repeat(green("."), 50).join("");
2011-02-28 22:39:59 +00:00
2011-03-01 01:50:16 +00:00
function simulateRun(reporter, specResults, suiteResults, finalRunner, startTime, endTime) {
reporter.reportRunnerStarting();
for(var i=0; i<specResults.length; i++) reporter.reportSpecResults(specResults[i]);
for(i=0; i<suiteResults.length; i++) reporter.reportSuiteResults(suiteResults[i]);
reporter.runnerStartTime = startTime;
reporter.now = function(){return endTime;};
reporter.reportRunnerResults(finalRunner);
}
beforeEach(function() {
this.fakeSys = (function(){
var output = "";
return {
puts:function(str) {output += str + "\n";},
print:function(str) {output += str;},
2011-02-28 23:31:48 +00:00
getOutput:function(){return output;},
clear: function(){output = "";}
};
})();
this.reporter = new jasmine.TrivialNodeReporter(this.fakeSys);
});
2011-02-28 22:51:03 +00:00
2011-03-01 01:50:16 +00:00
describe('Integration test', function(){
it("prints the proper output under a pass scenario. small numbers.", function(){
simulateRun(this.reporter,
repeat(passingSpec, 3),
[],
{
results:function(){
return {
specs: function(){return [null, null, null];},
totalCount: 7,
failedCount: 0
};
}
},
1000,
1777);
expect(this.fakeSys.getOutput()).toEqual(
[
"Started",
green(".") + green(".") + green("."),
"",
"Finished in 0.777 seconds",
green("3 specs, 7 assertions, 0 failures"),
""
].join("\n") + "\n"
);
});
it("prints the proper output under a pass scenario. large numbers.", function(){
simulateRun(this.reporter,
repeat(passingSpec, 57),
[],
{
results:function(){
return {
specs: function(){return [null, null, null];},
totalCount: 7,
failedCount: 0
};
}
},
1000,
1777);
expect(this.fakeSys.getOutput()).toEqual(
[
"Started",
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(".") +
green(".") + green(".") + green(".") + green(".") + green(".") +
green(".") + green(".") + green(".") + green(".") + green(".") +
green(".") + green(".") + green(".") + green(".") + green(".") + "\n" +
green(".") + green(".") + green(".") + green(".") + green(".") +
green(".") + green("."),
"",
"Finished in 0.777 seconds",
green("3 specs, 7 assertions, 0 failures"),
""
].join("\n") + "\n"
);
});
});
describe('A Test Run', function(){
2011-02-28 22:56:09 +00:00
describe('Starts', function(){
it("prints Started", function(){
this.reporter.reportRunnerStarting();
expect(this.fakeSys.getOutput()).toEqual(
"Started" + newline
);
});
});
describe('A spec runs', function(){
it("prints a green dot if the spec passes", function(){
this.reporter.reportSpecResults(passingSpec);
expect(this.fakeSys.getOutput()).toEqual(
green(".")
);
});
2011-02-28 21:45:22 +00:00
it("prints a red dot if the spec fails", function(){
this.reporter.reportSpecResults(failingSpec);
expect(this.fakeSys.getOutput()).toEqual(
2011-02-28 21:46:30 +00:00
red("F")
2011-02-28 21:45:22 +00:00
);
});
2011-02-28 22:51:03 +00:00
it("prints a yellow star if the spec was skipped", function(){
this.reporter.reportSpecResults(skippedSpec);
expect(this.fakeSys.getOutput()).toEqual(
yellow("*")
);
});
2011-02-28 22:39:59 +00:00
});
2011-02-28 21:45:22 +00:00
2011-02-28 22:56:09 +00:00
describe('Many specs run', function(){
2011-02-28 22:39:59 +00:00
it("starts a new line every 50 specs", function(){
var self = this;
repeatedlyInvoke(function(){self.reporter.reportSpecResults(failingSpec);}, 49);
expect(this.fakeSys.getOutput()).
toEqual(repeat(red("F"), 49).join(""));
repeatedlyInvoke(function(){self.reporter.reportSpecResults(failingSpec);}, 3);
expect(this.fakeSys.getOutput()).
toEqual(fiftyRedFs + newline +
red("F") + red("F"));
repeatedlyInvoke(function(){self.reporter.reportSpecResults(failingSpec);}, 48);
repeatedlyInvoke(function(){self.reporter.reportSpecResults(passingSpec);}, 2);
2011-02-28 22:51:03 +00:00
2011-02-28 22:39:59 +00:00
expect(this.fakeSys.getOutput()).
toEqual(fiftyRedFs + newline +
fiftyRedFs + newline +
green(".") + green("."));
});
});
2011-02-28 23:13:46 +00:00
describe('A suite runs', function(){
it("remembers suite results", function(){
var emptyResults = function(){return {items_:[]};};
2011-03-01 00:50:54 +00:00
this.reporter.reportSuiteResults({description:"Oven", results:emptyResults});
this.reporter.reportSuiteResults({description:"Mixer", results:emptyResults});
2011-03-01 00:50:54 +00:00
var self = this;
var descriptions = [];
for(var i=0; i<self.reporter.suiteResults.length; i++)
2011-03-01 00:50:54 +00:00
descriptions.push(self.reporter.suiteResults[i].description);
2011-03-01 00:50:54 +00:00
expect(descriptions).toEqual(["Oven", "Mixer"]);
});
it("creates a description out of the current suite and any parent suites", function(){
var emptyResults = function(){return {items_:[]};};
2011-03-01 00:50:54 +00:00
var grandparentSuite = {description:"My house", results:emptyResults};
var parentSuite = {description:"kitchen", parentSuite: grandparentSuite, results:emptyResults};
this.reporter.reportSuiteResults({description:"oven", parentSuite: parentSuite, results:emptyResults});
2011-03-01 00:50:54 +00:00
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(){
this.reporter.reportSuiteResults({description:"Oven",
results:function(){
return {
items_:[
{failedCount:0, description:"specOne"},
{failedCount:99, description:"specTwo"},
{failedCount:0, description:"specThree"},
{failedCount:88, description:"specFour"},
{failedCount:3}
]
2011-03-01 00:50:54 +00:00
};
}});
expect(this.reporter.suiteResults[0].failedSpecResults).
toEqual([
{failedCount:99, description:"specTwo"},
{failedCount:88, description:"specFour"}
2011-03-01 00:50:54 +00:00
]);
});
});
2011-02-28 23:13:46 +00:00
describe('Finishes', function(){
2011-03-01 01:13:04 +00:00
describe('Spec failure information', function(){
it("prints suite and spec descriptions together as a sentence", function(){
this.reporter.suiteResults = [
{description:"The oven", failedSpecResults:[
2011-03-01 01:20:22 +00:00
{description:"heats up", items_:[]},
{description:"cleans itself", items_:[]}
2011-03-01 01:13:04 +00:00
]},
{description:"The mixer", failedSpecResults:[
2011-03-01 01:20:22 +00:00
{description:"blends things together", items_:[]}
2011-03-01 01:13:04 +00:00
]}
2011-03-01 01:20:22 +00:00
];
2011-03-01 01:13:04 +00:00
this.reporter.reportRunnerResults(failingRun);
expect(this.fakeSys.getOutput()).toContain("The oven heats up");
expect(this.fakeSys.getOutput()).toContain("The oven cleans itself");
expect(this.fakeSys.getOutput()).toContain("The mixer blends things together");
});
2011-03-01 01:20:22 +00:00
it("prints stack trace of spec failure", function(){
this.reporter.suiteResults = [
{description:"The oven", failedSpecResults:[
{description:"heats up",
items_:[
{trace:{stack:"stack trace one"}},
{trace:{stack:"stack trace two"}}
]}
]}
];
this.reporter.reportRunnerResults(failingRun);
expect(this.fakeSys.getOutput()).toContain("The oven heats up");
expect(this.fakeSys.getOutput()).toContain("stack trace one");
expect(this.fakeSys.getOutput()).toContain("stack trace two");
});
});
2011-03-01 01:13:04 +00:00
2011-03-01 00:50:54 +00:00
describe('Finished line', function(){
2011-02-28 23:31:48 +00:00
2011-03-01 00:50:54 +00:00
it("prints the elapsed time in the summary message", function(){
this.reporter.now = function(){return 1000;};
this.reporter.reportRunnerStarting();
this.reporter.now = function(){return 1777;};
this.reporter.reportRunnerResults(passingRun);
expect(this.fakeSys.getOutput()).toContain("0.777 seconds");
});
2011-02-28 23:31:48 +00:00
2011-03-01 00:50:54 +00:00
it("prints round time numbers correctly", function(){
var self = this;
function run(startTime, endTime) {
self.fakeSys.clear();
self.reporter.runnerStartTime = startTime;
self.reporter.now = function(){return endTime;};
self.reporter.reportRunnerResults(passingRun);
}
2011-02-28 23:31:48 +00:00
2011-03-01 00:50:54 +00:00
run(1000, 11000);
expect(this.fakeSys.getOutput()).toContain("10 seconds");
2011-02-28 23:31:48 +00:00
2011-03-01 00:50:54 +00:00
run(1000, 2000);
expect(this.fakeSys.getOutput()).toContain("1 seconds");
2011-02-28 23:31:48 +00:00
2011-03-01 00:50:54 +00:00
run(1000, 1100);
expect(this.fakeSys.getOutput()).toContain("0.1 seconds");
run(1000, 1010);
expect(this.fakeSys.getOutput()).toContain("0.01 seconds");
run(1000, 1001);
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");
});
2011-02-28 23:31:48 +00:00
});
2011-03-01 00:50:54 +00:00
describe("specs/assertions/failures summary", function(){
it("prints statistics in green if there were no failures", function() {
this.reporter.reportRunnerResults({
results:function(){return {specs: function(){return [null, null, null];}, totalCount: 7, failedCount: 0};}
});
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");
});
2011-02-28 23:31:48 +00:00
});
2011-02-28 23:13:46 +00:00
});
});
});