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