Debian Codegraph @

test/TestToolsLib.cpp

raw
1 /*
2 * TestToolsLib.cpp
3 * ----------------
4 * Purpose: Unit test framework for libopenmpt.
5 * Notes : Currently somewhat unreadable :/
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10
11 #include "stdafx.h"
12 #include "TestToolsLib.h"
13
14
15 #ifdef ENABLE_TESTS
16 #ifndef MODPLUG_TRACKER
17
18
19 #include "mpt/string/utility.hpp"
20
21 #include <exception>
22 #include <iostream>
23
24 #include <cstdlib>
25
26
27 OPENMPT_NAMESPACE_BEGIN
28
29
30 namespace Test {
31
32
33
34 void mpt_test_reporter::case_run(const mpt::source_location& loc)
35 {
36 std::cout << "TEST..: " << MPT_AFORMAT("{}({}):")(loc.file_name() ? loc.file_name() : "", loc.line()) << ": " << std::endl;
37 }
38
39 void mpt_test_reporter::case_run(const mpt::source_location& loc, const char* text_e)
40 {
41 std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {}")(loc.file_name() ? loc.file_name() : "", loc.line(), text_e) << ": " << std::endl;
42 }
43
44 void mpt_test_reporter::case_run(const mpt::source_location& loc, const char* text_ex, const char* text_e)
45 {
46 if(text_ex)
47 {
48 std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {} throws {}")(loc.file_name() ? loc.file_name() : "", loc.line(), text_e, text_ex) << ": " << std::endl;
49 } else
50 {
51 std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {} throws")(loc.file_name() ? loc.file_name() : "", loc.line(), text_e) << ": " << std::endl;
52 }
53 }
54
55 void mpt_test_reporter::case_run(const mpt::source_location& loc, const char* text_a, const char* text_cmp, const char* text_b)
56 {
57 std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {} {} {}")(loc.file_name() ? loc.file_name() : "", loc.line(), text_a, text_cmp, text_b) << ": " << std::endl;
58 }
59
60 void mpt_test_reporter::case_result(const mpt::source_location& loc, const mpt::test::result& result)
61 {
62 MPT_UNUSED(loc);
63 if(std::holds_alternative<mpt::test::result_success>(result.info))
64 {
65 std::cout << "RESULT: PASS" << std::endl;
66 } else if(std::holds_alternative<mpt::test::result_failure>(result.info))
67 {
68 fail_count++;
69 std::cout << "RESULT: FAIL" << std::endl;
70 std::cout.flush();
71 std::cerr << "FAIL: " << "FAILURE: " << std::get<mpt::test::result_failure>(result.info).text << std::endl;
72 std::cerr.flush();
73 } else if(std::holds_alternative<mpt::test::result_unexpected_exception>(result.info))
74 {
75 fail_count++;
76 std::cout << "RESULT: FAIL" << std::endl;
77 std::cout.flush();
78 std::cerr << "FAIL: " << "UNEXPECTED EXCEPTION: " << std::get<mpt::test::result_unexpected_exception>(result.info).text << std::endl;
79 std::cerr.flush();
80 } else
81 {
82 fail_count++;
83 std::cout << "RESULT: FAIL" << std::endl;
84 std::cout.flush();
85 std::cerr << "FAIL: " << "UNKOWN" << std::endl;
86 std::cerr.flush();
87 }
88 }
89
90
91
92 int fail_count = 0;
93
94
95 static std::string remove_newlines(std::string str)
96 {
97 return mpt::replace(mpt::replace(str, std::string("\n"), std::string(" ")), std::string("\r"), std::string(" "));
98 }
99
100
101 Testcase::Testcase(Fatality fatality, Verbosity verbosity, const char * const desc, const mpt::source_location &loc)
102 : fatality(fatality)
103 , verbosity(verbosity)
104 , desc(desc)
105 , loc(loc)
106 {
107 return;
108 }
109
110
111 std::string Testcase::AsString() const
112 {
113 return MPT_AFORMAT("{}({}): {}")(loc.file_name() ? loc.file_name() : "", loc.line(), remove_newlines(desc));
114 }
115
116
117 void Testcase::ShowStart() const
118 {
119 switch(verbosity)
120 {
121 case VerbosityQuiet:
122 break;
123 case VerbosityNormal:
124 std::cout << "TEST..: " << AsString() << ": " << std::endl;
125 break;
126 case VerbosityVerbose:
127 std::cout << "TEST..: " << AsString() << ": " << std::endl;
128 break;
129 }
130 }
131
132
133 void Testcase::ShowProgress(const char * text) const
134 {
135 switch(verbosity)
136 {
137 case VerbosityQuiet:
138 break;
139 case VerbosityNormal:
140 break;
141 case VerbosityVerbose:
142 std::cout << "TEST..: " << AsString() << ": " << text << std::endl;
143 break;
144 }
145 }
146
147
148 void Testcase::ShowPass() const
149 {
150 switch(verbosity)
151 {
152 case VerbosityQuiet:
153 break;
154 case VerbosityNormal:
155 std::cout << "RESULT: PASS" << std::endl;
156 break;
157 case VerbosityVerbose:
158 std::cout << "PASS..: " << AsString() << std::endl;
159 break;
160 }
161 }
162
163
164 void Testcase::ShowFail(bool exception, const char * const text) const
165 {
166 switch(verbosity)
167 {
168 case VerbosityQuiet:
169 break;
170 case VerbosityNormal:
171 std::cout << "RESULT: FAIL" << std::endl;
172 break;
173 case VerbosityVerbose:
174 std::cout << "FAIL..: " << AsString() << std::endl;
175 break;
176 }
177 std::cout.flush();
178 if(!exception)
179 {
180 if(!text || (text && std::string(text).empty()))
181 {
182 std::cerr << "FAIL: " << AsString() << std::endl;
183 } else
184 {
185 std::cerr << "FAIL: " << AsString() << " : " << text << std::endl;
186 }
187 } else
188 {
189 if(!text || (text && std::string(text).empty()))
190 {
191 std::cerr << "FAIL: " << AsString() << " EXCEPTION!" << std::endl;
192 } else
193 {
194 std::cerr << "FAIL: " << AsString() << " EXCEPTION: " << text << std::endl;
195 }
196 }
197 std::cerr.flush();
198 }
199
200
201 void Testcase::ReportPassed()
202 {
203 ShowPass();
204 }
205
206
207 void Testcase::ReportFailed()
208 {
209 fail_count++;
210 ReportException();
211 }
212
213
214 void Testcase::ReportException()
215 {
216 try
217 {
218 throw; // get the exception
219 } catch(TestFailed & e)
220 {
221 ShowFail(false, e.values.c_str());
222 if(fatality == FatalityStop)
223 {
224 throw; // rethrow
225 }
226 } catch(std::exception & e)
227 {
228 ShowFail(true, e.what());
229 throw; // rethrow
230 } catch(...)
231 {
232 ShowFail(true);
233 throw; // rethrow
234 }
235 }
236
237
238 } // namespace Test
239
240
241 #if defined(MPT_ASSERT_HANDLER_NEEDED)
242
243 MPT_NOINLINE void AssertHandler(const mpt::source_location &loc, const char *expr, const char *msg)
244 {
245 Test::fail_count++;
246 if(msg)
247 {
248 mpt::log::GlobalLogger().SendLogMessage(loc, LogError, "ASSERT",
249 U_("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::Charset::ASCII, msg) + U_(" (") + mpt::ToUnicode(mpt::Charset::ASCII, expr) + U_(")")
250 );
251 } else
252 {
253 mpt::log::GlobalLogger().SendLogMessage(loc, LogError, "ASSERT",
254 U_("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::Charset::ASCII, expr)
255 );
256 }
257 #if defined(MPT_BUILD_FATAL_ASSERTS)
258 std::abort();
259 #endif // MPT_BUILD_FATAL_ASSERTS
260 }
261
262 #endif // MPT_ASSERT_HANDLER_NEEDED
263
264
265 OPENMPT_NAMESPACE_END
266
267
268 #endif // !MODPLUG_TRACKER
269 #endif // ENABLE_TESTS