mirror of
https://github.com/boostorg/core.git
synced 2025-07-29 20:37:22 +02:00
Add BOOST_TEST_NO_THROW and print expression in BOOST_TEST_THROWS
This commit is contained in:
@ -24,6 +24,7 @@
|
||||
|
||||
#include <boost/current_function.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <cstdlib>
|
||||
@ -118,14 +119,30 @@ inline void error_impl(char const * msg, char const * file, int line, char const
|
||||
++test_results().errors();
|
||||
}
|
||||
|
||||
inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
|
||||
inline void throw_failed_impl(const char* expr, char const * excep, char const * file, int line, char const * function)
|
||||
{
|
||||
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||
<< file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
|
||||
<< file << "(" << line << "): expression '" << expr << "' did not throw exception '" << excep << "' in function '"
|
||||
<< function << "'" << std::endl;
|
||||
++test_results().errors();
|
||||
}
|
||||
|
||||
inline void no_throw_failed_impl(const char* expr, const char* file, int line, const char* function)
|
||||
{
|
||||
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||
<< file << "(" << line << "): expression '" << expr << "' threw an exception in function '"
|
||||
<< function << "'" << std::endl;
|
||||
++test_results().errors();
|
||||
}
|
||||
|
||||
inline void no_throw_failed_impl(const char* expr, const char* what, const char* file, int line, const char* function)
|
||||
{
|
||||
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||
<< file << "(" << line << "): expression '" << expr << "' threw an exception in function '"
|
||||
<< function << "': " << what << std::endl;
|
||||
++test_results().errors();
|
||||
}
|
||||
|
||||
// In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
|
||||
// A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
|
||||
// the dependency we just disable the warnings.
|
||||
@ -446,22 +463,38 @@ inline int report_errors()
|
||||
#define BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) ( ::boost::detail::test_all_with_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2, predicate) )
|
||||
|
||||
#ifndef BOOST_NO_EXCEPTIONS
|
||||
#define BOOST_TEST_THROWS( EXPR, EXCEP ) \
|
||||
try { \
|
||||
EXPR; \
|
||||
::boost::detail::throw_failed_impl \
|
||||
(#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||
} \
|
||||
catch(EXCEP const&) { \
|
||||
::boost::detail::test_results(); \
|
||||
} \
|
||||
catch(...) { \
|
||||
::boost::detail::throw_failed_impl \
|
||||
(#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||
} \
|
||||
#define BOOST_TEST_THROWS( EXPR, EXCEP ) \
|
||||
try { \
|
||||
EXPR; \
|
||||
::boost::detail::throw_failed_impl \
|
||||
(#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||
} \
|
||||
catch(EXCEP const&) { \
|
||||
::boost::detail::test_results(); \
|
||||
} \
|
||||
catch(...) { \
|
||||
::boost::detail::throw_failed_impl \
|
||||
(#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||
} \
|
||||
//
|
||||
#else
|
||||
#define BOOST_TEST_THROWS( EXPR, EXCEP )
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_EXCEPTIONS
|
||||
# define BOOST_TEST_NO_THROW(EXPR) \
|
||||
try { \
|
||||
EXPR; \
|
||||
} catch (const std::exception& e) { \
|
||||
::boost::detail::no_throw_failed_impl \
|
||||
(#EXPR, e.what(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||
} catch (...) { \
|
||||
::boost::detail::no_throw_failed_impl \
|
||||
(#EXPR, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||
}
|
||||
//
|
||||
#else
|
||||
# define BOOST_TEST_NO_THROW(EXPR)
|
||||
#endif
|
||||
|
||||
#endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP
|
||||
|
@ -112,6 +112,8 @@ run-fail lightweight_test_fail9.cpp ;
|
||||
run-fail lightweight_test_fail10.cpp ;
|
||||
run-fail lightweight_test_fail11.cpp : ;
|
||||
run-fail lightweight_test_fail12.cpp ;
|
||||
run-fail lightweight_test_fail13.cpp ;
|
||||
run-fail lightweight_test_fail14.cpp ;
|
||||
run-fail lightweight_test_lt_fail.cpp ;
|
||||
run-fail lightweight_test_le_fail.cpp ;
|
||||
run-fail lightweight_test_gt_fail.cpp ;
|
||||
|
19
test/lightweight_test_fail13.cpp
Normal file
19
test/lightweight_test_fail13.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
Copyright 2020 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
void f()
|
||||
{
|
||||
throw 5;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST_NO_THROW(f());
|
||||
return boost::report_errors();
|
||||
}
|
26
test/lightweight_test_fail14.cpp
Normal file
26
test/lightweight_test_fail14.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
Copyright 2020 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
struct error
|
||||
: std::exception {
|
||||
const char* what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE {
|
||||
return "message";
|
||||
}
|
||||
};
|
||||
|
||||
void f()
|
||||
{
|
||||
throw error();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST_NO_THROW(f());
|
||||
return boost::report_errors();
|
||||
}
|
@ -108,5 +108,9 @@ int main()
|
||||
BOOST_TEST_THROWS( f(true), X );
|
||||
BOOST_TEST_THROWS( f(false), int );
|
||||
|
||||
// BOOST_TEST_NO_THROW
|
||||
|
||||
BOOST_TEST_NO_THROW(++y);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
Reference in New Issue
Block a user