2010-11-09 18:24:00 -05:00
/*
* Created by Phil on 18 / 10 / 2010.
* Copyright 2010 Two Blue Cubes Ltd . All rights reserved .
*
* Distributed under the Boost Software License , Version 1.0 . ( See accompanying
* file LICENSE_1_0 . txt or copy at http : //www.boost.org/LICENSE_1_0.txt)
*/
# ifndef TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
# define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
2017-08-08 12:53:01 -04:00
# include "catch_assertionhandler.h"
2011-01-11 04:13:31 -05:00
# include "catch_interfaces_capture.h"
2018-07-23 07:55:14 -04:00
# include "catch_message.h"
# include "catch_stringref.h"
2013-04-22 03:19:17 -04:00
2017-08-27 11:03:32 -04:00
# if !defined(CATCH_CONFIG_DISABLE)
2017-08-26 09:14:27 -04:00
# if !defined(CATCH_CONFIG_DISABLE_STRINGIFICATION)
# define CATCH_INTERNAL_STRINGIFY(...) #__VA_ARGS__
# else
# define CATCH_INTERNAL_STRINGIFY(...) "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION"
# endif
2012-10-29 15:55:13 -04:00
2018-09-03 11:52:48 -04:00
# if defined(CATCH_CONFIG_FAST_COMPILE) || defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
2017-03-17 08:21:40 -04:00
///////////////////////////////////////////////////////////////////////////////
// Another way to speed-up compilation is to omit local try-catch for REQUIRE*
// macros.
2017-11-23 11:52:46 -05:00
# define INTERNAL_CATCH_TRY
# define INTERNAL_CATCH_CATCH( capturer )
2017-08-06 19:09:54 -04:00
# else // CATCH_CONFIG_FAST_COMPILE
2017-11-23 11:52:46 -05:00
# define INTERNAL_CATCH_TRY try
2017-11-24 14:15:46 -05:00
# define INTERNAL_CATCH_CATCH( handler ) catch(...) { handler.handleUnexpectedInflightException(); }
2017-08-06 19:09:54 -04:00
# endif
2010-12-27 15:49:19 -05:00
2017-11-23 14:14:26 -05:00
# define INTERNAL_CATCH_REACT( handler ) handler.complete();
2012-10-24 16:59:47 -04:00
///////////////////////////////////////////////////////////////////////////////
2017-04-26 11:10:18 -04:00
# define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \
2012-10-29 15:55:13 -04:00
do { \
2018-07-23 07:55:14 -04:00
Catch : : AssertionHandler catchAssertionHandler ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , CATCH_INTERNAL_STRINGIFY ( __VA_ARGS__ ) , resultDisposition ) ; \
2017-11-23 11:52:46 -05:00
INTERNAL_CATCH_TRY { \
2016-02-29 03:01:06 -05:00
CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleExpr ( Catch : : Decomposer ( ) < = __VA_ARGS__ ) ; \
2017-02-15 04:38:38 -05:00
CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \
2017-08-09 06:36:33 -04:00
} INTERNAL_CATCH_CATCH ( catchAssertionHandler ) \
INTERNAL_CATCH_REACT ( catchAssertionHandler ) \
2019-02-12 07:25:03 -05:00
} 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
2017-01-30 05:56:29 -05:00
// The double negation silences MSVC's C4800 warning, the static_cast forces short-circuit evaluation if the type has overloaded &&.
2010-11-09 18:24:00 -05:00
2012-02-10 03:30:13 -05:00
///////////////////////////////////////////////////////////////////////////////
2017-05-03 13:10:27 -04:00
# define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \
INTERNAL_CATCH_TEST ( macroName , resultDisposition , __VA_ARGS__ ) ; \
2017-06-26 15:30:23 -04:00
if ( Catch : : getResultCapture ( ) . lastAssertionPassed ( ) )
2012-02-10 03:30:13 -05:00
///////////////////////////////////////////////////////////////////////////////
2017-05-03 13:10:27 -04:00
# define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \
INTERNAL_CATCH_TEST ( macroName , resultDisposition , __VA_ARGS__ ) ; \
2017-06-26 15:30:23 -04:00
if ( ! Catch : : getResultCapture ( ) . lastAssertionPassed ( ) )
2012-02-10 03:30:13 -05:00
2011-03-14 04:45:55 -04:00
///////////////////////////////////////////////////////////////////////////////
2017-05-03 13:10:27 -04:00
# define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \
2012-10-29 15:55:13 -04:00
do { \
2018-07-23 07:55:14 -04:00
Catch : : AssertionHandler catchAssertionHandler ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , CATCH_INTERNAL_STRINGIFY ( __VA_ARGS__ ) , resultDisposition ) ; \
2012-10-29 15:55:13 -04:00
try { \
2017-05-03 13:10:27 -04:00
static_cast < void > ( __VA_ARGS__ ) ; \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleExceptionNotThrownAsExpected ( ) ; \
2012-10-29 15:55:13 -04:00
} \
catch ( . . . ) { \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleUnexpectedInflightException ( ) ; \
2012-10-29 15:55:13 -04:00
} \
2017-08-09 06:36:33 -04:00
INTERNAL_CATCH_REACT ( catchAssertionHandler ) \
2017-11-21 15:39:40 -05:00
} while ( false )
2011-03-14 04:45:55 -04:00
2011-02-03 15:00:46 -05:00
///////////////////////////////////////////////////////////////////////////////
2017-06-26 14:48:41 -04:00
# define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \
2012-10-29 15:55:13 -04:00
do { \
2018-07-23 07:55:14 -04:00
Catch : : AssertionHandler catchAssertionHandler ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , CATCH_INTERNAL_STRINGIFY ( __VA_ARGS__ ) , resultDisposition ) ; \
2017-08-08 19:52:20 -04:00
if ( catchAssertionHandler . allowThrows ( ) ) \
2014-06-05 13:11:31 -04:00
try { \
2017-05-03 13:10:27 -04:00
static_cast < void > ( __VA_ARGS__ ) ; \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleUnexpectedExceptionNotThrown ( ) ; \
2014-06-05 13:11:31 -04:00
} \
catch ( . . . ) { \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleExceptionThrownAsExpected ( ) ; \
2014-06-05 13:11:31 -04:00
} \
else \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleThrowingCallSkipped ( ) ; \
2017-08-09 06:36:33 -04:00
INTERNAL_CATCH_REACT ( catchAssertionHandler ) \
2017-11-21 15:39:40 -05:00
} while ( false )
2012-10-29 15:55:13 -04:00
2011-02-03 15:00:46 -05:00
///////////////////////////////////////////////////////////////////////////////
2017-03-21 09:22:39 -04:00
# define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \
2012-10-29 15:55:13 -04:00
do { \
2018-07-23 07:55:14 -04:00
Catch : : AssertionHandler catchAssertionHandler ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , CATCH_INTERNAL_STRINGIFY ( expr ) " , " CATCH_INTERNAL_STRINGIFY ( exceptionType ) , resultDisposition ) ; \
2017-08-08 14:43:07 -04:00
if ( catchAssertionHandler . allowThrows ( ) ) \
2014-06-05 13:11:31 -04:00
try { \
2017-01-31 12:02:11 -05:00
static_cast < void > ( expr ) ; \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleUnexpectedExceptionNotThrown ( ) ; \
2014-06-05 13:11:31 -04:00
} \
2017-07-19 15:30:00 -04:00
catch ( exceptionType const & ) { \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleExceptionThrownAsExpected ( ) ; \
2014-06-05 13:11:31 -04:00
} \
catch ( . . . ) { \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleUnexpectedInflightException ( ) ; \
2014-06-05 13:11:31 -04:00
} \
else \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleThrowingCallSkipped ( ) ; \
2017-08-09 06:36:33 -04:00
INTERNAL_CATCH_REACT ( catchAssertionHandler ) \
2017-11-21 15:39:40 -05:00
} while ( false )
2010-11-09 18:24:00 -05:00
2013-02-02 14:58:04 -05:00
///////////////////////////////////////////////////////////////////////////////
2017-06-26 11:47:40 -04:00
# define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \
do { \
2018-07-23 07:55:14 -04:00
Catch : : AssertionHandler catchAssertionHandler ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , Catch : : StringRef ( ) , resultDisposition ) ; \
2017-11-27 01:53:56 -05:00
catchAssertionHandler . handleMessage ( messageType , ( Catch : : MessageStream ( ) < < __VA_ARGS__ + : : Catch : : StreamEndStop ( ) ) . m_stream . str ( ) ) ; \
2017-08-09 06:36:33 -04:00
INTERNAL_CATCH_REACT ( catchAssertionHandler ) \
2017-11-21 15:39:40 -05:00
} while ( false )
2010-11-09 18:24:00 -05:00
2018-06-27 16:34:35 -04:00
///////////////////////////////////////////////////////////////////////////////
# define INTERNAL_CATCH_CAPTURE( varName, macroName, ... ) \
auto varName = Catch : : Capturer ( macroName , CATCH_INTERNAL_LINEINFO , Catch : : ResultWas : : Info , # __VA_ARGS__ ) ; \
varName . captureValues ( 0 , __VA_ARGS__ )
2011-02-03 15:00:46 -05:00
///////////////////////////////////////////////////////////////////////////////
2017-03-21 09:22:39 -04:00
# define INTERNAL_CATCH_INFO( macroName, log ) \
2018-07-23 07:55:14 -04:00
Catch : : ScopedMessage INTERNAL_CATCH_UNIQUE_NAME ( scopedMessage ) ( Catch : : MessageBuilder ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , Catch : : ResultWas : : Info ) < < log ) ;
2013-02-02 14:58:04 -05:00
2017-06-26 14:48:41 -04:00
///////////////////////////////////////////////////////////////////////////////
2019-02-01 10:11:30 -05:00
# define INTERNAL_CATCH_UNSCOPED_INFO( macroName, log ) \
Catch : : getResultCapture ( ) . emplaceUnscopedMessage ( Catch : : MessageBuilder ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , Catch : : ResultWas : : Info ) < < log )
///////////////////////////////////////////////////////////////////////////////
2017-08-09 07:10:14 -04:00
// Although this is matcher-based, it can be used with just a string
2017-06-26 14:48:41 -04:00
# define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \
do { \
2018-07-23 07:55:14 -04:00
Catch : : AssertionHandler catchAssertionHandler ( macroName # # _catch_sr , CATCH_INTERNAL_LINEINFO , CATCH_INTERNAL_STRINGIFY ( __VA_ARGS__ ) " , " CATCH_INTERNAL_STRINGIFY ( matcher ) , resultDisposition ) ; \
2017-08-08 19:44:30 -04:00
if ( catchAssertionHandler . allowThrows ( ) ) \
2017-06-26 14:48:41 -04:00
try { \
static_cast < void > ( __VA_ARGS__ ) ; \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleUnexpectedExceptionNotThrown ( ) ; \
2017-06-26 14:48:41 -04:00
} \
catch ( . . . ) { \
2018-07-23 07:55:14 -04:00
Catch : : handleExceptionMatchExpr ( catchAssertionHandler , matcher , # matcher # # _catch_sr ) ; \
2017-06-26 14:48:41 -04:00
} \
else \
2017-11-24 14:15:46 -05:00
catchAssertionHandler . handleThrowingCallSkipped ( ) ; \
2017-08-09 06:36:33 -04:00
INTERNAL_CATCH_REACT ( catchAssertionHandler ) \
2017-11-21 15:39:40 -05:00
} while ( false )
2017-06-26 14:48:41 -04:00
2017-08-27 11:03:32 -04:00
# endif // CATCH_CONFIG_DISABLE
2017-06-26 14:48:41 -04:00
2010-11-09 18:24:00 -05:00
# endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED