Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Introduce compile time toggle to remove Matchers from TU

The toggle is `CATCH_CONFIG_DISABLE_MATCHERS` and the only use is
to speed up compilation of small TUs. For large ones it is likely
insignificant, because the speed up is constant relative to
number of tests/assertions in TU.
Commit
0ca4cfb7432d37dbca4164c274a6b4f6e37d2dd4
Author
Martin Hořeňovský ·
Parents
35c1301bd582

Files · Download .tar.gz

Changes

 docs/configuration.md                      |  8 ++++++++
 include/catch.hpp                          | 18 ++++++++++++++----
 include/internal/catch_capture.hpp         |  7 ++++---
 include/internal/catch_matchers.cpp        |  4 ++++
 include/internal/catch_matchers.hpp        |  4 ++++
 include/internal/catch_matchers_string.cpp |  4 ++++
 include/internal/catch_matchers_string.h   |  4 ++++
 include/internal/catch_matchers_vector.h   |  5 +++++
 include/internal/catch_objc.hpp            |  4 ++++
 include/internal/catch_result_builder.cpp  |  4 ++--
 include/internal/catch_result_builder.h    |  2 ++
 11 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/docs/configuration.md b/docs/configuration.md
index 48250b62..b7855383 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -57,6 +57,7 @@ This can be useful on certain platforms that do not provide ```std::cout``` and
     CATCH_CONFIG_COUNTER                    // Use __COUNTER__ to generate unique names for test cases
     CATCH_CONFIG_WINDOWS_SEH                // Enable SEH handling on Windows
     CATCH_CONFIG_FAST_COMPILE               // Sacrifices some (rather minor) features for compilation speed
+    CATCH_CONFIG_DISABLE_MATCHERS           // Do not compile Matchers in this compilation unit
     CATCH_CONFIG_POSIX_SIGNALS              // Enable handling POSIX signals
     CATCH_CONFIG_WINDOWS_CRTDBG             // Enable leak checking using Windows's CRT Debug Heap
 
@@ -75,6 +76,13 @@ Defining this flag speeds up compilation of test files by ~20%, by making 2 chan
 
 `CATCH_CONFIG_FAST_COMPILE` has to be either defined, or not defined, in all translation units that are linked into single test binary, or the behaviour of setting `-b` flag and throwing unexpected exceptions will be unpredictable.
 
+
+## `CATCH_CONFIG_DISABLE_MATCHERS`
+When `CATCH_CONFIG_DISABLE_MATCHERS` is defined, all mentions of Catch's Matchers are ifdef-ed away from the translation unit. Doing so will speed up compilation of that TU.
+
+_Note: If you define `CATCH_CONFIG_DISABLE_MATCHERS` in the same file as Catch's main is implemented, your test executable will fail to link if you use Matchers anywhere._
+
+
 # Windows header clutter
 
 On Windows Catch includes `windows.h`. To minimize global namespace clutter in the implementation file, it defines `NOMINMAX` and `WIN32_LEAN_AND_MEAN` before including it. You can control this behaviour via two macros:
diff --git a/include/catch.hpp b/include/catch.hpp
index a58fdb1c..ead6c337 100644
--- a/include/catch.hpp
+++ b/include/catch.hpp
@@ -63,7 +63,6 @@
 #endif
 
 //////
-
 // If this config identifier is defined then all CATCH macros are prefixed with CATCH_
 #ifdef CATCH_CONFIG_PREFIX_ALL
 
@@ -77,8 +76,10 @@
 
 #define CATCH_REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "CATCH_REQUIRE_THROWS", Catch::ResultDisposition::Normal, "", __VA_ARGS__ )
 #define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr )
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 #define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr )
 #define CATCH_REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr )
+#endif// CATCH_CONFIG_DISABLE_MATCHERS
 #define CATCH_REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ )
 
 #define CATCH_CHECK( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ )
@@ -89,10 +90,13 @@
 
 #define CATCH_CHECK_THROWS( ... )  INTERNAL_CATCH_THROWS( "CATCH_CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, "", __VA_ARGS__ )
 #define CATCH_CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr )
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 #define CATCH_CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr )
 #define CATCH_CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr )
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
 #define CATCH_CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ )
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 #define CATCH_CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg )
 
 #if defined(CATCH_CONFIG_FAST_COMPILE)
@@ -100,7 +104,7 @@
 #else
 #define CATCH_REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg )
 #endif
-
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
 
 #define CATCH_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg )
 #define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( "CATCH_WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg )
@@ -144,8 +148,10 @@
 
 #define REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "REQUIRE_THROWS", Catch::ResultDisposition::Normal, __VA_ARGS__ )
 #define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr )
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 #define REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr )
 #define REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr )
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
 #define REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ )
 
 #define CHECK( ... ) INTERNAL_CATCH_TEST( "CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ )
@@ -156,18 +162,22 @@
 
 #define CHECK_THROWS( ... )  INTERNAL_CATCH_THROWS( "CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ )
 #define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr )
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 #define CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr )
 #define CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr )
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
 #define CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ )
 
+
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 #define CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg )
 
 #if defined(CATCH_CONFIG_FAST_COMPILE)
 #define REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT_NO_TRY( "REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg )
 #else
 #define REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg )
-#endif
-
+#endif // CATCH_CONFIG_FAST_COMPILE
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
 
 #define INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg )
 #define WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg )
diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp
index 1c862505..fa6952e4 100644
--- a/include/internal/catch_capture.hpp
+++ b/include/internal/catch_capture.hpp
@@ -42,7 +42,7 @@
         INTERNAL_CATCH_REACT( __catchResult ) \
     } while( Catch::isTrue( false && static_cast<bool>( !!(expr) ) ) ) // expr here is never evaluated at runtime but it forces the compiler to give it a look
 // The double negation silences MSVC's C4800 warning, the static_cast forces short-circuit evaluation if the type has overloaded &&.
-
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 #define INTERNAL_CHECK_THAT_NO_TRY( macroName, matcher, resultDisposition, arg ) \
     do { \
         Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #arg ", " #matcher, resultDisposition ); \
@@ -51,7 +51,7 @@
         __catchResult.unsetExceptionGuard(); \
         INTERNAL_CATCH_REACT( __catchResult ) \
     } while( Catch::alwaysFalse() )
-
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
 #else
 ///////////////////////////////////////////////////////////////////////////////
 // In the event of a failure works out if the debugger needs to be invoked
@@ -155,6 +155,7 @@
 #define INTERNAL_CATCH_INFO( macroName, log ) \
     Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage ) = Catch::MessageBuilder( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log;
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
 ///////////////////////////////////////////////////////////////////////////////
 #define INTERNAL_CHECK_THAT( macroName, matcher, resultDisposition, arg ) \
     do { \
@@ -204,7 +205,7 @@
             __catchResult.captureResult( Catch::ResultWas::Ok ); \
         INTERNAL_CATCH_REACT( __catchResult ) \
     } while( Catch::alwaysFalse() )
-
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
 
 
 #endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
diff --git a/include/internal/catch_matchers.cpp b/include/internal/catch_matchers.cpp
index 38ae8c77..a2620bc5 100644
--- a/include/internal/catch_matchers.cpp
+++ b/include/internal/catch_matchers.cpp
@@ -5,6 +5,8 @@
  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  */
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
+
 #include "catch_matchers.hpp"
 
 namespace Catch {
@@ -24,3 +26,5 @@ using namespace Matchers;
 using Matchers::Impl::MatcherBase;
 
 } // namespace Catch
+
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
\ No newline at end of file
diff --git a/include/internal/catch_matchers.hpp b/include/internal/catch_matchers.hpp
index 4f41cb01..37787a9c 100644
--- a/include/internal/catch_matchers.hpp
+++ b/include/internal/catch_matchers.hpp
@@ -8,6 +8,8 @@
 #ifndef TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
 #define TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
+
 #include "catch_common.h"
 
 #include <vector>
@@ -179,4 +181,6 @@ using Matchers::Impl::MatcherBase;
 
 } // namespace Catch
 
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
+
 #endif // TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
diff --git a/include/internal/catch_matchers_string.cpp b/include/internal/catch_matchers_string.cpp
index 2f8ffdb8..54f44d6d 100644
--- a/include/internal/catch_matchers_string.cpp
+++ b/include/internal/catch_matchers_string.cpp
@@ -6,6 +6,8 @@
  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  */
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
+
 #include "catch_matchers_string.h"
 #include "catch_string_manip.h"
 
@@ -92,3 +94,5 @@ namespace Matchers {
 
 } // namespace Matchers
 } // namespace Catch
+
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
diff --git a/include/internal/catch_matchers_string.h b/include/internal/catch_matchers_string.h
index eca35789..ab3f1c44 100644
--- a/include/internal/catch_matchers_string.h
+++ b/include/internal/catch_matchers_string.h
@@ -8,6 +8,8 @@
 #ifndef TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED
 #define TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
+
 #include "catch_matchers.hpp"
 
 #include <string>
@@ -66,4 +68,6 @@ namespace Matchers {
 } // namespace Matchers
 } // namespace Catch
 
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
+
 #endif // TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED
diff --git a/include/internal/catch_matchers_vector.h b/include/internal/catch_matchers_vector.h
index 4bf39d70..de1c52b2 100644
--- a/include/internal/catch_matchers_vector.h
+++ b/include/internal/catch_matchers_vector.h
@@ -8,6 +8,9 @@
 #ifndef TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED
 #define TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
+
+
 #include "catch_matchers.hpp"
 
 namespace Catch {
@@ -112,4 +115,6 @@ namespace Matchers {
 } // namespace Matchers
 } // namespace Catch
 
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
+
 #endif // TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED
diff --git a/include/internal/catch_objc.hpp b/include/internal/catch_objc.hpp
index 350dc72c..2a8ec2f9 100644
--- a/include/internal/catch_objc.hpp
+++ b/include/internal/catch_objc.hpp
@@ -101,6 +101,8 @@ namespace Catch {
         return noTestMethods;
     }
 
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
+
     namespace Matchers {
         namespace Impl {
         namespace NSStringMatchers {
@@ -189,6 +191,8 @@ namespace Catch {
 
     using namespace Matchers;
 
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
+
 } // namespace Catch
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/include/internal/catch_result_builder.cpp b/include/internal/catch_result_builder.cpp
index a1fed54e..cf0b1b16 100644
--- a/include/internal/catch_result_builder.cpp
+++ b/include/internal/catch_result_builder.cpp
@@ -72,7 +72,7 @@ namespace Catch {
         setResultType( resultType );
         captureExpression();
     }
-
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
     void ResultBuilder::captureExpectedException( std::string const& expectedMessage ) {
         if( expectedMessage.empty() )
             captureExpectedException( Matchers::Impl::MatchAllOf<std::string>() );
@@ -96,7 +96,7 @@ namespace Catch {
         AssertionResult result( m_assertionInfo, data );
         handleResult( result );
     }
-
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
     void ResultBuilder::captureExpression() {
         AssertionResult result = build();
         handleResult( result );
diff --git a/include/internal/catch_result_builder.h b/include/internal/catch_result_builder.h
index 45bc8920..2b5dd145 100644
--- a/include/internal/catch_result_builder.h
+++ b/include/internal/catch_result_builder.h
@@ -58,8 +58,10 @@ namespace Catch {
         void useActiveException( ResultDisposition::Flags resultDisposition = ResultDisposition::Normal );
         void captureResult( ResultWas::OfType resultType );
         void captureExpression();
+#if defined(CATCH_CONFIG_DISABLE_MATCHERS)
         void captureExpectedException( std::string const& expectedMessage );
         void captureExpectedException( Matchers::Impl::MatcherBase<std::string> const& matcher );
+#endif // CATCH_CONFIG_DISABLE_MATCHERS
         void handleResult( AssertionResult const& result );
         void react();
         bool shouldDebugBreak() const;