X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/355911106bc27f5504eb904a064367ef1b5f0aba..261442af210d652fc2c8a3d9197097982701bd16:/tests/tut/tut_macros.hpp?ds=sidebyside diff --git a/tests/tut/tut_macros.hpp b/tests/tut/tut_macros.hpp new file mode 100644 index 00000000..ed517d66 --- /dev/null +++ b/tests/tut/tut_macros.hpp @@ -0,0 +1,63 @@ +#ifndef TUT_MACROS_HPP +#define TUT_MACROS_HPP + +#include + +#ifdef ensure_THROW +#error ensure_THROW macro is already defined +#endif + +/** Helper macros to ensure that a call throws exception. + * \code + * #include + * ensure_THROW( this_function_should_throw_bad_alloc(), std::bad_alloc ); + * \endcode + */ +#define ensure_THROW( x, e ) \ +try \ +{ \ + x; \ + fail(#x " has not thrown expected exception " #e); \ +} \ +catch(const e &) \ +{ \ +} \ +catch(const std::exception &ex) \ +{ \ + fail( std::string(#x " has thrown unexpected exception ")+tut::type_name(ex)+": "+ex.what()); \ +} \ +catch(...) \ +{ \ + fail(#x " has thrown unexpected unknown exception"); \ +} + +#ifdef ensure_NO_THROW +#error ensure_NO_THROW macro is already defined +#endif + +/** Helper macro to ensure a call does not throw any exceptions. + * \code + * #include + * ensure_NO_THROW( this_function_should_never_throw() ); + * \endcode + */ +#define ensure_NO_THROW( x ) \ +try \ +{ \ + x; \ +} \ +catch(const std::exception &ex) \ +{ \ + fail( std::string(#x " has thrown unexpected exception ")+tut::type_name(ex)+": "+ex.what()); \ +} \ +catch(...) \ +{ \ + fail(#x " has thrown unexpected unknown exception"); \ +} + +#ifdef __COUNTER__ +#define TUT_TESTCASE(object) template<> template<> void object::test<__COUNTER__>() +#endif + +#endif +