| 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 | #ifndef _FG_TEST_LISTENER_HXX |
| 22 | #define _FG_TEST_LISTENER_HXX |
| 23 | |
| 24 | |
| 25 | #include <time.h> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "formatting.hxx" |
| 29 | |
| 30 | |
| 31 | // Data structure for holding the captured IO for a failed test. |
| 32 | struct TestIOCapt { |
| 33 | std::string name; |
| 34 | std::string log_class; |
| 35 | std::string log_priority; |
| 36 | std::string stdio; |
| 37 | std::string sg_interleaved; |
| 38 | std::string sg_bulk_only; |
| 39 | std::string sg_debug_only; |
| 40 | std::string sg_info_only; |
| 41 | std::string sg_warn_only; |
| 42 | std::string sg_alert_only; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | // Match the test by name for std:find using a vector<TestIOCapt>. |
| 47 | class matchTestName |
| 48 | { |
| 49 | std::string _name; |
| 50 | |
| 51 | public: |
| 52 | matchTestName(const std::string &name) : _name(name) {} |
| 53 | |
| 54 | bool operator()(const TestIOCapt &item) const { |
| 55 | return item.name == _name; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | // The custom test runner for the FlightGear test suite. |
| 61 | class fgTestListener : public CppUnit::TestListener |
| 62 | { |
| 63 | protected: |
| 64 | // Failure state. |
| 65 | bool m_failure, m_error; |
| 66 | |
| 67 | public: |
| 68 | // Constructor. |
| 69 | fgTestListener(): m_failure(false), m_error(false), sum_time(0) { }; |
| 70 | |
| 71 | // Override the base class function to capture IO. streams |
| 72 | void startTest(CppUnit::Test *test); |
| 73 | |
| 74 | // Override the base class function to restore IO streams. |
| 75 | void endTest(CppUnit::Test *test); |
| 76 | |
| 77 | // Handle failures. |
| 78 | void addFailure(const CppUnit::TestFailure &failure); |
| 79 | |
| 80 | // Test suite timing. |
| 81 | clock_t sum_time; |
| 82 | |
| 83 | // IO capture for all failed tests. |
| 84 | std::vector<TestIOCapt> io_capt; |
| 85 | |
| 86 | // Output settings. |
| 87 | bool timings; |
| 88 | bool ctest_output; |
| 89 | bool debug; |
| 90 | |
| 91 | protected: |
| 92 | // The original IO streams. |
| 93 | std::streambuf *orig_cerr, *orig_cout; |
| 94 | |
| 95 | // Captured IO streams. |
| 96 | std::stringstream capt; |
| 97 | |
| 98 | // Test timings. |
| 99 | clock_t m_time; |
| 100 | }; |
| 101 | |
| 102 | |
| 103 | #endif // _FG_TEST_LISTENER_HXX |