1 // (C) Copyright Gennadiy Rozental 2001-2008.
2 // (C) Copyright Beman Dawes 2001.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/test for the library home page.
12 // Version : $Revision$
14 // Description : tests an ability of Unit Test Framework to catch all kinds
15 // of test errors in a user code and properly report it.
16 // ***************************************************************************
19 #define BOOST_TEST_MAIN
20 #include <boost/test/unit_test.hpp>
21 #include <boost/test/output_test_stream.hpp>
22 #include <boost/test/unit_test_log.hpp>
23 #include <boost/test/unit_test_suite.hpp>
24 #include <boost/test/framework.hpp>
25 #include <boost/test/detail/unit_test_parameters.hpp>
26 #include <boost/test/output/compiler_log_formatter.hpp>
27 #include <boost/test/results_reporter.hpp>
33 using namespace boost::unit_test;
34 using namespace boost::test_tools;
36 #if defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__DECCXX_VER)
42 struct this_test_log_formatter : public boost::unit_test::output::compiler_log_formatter
44 void print_prefix( std::ostream& output, boost::unit_test::const_string, std::size_t line )
46 output << line << ": ";
49 void test_unit_finish( std::ostream& output, test_unit const& tu, unsigned long )
51 output << "Leaving test " << tu.p_type_name << " \"" << tu.p_name << "\"" << std::endl;
56 //____________________________________________________________________________//
58 char const* log_level_name[] = {
59 "log_successful_tests",
64 "log_cpp_exception_errors",
70 enum error_type_enum {
87 char const* error_type_name[] = {
88 "no error", "user message", "user warning",
89 "user non-fatal error", "cpp exception", " system error",
90 "user fatal error", "system fatal error"
93 int divide_by_zero = 0;
95 void error_on_demand()
97 switch( error_type ) {
99 BOOST_CHECK_MESSAGE( divide_by_zero == 0, "no error" );
103 BOOST_TEST_MESSAGE( "message" );
107 BOOST_WARN_MESSAGE( divide_by_zero != 0, "warning" );
111 BOOST_ERROR( "non-fatal error" );
115 BOOST_FAIL( "fatal error" );
117 BOOST_ERROR( "Should never reach this code!" );
120 case et_cpp_exception:
121 BOOST_TEST_CHECKPOINT( "error_on_demand() throw runtime_error" );
122 throw std::runtime_error( "test std::runtime error what() message" );
126 BOOST_TEST_CHECKPOINT( "error_on_demand() divide by zero" );
127 divide_by_zero = 1 / divide_by_zero;
130 case et_fatal_system:
131 BOOST_TEST_CHECKPOINT( "write to an invalid address" );
136 BOOST_ERROR( "Should never reach this code!" );
141 BOOST_ERROR( "Should never reach this code!" );
148 //____________________________________________________________________________//
150 BOOST_AUTO_TEST_CASE( test_errors_handling )
152 #define PATTERN_FILE_NAME "errors_handling_test.pattern"
153 std::string pattern_file_name(
154 framework::master_test_suite().argc <= 1
155 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME : "./test_files/" PATTERN_FILE_NAME)
156 : framework::master_test_suite().argv[1] );
159 pattern_file_name += "2";
162 output_test_stream test_output( pattern_file_name, !runtime_config::save_pattern() );
164 test_case* test = BOOST_TEST_CASE( &error_on_demand );
166 // for each log level
167 for( log_level level = log_successful_tests;
168 level <= log_nothing;
169 level = static_cast<log_level>(level+1) )
171 // for each error type
172 for( error_type = et_begin;
173 error_type != et_end;
174 error_type = static_cast<error_type_enum>(error_type+1) )
176 test_output << "\n===========================\n"
177 << "log level: " << log_level_name[level] << ';'
178 << " error type: " << error_type_name[error_type] << ";\n" << std::endl;
180 unit_test_log.set_stream( test_output );
181 unit_test_log.set_formatter( new this_test_log_formatter );
182 unit_test_log.set_threshold_level( level );
183 framework::run( test );
185 unit_test_log.set_stream( std::cout );
186 unit_test_log.set_format( runtime_config::log_format() );
187 unit_test_log.set_threshold_level( runtime_config::log_level() != invalid_log_level
188 ? runtime_config::log_level()
190 BOOST_CHECK( test_output.match_pattern() );
195 //____________________________________________________________________________//