File: PrettyResultPrinter.cpp

package info (click to toggle)
skypat 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 664 kB
  • sloc: cpp: 2,545; makefile: 220; ansic: 78; sh: 67
file content (190 lines) | stat: -rw-r--r-- 6,999 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//===- PrettyResultPrinter.cpp ---------------------------------------------===//
//
//                     The SkyPat Team
//
// This file is distributed under the New BSD License. 
// See LICENSE for details.
//
//===----------------------------------------------------------------------===//
#include <skypat/Listeners/PrettyResultPrinter.h>
#include <skypat/ADT/Color.h>
#include <iostream>

using namespace skypat;

//===----------------------------------------------------------------------===//
// PrettyResultPrinter
//===----------------------------------------------------------------------===//
void PrettyResultPrinter::PrintCaseName(const std::string& pCase,
                                        const std::string& pTest)
{
  testing::Log::getOStream() << pCase << "." << pTest;
}

void PrettyResultPrinter::OnTestProgramStart(const testing::UnitTest& pUnitTest)
{
  testing::Log::getOStream() << Color::CYAN << "[  skypat  ] "
    << "Running " << pUnitTest.getNumOfTests()
    << " tests from " << pUnitTest.getNumOfCases() << " cases." << std::endl;
}

void PrettyResultPrinter::OnTestCaseStart(const testing::TestCase& pTestCase)
{
  testing::Log::getOStream() << Color::GREEN << "[----------] " << Color::YELLOW;
  int num_tests = pTestCase.getNumOfTests();
  if (1 == num_tests)
    testing::Log::getOStream() << "1 test from ";
  else if (1 < num_tests)
    testing::Log::getOStream() << num_tests << " tests from ";

  testing::Log::getOStream() << pTestCase.getCaseName() << "."
                             << Color::RESET << std::endl;
}

void PrettyResultPrinter::OnTestStart(const testing::TestInfo& pTestInfo)
{
  testing::Log::getOStream() << Color::GREEN
                             << "[ RUN      ] " << Color::WHITE;
  PrintCaseName(pTestInfo.getCaseName(), pTestInfo.getTestName());
  testing::Log::getOStream() << Color::RESET << std::endl;
}

void PrettyResultPrinter::OnTestEnd(const testing::TestInfo& pTestInfo)
{
  // unit-test results
  if (pTestInfo.result().isPassed()) {
    testing::Log::getOStream() << Color::GREEN
                               << "[       OK ] \n";
  }
  if (pTestInfo.result().isFailed()) {
    testing::Log::getOStream() << Color::RED
                               << "[  FAILED  ] ";
    testing::TestResult::Reliability::const_iterator test =
                                    pTestInfo.result().reliability().begin();
    testing::TestResult::Reliability::const_iterator tEnd =
                                      pTestInfo.result().reliability().end();
    while (test != tEnd) {
      testing::Log::getOStream() << "\n" << Color::Bold(Color::YELLOW)
                                         << (*test)->filename()
                                         << ':'
                                         << (*test)->lineNumber()
                                         << ": ";
      switch ((*test)->type()) {
        case testing::TestPartResult::kFatalFailure: {
          testing::Log::getOStream()
                               << Color::RED
                               << "fatal: "
                               << Color::RESET << Color::Bold(Color::YELLOW)
                               << "failed to assert\n";
          break;
        }
        case testing::TestPartResult::kNonFatalFailure: {
          testing::Log::getOStream()
                               << Color::MAGENTA
                               << "error: "
                               << Color::RESET << Color::Bold(Color::YELLOW)
                               << "failed to expect\n";
          break;
        }
        default:
          break;
      }

      testing::Log::getOStream() << Color::RESET
                                 << (*test)->message();
      ++test;
    }
    testing::Log::getOStream() << std::endl;
  }

  // performance test results
  if (!pTestInfo.result().performance().empty()) {
    // timer's result
    testing::Log::getOStream() << Color::Bold(Color::BLUE)
                               << "[ TIME (ns)]";

    testing::TestResult::Performance::const_iterator perf =
                                      pTestInfo.result().performance().begin();
    testing::TestResult::Performance::const_iterator pEnd =
                                      pTestInfo.result().performance().end();

    while (perf != pEnd) {
      testing::Log::getOStream() << " " << std::setw(12)
                                 << (*perf)->getTimerNum();
      ++perf;
    }
    testing::Log::getOStream() << Color::RESET << std::endl;

    // perf_event's types
    testing::Log::getOStream() << Color::Bold(Color::BLUE)
                               << "[EVENT TYPE]";

    perf = pTestInfo.result().performance().begin();
    pEnd = pTestInfo.result().performance().end();

    while (perf != pEnd) {
      testing::Log::getOStream() << " [" << std::setw(10)
                                 << skypat::Perf_event_name[(*perf)->getPerfEventType()]
                                 << "]";
      ++perf;
    }
    testing::Log::getOStream() << Color::RESET << std::endl;

    // perf_event's results
    testing::Log::getOStream() << Color::Bold(Color::BLUE)
                               << "[RESULT NUM]";

    perf = pTestInfo.result().performance().begin();
    pEnd = pTestInfo.result().performance().end();

    while (perf != pEnd) {
      testing::Log::getOStream() << " " << std::setw(12)
                                 << (*perf)->getPerfEventNum();
      ++perf;
    }
    testing::Log::getOStream() << Color::RESET << std::endl;
  }
}

void PrettyResultPrinter::OnTestProgramEnd(const testing::UnitTest& pUnitTest)
{
  testing::Log::getOStream() << Color::CYAN << "[==========] "
                             << pUnitTest.getNumOfTests();
  if (1 == pUnitTest.getNumOfTests())
    testing::Log::getOStream() << " test from ";
  if (1 < pUnitTest.getNumOfTests())
    testing::Log::getOStream() << " tests from ";

  testing::Log::getOStream() << pUnitTest.getNumOfCases()
                             << " cases ran.\n";

  if (0 == pUnitTest.getNumOfFails()) {
    testing::Log::getOStream() << Color::GREEN << "[  PASSED  ] "
                               << Color::RESET
                               << pUnitTest.getNumOfTests();

    if (1 == pUnitTest.getNumOfTests()) {
      testing::Log::getOStream() << " test.\n"
                                 << std::flush;
    }

    if (1 < pUnitTest.getNumOfTests()) {
      testing::Log::getOStream() << " tests.\n"
                                 << std::flush;
    }
  }
  else {
    testing::Log::getOStream() << Color::RED << "[  FAILED  ] "
                               << Color::RESET
                               << pUnitTest.getNumOfFails();
    if (1 == pUnitTest.getNumOfFails()) {
      testing::Log::getOStream() << " test.\n"
                                 << std::flush;
    }

    if (1 < pUnitTest.getNumOfFails()) {
      testing::Log::getOStream() << " tests.\n"
                                 << std::flush;
    }
  }
}