UTPP
Loading...
Searching...
No Matches
reporter_dbgout.h
Go to the documentation of this file.
1#pragma once
2/*
3 UTPP - A New Generation of UnitTest++
4 (c) Mircea Neacsu 2017-2024
5
6 See LICENSE file for full copyright information.
7*/
8
13
14#include <iomanip>
15
16#ifdef _WIN32
17#ifndef WIN32_LEAN_AND_MEAN
18#define WIN32_LEAN_AND_MEAN
19#define UTPP_MUST_UNDEF_LEAN_AND_MEAN
20#endif
21
22#ifndef NOMINMAX
23#define NOMINMAX
24#define UTPP_MUST_UNDEF_NOMINMAX
25#endif
26
27#include <windows.h>
28
29#ifdef UTPP_MUST_UNDEF_NOMINMAX
30#undef NOMINMAX
31#endif
32
33#ifdef UTPP_MUST_UNDEF_LEAN_AND_MEAN
34#undef WIN32_LEAN_AND_MEAN
35#endif
36
37#undef UTPP_MUST_UNDEF_LEAN_AND_MEAN
38#undef UTPP_MUST_UNDEF_NOMINMAX
39#endif
40
41namespace UnitTest {
42
44class ReporterDbgout : public Reporter
45{
46protected:
47 void SuiteStart (const TestSuite& suite) override;
48 void TestStart (const Test& test) override;
49 void TestFinish (const Test& test) override;
50 int SuiteFinish (const TestSuite& suite) override;
51
52 void ReportFailure (const Failure& failure) override;
53 int Summary () override;
54private:
55#ifdef _UNICODE
56 std::wstring widen (const std::string& s);
57 inline void ODS (std::stringstream& ss) {
58 OutputDebugString (widen (ss.str ()).c_str ());
59 }
60#else
61 inline void ODS (std::stringstream& ss) {
62 OutputDebugString (ss.str ().c_str());
63 }
64#endif
65};
66
67
69inline
71{
73 if (!trace)
74 return;
75
76 std::stringstream ss;
77 ss << "Suite starting: " << suite.name << std::endl;
78 ODS (ss);
79}
80
82inline
84{
86 if (!trace)
87 return;
88 std::stringstream ss;
89 ss << "Test starting: " << test.test_name () << std::endl;
90 ODS (ss);
91}
92
94inline
96{
97 if (trace)
98 {
99 std::stringstream ss;
100 ss << "Test finished: " << test.test_name () << std::endl;
101 ODS (ss);
102 }
104}
105
107inline
109{
110 if (trace)
111 {
112 std::stringstream ss;
113 ss << "Suite finishing: " << suite.name << std::endl;
114 ODS (ss);
115 }
116 return Reporter::SuiteFinish (suite);
117}
118
119
126inline
128{
129 std::stringstream ss;
130 ss << "Failure in ";
131 if (CurrentTest)
132 {
134 ss << "suite " << CurrentSuite << ' ';
135 ss << "test " << CurrentTest->test_name ();
136 }
137 ss << std::endl;
138 ODS (ss);
139
140 ss.clear ();
141 ss.str ("");
142 ss << failure.filename << "(" << failure.line_number << "):"
143 << failure.message << std::endl;
144 ODS (ss);
145 Reporter::ReportFailure (failure);
146}
147
152inline
154{
155 using namespace std::chrono;
156
157 std::stringstream ss;
158 if (total_failed_count > 0)
159 {
160 ss << "FAILURE: " << total_failed_count << " out of "
161 << total_test_count << " tests failed (" << total_failures_count
162 << " failures).";
163 }
164 else
165 {
166 ss << "Success: " << total_test_count << " tests passed.";
167 }
168 ss << std::endl;
169 ODS (ss);
170
171 ss.clear ();
172 ss.str ("");
173 ss.setf (std::ios::fixed);
174 auto total_time_s = duration_cast<duration<float, std::chrono::seconds::period>>(total_time);
175 ss << "Run time: " << std::setprecision (2) << total_time_s.count();
176 ODS (ss);
177
178 return Reporter::Summary ();
179}
180
181#ifdef _UNICODE
183inline
184std::wstring ReporterDbgout::widen (const std::string& s)
185{
186 int nsz = (int)s.size ();
187 int wsz = MultiByteToWideChar (CP_UTF8, 0, s.c_str (), nsz, 0, 0);
188 std::wstring out (wsz, 0);
189 if (wsz)
190 MultiByteToWideChar (CP_UTF8, 0, s.c_str (), nsz, &out[0], wsz);
191 return out;
192}
193#endif
194
195}
A Reporter that sends messages to debug output.
Definition reporter_dbgout.h:45
void SuiteStart(const TestSuite &suite) override
If tracing is enabled, show a suite start message.
Definition reporter_dbgout.h:70
void TestStart(const Test &test) override
If tracing is enabled, show a test start message.
Definition reporter_dbgout.h:83
void ReportFailure(const Failure &failure) override
Definition reporter_dbgout.h:127
int Summary() override
Definition reporter_dbgout.h:153
int SuiteFinish(const TestSuite &suite) override
If tracing is enabled, show a suite finish message.
Definition reporter_dbgout.h:108
void TestFinish(const Test &test) override
If tracing is enabled, show a test finish message.
Definition reporter_dbgout.h:95
int total_failures_count
total number of failures
Definition utpp.h:320
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:583
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:590
std::chrono::milliseconds total_time
total running time in milliseconds
Definition utpp.h:321
bool trace
true if tracing is enabled
Definition utpp.h:324
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:602
int total_test_count
total number of tests
Definition utpp.h:318
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:597
virtual int Summary()
Generate results report.
Definition utpp.h:307
int total_failed_count
total number of failed tests
Definition utpp.h:319
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:619
Representation of a test case.
Definition utpp.h:243
const std::string & test_name() const
Return test name.
Definition utpp.h:546
Definition utpp.h:365
std::string name
Suite name.
Definition utpp.h:392
The failure object records the file name, the line number and a message.
Definition utpp.h:274
int line_number
Line number where the failure has occurred.
Definition utpp.h:277
std::string message
Description of failure.
Definition utpp.h:276
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:275
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:64
Test * CurrentTest
Currently executing test.
std::string CurrentSuite
Name of currently running suite.