1 // (C) Copyright Raffi Enficiaud 2018.
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.
8 // Checks boost::exception, see https://github.com/boostorg/test/issues/147
9 // ***************************************************************************
12 #define BOOST_TEST_MAIN
13 #include <boost/test/unit_test.hpp>
14 #include <boost/test/tools/output_test_stream.hpp>
17 #include <boost/exception/exception.hpp>
18 #include <boost/exception/info.hpp>
19 #include <boost/cstdint.hpp>
24 using boost::test_tools::output_test_stream;
25 using namespace boost::unit_test;
27 typedef boost::error_info<struct tag_error_code, boost::uint32_t> error_code;
28 typedef boost::error_info<struct tag_error_string, std::string> error_string;
29 struct myexception : std::exception, virtual boost::exception
32 // this one should generate a message as it does not execute any assertion
33 void exception_raised() {
34 BOOST_THROW_EXCEPTION( myexception() << error_code(123) << error_string("error%%string") );
37 struct output_test_stream2 : public output_test_stream {
38 std::string get_stream_string_representation() const {
39 (void)const_cast<output_test_stream2*>(this)->output_test_stream::check_length(0, false); // to sync only!!
40 return output_test_stream::get_stream_string_representation();
44 BOOST_AUTO_TEST_CASE( test_logs )
46 test_suite* ts_main = BOOST_TEST_SUITE( "fake master" );
47 ts_main->add( BOOST_TEST_CASE( exception_raised ) );
49 output_test_stream2 test_output;
51 ts_main->p_default_status.value = test_unit::RS_ENABLED;
52 boost::unit_test::unit_test_log.set_stream(test_output);
53 boost::unit_test::unit_test_log.set_threshold_level( log_successful_tests );
55 framework::finalize_setup_phase( ts_main->p_id );
56 framework::run( ts_main->p_id, false ); // do not continue the test tree to have the test_log_start/end
58 boost::unit_test::unit_test_log.set_stream(std::cout);
60 std::string error_string(test_output.get_stream_string_representation());
61 // the message is "Dynamic exception type: boost::exception_detail::clone_impl<myexception>" on Unix
62 // and "Dynamic exception type: boost::exception_detail::clone_impl<struct myexception>" on Windows.
63 // Also contains "[tag_error_code*] = 123" on Unix and "[struct tag_error_code * __ptr64] = 123" On Windows
64 // Also contains "[tag_error_string*] = error%%string" on Unix and "[struct tag_error_string * __ptr64] = error%%string" On Windows
65 BOOST_TEST(error_string.find("tag_error_code") != std::string::npos);
66 BOOST_TEST(error_string.find("= 123") != std::string::npos);
67 BOOST_TEST(error_string.find("tag_error_string") != std::string::npos);
68 BOOST_TEST(error_string.find("= error%%string") != std::string::npos);
69 BOOST_TEST(error_string.find("Dynamic exception type") != std::string::npos);
70 BOOST_TEST(error_string.find("boost::wrapexcept<") != std::string::npos);
71 BOOST_TEST(error_string.find("myexception>") != std::string::npos);