factor out the console reporting from the runner

This commit is contained in:
John Bintz 2011-08-30 11:56:35 -04:00
parent 904be27e42
commit 0c368ec9f2
7 changed files with 128 additions and 47 deletions

View File

@ -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.";
}
}
}

View File

@ -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;
@ -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);
};
}

View File

@ -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);

View File

@ -22,6 +22,12 @@ namespace HeadlessSpecRunner {
void testConsoleLog();
void testConsoleLogUsed();
void testLogSpecFilename();
void testLogSpecResult();
void testReportResultsFailedSingular();
void testReportResultsFailedPlural();
void testReportResultsSucceeded();
void testReportResultsSucceededWithJSErrors();
};
}

View File

@ -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;
}

View File

@ -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);

View File

@ -43,7 +43,6 @@ namespace HeadlessSpecRunner {
int m_runs;
bool hasErrors;
bool usedConsole;
bool showColors;
bool isFinished;
bool didFail;
QQueue<QString> runnerFiles;
@ -52,10 +51,6 @@ namespace HeadlessSpecRunner {
HeadlessSpecRunner::ConsoleOutput consoleOutput;
void red();
void green();
void yellow();
void clear();
void loadSpec();
};
}