File: fgTestListener.cxx

package info (click to toggle)
flightgear 1%3A2020.3.19%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 51,068 kB
  • sloc: cpp: 245,588; ansic: 181,318; sh: 13,686; perl: 4,475; python: 3,139; xml: 899; asm: 642; makefile: 347; java: 314
file content (138 lines) | stat: -rw-r--r-- 4,112 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2016 Edward d'Auvergne
 *
 * This file is part of the program FlightGear.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <cppunit/Test.h>
#include <cppunit/TestFailure.h>
#include <cppunit/TestListener.h>

#include "fgTestListener.hxx"
#include "logging.hxx"

using namespace std;


// Handle failures.
void fgTestListener::addFailure(const CppUnit::TestFailure &failure)
{
    m_failure = true;
    if (failure.isError())
        m_error = true;
}


// Override the base class function to restore IO streams.
void fgTestListener::endTest(CppUnit::Test *test)
{
    // Test timing.
    sum_time += clock() - m_time;

    // Restore the IO streams.
    if (!debug) {
        cout.rdbuf(orig_cout);
        cerr.rdbuf(orig_cerr);
    }

    // Debugging output.
    if (debug)
        cerr << string(WIDTH_DIVIDER, '-') << endl;

    // Per-test single character status feedback.
    if (m_failure)
        cerr << (m_error ? "E" : "F");
    else
        cerr << '.';

    // Timing output.
    if (timings || ctest_output || debug) {
        // Test timing.
        float time = ((float)(clock()-m_time))/CLOCKS_PER_SEC;
        char buffer[100];
        if (time > 60.0)
            snprintf(buffer, sizeof(buffer), "%10.3f %-3s", time/60, "min");
        else if (time > 1.0)
            snprintf(buffer, sizeof(buffer), "%10.3f %-3s", time, "s");
        else
            snprintf(buffer, sizeof(buffer), "%10.3f %-3s", time*1000, "ms");
        cerr << buffer;

        // Test name.
        cerr << " for " << test->getName() << endl;
    }
    cerr.flush();

    // Store the captured IO for any failed tests.
    if (m_failure && !debug) {
        // Set up the data structure.
        TestIOCapt test_io;
        test_io.name = test->getName();

        // Standard IO.
        test_io.stdio = capt.str();

        // The simgear logstreams.
        capturedIO &obj = getIOstreams();
        test_io.log_class = obj.log_class;
        test_io.log_priority = obj.log_priority;
        test_io.sg_interleaved = obj.sg_interleaved.str();
        test_io.sg_bulk_only = obj.sg_bulk_only.str();
        test_io.sg_debug_only = obj.sg_debug_only.str();
        test_io.sg_info_only = obj.sg_info_only.str();
        test_io.sg_warn_only = obj.sg_warn_only.str();
        test_io.sg_alert_only = obj.sg_alert_only.str();

        // Add the test's IO to the list.
        io_capt.push_back(test_io);
    }
}

// Override the base class function to capture IO streams.
void fgTestListener::startTest(CppUnit::Test *test)
{
    // IO capture.
    if (!debug) {
        // Clear the simgear logstream buffers.
        capturedIO &obj = getIOstreams();
        obj.sg_interleaved.str("");
        obj.sg_bulk_only.str("");
        obj.sg_debug_only.str("");
        obj.sg_info_only.str("");
        obj.sg_warn_only.str("");
        obj.sg_alert_only.str("");

        // Store the original STDOUT and STDERR for restoring later on.
        orig_cout = cout.rdbuf();
        orig_cerr = cerr.rdbuf();

        // Clear the captured stream and then catch stdout and stderr.
        capt.str(string());
        cout.rdbuf(capt.rdbuf());
        cerr.rdbuf(capt.rdbuf());

    // Debugging output.
    } else {
        cerr << string(WIDTH_DIVIDER, '=') << endl;
        cerr << "Starting test: " << test->getName() << endl;
        cerr << string(WIDTH_DIVIDER, '-') << endl;
    }
    // Reset the test status.
    m_failure = false;
    m_error = false;
    m_time = clock();
}