fb51aaa0fbae711edd9f297e09babd5794c27742
[platform/core/security/key-manager.git] / tests / colour_log_formatter.cpp
1 /*
2  *  (C) Copyright Gennadiy Rozental 2005-2008.
3  *  Distributed under the Boost Software License, Version 1.0.
4  *  (See accompanying file LICENSE_1_0.txt or copy at
5  *  http://www.boost.org/LICENSE_1_0.txt)
6  *
7  *  See http://www.boost.org/libs/test for the library home page.
8  */
9 /*
10  * @file        colour_log_formatter.cpp
11  * @author      Zofia Abramowska (z.abramowska@samsung.com)
12  * @version
13  * @brief
14  */
15 // Boost.Test
16 #include <colour_log_formatter.h>
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
27 // Boost
28 #include <boost/version.hpp>
29
30 // STL
31 #include <iostream>
32 #include <string>
33
34 #include <dpl/colors.h>
35
36 // ************************************************************************** //
37 // **************            colour_log_formatter            ************** //
38 // ************************************************************************** //
39
40 using namespace boost::unit_test;
41 namespace CKM {
42
43 namespace {
44
45 const_string
46 test_unit_type_name(const test_unit &tu)
47 {
48 #if BOOST_VERSION >= 105900
49         return const_string(tu.p_type_name);
50 #else
51         return tu.p_type_name.get();
52 #endif
53 }
54
55 const_string
56 test_unit_name(const test_unit &tu)
57 {
58 #if BOOST_VERSION >= 105900
59         return const_string(tu.p_name);
60 #else
61         return tu.p_name.get();
62 #endif
63 }
64
65 const_string
66 test_phase_identifier()
67 {
68     return test_unit_name(framework::current_test_case());
69 }
70
71 const_string
72 get_basename(const const_string &file_name)
73 {
74         return basename(file_name.begin());
75 }
76
77 std::string
78 get_basename(const std::string &file_name)
79 {
80         return basename(file_name.c_str());
81 }
82
83 bool
84 test_unit_type_name_contains(const test_unit &tu, const std::string &substr)
85 {
86         return test_unit_type_name(tu).find(const_string(substr)) == 0;
87 }
88
89 } // local namespace
90
91 //____________________________________________________________________________//
92
93 void
94 colour_log_formatter::log_start(
95         std::ostream &output,
96         counter_t test_cases_amount)
97 {
98         if (test_cases_amount > 0)
99                 output  << "Running " << test_cases_amount << " test "
100                                 << (test_cases_amount > 1 ? "cases" : "case") << "...\n";
101 }
102
103 //____________________________________________________________________________//
104
105 void
106 colour_log_formatter::log_finish(std::ostream &ostr)
107 {
108         ostr.flush();
109 }
110
111 //____________________________________________________________________________//
112
113 void
114 colour_log_formatter::log_build_info(std::ostream &output)
115 {
116         output  << "Platform: " << BOOST_PLATFORM            << '\n'
117                         << "Compiler: " << BOOST_COMPILER            << '\n'
118                         << "STL     : " << BOOST_STDLIB              << '\n'
119                         << "Boost   : " << BOOST_VERSION / 100000      << "."
120                         << BOOST_VERSION / 100 % 1000  << "."
121                         << BOOST_VERSION % 100       << std::endl;
122 }
123
124 //____________________________________________________________________________//
125
126 void
127 colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info)
128 {
129         if (log_build_info)
130                 output  << "Platform: " << BOOST_PLATFORM            << '\n'
131                                 << "Compiler: " << BOOST_COMPILER            << '\n'
132                                 << "STL     : " << BOOST_STDLIB              << '\n';
133         output  << "Boost   : " << BOOST_VERSION / 100000      << '.'
134                         << BOOST_VERSION / 100 % 1000  << '.'
135                         << BOOST_VERSION % 100       << std::endl;
136 }
137
138 //____________________________________________________________________________//
139
140 void
141 colour_log_formatter::test_unit_start(
142         std::ostream &output,
143         test_unit const &tu)
144 {
145         if (test_unit_type_name_contains(tu, "suite")) {
146                 output << "Starting test ";
147         } else {
148                 output << "Running test ";
149         }
150         output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) 
151                << "\"" << std::endl;
152
153 }
154
155 //____________________________________________________________________________//
156
157 void
158 colour_log_formatter::test_unit_finish(
159         std::ostream &output,
160         test_unit const &tu,
161         unsigned long elapsed)
162 {
163         if (test_unit_type_name_contains(tu, "suite")) {
164                 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
165                            std::endl;
166                 return;
167         }
168
169         std::string color = CKM::Colors::Text::GREEN_BEGIN;
170         std::string status = "OK";
171
172         if (m_isTestCaseFailed) {
173                 color = CKM::Colors::Text::RED_BEGIN;
174                 status = "FAIL";
175         }
176
177         output << "\t" << "[   " << color << status << CKM::Colors::Text::COLOR_END <<
178                    "   ]";
179
180
181         output << ", " << CKM::Colors::Text::CYAN_BEGIN << "time: ";
182
183         if (elapsed > 0) {
184                 if (elapsed % 1000 == 0)
185                         output << elapsed / 1000 << "ms";
186                 else
187                         output << elapsed << "mks";
188         } else {
189                 output << "N/A";
190         }
191
192         output << CKM::Colors::Text::COLOR_END << std::endl;
193         m_isTestCaseFailed = false;
194 }
195
196 //____________________________________________________________________________//
197
198 void
199 colour_log_formatter::test_unit_skipped(
200         std::ostream &output,
201         test_unit const &tu)
202 {
203         output  << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
204                         "is skipped" << std::endl;
205 }
206
207 //____________________________________________________________________________//
208
209 void
210 colour_log_formatter::log_exception(
211         std::ostream &output,
212         log_checkpoint_data const &checkpoint_data,
213         boost::execution_exception const &ex)
214 {
215         boost::execution_exception::location const &loc = ex.where();
216         output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
217                            loc.m_file_name)
218                    << '(' << loc.m_line_num << "), ";
219
220         output << "fatal error in \""
221                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
222                    "\": ";
223
224         output << CKM::Colors::Text::COLOR_END << ex.what();
225
226         if (!checkpoint_data.m_file_name.is_empty()) {
227                 output << '\n';
228                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
229                            << '(' << checkpoint_data.m_line_num << ")";
230
231                 if (!checkpoint_data.m_message.empty())
232                         output << ": " << checkpoint_data.m_message;
233         }
234
235         output << std::endl;
236         m_isTestCaseFailed = true;
237 }
238
239 //____________________________________________________________________________//
240
241 void
242 colour_log_formatter::log_entry_start(
243         std::ostream &output,
244         log_entry_data const &entry_data,
245         log_entry_types let)
246 {
247         switch (let) {
248         case BOOST_UTL_ET_INFO:
249                 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
250                            "), ";
251                 output << "info: ";
252                 break;
253
254         case BOOST_UTL_ET_MESSAGE:
255                 break;
256
257         case BOOST_UTL_ET_WARNING:
258                 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
259                            entry_data.m_line_num << "), ";
260                 output << "warning in \"" << test_phase_identifier() << "\": ";
261                 break;
262
263         case BOOST_UTL_ET_ERROR:
264                 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN <<  get_basename(
265                                    entry_data.m_file_name)
266                            << '(' << entry_data.m_line_num << "), ";
267                 output << "error in \"" << test_phase_identifier() << "\": ";
268                 m_isTestCaseFailed = true;
269                 break;
270
271         case BOOST_UTL_ET_FATAL_ERROR:
272                 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN <<  get_basename(
273                                    entry_data.m_file_name)
274                            << '(' << entry_data.m_line_num << "),  ";
275                 output <<  " fatal error in \"" << test_phase_identifier() << "\": ";
276                 m_isTestCaseFailed = true;
277                 break;
278         }
279
280         output << CKM::Colors::Text::COLOR_END;
281 }
282
283 //____________________________________________________________________________//
284
285 void
286 colour_log_formatter::log_entry_value(
287         std::ostream &output,
288         const_string value)
289 {
290         output << value;
291 }
292
293 //____________________________________________________________________________//
294
295 void
296 colour_log_formatter::log_entry_value(
297         std::ostream &output,
298         lazy_ostream const &value)
299 {
300         output << value;
301 }
302
303 //____________________________________________________________________________//
304
305 void
306 colour_log_formatter::log_entry_finish(
307         std::ostream &output)
308 {
309         output << std::endl;
310 }
311
312 //____________________________________________________________________________//
313
314 #if BOOST_VERSION >= 106501
315 void colour_log_formatter::log_exception_start(std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex)
316 {
317         (void)os;
318         (void)lcd;
319         (void)ex;
320 }
321
322 void colour_log_formatter::log_exception_finish(std::ostream& os)
323 {
324         (void)os;
325 }
326
327 void colour_log_formatter::entry_context_start(std::ostream& os, boost::unit_test::log_level l)
328 {
329         (void)os;
330         (void)l;
331 }
332
333 void colour_log_formatter::log_entry_context(std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value)
334 {
335     (void)os;
336     (void)l;
337     (void)value;
338 }
339
340 void colour_log_formatter::entry_context_finish(std::ostream& os, boost::unit_test::log_level l)
341 {
342     (void)os;
343     (void)l;
344 }
345 #endif
346
347 //____________________________________________________________________________//
348
349 } // namespace CKM
350
351 //____________________________________________________________________________//
352