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