From 0c368ec9f2a537e59a06f7251ab9a48e75f5947e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 30 Aug 2011 11:56:35 -0400 Subject: [PATCH] factor out the console reporting from the runner --- .../ConsoleOutput.cpp | 65 ++++++++++++++++++- ext/jasmine-webkit-specrunner/ConsoleOutput.h | 7 ++ .../ConsoleOutput_test.cpp | 45 +++++++++++++ .../ConsoleOutput_test.h | 6 ++ ext/jasmine-webkit-specrunner/Page_test.cpp | 4 +- ext/jasmine-webkit-specrunner/Runner.cpp | 43 ++---------- ext/jasmine-webkit-specrunner/Runner.h | 5 -- 7 files changed, 128 insertions(+), 47 deletions(-) diff --git a/ext/jasmine-webkit-specrunner/ConsoleOutput.cpp b/ext/jasmine-webkit-specrunner/ConsoleOutput.cpp index f6d9804..cb4b937 100644 --- a/ext/jasmine-webkit-specrunner/ConsoleOutput.cpp +++ b/ext/jasmine-webkit-specrunner/ConsoleOutput.cpp @@ -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(); @@ -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] "; @@ -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."; + } + } } diff --git a/ext/jasmine-webkit-specrunner/ConsoleOutput.h b/ext/jasmine-webkit-specrunner/ConsoleOutput.h index 51afb93..cb71cca 100644 --- a/ext/jasmine-webkit-specrunner/ConsoleOutput.h +++ b/ext/jasmine-webkit-specrunner/ConsoleOutput.h @@ -16,6 +16,11 @@ namespace HeadlessSpecRunner { void internalLog(const QString ¬e, 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 successes; @@ -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); }; } diff --git a/ext/jasmine-webkit-specrunner/ConsoleOutput_test.cpp b/ext/jasmine-webkit-specrunner/ConsoleOutput_test.cpp index e833549..baa3cf7 100644 --- a/ext/jasmine-webkit-specrunner/ConsoleOutput_test.cpp +++ b/ext/jasmine-webkit-specrunner/ConsoleOutput_test.cpp @@ -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); diff --git a/ext/jasmine-webkit-specrunner/ConsoleOutput_test.h b/ext/jasmine-webkit-specrunner/ConsoleOutput_test.h index 3e13942..5f1d371 100644 --- a/ext/jasmine-webkit-specrunner/ConsoleOutput_test.h +++ b/ext/jasmine-webkit-specrunner/ConsoleOutput_test.h @@ -22,6 +22,12 @@ namespace HeadlessSpecRunner { void testConsoleLog(); void testConsoleLogUsed(); void testLogSpecFilename(); + void testLogSpecResult(); + + void testReportResultsFailedSingular(); + void testReportResultsFailedPlural(); + void testReportResultsSucceeded(); + void testReportResultsSucceededWithJSErrors(); }; } diff --git a/ext/jasmine-webkit-specrunner/Page_test.cpp b/ext/jasmine-webkit-specrunner/Page_test.cpp index a184d26..9934af1 100644 --- a/ext/jasmine-webkit-specrunner/Page_test.cpp +++ b/ext/jasmine-webkit-specrunner/Page_test.cpp @@ -7,11 +7,11 @@ namespace HeadlessSpecRunner { PageTest::PageTest() : QObject(), internalLogCalled(false) { } - void PageTest::internalLog(const QString ¬e, 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; } diff --git a/ext/jasmine-webkit-specrunner/Runner.cpp b/ext/jasmine-webkit-specrunner/Runner.cpp index e129505..208be7c 100644 --- a/ext/jasmine-webkit-specrunner/Runner.cpp +++ b/ext/jasmine-webkit-specrunner/Runner.cpp @@ -12,7 +12,6 @@ namespace HeadlessSpecRunner { , m_runs(0) , hasErrors(false) , usedConsole(false) - , showColors(false) , isFinished(false) , didFail(false) { m_page.settings()->enablePersistentStorage(); @@ -64,7 +63,6 @@ namespace HeadlessSpecRunner { void Runner::setColors(bool colors) { - showColors = colors; consoleOutput.showColors = colors; } @@ -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(""); @@ -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); diff --git a/ext/jasmine-webkit-specrunner/Runner.h b/ext/jasmine-webkit-specrunner/Runner.h index 9c76fd0..a1275d6 100644 --- a/ext/jasmine-webkit-specrunner/Runner.h +++ b/ext/jasmine-webkit-specrunner/Runner.h @@ -43,7 +43,6 @@ namespace HeadlessSpecRunner { int m_runs; bool hasErrors; bool usedConsole; - bool showColors; bool isFinished; bool didFail; QQueue runnerFiles; @@ -52,10 +51,6 @@ namespace HeadlessSpecRunner { HeadlessSpecRunner::ConsoleOutput consoleOutput; - void red(); - void green(); - void yellow(); - void clear(); void loadSpec(); }; }