• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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( false )
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 #if defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
67 
68 #define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... )
69 #define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... )
70 #define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr )
71 
72 #else
73 
74 ///////////////////////////////////////////////////////////////////////////////
75 #define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \
76     do { \
77         Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \
78         try { \
79             static_cast<void>(__VA_ARGS__); \
80             catchAssertionHandler.handleExceptionNotThrownAsExpected(); \
81         } \
82         catch( ... ) { \
83             catchAssertionHandler.handleUnexpectedInflightException(); \
84         } \
85         INTERNAL_CATCH_REACT( catchAssertionHandler ) \
86     } while( false )
87 
88 ///////////////////////////////////////////////////////////////////////////////
89 #define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \
90     do { \
91         Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \
92         if( catchAssertionHandler.allowThrows() ) \
93             try { \
94                 static_cast<void>(__VA_ARGS__); \
95                 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
96             } \
97             catch( ... ) { \
98                 catchAssertionHandler.handleExceptionThrownAsExpected(); \
99             } \
100         else \
101             catchAssertionHandler.handleThrowingCallSkipped(); \
102         INTERNAL_CATCH_REACT( catchAssertionHandler ) \
103     } while( false )
104 
105 ///////////////////////////////////////////////////////////////////////////////
106 #define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \
107     do { \
108         Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \
109         if( catchAssertionHandler.allowThrows() ) \
110             try { \
111                 static_cast<void>(expr); \
112                 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
113             } \
114             catch( exceptionType const& ) { \
115                 catchAssertionHandler.handleExceptionThrownAsExpected(); \
116             } \
117             catch( ... ) { \
118                 catchAssertionHandler.handleUnexpectedInflightException(); \
119             } \
120         else \
121             catchAssertionHandler.handleThrowingCallSkipped(); \
122         INTERNAL_CATCH_REACT( catchAssertionHandler ) \
123     } while( false )
124 
125 #endif  // CATCH_CONFIG_DISABLE_EXCEPTIONS
126 
127 ///////////////////////////////////////////////////////////////////////////////
128 #define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \
129     do { \
130         Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), resultDisposition ); \
131         catchAssertionHandler.handleMessage( messageType, ( Catch::MessageStream() << __VA_ARGS__ + ::Catch::StreamEndStop() ).m_stream.str() ); \
132         INTERNAL_CATCH_REACT( catchAssertionHandler ) \
133     } while( false )
134 
135 ///////////////////////////////////////////////////////////////////////////////
136 #define INTERNAL_CATCH_CAPTURE( varName, macroName, ... ) \
137     auto varName = Catch::Capturer( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info, #__VA_ARGS__ ); \
138     varName.captureValues( 0, __VA_ARGS__ )
139 
140 ///////////////////////////////////////////////////////////////////////////////
141 #define INTERNAL_CATCH_INFO( macroName, log ) \
142     Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log );
143 
144 ///////////////////////////////////////////////////////////////////////////////
145 #define INTERNAL_CATCH_UNSCOPED_INFO( macroName, log ) \
146     Catch::getResultCapture().emplaceUnscopedMessage( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log )
147 
148 ///////////////////////////////////////////////////////////////////////////////
149 // Although this is matcher-based, it can be used with just a string
150 #define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \
151     do { \
152         Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \
153         if( catchAssertionHandler.allowThrows() ) \
154             try { \
155                 static_cast<void>(__VA_ARGS__); \
156                 catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
157             } \
158             catch( ... ) { \
159                 Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher##_catch_sr ); \
160             } \
161         else \
162             catchAssertionHandler.handleThrowingCallSkipped(); \
163         INTERNAL_CATCH_REACT( catchAssertionHandler ) \
164     } while( false )
165 
166 #endif // CATCH_CONFIG_DISABLE
167 
168 #endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
169