catch_capture.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Created by Phil on 18/10/2010.
  3. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
  9. #define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
  10. #include "catch_assertionhandler.h"
  11. #include "catch_interfaces_capture.h"
  12. #include "catch_message.h"
  13. #include "catch_stringref.h"
  14. #if !defined(CATCH_CONFIG_DISABLE)
  15. #if !defined(CATCH_CONFIG_DISABLE_STRINGIFICATION)
  16. #define CATCH_INTERNAL_STRINGIFY(...) #__VA_ARGS__
  17. #else
  18. #define CATCH_INTERNAL_STRINGIFY(...) "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION"
  19. #endif
  20. #if defined(CATCH_CONFIG_FAST_COMPILE) || defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Another way to speed-up compilation is to omit local try-catch for REQUIRE*
  23. // macros.
  24. #define INTERNAL_CATCH_TRY
  25. #define INTERNAL_CATCH_CATCH( capturer )
  26. #else // CATCH_CONFIG_FAST_COMPILE
  27. #define INTERNAL_CATCH_TRY try
  28. #define INTERNAL_CATCH_CATCH( handler ) catch(...) { handler.handleUnexpectedInflightException(); }
  29. #endif
  30. #define INTERNAL_CATCH_REACT( handler ) handler.complete();
  31. ///////////////////////////////////////////////////////////////////////////////
  32. #define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \
  33. do { \
  34. /* The expression should not be evaluated, but warnings should hopefully be checked */ \
  35. CATCH_INTERNAL_IGNORE_BUT_WARN(__VA_ARGS__); \
  36. Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \
  37. INTERNAL_CATCH_TRY { \
  38. CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
  39. CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \
  40. catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \
  41. CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
  42. } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \
  43. INTERNAL_CATCH_REACT( catchAssertionHandler ) \
  44. } while( (void)0, (false) && static_cast<bool>( !!(__VA_ARGS__) ) )
  45. ///////////////////////////////////////////////////////////////////////////////
  46. #define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \
  47. INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \
  48. if( Catch::getResultCapture().lastAssertionPassed() )
  49. ///////////////////////////////////////////////////////////////////////////////
  50. #define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \
  51. INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \
  52. if( !Catch::getResultCapture().lastAssertionPassed() )
  53. ///////////////////////////////////////////////////////////////////////////////
  54. #define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \
  55. do { \
  56. Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \
  57. try { \
  58. static_cast<void>(__VA_ARGS__); \
  59. catchAssertionHandler.handleExceptionNotThrownAsExpected(); \
  60. } \
  61. catch( ... ) { \
  62. catchAssertionHandler.handleUnexpectedInflightException(); \
  63. } \
  64. INTERNAL_CATCH_REACT( catchAssertionHandler ) \
  65. } while( false )
  66. ///////////////////////////////////////////////////////////////////////////////
  67. #define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \
  68. do { \
  69. Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \
  70. if( catchAssertionHandler.allowThrows() ) \
  71. try { \
  72. static_cast<void>(__VA_ARGS__); \
  73. catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
  74. } \
  75. catch( ... ) { \
  76. catchAssertionHandler.handleExceptionThrownAsExpected(); \
  77. } \
  78. else \
  79. catchAssertionHandler.handleThrowingCallSkipped(); \
  80. INTERNAL_CATCH_REACT( catchAssertionHandler ) \
  81. } while( false )
  82. ///////////////////////////////////////////////////////////////////////////////
  83. #define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \
  84. do { \
  85. Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \
  86. if( catchAssertionHandler.allowThrows() ) \
  87. try { \
  88. static_cast<void>(expr); \
  89. catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
  90. } \
  91. catch( exceptionType const& ) { \
  92. catchAssertionHandler.handleExceptionThrownAsExpected(); \
  93. } \
  94. catch( ... ) { \
  95. catchAssertionHandler.handleUnexpectedInflightException(); \
  96. } \
  97. else \
  98. catchAssertionHandler.handleThrowingCallSkipped(); \
  99. INTERNAL_CATCH_REACT( catchAssertionHandler ) \
  100. } while( false )
  101. ///////////////////////////////////////////////////////////////////////////////
  102. #define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \
  103. do { \
  104. Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), resultDisposition ); \
  105. catchAssertionHandler.handleMessage( messageType, ( Catch::MessageStream() << __VA_ARGS__ + ::Catch::StreamEndStop() ).m_stream.str() ); \
  106. INTERNAL_CATCH_REACT( catchAssertionHandler ) \
  107. } while( false )
  108. ///////////////////////////////////////////////////////////////////////////////
  109. #define INTERNAL_CATCH_CAPTURE( varName, macroName, ... ) \
  110. auto varName = Catch::Capturer( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info, #__VA_ARGS__ ); \
  111. varName.captureValues( 0, __VA_ARGS__ )
  112. ///////////////////////////////////////////////////////////////////////////////
  113. #define INTERNAL_CATCH_INFO( macroName, log ) \
  114. Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log );
  115. ///////////////////////////////////////////////////////////////////////////////
  116. #define INTERNAL_CATCH_UNSCOPED_INFO( macroName, log ) \
  117. Catch::getResultCapture().emplaceUnscopedMessage( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log )
  118. ///////////////////////////////////////////////////////////////////////////////
  119. // Although this is matcher-based, it can be used with just a string
  120. #define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \
  121. do { \
  122. Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \
  123. if( catchAssertionHandler.allowThrows() ) \
  124. try { \
  125. static_cast<void>(__VA_ARGS__); \
  126. catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
  127. } \
  128. catch( ... ) { \
  129. Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher##_catch_sr ); \
  130. } \
  131. else \
  132. catchAssertionHandler.handleThrowingCallSkipped(); \
  133. INTERNAL_CATCH_REACT( catchAssertionHandler ) \
  134. } while( false )
  135. #endif // CATCH_CONFIG_DISABLE
  136. #endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED