Fix build for boost 1.71.0
[platform/core/security/libwebappenc.git] / tests / colour_log_formatter.cpp
1 /*
2  *  Boost Software License - Version 1.0 - August 17th, 2003
3  *
4  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6  *  FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
7  *  SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
8  *  FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
9  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
10  *  DEALINGS IN THE SOFTWARE.
11  */
12 #include "colour_log_formatter.h"
13
14 #include <iostream>
15 #include <string>
16
17 #include <boost/test/impl/execution_monitor.ipp>
18 #if BOOST_VERSION >= 105900
19 #include <boost/test/tree/test_unit.hpp>
20 #else
21 #include <boost/test/unit_test_suite_impl.hpp>
22 #endif
23 #include <boost/test/framework.hpp>
24 #include <boost/test/utils/basic_cstring/io.hpp>
25 #include <boost/test/utils/lazy_ostream.hpp>
26 #include <boost/version.hpp>
27
28 // ************************************************************************** //
29 // **************            colour_log_formatter            ************** //
30 // ************************************************************************** //
31
32 using namespace boost::unit_test;
33 namespace Wae {
34 namespace Test {
35
36 namespace {
37
38 const char *GREEN_BEGIN = "\033[0;32m";
39 const char *RED_BEGIN = "\033[0;31m";
40 const char *CYAN_BEGIN = "\033[0;36m";
41 const char *BOLD_YELLOW_BEGIN = "\033[1;33m";
42 const char *COLOR_END = "\033[m";
43
44 const_string
45 test_unit_type_name(const test_unit &tu)
46 {
47 #if BOOST_VERSION >= 105900
48         return const_string(tu.p_type_name);
49 #else
50         return tu.p_type_name.get();
51 #endif
52 }
53
54 const_string
55 test_unit_name(const test_unit &tu)
56 {
57 #if BOOST_VERSION >= 105900
58         return const_string(tu.p_name);
59 #else
60         return tu.p_name.get();
61 #endif
62 }
63
64 const_string
65 test_phase_identifier()
66 {
67         return test_unit_name(framework::current_test_case());
68 }
69
70 const_string
71 get_basename(const const_string &file_name)
72 {
73         return basename(file_name.begin());
74 }
75
76 std::string
77 get_basename(const std::string &file_name)
78 {
79         return basename(file_name.c_str());
80 }
81
82 bool
83 test_unit_type_name_contains(const test_unit &tu, const std::string &substr)
84 {
85         return test_unit_type_name(tu).find(const_string(substr)) == 0;
86 }
87
88 } // local namespace
89
90 //____________________________________________________________________________//
91
92 void
93 colour_log_formatter::log_start(
94         std::ostream &output,
95         counter_t test_cases_amount)
96 {
97         if (test_cases_amount > 0)
98                 output  << "Running " << test_cases_amount << " test "
99                                 << (test_cases_amount > 1 ? "cases" : "case") << "..." << std::endl;
100 }
101
102 //____________________________________________________________________________//
103
104 void
105 colour_log_formatter::log_finish(std::ostream &ostr)
106 {
107         ostr.flush();
108 }
109
110 //____________________________________________________________________________//
111
112 void
113 colour_log_formatter::log_build_info(std::ostream &output)
114 {
115         output << "Platform: " << BOOST_PLATFORM << std::endl
116                    << "Compiler: " << BOOST_COMPILER << std::endl
117                    << "STL       : " << BOOST_STDLIB << std::endl
118                    << "Boost   : " << BOOST_VERSION / 100000 << "."
119                    << BOOST_VERSION / 100 % 1000 << "."
120                    << BOOST_VERSION % 100 << std::endl;
121 }
122
123
124
125 //____________________________________________________________________________//
126
127 void
128 colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info)
129 {
130         if (log_build_info)
131                 output  << "Platform: " << BOOST_PLATFORM            << '\n'
132                                 << "Compiler: " << BOOST_COMPILER            << '\n'
133                                 << "STL     : " << BOOST_STDLIB              << '\n';
134
135         output << "Boost   : " << BOOST_VERSION / 100000      << '.'
136                         << BOOST_VERSION / 100 % 1000  << '.'
137                         << BOOST_VERSION % 100       << std::endl;
138 }
139
140 //____________________________________________________________________________//
141
142 void
143 colour_log_formatter::test_unit_start(
144         std::ostream &output,
145         test_unit const &tu)
146 {
147         if (test_unit_type_name_contains(tu, "suite")) {
148                 output << "Starting test ";
149         } else {
150                 output << "Running test ";
151         }
152         output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
153                            std::endl;
154 }
155
156 //____________________________________________________________________________//
157
158 void
159 colour_log_formatter::test_unit_finish(
160         std::ostream &output,
161         test_unit const &tu,
162         unsigned long elapsed)
163 {
164         if (test_unit_type_name_contains(tu, "suite")) {
165                 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
166                            std::endl;
167                 return;
168         }
169
170         std::string color = GREEN_BEGIN;
171         std::string status = "OK";
172
173         if (m_isTestCaseFailed) {
174                 color = RED_BEGIN;
175                 status = "FAIL";
176         }
177
178         output << "\t" << "[   " << color << status << COLOR_END << "   ]";
179
180         output << ", " << CYAN_BEGIN << "time: ";
181
182         if (elapsed > 0) {
183                 if (elapsed % 1000 == 0)
184                         output << elapsed / 1000 << "ms";
185                 else
186                         output << elapsed << "mks";
187         } else {
188                 output << "N/A";
189         }
190
191         output << COLOR_END << std::endl;
192         m_isTestCaseFailed = false;
193 }
194
195 //____________________________________________________________________________//
196
197 void
198 colour_log_formatter::test_unit_skipped(
199         std::ostream &output,
200         test_unit const &tu)
201 {
202         output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
203                    << "\"" << "is skipped" << std::endl;
204 }
205
206 //____________________________________________________________________________//
207
208 void
209 colour_log_formatter::log_exception(
210         std::ostream &output,
211         log_checkpoint_data const &checkpoint_data,
212         boost::execution_exception const &ex)
213 {
214         boost::execution_exception::location const &loc = ex.where();
215         output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
216                    << '(' << loc.m_line_num << "), ";
217
218         output << "fatal error in \""
219                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
220                    "\": ";
221
222         output << COLOR_END << ex.what();
223
224         if (!checkpoint_data.m_file_name.is_empty()) {
225                 output << '\n';
226                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
227                            << '(' << checkpoint_data.m_line_num << ")";
228
229                 if (!checkpoint_data.m_message.empty())
230                         output << ": " << checkpoint_data.m_message;
231         }
232
233         output << std::endl;
234         m_isTestCaseFailed = true;
235 }
236
237 void
238 colour_log_formatter::log_exception_start(
239         std::ostream &output,
240         log_checkpoint_data const &checkpoint_data,
241         boost::execution_exception const &ex)
242 {
243         boost::execution_exception::location const &loc = ex.where();
244         output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
245                    << '(' << loc.m_line_num << "), ";
246
247         output << "fatal error in \""
248                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
249                    "\": ";
250
251         output << COLOR_END << ex.what();
252
253         if (!checkpoint_data.m_file_name.is_empty()) {
254                 output << '\n';
255                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
256                            << '(' << checkpoint_data.m_line_num << ")";
257
258                 if (!checkpoint_data.m_message.empty())
259                         output << ": " << checkpoint_data.m_message;
260         }
261
262         output << std::endl;
263         m_isTestCaseFailed = true;
264 }
265
266 void
267 colour_log_formatter::log_exception_finish(std::ostream &os)
268 {
269         os.flush();
270 }
271
272 //____________________________________________________________________________//
273
274 void
275 colour_log_formatter::log_entry_start(
276         std::ostream &output,
277         log_entry_data const &entry_data,
278         log_entry_types let)
279 {
280         switch (let) {
281         case BOOST_UTL_ET_INFO:
282                 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
283                            "), ";
284                 output << "info: ";
285                 break;
286
287         case BOOST_UTL_ET_MESSAGE:
288                 break;
289
290         case BOOST_UTL_ET_WARNING:
291                 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
292                            entry_data.m_line_num << "), ";
293                 output << "warning in \"" << test_phase_identifier() << "\": ";
294                 break;
295
296         case BOOST_UTL_ET_ERROR:
297                 output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
298                            << '(' << entry_data.m_line_num << "), ";
299                 output << "error in \"" << test_phase_identifier() << "\": ";
300                 m_isTestCaseFailed = true;
301                 break;
302
303         case BOOST_UTL_ET_FATAL_ERROR:
304                 output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
305                            << '(' << entry_data.m_line_num << "),  ";
306                 output <<  " fatal error in \"" << test_phase_identifier() << "\": ";
307                 m_isTestCaseFailed = true;
308                 break;
309         }
310
311         output << COLOR_END;
312 }
313
314 //____________________________________________________________________________//
315
316 void
317 colour_log_formatter::log_entry_value(
318         std::ostream &output,
319         const_string value)
320 {
321         output << value;
322 }
323
324 //____________________________________________________________________________//
325
326 void
327 colour_log_formatter::log_entry_value(
328         std::ostream &output,
329         lazy_ostream const &value)
330 {
331         output << value;
332 }
333
334 //____________________________________________________________________________//
335
336 void
337 colour_log_formatter::log_entry_finish(
338         std::ostream &output)
339 {
340         output << std::endl;
341 }
342
343 //____________________________________________________________________________//
344
345 void
346 colour_log_formatter::entry_context_start(
347                 std::ostream& output,
348                 boost::unit_test::log_level l)
349 {
350         output << (l == log_successful_tests ? "\nAssertion" : "\nFailure")
351                    << " occurred in a following context:";
352 }
353
354 void
355 colour_log_formatter::log_entry_context(
356                 std::ostream& output,
357                 boost::unit_test::const_string value)
358 {
359         output << "\n    " << value;
360 }
361
362 void
363 colour_log_formatter::entry_context_finish(std::ostream& output)
364 {
365         output.flush();
366 }
367
368 #if BOOST_VERSION >= 106500
369 void
370 colour_log_formatter::log_entry_context(std::ostream& os, log_level l, const_string value)
371 {
372         (void)l;
373         os << "\n    " << value;
374 }
375 void
376 colour_log_formatter::entry_context_finish(std::ostream& os, log_level l)
377 {
378         (void)l;
379         os.flush();
380 }
381 #endif
382
383 //____________________________________________________________________________//
384 } // namespace Test
385 } // namespace Wae
386
387 //____________________________________________________________________________//
388