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 /* The expression should not be evaluated, but warnings should hopefully be checked */ \ 45 CATCH_INTERNAL_IGNORE_BUT_WARN(__VA_ARGS__); \ 46 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ 47 INTERNAL_CATCH_TRY { \ 48 CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ 49 CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ 50 catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \ 51 CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ 52 } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ 53 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 54 } while( (void)0, (false) && static_cast<bool>( !!(__VA_ARGS__) ) ) 55 56 /////////////////////////////////////////////////////////////////////////////// 57 #define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \ 58 INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ 59 if( Catch::getResultCapture().lastAssertionPassed() ) 60 61 /////////////////////////////////////////////////////////////////////////////// 62 #define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \ 63 INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ 64 if( !Catch::getResultCapture().lastAssertionPassed() ) 65 66 /////////////////////////////////////////////////////////////////////////////// 67 #define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \ 68 do { \ 69 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ 70 try { \ 71 static_cast<void>(__VA_ARGS__); \ 72 catchAssertionHandler.handleExceptionNotThrownAsExpected(); \ 73 } \ 74 catch( ... ) { \ 75 catchAssertionHandler.handleUnexpectedInflightException(); \ 76 } \ 77 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 78 } while( false ) 79 80 /////////////////////////////////////////////////////////////////////////////// 81 #define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \ 82 do { \ 83 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \ 84 if( catchAssertionHandler.allowThrows() ) \ 85 try { \ 86 static_cast<void>(__VA_ARGS__); \ 87 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ 88 } \ 89 catch( ... ) { \ 90 catchAssertionHandler.handleExceptionThrownAsExpected(); \ 91 } \ 92 else \ 93 catchAssertionHandler.handleThrowingCallSkipped(); \ 94 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 95 } while( false ) 96 97 /////////////////////////////////////////////////////////////////////////////// 98 #define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \ 99 do { \ 100 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \ 101 if( catchAssertionHandler.allowThrows() ) \ 102 try { \ 103 static_cast<void>(expr); \ 104 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ 105 } \ 106 catch( exceptionType const& ) { \ 107 catchAssertionHandler.handleExceptionThrownAsExpected(); \ 108 } \ 109 catch( ... ) { \ 110 catchAssertionHandler.handleUnexpectedInflightException(); \ 111 } \ 112 else \ 113 catchAssertionHandler.handleThrowingCallSkipped(); \ 114 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 115 } while( false ) 116 117 118 /////////////////////////////////////////////////////////////////////////////// 119 #define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \ 120 do { \ 121 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), resultDisposition ); \ 122 catchAssertionHandler.handleMessage( messageType, ( Catch::MessageStream() << __VA_ARGS__ + ::Catch::StreamEndStop() ).m_stream.str() ); \ 123 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 124 } while( false ) 125 126 /////////////////////////////////////////////////////////////////////////////// 127 #define INTERNAL_CATCH_CAPTURE( varName, macroName, ... ) \ 128 auto varName = Catch::Capturer( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info, #__VA_ARGS__ ); \ 129 varName.captureValues( 0, __VA_ARGS__ ) 130 131 /////////////////////////////////////////////////////////////////////////////// 132 #define INTERNAL_CATCH_INFO( macroName, log ) \ 133 Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log ); 134 135 /////////////////////////////////////////////////////////////////////////////// 136 #define INTERNAL_CATCH_UNSCOPED_INFO( macroName, log ) \ 137 Catch::getResultCapture().emplaceUnscopedMessage( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log ) 138 139 /////////////////////////////////////////////////////////////////////////////// 140 // Although this is matcher-based, it can be used with just a string 141 #define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \ 142 do { \ 143 Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ 144 if( catchAssertionHandler.allowThrows() ) \ 145 try { \ 146 static_cast<void>(__VA_ARGS__); \ 147 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ 148 } \ 149 catch( ... ) { \ 150 Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher##_catch_sr ); \ 151 } \ 152 else \ 153 catchAssertionHandler.handleThrowingCallSkipped(); \ 154 INTERNAL_CATCH_REACT( catchAssertionHandler ) \ 155 } while( false ) 156 157 #endif // CATCH_CONFIG_DISABLE 158 159 #endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED 160