Migrate secure-storage data
[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/unit_test_suite_impl.hpp>
18 #include <boost/test/framework.hpp>
19 #include <boost/test/utils/basic_cstring/io.hpp>
20 #include <boost/test/utils/lazy_ostream.hpp>
21
22 // Boost
23 #include <boost/version.hpp>
24
25 // STL
26 #include <iostream>
27 #include <string>
28
29 #include <dpl/colors.h>
30
31 // ************************************************************************** //
32 // **************            colour_log_formatter            ************** //
33 // ************************************************************************** //
34
35 using namespace boost::unit_test;
36 namespace CKM {
37
38 namespace {
39
40 const_string
41 test_phase_identifier()
42 {
43         return framework::is_initialized()
44                    ? const_string(framework::current_test_case().p_name.get())
45                    : BOOST_TEST_L("Test setup");
46 }
47
48 const_string
49 get_basename(const const_string &file_name)
50 {
51         return basename(file_name.begin());
52 }
53
54 std::string
55 get_basename(const std::string &file_name)
56 {
57         return basename(file_name.c_str());
58 }
59
60 } // local namespace
61
62 //____________________________________________________________________________//
63
64 void
65 colour_log_formatter::log_start(
66         std::ostream &output,
67         counter_t test_cases_amount)
68 {
69         if (test_cases_amount > 0)
70                 output  << "Running " << test_cases_amount << " test "
71                                 << (test_cases_amount > 1 ? "cases" : "case") << "...\n";
72 }
73
74 //____________________________________________________________________________//
75
76 void
77 colour_log_formatter::log_finish(std::ostream &ostr)
78 {
79         ostr.flush();
80 }
81
82 //____________________________________________________________________________//
83
84 void
85 colour_log_formatter::log_build_info(std::ostream &output)
86 {
87         output  << "Platform: " << BOOST_PLATFORM            << '\n'
88                         << "Compiler: " << BOOST_COMPILER            << '\n'
89                         << "STL     : " << BOOST_STDLIB              << '\n'
90                         << "Boost   : " << BOOST_VERSION / 100000      << "."
91                         << BOOST_VERSION / 100 % 1000  << "."
92                         << BOOST_VERSION % 100       << std::endl;
93 }
94
95 //____________________________________________________________________________//
96
97 void
98 colour_log_formatter::test_unit_start(
99         std::ostream &output,
100         test_unit const &tu)
101 {
102         if (tu.p_type_name->find(const_string("suite")) == 0) {
103                 output << "Starting test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
104                            std::endl;
105         } else {
106                 output << "Running test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
107                            std::endl;
108         }
109 }
110
111 //____________________________________________________________________________//
112
113 void
114 colour_log_formatter::test_unit_finish(
115         std::ostream &output,
116         test_unit const &tu,
117         unsigned long elapsed)
118 {
119         if (tu.p_type_name->find(const_string("suite")) == 0) {
120                 output << "Finished test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
121                            std::endl;
122                 return;
123         }
124
125         std::string color = CKM::Colors::Text::GREEN_BEGIN;
126         std::string status = "OK";
127
128         if (m_isTestCaseFailed) {
129                 color = CKM::Colors::Text::RED_BEGIN;
130                 status = "FAIL";
131         }
132
133         output << "\t" << "[   " << color << status << CKM::Colors::Text::COLOR_END <<
134                    "   ]";
135
136
137         output << ", " << CKM::Colors::Text::CYAN_BEGIN << "time: ";
138
139         if (elapsed > 0) {
140                 if (elapsed % 1000 == 0)
141                         output << elapsed / 1000 << "ms";
142                 else
143                         output << elapsed << "mks";
144         } else {
145                 output << "N/A";
146         }
147
148         output << CKM::Colors::Text::COLOR_END << std::endl;
149         m_isTestCaseFailed = false;
150 }
151
152 //____________________________________________________________________________//
153
154 void
155 colour_log_formatter::test_unit_skipped(
156         std::ostream &output,
157         test_unit const &tu)
158 {
159         output  << "Test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
160                         "is skipped" << std::endl;
161 }
162
163 //____________________________________________________________________________//
164
165 void
166 colour_log_formatter::log_exception(
167         std::ostream &output,
168         log_checkpoint_data const &checkpoint_data,
169         boost::execution_exception const &ex)
170 {
171         boost::execution_exception::location const &loc = ex.where();
172         output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
173                            loc.m_file_name)
174                    << '(' << loc.m_line_num << "), ";
175
176         output << "fatal error in \""
177                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
178                    "\": ";
179
180         output << CKM::Colors::Text::COLOR_END << ex.what();
181
182         if (!checkpoint_data.m_file_name.is_empty()) {
183                 output << '\n';
184                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
185                            << '(' << checkpoint_data.m_line_num << ")";
186
187                 if (!checkpoint_data.m_message.empty())
188                         output << ": " << checkpoint_data.m_message;
189         }
190
191         output << std::endl;
192         m_isTestCaseFailed = true;
193 }
194
195 //____________________________________________________________________________//
196
197 void
198 colour_log_formatter::log_entry_start(
199         std::ostream &output,
200         log_entry_data const &entry_data,
201         log_entry_types let)
202 {
203         switch (let) {
204         case BOOST_UTL_ET_INFO:
205                 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
206                            "), ";
207                 output << "info: ";
208                 break;
209
210         case BOOST_UTL_ET_MESSAGE:
211                 break;
212
213         case BOOST_UTL_ET_WARNING:
214                 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
215                            entry_data.m_line_num << "), ";
216                 output << "warning in \"" << test_phase_identifier() << "\": ";
217                 break;
218
219         case BOOST_UTL_ET_ERROR:
220                 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN <<  get_basename(
221                                    entry_data.m_file_name)
222                            << '(' << entry_data.m_line_num << "), ";
223                 output << "error in \"" << test_phase_identifier() << "\": ";
224                 m_isTestCaseFailed = true;
225                 break;
226
227         case BOOST_UTL_ET_FATAL_ERROR:
228                 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN <<  get_basename(
229                                    entry_data.m_file_name)
230                            << '(' << entry_data.m_line_num << "),  ";
231                 output <<  " fatal error in \"" << test_phase_identifier() << "\": ";
232                 m_isTestCaseFailed = true;
233                 break;
234         }
235
236         output << CKM::Colors::Text::COLOR_END;
237 }
238
239 //____________________________________________________________________________//
240
241 void
242 colour_log_formatter::log_entry_value(
243         std::ostream &output,
244         const_string value)
245 {
246         output << value;
247 }
248
249 //____________________________________________________________________________//
250
251 void
252 colour_log_formatter::log_entry_value(
253         std::ostream &output,
254         lazy_ostream const &value)
255 {
256         output << value;
257 }
258
259 //____________________________________________________________________________//
260
261 void
262 colour_log_formatter::log_entry_finish(
263         std::ostream &output)
264 {
265         output << std::endl;
266 }
267
268 //____________________________________________________________________________//
269
270 //____________________________________________________________________________//
271
272 } // namespace CKM
273
274 //____________________________________________________________________________//
275