Fix C code naming rules & minor fixes
[platform/core/security/libwebappenc.git] / tests / colour_log_formatter.cpp
1 /*
2  *  Boost Software License - Version 1.0 - August 17th, 2003
3  *
4  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6  *  FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
7  *  SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
8  *  FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
9  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
10  *  DEALINGS IN THE SOFTWARE.
11  */
12 #include "colour_log_formatter.h"
13
14 #include <iostream>
15 #include <string>
16
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 #include <boost/version.hpp>
22
23 // ************************************************************************** //
24 // **************            colour_log_formatter            ************** //
25 // ************************************************************************** //
26
27 using namespace boost::unit_test;
28 namespace Wae {
29 namespace Test {
30
31 namespace {
32
33 const char *GREEN_BEGIN = "\033[0;32m";
34 const char *RED_BEGIN = "\033[0;31m";
35 const char *CYAN_BEGIN = "\033[0;36m";
36 const char *BOLD_YELLOW_BEGIN = "\033[1;33m";
37 const char *COLOR_END = "\033[m";
38
39 const_string
40 test_phase_identifier()
41 {
42         return framework::is_initialized()
43                    ? const_string(framework::current_test_case().p_name.get())
44                    : BOOST_TEST_L("Test setup");
45 }
46
47 const_string
48 get_basename(const const_string &file_name)
49 {
50         return basename(file_name.begin());
51 }
52
53 std::string
54 get_basename(const std::string &file_name)
55 {
56         return basename(file_name.c_str());
57 }
58
59 } // local namespace
60
61 //____________________________________________________________________________//
62
63 void
64 colour_log_formatter::log_start(
65         std::ostream &output,
66         counter_t test_cases_amount)
67 {
68         if (test_cases_amount > 0)
69                 output  << "Running " << test_cases_amount << " test "
70                                 << (test_cases_amount > 1 ? "cases" : "case") << "..." << std::endl;
71 }
72
73 //____________________________________________________________________________//
74
75 void
76 colour_log_formatter::log_finish(std::ostream &ostr)
77 {
78         ostr.flush();
79 }
80
81 //____________________________________________________________________________//
82
83 void
84 colour_log_formatter::log_build_info(std::ostream &output)
85 {
86         output << "Platform: " << BOOST_PLATFORM << std::endl
87                    << "Compiler: " << BOOST_COMPILER << std::endl
88                    << "STL       : " << BOOST_STDLIB << std::endl
89                    << "Boost   : " << BOOST_VERSION / 100000 << "."
90                    << BOOST_VERSION / 100 % 1000 << "."
91                    << BOOST_VERSION % 100 << std::endl;
92 }
93
94 //____________________________________________________________________________//
95
96 void
97 colour_log_formatter::test_unit_start(
98         std::ostream &output,
99         test_unit const &tu)
100 {
101         if (tu.p_type_name->find(const_string("suite")) == 0)
102                 output << "Starting test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
103                            std::endl;
104         else
105                 output << "Running test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
106                            std::endl;
107 }
108
109 //____________________________________________________________________________//
110
111 void
112 colour_log_formatter::test_unit_finish(
113         std::ostream &output,
114         test_unit const &tu,
115         unsigned long elapsed)
116 {
117         if (tu.p_type_name->find(const_string("suite")) == 0) {
118                 output << "Finished test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
119                            std::endl;
120                 return;
121         }
122
123         std::string color = GREEN_BEGIN;
124         std::string status = "OK";
125
126         if (m_isTestCaseFailed) {
127                 color = RED_BEGIN;
128                 status = "FAIL";
129         }
130
131         output << "\t" << "[   " << color << status << COLOR_END << "   ]";
132
133         output << ", " << CYAN_BEGIN << "time: ";
134
135         if (elapsed > 0) {
136                 if (elapsed % 1000 == 0)
137                         output << elapsed / 1000 << "ms";
138                 else
139                         output << elapsed << "mks";
140         } else {
141                 output << "N/A";
142         }
143
144         output << COLOR_END << std::endl;
145         m_isTestCaseFailed = false;
146 }
147
148 //____________________________________________________________________________//
149
150 void
151 colour_log_formatter::test_unit_skipped(
152         std::ostream &output,
153         test_unit const &tu)
154 {
155         output  << "Test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
156                         "is skipped" << std::endl;
157 }
158
159 //____________________________________________________________________________//
160
161 void
162 colour_log_formatter::log_exception(
163         std::ostream &output,
164         log_checkpoint_data const &checkpoint_data,
165         boost::execution_exception const &ex)
166 {
167         boost::execution_exception::location const &loc = ex.where();
168         output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
169                    << '(' << loc.m_line_num << "), ";
170
171         output << "fatal error in \""
172                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
173                    "\": ";
174
175         output << COLOR_END << ex.what();
176
177         if (!checkpoint_data.m_file_name.is_empty()) {
178                 output << '\n';
179                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
180                            << '(' << checkpoint_data.m_line_num << ")";
181
182                 if (!checkpoint_data.m_message.empty())
183                         output << ": " << checkpoint_data.m_message;
184         }
185
186         output << std::endl;
187         m_isTestCaseFailed = true;
188 }
189
190 //____________________________________________________________________________//
191
192 void
193 colour_log_formatter::log_entry_start(
194         std::ostream &output,
195         log_entry_data const &entry_data,
196         log_entry_types let)
197 {
198         switch (let) {
199         case BOOST_UTL_ET_INFO:
200                 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
201                            "), ";
202                 output << "info: ";
203                 break;
204
205         case BOOST_UTL_ET_MESSAGE:
206                 break;
207
208         case BOOST_UTL_ET_WARNING:
209                 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
210                            entry_data.m_line_num << "), ";
211                 output << "warning in \"" << test_phase_identifier() << "\": ";
212                 break;
213
214         case BOOST_UTL_ET_ERROR:
215                 output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
216                            << '(' << entry_data.m_line_num << "), ";
217                 output << "error in \"" << test_phase_identifier() << "\": ";
218                 m_isTestCaseFailed = true;
219                 break;
220
221         case BOOST_UTL_ET_FATAL_ERROR:
222                 output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
223                            << '(' << entry_data.m_line_num << "),  ";
224                 output <<  " fatal error in \"" << test_phase_identifier() << "\": ";
225                 m_isTestCaseFailed = true;
226                 break;
227         }
228
229         output << COLOR_END;
230 }
231
232 //____________________________________________________________________________//
233
234 void
235 colour_log_formatter::log_entry_value(
236         std::ostream &output,
237         const_string value)
238 {
239         output << value;
240 }
241
242 //____________________________________________________________________________//
243
244 void
245 colour_log_formatter::log_entry_value(
246         std::ostream &output,
247         lazy_ostream const &value)
248 {
249         output << value;
250 }
251
252 //____________________________________________________________________________//
253
254 void
255 colour_log_formatter::log_entry_finish(
256         std::ostream &output)
257 {
258         output << std::endl;
259 }
260
261 //____________________________________________________________________________//
262
263 //____________________________________________________________________________//
264 } // namespace Test
265 } // namespace Wae
266
267 //____________________________________________________________________________//
268