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