TrivialConsoleReporter now reports the correct number of specs that were executed. TCR is still very heavily mocked instead of using testing an actual Jasmine environment. But now at least the numbers are correct.

This commit is contained in:
Davis W. Frank 2011-05-16 18:28:46 -07:00
parent 1d65d56a92
commit a445a62e7c
3 changed files with 229 additions and 151 deletions

View File

@ -6,24 +6,26 @@ var path = require('path');
// undefined = "diz be undefined yo"; // undefined = "diz be undefined yo";
var jasmineGlobals = require("../src/base"); var jasmineGlobals = require("../src/base");
for(var k in jasmineGlobals) {global[k] = jasmineGlobals[k];} for (var k in jasmineGlobals) {
global[k] = jasmineGlobals[k];
}
//load jasmine src files based on the order in runner.html //load jasmine src files based on the order in runner.html
var srcFilesInProperRequireOrder = []; var srcFilesInProperRequireOrder = [];
var runnerHtmlLines = fs.readFileSync("spec/runner.html", "utf8").split("\n"); var runnerHtmlLines = fs.readFileSync("spec/runner.html", "utf8").split("\n");
var srcFileLines = []; var srcFileLines = [];
for (var i=0; i<runnerHtmlLines.length; i++) for (var i = 0; i < runnerHtmlLines.length; i++)
if (runnerHtmlLines[i].match(/script(.*?)\/src\//)) if (runnerHtmlLines[i].match(/script(.*?)\/src\//))
srcFileLines.push(runnerHtmlLines[i]); srcFileLines.push(runnerHtmlLines[i]);
for (i=0; i<srcFileLines.length; i++) srcFilesInProperRequireOrder.push(srcFileLines[i].match(/src=\"(.*?)\"/)[1]); for (i = 0; i < srcFileLines.length; i++) srcFilesInProperRequireOrder.push(srcFileLines[i].match(/src=\"(.*?)\"/)[1]);
for (i=0; i<srcFilesInProperRequireOrder.length; i++) require(srcFilesInProperRequireOrder[i]); for (i = 0; i < srcFilesInProperRequireOrder.length; i++) require(srcFilesInProperRequireOrder[i]);
/* /*
Pulling in code from jasmine-node. Pulling in code from jasmine-node.
We can't just depend on jasmine-node because it has its own jasmine that it uses. We can't just depend on jasmine-node because it has its own jasmine that it uses.
*/ */
global.window = { global.window = {
setTimeout: setTimeout, setTimeout: setTimeout,
@ -34,61 +36,54 @@ global.window = {
delete global.window; delete global.window;
function noop(){} function noop() {
}
jasmine.executeSpecs = function(specs, done){ jasmine.executeSpecs = function(specs, done) {
for (var i = 0, len = specs.length; i < len; ++i){ for (var i = 0, len = specs.length; i < len; ++i) {
var filename = specs[i]; var filename = specs[i];
require(filename.replace(/\.\w+$/, "")); require(filename.replace(/\.\w+$/, ""));
} }
var jasmineEnv = jasmine.getEnv(); var jasmineEnv = jasmine.getEnv();
jasmineEnv.reporter = new jasmine.TrivialConsoleReporter(sys.print, done); var consoleReporter = new jasmine.TrivialConsoleReporter(sys.print, done);
jasmineEnv.addReporter(consoleReporter);
jasmineEnv.execute(); jasmineEnv.execute();
}; };
jasmine.getAllSpecFiles = function(dir, matcher){ jasmine.getAllSpecFiles = function(dir, matcher) {
var specs = []; var specs = [];
if (fs.statSync(dir).isFile() && dir.match(matcher)) { if (fs.statSync(dir).isFile() && dir.match(matcher)) {
specs.push(dir); specs.push(dir);
} else { } else {
var files = fs.readdirSync(dir); var files = fs.readdirSync(dir);
for (var i = 0, len = files.length; i < len; ++i){ for (var i = 0, len = files.length; i < len; ++i) {
var filename = dir + '/' + files[i]; var filename = dir + '/' + files[i];
if (fs.statSync(filename).isFile() && filename.match(matcher)){ if (fs.statSync(filename).isFile() && filename.match(matcher)) {
specs.push(filename); specs.push(filename);
}else if (fs.statSync(filename).isDirectory()){ } else if (fs.statSync(filename).isDirectory()) {
var subfiles = this.getAllSpecFiles(filename, matcher); var subfiles = this.getAllSpecFiles(filename, matcher);
subfiles.forEach(function(result){ subfiles.forEach(function(result) {
specs.push(result); specs.push(result);
}); });
} }
} }
} }
return specs; return specs;
}; };
jasmine.printRunnerResults = function(runner){ function now() {
var results = runner.results();
var suites = runner.suites();
var msg = '';
msg += suites.length + ' spec' + ((suites.length === 1) ? '' : 's') + ', ';
msg += results.totalCount + ' expectation' + ((results.totalCount === 1) ? '' : 's') + ', ';
msg += results.failedCount + ' failure' + ((results.failedCount === 1) ? '' : 's') + '\n';
return msg;
};
function now(){
return new Date().getTime(); return new Date().getTime();
} }
jasmine.asyncSpecWait = function(){ jasmine.asyncSpecWait = function() {
var wait = jasmine.asyncSpecWait; var wait = jasmine.asyncSpecWait;
wait.start = now(); wait.start = now();
wait.done = false; wait.done = false;
(function innerWait(){ (function innerWait() {
waits(10); waits(10);
runs(function() { runs(function() {
if (wait.start + wait.timeout < now()) { if (wait.start + wait.timeout < now()) {
@ -102,41 +97,37 @@ jasmine.asyncSpecWait = function(){
})(); })();
}; };
jasmine.asyncSpecWait.timeout = 4 * 1000; jasmine.asyncSpecWait.timeout = 4 * 1000;
jasmine.asyncSpecDone = function(){ jasmine.asyncSpecDone = function() {
jasmine.asyncSpecWait.done = true; jasmine.asyncSpecWait.done = true;
}; };
for ( var key in jasmine) { for (var key in jasmine) {
exports[key] = jasmine[key]; exports[key] = jasmine[key];
} }
/* /*
End jasmine-node runner End jasmine-node runner
*/ */
var isVerbose = false; var isVerbose = false;
var showColors = true; var showColors = true;
process.argv.forEach(function(arg){ process.argv.forEach(function(arg) {
switch(arg) { switch (arg) {
case '--color': showColors = true; break; case '--color': showColors = true; break;
case '--noColor': showColors = false; break; case '--noColor': showColors = false; break;
case '--verbose': isVerbose = true; break; case '--verbose': isVerbose = true; break;
} }
}); });
var specs = jasmine.getAllSpecFiles(__dirname + '/suites', new RegExp(".js$")); var specs = jasmine.getAllSpecFiles(__dirname + '/suites', new RegExp(".js$"));
var domIndependentSpecs = []; var domIndependentSpecs = [];
for(var i=0; i<specs.length; i++) { for (var i = 0; i < specs.length; i++) {
if (fs.readFileSync(specs[i], "utf8").indexOf("document.createElement")<0) { if (fs.readFileSync(specs[i], "utf8").indexOf("document.createElement") < 0) {
domIndependentSpecs.push(specs[i]); domIndependentSpecs.push(specs[i]);
} }
} }
jasmine.executeSpecs(domIndependentSpecs, function(runner, log){ jasmine.executeSpecs(domIndependentSpecs, function(runner, log) {
if (runner.results().failedCount === 0) { if (runner.results().failedCount === 0) {
process.exit(0); process.exit(0);
} else { } else {

View File

@ -31,20 +31,37 @@ describe("TrivialConsoleReporter", function() {
}; };
} }
}, },
failingSpec = { results: function() { failingSpec = {
return {passed: function() { results: function() {
return false; return {
}}; passed: function() {
} }, return false;
skippedSpec = { results: function() { }
return {skipped: true}; };
} }, }
passingRun = { results: function() { },
return {failedCount: 0, items_: [null, null, null]}; skippedSpec = {
} }, results: function() {
failingRun = { results: function() { return {skipped: true};
return {failedCount: 7, items_: [null, null, null]}; }
} }; },
passingRun = {
specs: function() {
return [null, null, null];
},
results: function() {
return {failedCount: 0, items_: [null, null, null]};
}
},
failingRun = {
specs: function() {
return [null, null, null];
},
results: function() {
return {
failedCount: 7, items_: [null, null, null]};
}
};
function repeatedlyInvoke(f, times) { function repeatedlyInvoke(f, times) {
for (var i = 0; i < times; i++) f(times + 1); for (var i = 0; i < times; i++) f(times + 1);
@ -101,18 +118,21 @@ describe("TrivialConsoleReporter", function() {
simulateRun(reporter, simulateRun(reporter,
repeat(passingSpec, 3), repeat(passingSpec, 3),
[], [],
{ {
results:function() { specs: function() {
return { return [null, null, null];
items_: [null, null, null],
totalCount: 7,
failedCount: 0
};
}
}, },
results:function() {
return {
items_: [null, null, null],
totalCount: 7,
failedCount: 0
};
}
},
1000, 1000,
1777 1777
); );
expect(out.getOutput()).toEqual( expect(out.getOutput()).toEqual(
[ [
@ -131,6 +151,9 @@ describe("TrivialConsoleReporter", function() {
repeat(passingSpec, 57), repeat(passingSpec, 57),
[], [],
{ {
specs: function() {
return [null, null, null];
},
results:function() { results:function() {
return { return {
items_: [null, null, null], items_: [null, null, null],
@ -200,6 +223,9 @@ describe("TrivialConsoleReporter", function() {
}} }}
], ],
{ {
specs: function() {
return [null, null, null];
},
results:function() { results:function() {
return { return {
items_: [null, null, null], items_: [null, null, null],
@ -407,6 +433,9 @@ describe("TrivialConsoleReporter", function() {
describe("when reporting the results 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() {
reporter.reportRunnerResults({ reporter.reportRunnerResults({
specs: function() {
return [null, null, null];
},
results:function() { results:function() {
return {items_: [null, null, null], totalCount: 7, failedCount: 0}; return {items_: [null, null, null], totalCount: 7, failedCount: 0};
} }
@ -417,6 +446,9 @@ describe("TrivialConsoleReporter", function() {
it("prints statistics in red if there was a failure", function() { it("prints statistics in red if there was a failure", function() {
reporter.reportRunnerResults({ reporter.reportRunnerResults({
specs: function() {
return [null, null, null];
},
results:function() { results:function() {
return {items_: [null, null, null], totalCount: 7, failedCount: 3}; return {items_: [null, null, null], totalCount: 7, failedCount: 3};
} }
@ -427,6 +459,9 @@ describe("TrivialConsoleReporter", function() {
it("handles pluralization with 1's ones appropriately", function() { it("handles pluralization with 1's ones appropriately", function() {
reporter.reportRunnerResults({ reporter.reportRunnerResults({
specs: function() {
return [null];
},
results:function() { results:function() {
return {items_: [null], totalCount: 1, failedCount: 1}; return {items_: [null], totalCount: 1, failedCount: 1};
} }
@ -440,6 +475,9 @@ describe("TrivialConsoleReporter", function() {
it("calls back when done", function() { it("calls back when done", function() {
expect(done).toBeFalsy(); expect(done).toBeFalsy();
reporter.reportRunnerResults({ reporter.reportRunnerResults({
specs: function() {
return [null, null, null];
},
results:function() { results:function() {
return {items_: [null, null, null], totalCount: 7, failedCount: 0}; return {items_: [null, null, null], totalCount: 7, failedCount: 0};
} }

View File

@ -1,65 +1,111 @@
jasmine.TrivialConsoleReporter = function(print, doneCallback) { jasmine.TrivialConsoleReporter = function(print, doneCallback) {
//inspired by mhevery's jasmine-node reporter //inspired by mhevery's jasmine-node reporter
//https://github.com/mhevery/jasmine-node //https://github.com/mhevery/jasmine-node
doneCallback = doneCallback || function(){};
var defaultColumnsPerLine = 50,
ansi = { green: '\033[32m', red: '\033[31m', yellow: '\033[33m', none: '\033[0m' },
language = { spec:"spec", expectation:"expectation", failure:"failure" };
function coloredStr(color, str) { return ansi[color] + str + ansi.none; }
function greenStr(str) { return coloredStr("green", str); }
function redStr(str) { return coloredStr("red", str); }
function yellowStr(str) { return coloredStr("yellow", str); }
function newline() { print("\n"); }
function started() { print("Started");
newline(); }
function greenDot() { print(greenStr(".")); } doneCallback = doneCallback || function() {
function redF() { print(redStr("F")); } };
function yellowStar() { print(yellowStr("*")); }
var defaultColumnsPerLine = 50,
function plural(str, count) { return count == 1 ? str : str + "s"; } ansi = {
green: '\033[32m',
function repeat(thing, times) { var arr = []; red: '\033[31m',
for(var i=0; i<times; i++) arr.push(thing); yellow: '\033[33m',
return arr; none: '\033[0m'
} },
language = {
function indent(str, spaces) { var lines = (str || '').split("\n"); spec: "spec",
var newArr = []; expectation: "expectation",
for(var i = 0; i < lines.length; i++) { failure: "failure"
newArr.push(repeat(" ", spaces).join("") + lines[i]); };
}
return newArr.join("\n"); function coloredStr(color, str) {
} return ansi[color] + str + ansi.none;
}
function specFailureDetails(suiteDescription, specDescription, stackTraces) {
newline(); function greenStr(str) {
print(suiteDescription + " " + specDescription); return coloredStr("green", str);
newline(); }
for(var i=0; i<stackTraces.length; i++) {
print(indent(stackTraces[i], 2)); function redStr(str) {
newline(); return coloredStr("red", str);
} }
}
function finished(elapsed) { newline(); function yellowStr(str) {
print("Finished in " + elapsed/1000 + " seconds"); } return coloredStr("yellow", str);
function summary(colorF, specs, expectations, failed) { newline(); }
print(colorF(specs + " " + plural(language.spec, specs) + ", " +
expectations + " " + plural(language.expectation, expectations) + ", " + function newline() {
failed + " " + plural(language.failure, failed))); print("\n");
newline(); }
newline(); }
function greenSummary(specs, expectations, failed){ summary(greenStr, specs, expectations, failed); } function started() {
function redSummary(specs, expectations, failed){ summary(redStr, specs, expectations, failed); } print("Started");
newline();
}
function greenDot() {
print(greenStr("."));
}
function redF() {
print(redStr("F"));
}
function yellowStar() {
print(yellowStr("*"));
}
function plural(str, count) {
return count == 1 ? str : str + "s";
}
function repeat(thing, times) {
var arr = [];
for (var i = 0; i < times; i++) arr.push(thing);
return arr;
}
function indent(str, spaces) {
var lines = (str || '').split("\n");
var newArr = [];
for (var i = 0; i < lines.length; i++) {
newArr.push(repeat(" ", spaces).join("") + lines[i]);
}
return newArr.join("\n");
}
function specFailureDetails(suiteDescription, specDescription, stackTraces) {
newline();
print(suiteDescription + " " + specDescription);
newline();
for (var i = 0; i < stackTraces.length; i++) {
print(indent(stackTraces[i], 2));
newline();
}
}
function finished(elapsed) {
newline();
print("Finished in " + elapsed / 1000 + " seconds");
}
function summary(colorF, specs, expectations, failed) {
newline();
print(colorF(specs + " " + plural(language.spec, specs) + ", " +
expectations + " " + plural(language.expectation, expectations) + ", " +
failed + " " + plural(language.failure, failed)));
newline();
newline();
}
function greenSummary(specs, expectations, failed) {
summary(greenStr, specs, expectations, failed);
}
function redSummary(specs, expectations, failed) {
summary(redStr, specs, expectations, failed);
}
function lineEnder(columnsPerLine) { function lineEnder(columnsPerLine) {
var columnsSoFar = 0; var columnsSoFar = 0;
return function() { return function() {
@ -73,21 +119,24 @@ jasmine.TrivialConsoleReporter = function(print, doneCallback) {
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);
this.now = function() { return new Date().getTime(); }; this.now = function() {
return new Date().getTime();
};
this.reportRunnerStarting = function() { this.reportRunnerStarting = function() {
this.runnerStartTime = this.now(); this.runnerStartTime = this.now();
started(); started();
}; };
this.reportSpecStarting = function() { /* do nothing */ }; this.reportSpecStarting = function() { /* do nothing */
};
this.reportSpecResults = function(spec) { this.reportSpecResults = function(spec) {
var results = spec.results(); var results = spec.results();
if (results.skipped) { if (results.skipped) {
@ -96,49 +145,49 @@ jasmine.TrivialConsoleReporter = function(print, doneCallback) {
greenDot(); greenDot();
} else { } else {
redF(); redF();
} }
// startNewLineIfNecessary(); // startNewLineIfNecessary();
}; };
this.suiteResults = []; this.suiteResults = [];
this.reportSuiteResults = function(suite) { this.reportSuiteResults = function(suite) {
var suiteResult = { var suiteResult = {
description: fullSuiteDescription(suite), description: fullSuiteDescription(suite),
failedSpecResults: [] failedSpecResults: []
}; };
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);
}; };
function eachSpecFailure(suiteResults, callback) { function eachSpecFailure(suiteResults, callback) {
for(var i=0; i<suiteResults.length; i++) { for (var i = 0; i < suiteResults.length; i++) {
var suiteResult = suiteResults[i]; var suiteResult = suiteResults[i];
for(var j=0; j<suiteResult.failedSpecResults.length; j++) { for (var j = 0; j < suiteResult.failedSpecResults.length; j++) {
var failedSpecResult = suiteResult.failedSpecResults[j]; var failedSpecResult = suiteResult.failedSpecResults[j];
var stackTraces = []; var stackTraces = [];
for(var k=0; k<failedSpecResult.items_.length; k++) stackTraces.push(failedSpecResult.items_[k].trace.stack); for (var k = 0; k < failedSpecResult.items_.length; k++) stackTraces.push(failedSpecResult.items_[k].trace.stack);
callback(suiteResult.description, failedSpecResult.description, stackTraces); callback(suiteResult.description, failedSpecResult.description, stackTraces);
} }
} }
} }
this.reportRunnerResults = function(runner) { this.reportRunnerResults = function(runner) {
newline(); newline();
eachSpecFailure(this.suiteResults, function(suiteDescription, specDescription, stackTraces) { eachSpecFailure(this.suiteResults, function(suiteDescription, specDescription, stackTraces) {
specFailureDetails(suiteDescription, specDescription, stackTraces); specFailureDetails(suiteDescription, specDescription, stackTraces);
}); });
finished(this.now() - this.runnerStartTime); finished(this.now() - this.runnerStartTime);
var results = runner.results(); var results = runner.results();
var summaryFunction = results.failedCount === 0 ? greenSummary : redSummary; var summaryFunction = results.failedCount === 0 ? greenSummary : redSummary;
summaryFunction(results.items_.length, results.totalCount, results.failedCount); summaryFunction(runner.specs().length, results.totalCount, results.failedCount);
doneCallback(runner); doneCallback(runner);
}; };
}; };