041fef8bde878757a54b8cc2057a0d9f7ab4d2d1
[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 void
126 colour_log_formatter::test_unit_start(
127         std::ostream &output,
128         test_unit const &tu)
129 {
130         if (test_unit_type_name_contains(tu, "suite")) {
131                 output << "Starting test ";
132         } else {
133                 output << "Running test ";
134         }
135         output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
136                            std::endl;
137 }
138
139 //____________________________________________________________________________//
140
141 void
142 colour_log_formatter::test_unit_finish(
143         std::ostream &output,
144         test_unit const &tu,
145         unsigned long elapsed)
146 {
147         if (test_unit_type_name_contains(tu, "suite")) {
148                 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
149                            std::endl;
150                 return;
151         }
152
153         std::string color = GREEN_BEGIN;
154         std::string status = "OK";
155
156         if (m_isTestCaseFailed) {
157                 color = RED_BEGIN;
158                 status = "FAIL";
159         }
160
161         output << "\t" << "[   " << color << status << COLOR_END << "   ]";
162
163         output << ", " << CYAN_BEGIN << "time: ";
164
165         if (elapsed > 0) {
166                 if (elapsed % 1000 == 0)
167                         output << elapsed / 1000 << "ms";
168                 else
169                         output << elapsed << "mks";
170         } else {
171                 output << "N/A";
172         }
173
174         output << COLOR_END << std::endl;
175         m_isTestCaseFailed = false;
176 }
177
178 //____________________________________________________________________________//
179
180 void
181 colour_log_formatter::test_unit_skipped(
182         std::ostream &output,
183         test_unit const &tu)
184 {
185         output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
186                    << "\"" << "is skipped" << std::endl;
187 }
188
189 //____________________________________________________________________________//
190
191 void
192 colour_log_formatter::log_exception(
193         std::ostream &output,
194         log_checkpoint_data const &checkpoint_data,
195         boost::execution_exception const &ex)
196 {
197         boost::execution_exception::location const &loc = ex.where();
198         output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
199                    << '(' << loc.m_line_num << "), ";
200
201         output << "fatal error in \""
202                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
203                    "\": ";
204
205         output << COLOR_END << ex.what();
206
207         if (!checkpoint_data.m_file_name.is_empty()) {
208                 output << '\n';
209                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
210                            << '(' << checkpoint_data.m_line_num << ")";
211
212                 if (!checkpoint_data.m_message.empty())
213                         output << ": " << checkpoint_data.m_message;
214         }
215
216         output << std::endl;
217         m_isTestCaseFailed = true;
218 }
219
220 void
221 colour_log_formatter::log_exception_start(
222         std::ostream &output,
223         log_checkpoint_data const &checkpoint_data,
224         boost::execution_exception const &ex)
225 {
226         boost::execution_exception::location const &loc = ex.where();
227         output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
228                    << '(' << loc.m_line_num << "), ";
229
230         output << "fatal error in \""
231                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
232                    "\": ";
233
234         output << COLOR_END << ex.what();
235
236         if (!checkpoint_data.m_file_name.is_empty()) {
237                 output << '\n';
238                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
239                            << '(' << checkpoint_data.m_line_num << ")";
240
241                 if (!checkpoint_data.m_message.empty())
242                         output << ": " << checkpoint_data.m_message;
243         }
244
245         output << std::endl;
246         m_isTestCaseFailed = true;
247 }
248
249 void
250 colour_log_formatter::log_exception_finish(std::ostream &os)
251 {
252         os.flush();
253 }
254
255 //____________________________________________________________________________//
256
257 void
258 colour_log_formatter::log_entry_start(
259         std::ostream &output,
260         log_entry_data const &entry_data,
261         log_entry_types let)
262 {
263         switch (let) {
264         case BOOST_UTL_ET_INFO:
265                 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
266                            "), ";
267                 output << "info: ";
268                 break;
269
270         case BOOST_UTL_ET_MESSAGE:
271                 break;
272
273         case BOOST_UTL_ET_WARNING:
274                 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
275                            entry_data.m_line_num << "), ";
276                 output << "warning in \"" << test_phase_identifier() << "\": ";
277                 break;
278
279         case BOOST_UTL_ET_ERROR:
280                 output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
281                            << '(' << entry_data.m_line_num << "), ";
282                 output << "error in \"" << test_phase_identifier() << "\": ";
283                 m_isTestCaseFailed = true;
284                 break;
285
286         case BOOST_UTL_ET_FATAL_ERROR:
287                 output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
288                            << '(' << entry_data.m_line_num << "),  ";
289                 output <<  " fatal error in \"" << test_phase_identifier() << "\": ";
290                 m_isTestCaseFailed = true;
291                 break;
292         }
293
294         output << COLOR_END;
295 }
296
297 //____________________________________________________________________________//
298
299 void
300 colour_log_formatter::log_entry_value(
301         std::ostream &output,
302         const_string value)
303 {
304         output << value;
305 }
306
307 //____________________________________________________________________________//
308
309 void
310 colour_log_formatter::log_entry_value(
311         std::ostream &output,
312         lazy_ostream const &value)
313 {
314         output << value;
315 }
316
317 //____________________________________________________________________________//
318
319 void
320 colour_log_formatter::log_entry_finish(
321         std::ostream &output)
322 {
323         output << std::endl;
324 }
325
326 //____________________________________________________________________________//
327
328 void
329 colour_log_formatter::entry_context_start(
330                 std::ostream& output,
331                 boost::unit_test::log_level l)
332 {
333         output << (l == log_successful_tests ? "\nAssertion" : "\nFailure")
334                    << " occurred in a following context:";
335 }
336
337 void
338 colour_log_formatter::log_entry_context(
339                 std::ostream& output,
340                 boost::unit_test::const_string value)
341 {
342         output << "\n    " << value;
343 }
344
345 void
346 colour_log_formatter::entry_context_finish(std::ostream& output)
347 {
348         output.flush();
349 }
350
351 #if BOOST_VERSION >= 106500
352 void
353 colour_log_formatter::log_entry_context(std::ostream& os, log_level l, const_string value)
354 {
355         (void)l;
356         os << "\n    " << value;
357 }
358 void
359 colour_log_formatter::entry_context_finish(std::ostream& os, log_level l)
360 {
361         (void)l;
362         os.flush();
363 }
364 #endif
365
366 //____________________________________________________________________________//
367 } // namespace Test
368 } // namespace Wae
369
370 //____________________________________________________________________________//
371