2 * Boost Software License - Version 1.0 - August 17th, 2003
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.
12 #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>
21 #include <boost/test/unit_test_suite_impl.hpp>
23 #include <boost/test/framework.hpp>
24 #include <boost/test/utils/basic_cstring/io.hpp>
25 #include <boost/test/utils/lazy_ostream.hpp>
26 #include <boost/version.hpp>
28 // ************************************************************************** //
29 // ************** colour_log_formatter ************** //
30 // ************************************************************************** //
32 using namespace boost::unit_test;
38 const char *GREEN_BEGIN = "\033[0;32m";
39 const char *RED_BEGIN = "\033[0;31m";
40 const char *CYAN_BEGIN = "\033[0;36m";
41 const char *BOLD_YELLOW_BEGIN = "\033[1;33m";
42 const char *COLOR_END = "\033[m";
45 test_unit_type_name(const test_unit &tu)
47 #if BOOST_VERSION >= 105900
48 return const_string(tu.p_type_name);
50 return tu.p_type_name.get();
55 test_unit_name(const test_unit &tu)
57 #if BOOST_VERSION >= 105900
58 return const_string(tu.p_name);
60 return tu.p_name.get();
65 test_phase_identifier()
67 return test_unit_name(framework::current_test_case());
71 get_basename(const const_string &file_name)
73 return basename(file_name.begin());
77 get_basename(const std::string &file_name)
79 return basename(file_name.c_str());
83 test_unit_type_name_contains(const test_unit &tu, const std::string &substr)
85 return test_unit_type_name(tu).find(const_string(substr)) == 0;
90 //____________________________________________________________________________//
93 colour_log_formatter::log_start(
95 counter_t test_cases_amount)
97 if (test_cases_amount > 0)
98 output << "Running " << test_cases_amount << " test "
99 << (test_cases_amount > 1 ? "cases" : "case") << "..." << std::endl;
102 //____________________________________________________________________________//
105 colour_log_formatter::log_finish(std::ostream &ostr)
110 //____________________________________________________________________________//
113 colour_log_formatter::log_build_info(std::ostream &output)
115 output << "Platform: " << BOOST_PLATFORM << std::endl
116 << "Compiler: " << BOOST_COMPILER << std::endl
117 << "STL : " << BOOST_STDLIB << std::endl
118 << "Boost : " << BOOST_VERSION / 100000 << "."
119 << BOOST_VERSION / 100 % 1000 << "."
120 << BOOST_VERSION % 100 << std::endl;
125 //____________________________________________________________________________//
128 colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info)
131 output << "Platform: " << BOOST_PLATFORM << '\n'
132 << "Compiler: " << BOOST_COMPILER << '\n'
133 << "STL : " << BOOST_STDLIB << '\n';
135 output << "Boost : " << BOOST_VERSION / 100000 << '.'
136 << BOOST_VERSION / 100 % 1000 << '.'
137 << BOOST_VERSION % 100 << std::endl;
140 //____________________________________________________________________________//
143 colour_log_formatter::test_unit_start(
144 std::ostream &output,
147 if (test_unit_type_name_contains(tu, "suite")) {
148 output << "Starting test ";
150 output << "Running test ";
152 output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
156 //____________________________________________________________________________//
159 colour_log_formatter::test_unit_finish(
160 std::ostream &output,
162 unsigned long elapsed)
164 if (test_unit_type_name_contains(tu, "suite")) {
165 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
170 std::string color = GREEN_BEGIN;
171 std::string status = "OK";
173 if (m_isTestCaseFailed) {
178 output << "\t" << "[ " << color << status << COLOR_END << " ]";
180 output << ", " << CYAN_BEGIN << "time: ";
183 if (elapsed % 1000 == 0)
184 output << elapsed / 1000 << "ms";
186 output << elapsed << "mks";
191 output << COLOR_END << std::endl;
192 m_isTestCaseFailed = false;
195 //____________________________________________________________________________//
198 colour_log_formatter::test_unit_skipped(
199 std::ostream &output,
202 output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
203 << "\"" << "is skipped" << std::endl;
206 //____________________________________________________________________________//
209 colour_log_formatter::log_exception(
210 std::ostream &output,
211 log_checkpoint_data const &checkpoint_data,
212 boost::execution_exception const &ex)
214 boost::execution_exception::location const &loc = ex.where();
215 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
216 << '(' << loc.m_line_num << "), ";
218 output << "fatal error in \""
219 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
222 output << COLOR_END << ex.what();
224 if (!checkpoint_data.m_file_name.is_empty()) {
226 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
227 << '(' << checkpoint_data.m_line_num << ")";
229 if (!checkpoint_data.m_message.empty())
230 output << ": " << checkpoint_data.m_message;
234 m_isTestCaseFailed = true;
238 colour_log_formatter::log_exception_start(
239 std::ostream &output,
240 log_checkpoint_data const &checkpoint_data,
241 boost::execution_exception const &ex)
243 boost::execution_exception::location const &loc = ex.where();
244 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
245 << '(' << loc.m_line_num << "), ";
247 output << "fatal error in \""
248 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
251 output << COLOR_END << ex.what();
253 if (!checkpoint_data.m_file_name.is_empty()) {
255 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
256 << '(' << checkpoint_data.m_line_num << ")";
258 if (!checkpoint_data.m_message.empty())
259 output << ": " << checkpoint_data.m_message;
263 m_isTestCaseFailed = true;
267 colour_log_formatter::log_exception_finish(std::ostream &os)
272 //____________________________________________________________________________//
275 colour_log_formatter::log_entry_start(
276 std::ostream &output,
277 log_entry_data const &entry_data,
281 case BOOST_UTL_ET_INFO:
282 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
287 case BOOST_UTL_ET_MESSAGE:
290 case BOOST_UTL_ET_WARNING:
291 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
292 entry_data.m_line_num << "), ";
293 output << "warning in \"" << test_phase_identifier() << "\": ";
296 case BOOST_UTL_ET_ERROR:
297 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(entry_data.m_file_name)
298 << '(' << entry_data.m_line_num << "), ";
299 output << "error in \"" << test_phase_identifier() << "\": ";
300 m_isTestCaseFailed = true;
303 case BOOST_UTL_ET_FATAL_ERROR:
304 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(entry_data.m_file_name)
305 << '(' << entry_data.m_line_num << "), ";
306 output << " fatal error in \"" << test_phase_identifier() << "\": ";
307 m_isTestCaseFailed = true;
314 //____________________________________________________________________________//
317 colour_log_formatter::log_entry_value(
318 std::ostream &output,
324 //____________________________________________________________________________//
327 colour_log_formatter::log_entry_value(
328 std::ostream &output,
329 lazy_ostream const &value)
334 //____________________________________________________________________________//
337 colour_log_formatter::log_entry_finish(
338 std::ostream &output)
343 //____________________________________________________________________________//
346 colour_log_formatter::entry_context_start(
347 std::ostream& output,
348 boost::unit_test::log_level l)
350 output << (l == log_successful_tests ? "\nAssertion" : "\nFailure")
351 << " occurred in a following context:";
355 colour_log_formatter::log_entry_context(
356 std::ostream& output,
357 boost::unit_test::const_string value)
359 output << "\n " << value;
363 colour_log_formatter::entry_context_finish(std::ostream& output)
368 #if BOOST_VERSION >= 106500
370 colour_log_formatter::log_entry_context(std::ostream& os, log_level l, const_string value)
373 os << "\n " << value;
376 colour_log_formatter::entry_context_finish(std::ostream& os, log_level l)
383 //____________________________________________________________________________//
387 //____________________________________________________________________________//