| 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 <cppunit/Test.h> |
| 22 | #include <cppunit/TestFailure.h> |
| 23 | #include <cppunit/TestListener.h> |
| 24 | |
| 25 | #include "fgTestListener.hxx" |
| 26 | #include "logging.hxx" |
| 27 | |
| 28 | using namespace std; |
| 29 | |
| 30 | |
| 31 | // Handle failures. |
| 32 | void fgTestListener::addFailure(const CppUnit::TestFailure &failure) |
| 33 | { |
| 34 | m_failure = true; |
| 35 | if (failure.isError()) |
| 36 | m_error = true; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | // Override the base class function to restore IO streams. |
| 41 | void fgTestListener::endTest(CppUnit::Test *test) |
| 42 | { |
| 43 | // Test timing. |
| 44 | sum_time += clock() - m_time; |
| 45 | |
| 46 | // Restore the IO streams. |
| 47 | if (!debug) { |
| 48 | cout.rdbuf(orig_cout); |
| 49 | cerr.rdbuf(orig_cerr); |
| 50 | } |
| 51 | |
| 52 | // Debugging output. |
| 53 | if (debug) |
| 54 | cerr << string(WIDTH_DIVIDER, '-') << endl; |
| 55 | |
| 56 | // Per-test single character status feedback. |
| 57 | if (m_failure) |
| 58 | cerr << (m_error ? "E" : "F"); |
| 59 | else |
| 60 | cerr << '.'; |
| 61 | |
| 62 | // Timing output. |
| 63 | if (timings || ctest_output || debug) { |
| 64 | // Test timing. |
| 65 | float time = ((float)(clock()-m_time))/CLOCKS_PER_SEC; |
| 66 | char buffer[100]; |
| 67 | if (time > 60.0) |
| 68 | snprintf(buffer, sizeof(buffer), "%10.3f %-3s", time/60, "min"); |
| 69 | else if (time > 1.0) |
| 70 | snprintf(buffer, sizeof(buffer), "%10.3f %-3s", time, "s"); |
| 71 | else |
| 72 | snprintf(buffer, sizeof(buffer), "%10.3f %-3s", time*1000, "ms"); |
| 73 | cerr << buffer; |
| 74 | |
| 75 | // Test name. |
| 76 | cerr << " for " << test->getName() << endl; |
| 77 | } |
| 78 | cerr.flush(); |
| 79 | |
| 80 | // Store the captured IO for any failed tests. |
| 81 | if (m_failure && !debug) { |
| 82 | // Set up the data structure. |
| 83 | TestIOCapt test_io; |
| 84 | test_io.name = test->getName(); |
| 85 | |
| 86 | // Standard IO. |
| 87 | test_io.stdio = capt.str(); |
| 88 | |
| 89 | // The simgear logstreams. |
| 90 | capturedIO &obj = getIOstreams(); |
| 91 | test_io.log_class = obj.log_class; |
| 92 | test_io.log_priority = obj.log_priority; |
| 93 | test_io.sg_interleaved = obj.sg_interleaved.str(); |
| 94 | test_io.sg_bulk_only = obj.sg_bulk_only.str(); |
| 95 | test_io.sg_debug_only = obj.sg_debug_only.str(); |
| 96 | test_io.sg_info_only = obj.sg_info_only.str(); |
| 97 | test_io.sg_warn_only = obj.sg_warn_only.str(); |
| 98 | test_io.sg_alert_only = obj.sg_alert_only.str(); |
| 99 | |
| 100 | // Add the test's IO to the list. |
| 101 | io_capt.push_back(test_io); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Override the base class function to capture IO streams. |
| 106 | void fgTestListener::startTest(CppUnit::Test *test) |
| 107 | { |
| 108 | // IO capture. |
| 109 | if (!debug) { |
| 110 | // Clear the simgear logstream buffers. |
| 111 | capturedIO &obj = getIOstreams(); |
| 112 | obj.sg_interleaved.str(""); |
| 113 | obj.sg_bulk_only.str(""); |
| 114 | obj.sg_debug_only.str(""); |
| 115 | obj.sg_info_only.str(""); |
| 116 | obj.sg_warn_only.str(""); |
| 117 | obj.sg_alert_only.str(""); |
| 118 | |
| 119 | // Store the original STDOUT and STDERR for restoring later on. |
| 120 | orig_cout = cout.rdbuf(); |
| 121 | orig_cerr = cerr.rdbuf(); |
| 122 | |
| 123 | // Clear the captured stream and then catch stdout and stderr. |
| 124 | capt.str(string()); |
| 125 | cout.rdbuf(capt.rdbuf()); |
| 126 | cerr.rdbuf(capt.rdbuf()); |
| 127 | |
| 128 | // Debugging output. |
| 129 | } else { |
| 130 | cerr << string(WIDTH_DIVIDER, '=') << endl; |
| 131 | cerr << "Starting test: " << test->getName() << endl; |
| 132 | cerr << string(WIDTH_DIVIDER, '-') << endl; |
| 133 | } |
| 134 | // Reset the test status. |
| 135 | m_failure = false; |
| 136 | m_error = false; |
| 137 | m_time = clock(); |
| 138 | } |