Skip to content

Commit

Permalink
factor out the console reporting from the runner
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbintz committed Aug 30, 2011
1 parent 904be27 commit 0c368ec
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 47 deletions.
65 changes: 63 additions & 2 deletions ext/jasmine-webkit-specrunner/ConsoleOutput.cpp
Expand Up @@ -17,8 +17,7 @@ namespace HeadlessSpecRunner {
successes.push(specDetail);
}

void ConsoleOutput::failed(const QString &specDetail)
{
void ConsoleOutput::failed(const QString &specDetail) {
red();
*outputIO << 'F';
clear();
Expand All @@ -40,6 +39,11 @@ namespace HeadlessSpecRunner {
if (showColors) std::cout << "\033[0;31m";
}

void ConsoleOutput::yellow()
{
if (showColors) std::cout << "\033[0;33m";
}

void ConsoleOutput::errorLog(const QString &msg, int lineNumber, const QString &sourceID) {
red();
*outputIO << "[error] ";
Expand Down Expand Up @@ -77,5 +81,62 @@ namespace HeadlessSpecRunner {
*outputIO << qPrintable(name) << std::endl;
clear();
}

void ConsoleOutput::logSpecResult(const QString &result) {
red();
*outputIO << " " << qPrintable(result) << std::endl;
clear();
}

void ConsoleOutput::reportFailure(const QString &totalTests, const QString &failedTests, const QString &duration) {
red();
*outputIO << std::endl << "FAIL: ";
formatTestResults(totalTests, failedTests, duration);
*outputIO << std::endl;
clear();
}

void ConsoleOutput::reportSuccess(const QString &totalTests, const QString &failedTests, const QString &duration) {
green();
*outputIO << std::endl << "PASS: ";
formatTestResults(totalTests, failedTests, duration);
*outputIO << std::endl;
clear();
}

void ConsoleOutput::reportSuccessWithJSErrors(const QString &totalTests, const QString &failedTests, const QString &duration) {
yellow();
*outputIO << std::endl << "PASS with JS errors: ";
formatTestResults(totalTests, failedTests, duration);
*outputIO << std::endl;
clear();
}

void ConsoleOutput::formatTestResults(const QString &totalTests, const QString &failedTests, const QString &duration) {
*outputIO << qPrintable(totalTests) << " ";
if (totalTests == "1") {
*outputIO << "test";
} else {
*outputIO << "tests";
}

*outputIO << ", ";

*outputIO << qPrintable(failedTests) << " ";
if (failedTests == "1") {
*outputIO << "failure";
} else {
*outputIO << "failures";
}

*outputIO << ", ";

*outputIO << qPrintable(duration) << " ";
if (duration == "1") {
*outputIO << "sec.";
} else {
*outputIO << "secs.";
}
}
}

7 changes: 7 additions & 0 deletions ext/jasmine-webkit-specrunner/ConsoleOutput.h
Expand Up @@ -16,6 +16,11 @@ namespace HeadlessSpecRunner {
void internalLog(const QString &note, const QString &msg);
void consoleLog(const QString &msg);
void logSpecFilename(const QString &name);
void logSpecResult(const QString &result);

void reportFailure(const QString &totalTests, const QString &failedTests, const QString &duration);
void reportSuccess(const QString &totalTests, const QString &failedTests, const QString &duration);
void reportSuccessWithJSErrors(const QString &totalTests, const QString &failedTests, const QString &duration);

std::ostream *outputIO;
QStack<QString> successes;
Expand All @@ -26,6 +31,8 @@ namespace HeadlessSpecRunner {
void green();
void clear();
void red();
void yellow();
void formatTestResults(const QString &totalTests, const QString &failedTests, const QString &duration);
};
}

Expand Down
45 changes: 45 additions & 0 deletions ext/jasmine-webkit-specrunner/ConsoleOutput_test.cpp
Expand Up @@ -81,6 +81,51 @@ namespace HeadlessSpecRunner {
output.logSpecFilename("whatever");
QVERIFY(buffer.str() == "\n\nwhatever\n");
}

void ConsoleOutputTest::testLogSpecResult() {
stringstream buffer;
HeadlessSpecRunner::ConsoleOutput output;

output.outputIO = &buffer;
output.logSpecResult("whatever");
QVERIFY(buffer.str() == " whatever\n");
}

void ConsoleOutputTest::testReportResultsFailedSingular() {
stringstream buffer;
HeadlessSpecRunner::ConsoleOutput output;

output.outputIO = &buffer;
output.reportFailure("1", "1", "1");
QVERIFY(buffer.str() == "\nFAIL: 1 test, 1 failure, 1 sec.\n");
}

void ConsoleOutputTest::testReportResultsFailedPlural() {
stringstream buffer;
HeadlessSpecRunner::ConsoleOutput output;

output.outputIO = &buffer;
output.reportFailure("2", "2", "2");
QVERIFY(buffer.str() == "\nFAIL: 2 tests, 2 failures, 2 secs.\n");
}

void ConsoleOutputTest::testReportResultsSucceeded() {
stringstream buffer;
HeadlessSpecRunner::ConsoleOutput output;

output.outputIO = &buffer;
output.reportSuccess("2", "2", "2");
QVERIFY(buffer.str() == "\nPASS: 2 tests, 2 failures, 2 secs.\n");
}

void ConsoleOutputTest::testReportResultsSucceededWithJSErrors() {
stringstream buffer;
HeadlessSpecRunner::ConsoleOutput output;

output.outputIO = &buffer;
output.reportSuccessWithJSErrors("2", "2", "2");
QVERIFY(buffer.str() == "\nPASS with JS errors: 2 tests, 2 failures, 2 secs.\n");
}
}

QTEST_MAIN(HeadlessSpecRunner::ConsoleOutputTest);
Expand Down
6 changes: 6 additions & 0 deletions ext/jasmine-webkit-specrunner/ConsoleOutput_test.h
Expand Up @@ -22,6 +22,12 @@ namespace HeadlessSpecRunner {
void testConsoleLog();
void testConsoleLogUsed();
void testLogSpecFilename();
void testLogSpecResult();

void testReportResultsFailedSingular();
void testReportResultsFailedPlural();
void testReportResultsSucceeded();
void testReportResultsSucceededWithJSErrors();
};
}

Expand Down
4 changes: 2 additions & 2 deletions ext/jasmine-webkit-specrunner/Page_test.cpp
Expand Up @@ -7,11 +7,11 @@ namespace HeadlessSpecRunner {
PageTest::PageTest() : QObject(), internalLogCalled(false) {
}

void PageTest::internalLog(const QString &note, const QString &msg) {
void PageTest::internalLog(const QString &, const QString &) {
internalLogCalled = true;
}

void PageTest::consoleLog(const QString &message, int lineNumber, const QString &source) {
void PageTest::consoleLog(const QString &, int, const QString &) {
consoleLogCalled = true;
}

Expand Down
43 changes: 5 additions & 38 deletions ext/jasmine-webkit-specrunner/Runner.cpp
Expand Up @@ -12,7 +12,6 @@ namespace HeadlessSpecRunner {
, m_runs(0)
, hasErrors(false)
, usedConsole(false)
, showColors(false)
, isFinished(false)
, didFail(false) {
m_page.settings()->enablePersistentStorage();
Expand Down Expand Up @@ -64,7 +63,6 @@ namespace HeadlessSpecRunner {

void Runner::setColors(bool colors)
{
showColors = colors;
consoleOutput.showColors = colors;
}

Expand All @@ -73,30 +71,10 @@ namespace HeadlessSpecRunner {
reportFilename = file;
}

void Runner::red()
{
if (showColors) std::cout << "\033[0;31m";
}

void Runner::green()
{
if (showColors) std::cout << "\033[0;32m";
}

bool Runner::hasError() {
return hasErrors;
}

void Runner::yellow()
{
if (showColors) std::cout << "\033[0;33m";
}

void Runner::clear()
{
if (showColors) std::cout << "\033[m";
}

void Runner::specPassed()
{
consoleOutput.passed("");
Expand Down Expand Up @@ -142,32 +120,21 @@ namespace HeadlessSpecRunner {

void Runner::printResult(const QString &result)
{
red();
std::cout << " " << qPrintable(result) << std::endl;
clear();
consoleOutput.logSpecResult(result);
}

void Runner::finishSuite(const QString &duration, const QString &total, const QString& failed)
{
std::cout << std::endl;
if (didFail) {
red();
std::cout << "FAIL: ";
consoleOutput.reportFailure(total, failed, duration);
} else {
green();
std::cout << "PASS";

if (hasErrors) {
std::cout << " with JS errors";
consoleOutput.reportSuccessWithJSErrors(total, failed, duration);
} else {
consoleOutput.reportSuccess(total, failed, duration);
}

std::cout << ": ";
}

std::cout << qPrintable(total) << " tests, " << qPrintable(failed) << " failures, " << qPrintable(duration) << " secs.";
clear();
std::cout << std::endl;

if (!reportFilename.isEmpty()) {
QFile reportFH(reportFilename);

Expand Down
5 changes: 0 additions & 5 deletions ext/jasmine-webkit-specrunner/Runner.h
Expand Up @@ -43,7 +43,6 @@ namespace HeadlessSpecRunner {
int m_runs;
bool hasErrors;
bool usedConsole;
bool showColors;
bool isFinished;
bool didFail;
QQueue<QString> runnerFiles;
Expand All @@ -52,10 +51,6 @@ namespace HeadlessSpecRunner {

HeadlessSpecRunner::ConsoleOutput consoleOutput;

void red();
void green();
void yellow();
void clear();
void loadSpec();
};
}
Expand Down

0 comments on commit 0c368ec

Please sign in to comment.