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 11 #include "catch_assertionhandler.h" 12 #include "catch_interfaces_capture.h" 13 #include "catch_message.h" 14 #include "catch_stringref.h" 15 16 #if !defined(CATCH_CONFIG_DISABLE) 17 18 #if !defined(CATCH_CONFIG_DISABLE_STRINGIFICATION) 19 #define CATCH_INTERNAL_STRINGIFY(...) #__VA_ARGS__ 20 #else 21 #define CATCH_INTERNAL_STRINGIFY(...) "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION" 22 #endif 23 24 #if defined(CATCH_CONFIG_FAST_COMPILE) || defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) 25 26 /////////////////////////////////////////////////////////////////////////////// 27 // Another way to speed-up compilation is to omit local try-catch for REQUIRE* 28 // macros. 29 #define INTERNAL_CATCH_TRY 30 #define INTERNAL_CATCH_CATCH( capturer ) 31 32 #else // CATCH_CONFIG_FAST_COMPILE 33 34 #define INTERNAL_CATCH_TRY try 35 #define INTERNAL_CATCH_CATCH( handler ) catch(...) { handler.handleUnexpectedInflightException(); } 36 37 #endif 38 39 #define INTERNAL_CATCH_REACT( handler ) handler.complete(); 40 41 /////////////////////////////////////////////////////////////////////////////// 42 #define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \ 43 do { \ 44 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ 45 INTERNAL_CATCH_TRY { \ 46 CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ 47 CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ 48 catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \ 49 CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ 50 } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ 51 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 52 } while( (void)0, (false) && static_cast<bool>( !!(__VA_ARGS__) ) ) // the expression here is never evaluated at runtime but it forces the compiler to give it a look 53 // The double negation silences MSVC's C4800 warning, the static_cast forces short-circuit evaluation if the type has overloaded &&. 54 55 /////////////////////////////////////////////////////////////////////////////// 56 #define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \ 57 INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ 58 if( Catch::getResultCapture().lastAssertionPassed() ) 59 60 /////////////////////////////////////////////////////////////////////////////// 61 #define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \ 62 INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ 63 if( !Catch::getResultCapture().lastAssertionPassed() ) 64 65 /////////////////////////////////////////////////////////////////////////////// 66 #define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \ 67 do { \ 68 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ 69 try { \ 70 static_cast<void>(__VA_ARGS__); \ 71 catchAssertionHandler.handleExceptionNotThrownAsExpected(); \ 72 } \ 73 catch( ... ) { \ 74 catchAssertionHandler.handleUnexpectedInflightException(); \ 75 } \ 76 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 77 } while( false ) 78 79 /////////////////////////////////////////////////////////////////////////////// 80 #define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \ 81 do { \ 82 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \ 83 if( catchAssertionHandler.allowThrows() ) \ 84 try { \ 85 static_cast<void>(__VA_ARGS__); \ 86 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ 87 } \ 88 catch( ... ) { \ 89 catchAssertionHandler.handleExceptionThrownAsExpected(); \ 90 } \ 91 else \ 92 catchAssertionHandler.handleThrowingCallSkipped(); \ 93 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 94 } while( false ) 95 96 /////////////////////////////////////////////////////////////////////////////// 97 #define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \ 98 do { \ 99 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \ 100 if( catchAssertionHandler.allowThrows() ) \ 101 try { \ 102 static_cast<void>(expr); \ 103 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ 104 } \ 105 catch( exceptionType const& ) { \ 106 catchAssertionHandler.handleExceptionThrownAsExpected(); \ 107 } \ 108 catch( ... ) { \ 109 catchAssertionHandler.handleUnexpectedInflightException(); \ 110 } \ 111 else \ 112 catchAssertionHandler.handleThrowingCallSkipped(); \ 113 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 114 } while( false ) 115 116 117 /////////////////////////////////////////////////////////////////////////////// 118 #define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \ 119 do { \ 120 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), resultDisposition ); \ 121 catchAssertionHandler.handleMessage( messageType, ( Catch::MessageStream() << __VA_ARGS__ + ::Catch::StreamEndStop() ).m_stream.str() ); \ 122 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 123 } while( false ) 124 125 /////////////////////////////////////////////////////////////////////////////// 126 #define INTERNAL_CATCH_CAPTURE( varName, macroName, ... ) \ 127 auto varName = Catch::Capturer( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info, #__VA_ARGS__ ); \ 128 varName.captureValues( 0, __VA_ARGS__ ) 129 130 /////////////////////////////////////////////////////////////////////////////// 131 #define INTERNAL_CATCH_INFO( macroName, log ) \ 132 Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log ); 133 134 /////////////////////////////////////////////////////////////////////////////// 135 #define INTERNAL_CATCH_UNSCOPED_INFO( macroName, log ) \ 136 Catch::getResultCapture().emplaceUnscopedMessage( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log ) 137 138 /////////////////////////////////////////////////////////////////////////////// 139 // Although this is matcher-based, it can be used with just a string 140 #define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \ 141 do { \ 142 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ 143 if( catchAssertionHandler.allowThrows() ) \ 144 try { \ 145 static_cast<void>(__VA_ARGS__); \ 146 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ 147 } \ 148 catch( ... ) { \ 149 Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher##_catch_sr ); \ 150 } \ 151 else \ 152 catchAssertionHandler.handleThrowingCallSkipped(); \ 153 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 154 } while( false ) 155 156 #endif // CATCH_CONFIG_DISABLE 157 158 #endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED 159