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;
123 //____________________________________________________________________________//
126 colour_log_formatter::test_unit_start(
127 std::ostream &output,
130 if (test_unit_type_name_contains(tu, "suite")) {
131 output << "Starting test ";
133 output << "Running test ";
135 output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
139 //____________________________________________________________________________//
142 colour_log_formatter::test_unit_finish(
143 std::ostream &output,
145 unsigned long elapsed)
147 if (test_unit_type_name_contains(tu, "suite")) {
148 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
153 std::string color = GREEN_BEGIN;
154 std::string status = "OK";
156 if (m_isTestCaseFailed) {
161 output << "\t" << "[ " << color << status << COLOR_END << " ]";
163 output << ", " << CYAN_BEGIN << "time: ";
166 if (elapsed % 1000 == 0)
167 output << elapsed / 1000 << "ms";
169 output << elapsed << "mks";
174 output << COLOR_END << std::endl;
175 m_isTestCaseFailed = false;
178 //____________________________________________________________________________//
181 colour_log_formatter::test_unit_skipped(
182 std::ostream &output,
185 output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
186 << "\"" << "is skipped" << std::endl;
189 //____________________________________________________________________________//
192 colour_log_formatter::log_exception(
193 std::ostream &output,
194 log_checkpoint_data const &checkpoint_data,
195 boost::execution_exception const &ex)
197 boost::execution_exception::location const &loc = ex.where();
198 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
199 << '(' << loc.m_line_num << "), ";
201 output << "fatal error in \""
202 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
205 output << COLOR_END << ex.what();
207 if (!checkpoint_data.m_file_name.is_empty()) {
209 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
210 << '(' << checkpoint_data.m_line_num << ")";
212 if (!checkpoint_data.m_message.empty())
213 output << ": " << checkpoint_data.m_message;
217 m_isTestCaseFailed = true;
221 colour_log_formatter::log_exception_start(
222 std::ostream &output,
223 log_checkpoint_data const &checkpoint_data,
224 boost::execution_exception const &ex)
226 boost::execution_exception::location const &loc = ex.where();
227 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
228 << '(' << loc.m_line_num << "), ";
230 output << "fatal error in \""
231 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
234 output << COLOR_END << ex.what();
236 if (!checkpoint_data.m_file_name.is_empty()) {
238 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
239 << '(' << checkpoint_data.m_line_num << ")";
241 if (!checkpoint_data.m_message.empty())
242 output << ": " << checkpoint_data.m_message;
246 m_isTestCaseFailed = true;
250 colour_log_formatter::log_exception_finish(std::ostream &os)
255 //____________________________________________________________________________//
258 colour_log_formatter::log_entry_start(
259 std::ostream &output,
260 log_entry_data const &entry_data,
264 case BOOST_UTL_ET_INFO:
265 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
270 case BOOST_UTL_ET_MESSAGE:
273 case BOOST_UTL_ET_WARNING:
274 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
275 entry_data.m_line_num << "), ";
276 output << "warning in \"" << test_phase_identifier() << "\": ";
279 case BOOST_UTL_ET_ERROR:
280 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(entry_data.m_file_name)
281 << '(' << entry_data.m_line_num << "), ";
282 output << "error in \"" << test_phase_identifier() << "\": ";
283 m_isTestCaseFailed = true;
286 case BOOST_UTL_ET_FATAL_ERROR:
287 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(entry_data.m_file_name)
288 << '(' << entry_data.m_line_num << "), ";
289 output << " fatal error in \"" << test_phase_identifier() << "\": ";
290 m_isTestCaseFailed = true;
297 //____________________________________________________________________________//
300 colour_log_formatter::log_entry_value(
301 std::ostream &output,
307 //____________________________________________________________________________//
310 colour_log_formatter::log_entry_value(
311 std::ostream &output,
312 lazy_ostream const &value)
317 //____________________________________________________________________________//
320 colour_log_formatter::log_entry_finish(
321 std::ostream &output)
326 //____________________________________________________________________________//
329 colour_log_formatter::entry_context_start(
330 std::ostream& output,
331 boost::unit_test::log_level l)
333 output << (l == log_successful_tests ? "\nAssertion" : "\nFailure")
334 << " occurred in a following context:";
338 colour_log_formatter::log_entry_context(
339 std::ostream& output,
340 boost::unit_test::const_string value)
342 output << "\n " << value;
346 colour_log_formatter::entry_context_finish(std::ostream& output)
351 #if BOOST_VERSION >= 106500
353 colour_log_formatter::log_entry_context(std::ostream& os, log_level l, const_string value)
356 os << "\n " << value;
359 colour_log_formatter::entry_context_finish(std::ostream& os, log_level l)
366 //____________________________________________________________________________//
370 //____________________________________________________________________________//