| 1 | /* |
| 2 | * Copyright (C) 2016 Edward d'Auvergne |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <iomanip> |
| 23 | |
| 24 | #include <cppunit/SourceLine.h> |
| 25 | #include <cppunit/TestResultCollector.h> |
| 26 | |
| 27 | #include "fgCompilerOutputter.hxx" |
| 28 | #include "formatting.hxx" |
| 29 | #include "logging.hxx" |
| 30 | |
| 31 | using namespace std; |
| 32 | |
| 33 | |
| 34 | // Create a new class instance. |
| 35 | fgCompilerOutputter * fgCompilerOutputter::defaultOutputter(CppUnit::TestResultCollector *result, vector<TestIOCapt> *capt, const clock_t *clock, CppUnit::OStream &stream) |
| 36 | { |
| 37 | return new fgCompilerOutputter(result, capt, clock, stream); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // Printout for each failed test. |
| 42 | void fgCompilerOutputter::printFailureDetail(CppUnit::TestFailure *failure) |
| 43 | { |
| 44 | // Declarations. |
| 45 | TestIOCapt test_io; |
| 46 | vector<TestIOCapt>::iterator test_iter; |
| 47 | |
| 48 | // Initial separator. |
| 49 | #ifdef _WIN32 |
| 50 | fg_stream << endl; |
| 51 | #endif |
| 52 | fg_stream << string(WIDTH_DIVIDER, '=') << endl; |
| 53 | |
| 54 | // Test info. |
| 55 | fg_stream << (failure->isError() ? "ERROR: " : "FAIL: ") << failure->failedTestName() << endl; |
| 56 | fg_stream << string(WIDTH_DIVIDER, '-') << endl; |
| 57 | fg_stream << (failure->isError() ? "Error" : "Assertion") << ": "; |
| 58 | printFailureLocation(failure->sourceLine()); |
| 59 | printFailureMessage(failure); |
| 60 | fg_stream.flush(); |
| 61 | |
| 62 | if (debug) |
| 63 | return; |
| 64 | |
| 65 | // The captured IO for this test. |
| 66 | test_iter = find_if(io_capt->begin(), io_capt->end(), matchTestName(failure->failedTestName())); |
| 67 | if (test_iter != io_capt->end()) |
| 68 | test_io = *test_iter; |
| 69 | |
| 70 | // SG_LOG IO streams. |
| 71 | if (!test_io.sg_interleaved.empty()) |
| 72 | fgCompilerOutputter::printIOStreamMessages("SG_LOG, "+test_io.log_class+" class, "+test_io.log_priority+" priority", test_io.sg_interleaved, true); |
| 73 | if (!test_io.sg_bulk_only.empty()) |
| 74 | fgCompilerOutputter::printIOStreamMessages("SG_LOG, "+test_io.log_class+" class, SG_BULK only priority", test_io.sg_bulk_only); |
| 75 | if (!test_io.sg_debug_only.empty()) |
| 76 | fgCompilerOutputter::printIOStreamMessages("SG_LOG, "+test_io.log_class+" class, SG_DEBUG only priority", test_io.sg_debug_only); |
| 77 | if (!test_io.sg_info_only.empty()) |
| 78 | fgCompilerOutputter::printIOStreamMessages("SG_LOG, "+test_io.log_class+" class, SG_INFO only priority", test_io.sg_info_only); |
| 79 | if (!test_io.sg_warn_only.empty()) |
| 80 | fgCompilerOutputter::printIOStreamMessages("SG_LOG, "+test_io.log_class+" class, SG_WARN only priority", test_io.sg_warn_only); |
| 81 | if (!test_io.sg_alert_only.empty()) |
| 82 | fgCompilerOutputter::printIOStreamMessages("SG_LOG, "+test_io.log_class+" class, SG_ALERT only priority", test_io.sg_alert_only); |
| 83 | |
| 84 | // Default IO streams. |
| 85 | fgCompilerOutputter::printIOStreamMessages("STDOUT and STDERR", test_io.stdio); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | // Detailed printout after a failed run of the test suite. |
| 90 | void fgCompilerOutputter::printFailureReport() |
| 91 | { |
| 92 | // Custom printouts for each failed test. |
| 93 | printFailuresList(); |
| 94 | |
| 95 | // CTest output (nothing). |
| 96 | if (ctest_output) |
| 97 | return; |
| 98 | |
| 99 | // A summary with timing info. |
| 100 | printSuiteStats(); |
| 101 | |
| 102 | // Final summary. |
| 103 | fg_stream << endl << "[ FAILED ]" << endl << endl; |
| 104 | fg_stream.flush(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void fgCompilerOutputter::printIOStreamMessages(string heading, string messages, bool empty) |
| 109 | { |
| 110 | // Silence. |
| 111 | if (!empty && messages.size() == 0) |
| 112 | return; |
| 113 | |
| 114 | // Divider. |
| 115 | fg_stream << string(WIDTH_DIVIDER, '-') << endl; |
| 116 | |
| 117 | // Heading. |
| 118 | fg_stream << "# " << heading << endl << endl; |
| 119 | |
| 120 | // Nothing to do |
| 121 | if (messages.size() == 0) |
| 122 | fg_stream << "(empty)" << endl << endl; |
| 123 | |
| 124 | // The IO stream contents. |
| 125 | else |
| 126 | fg_stream << messages << endl; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // Printout of the test suite stats. |
| 131 | void fgCompilerOutputter::printSuiteStats() |
| 132 | { |
| 133 | // A divider. |
| 134 | #ifdef _WIN32 |
| 135 | fg_stream << endl; |
| 136 | #endif |
| 137 | fg_stream << string(WIDTH_DIVIDER, '-') << endl; |
| 138 | |
| 139 | // Timing and test count line. |
| 140 | fg_stream << "Ran " << fg_result->runTests() << " tests"; |
| 141 | streamsize prec = fg_stream.precision(); |
| 142 | fg_stream << setprecision(3); |
| 143 | fg_stream << " in " << ((double)*suite_timer)/CLOCKS_PER_SEC << " seconds." << endl; |
| 144 | fg_stream << setprecision(prec); |
| 145 | |
| 146 | // Failure line. |
| 147 | if (!fg_result->wasSuccessful()) { |
| 148 | fg_stream << endl << "Failures = " << fg_result->testFailures() << endl; |
| 149 | fg_stream << "Errors = " << fg_result->testErrors() << endl; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | // Print a summary after a successful run of the test suite. |
| 155 | void fgCompilerOutputter::printSuccess() |
| 156 | { |
| 157 | // CTest output (nothing). |
| 158 | if (ctest_output) |
| 159 | return; |
| 160 | |
| 161 | // A summary with timing info. |
| 162 | printSuiteStats(); |
| 163 | |
| 164 | // Final summary. |
| 165 | fg_stream << endl << "[ OK ]" << endl << endl; |
| 166 | fg_stream.flush(); |
| 167 | } |