catch_enforce.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Created by Martin on 01/08/2017.
  3. *
  4. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef TWOBLUECUBES_CATCH_ENFORCE_H_INCLUDED
  8. #define TWOBLUECUBES_CATCH_ENFORCE_H_INCLUDED
  9. #include "catch_common.h"
  10. #include "catch_compiler_capabilities.h"
  11. #include "catch_stream.h"
  12. #include <exception>
  13. namespace Catch {
  14. #if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
  15. template <typename Ex>
  16. [[noreturn]]
  17. void throw_exception(Ex const& e) {
  18. throw e;
  19. }
  20. #else // ^^ Exceptions are enabled // Exceptions are disabled vv
  21. [[noreturn]]
  22. void throw_exception(std::exception const& e);
  23. #endif
  24. [[noreturn]]
  25. void throw_logic_error(std::string const& msg);
  26. [[noreturn]]
  27. void throw_domain_error(std::string const& msg);
  28. [[noreturn]]
  29. void throw_runtime_error(std::string const& msg);
  30. } // namespace Catch;
  31. #define CATCH_MAKE_MSG(...) \
  32. (Catch::ReusableStringStream() << __VA_ARGS__).str()
  33. #define CATCH_INTERNAL_ERROR(...) \
  34. Catch::throw_logic_error(CATCH_MAKE_MSG( CATCH_INTERNAL_LINEINFO << ": Internal Catch2 error: " << __VA_ARGS__))
  35. #define CATCH_ERROR(...) \
  36. Catch::throw_domain_error(CATCH_MAKE_MSG( __VA_ARGS__ ))
  37. #define CATCH_RUNTIME_ERROR(...) \
  38. Catch::throw_runtime_error(CATCH_MAKE_MSG( __VA_ARGS__ ))
  39. #define CATCH_ENFORCE( condition, ... ) \
  40. do{ if( !(condition) ) CATCH_ERROR( __VA_ARGS__ ); } while(false)
  41. #endif // TWOBLUECUBES_CATCH_ENFORCE_H_INCLUDED