1 // (C) Copyright 2016 Raffi Enficiaud.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
9 ///@brief Contains the definition of the Junit log formatter (OF_JUNIT)
10 // ***************************************************************************
12 #ifndef BOOST_TEST_JUNIT_LOG_FORMATTER__
13 #define BOOST_TEST_JUNIT_LOG_FORMATTER__
16 #include <boost/test/detail/global_typedef.hpp>
17 #include <boost/test/unit_test_log_formatter.hpp>
18 #include <boost/test/tree/test_unit.hpp>
20 //#include <boost/test/results_collector.hpp>
23 #include <cstddef> // std::size_t
27 #include <boost/test/detail/suppress_warnings.hpp>
29 //____________________________________________________________________________//
36 namespace junit_impl {
38 // helper for the JUnit logger
39 struct junit_log_helper
41 struct assertion_entry {
49 assertion_entry() : sealed(false)
52 std::string logentry_message; // the message associated to the JUnit error/entry
53 std::string logentry_type; // the one that will get expanded in the final junit (failure, error)
54 std::string output; // additional information/message generated by the assertion
56 log_entry_t log_entry; // the type associated to the assertion (or error)
58 bool sealed; // indicates if the entry can accept additional information
61 std::list<std::string> system_out; // sysout: additional information
62 std::list<std::string> system_err; // syserr: additional information
63 std::string skipping_reason;
65 // list of failure, errors and messages (assertions message and the full log)
66 std::vector< assertion_entry > assertion_entries;
70 junit_log_helper(): skipping(false)
74 assertion_entries.clear();
77 skipping_reason.clear();
84 // ************************************************************************** //
85 // ************** junit_log_formatter ************** //
86 // ************************************************************************** //
88 /// JUnit logger class
89 class junit_log_formatter : public unit_test_log_formatter {
92 junit_log_formatter() : m_display_build_info(false)
94 // we log everything from the logger singleton point of view
95 // because we need to know about all the messages/commands going to the logger
96 // we decide what we put inside the logs internally
97 this->m_log_level = log_successful_tests;
98 m_log_level_internal = log_messages;
101 // Formatter interface
102 void log_start( std::ostream&, counter_t test_cases_amount );
103 void log_finish( std::ostream& );
104 void log_build_info( std::ostream& );
106 void test_unit_start( std::ostream&, test_unit const& tu );
107 void test_unit_finish( std::ostream&, test_unit const& tu, unsigned long elapsed );
108 void test_unit_skipped( std::ostream&, test_unit const& tu, const_string reason );
109 void test_unit_aborted( std::ostream& os, test_unit const& tu );
111 void log_exception_start( std::ostream&, log_checkpoint_data const&, execution_exception const& ex );
112 void log_exception_finish( std::ostream& );
114 void log_entry_start( std::ostream&, log_entry_data const&, log_entry_types let );
116 using unit_test_log_formatter::log_entry_value; // bring base class functions into overload set
117 void log_entry_value( std::ostream&, const_string value );
118 void log_entry_finish( std::ostream& );
120 void entry_context_start( std::ostream&, log_level );
121 void log_entry_context( std::ostream&, log_level, const_string );
122 void entry_context_finish( std::ostream&, log_level );
124 //! Discards changes in the log level
125 virtual void set_log_level(log_level ll)
127 if(ll > log_successful_tests && ll < log_messages)
128 ll = log_successful_tests;
129 else if (ll > log_all_errors)
132 this->m_log_level_internal = ll;
135 //! Instead of a regular stream, returns a file name corresponding to
136 //! the current master test suite. If the file already exists, adds an index
138 virtual std::string get_default_stream_description() const;
142 typedef std::map<test_unit_id, junit_impl::junit_log_helper> map_trace_t;
143 map_trace_t map_tests;
144 junit_impl::junit_log_helper runner_log_entry;
146 junit_impl::junit_log_helper& get_current_log_entry() {
147 if(list_path_to_root.empty())
148 return runner_log_entry;
149 map_trace_t::iterator it = map_tests.find(list_path_to_root.back());
150 return (it == map_tests.end() ? runner_log_entry : it->second);
153 std::list<test_unit_id> list_path_to_root;
154 bool m_display_build_info;
155 bool m_is_last_assertion_or_error; // true if failure, false if error
157 log_level m_log_level_internal;
158 friend class junit_result_helper;
161 } // namespace output
162 } // namespace unit_test
165 #include <boost/test/detail/enable_warnings.hpp>
167 #endif // BOOST_TEST_JUNIT_LOG_FORMATTER__