diff --git a/include/boost/core/lightweight_test.hpp b/include/boost/core/lightweight_test.hpp index 49ff626..23f5c52 100644 --- a/include/boost/core/lightweight_test.hpp +++ b/include/boost/core/lightweight_test.hpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -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 diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 8780556..d565ceb 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -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 ; diff --git a/test/lightweight_test_fail13.cpp b/test/lightweight_test_fail13.cpp new file mode 100644 index 0000000..4330a2c --- /dev/null +++ b/test/lightweight_test_fail13.cpp @@ -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 + +void f() +{ + throw 5; +} + +int main() +{ + BOOST_TEST_NO_THROW(f()); + return boost::report_errors(); +} diff --git a/test/lightweight_test_fail14.cpp b/test/lightweight_test_fail14.cpp new file mode 100644 index 0000000..89988f5 --- /dev/null +++ b/test/lightweight_test_fail14.cpp @@ -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 + +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(); +} diff --git a/test/lightweight_test_test.cpp b/test/lightweight_test_test.cpp index 73538b4..7460af9 100644 --- a/test/lightweight_test_test.cpp +++ b/test/lightweight_test_test.cpp @@ -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(); }