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
Backport Catch2 3.16.1
- Commit
1f78d8903c2fb18afea8c3055f8df83d8c60d9b5- Author
- Leitwolf11 ·
- Parents
5633c5c98ec0
Changes
.github/workflows/mac-builds.yml | 2 +- CMakeLists.txt | 2 +- README.md | 2 +- docs/deprecations.md | 10 + docs/release-notes.md | 35 ++ docs/upstream-status.md | 27 ++ docs/why-catch3.md | 4 +- extras/catch_amalgamated.cpp | 96 ++++- extras/catch_amalgamated.hpp | 451 +++++++++++---------- meson.build | 2 +- src/catch2/benchmark/catch_optimizer.hpp | 2 +- src/catch2/catch_session.cpp | 12 +- src/catch2/catch_session.hpp | 1 + src/catch2/catch_version.cpp | 2 +- src/catch2/catch_version_macros.hpp | 2 +- src/catch2/internal/catch_assertion_handler.cpp | 2 +- src/catch2/internal/catch_assertion_handler.hpp | 15 +- src/catch2/internal/catch_decomposer.cpp | 7 - src/catch2/internal/catch_decomposer.hpp | 342 ++++++++-------- src/catch2/internal/catch_preprocessor.hpp | 4 +- src/catch2/internal/catch_test_macro_impl.hpp | 16 +- .../matchers/internal/catch_matchers_impl.hpp | 17 +- src/catch2/reporters/catch_reporter_automake.cpp | 9 + src/catch2/reporters/catch_reporter_automake.hpp | 6 +- src/catch2/reporters/catch_reporter_compact.cpp | 7 + src/catch2/reporters/catch_reporter_compact.hpp | 6 +- src/catch2/reporters/catch_reporter_console.cpp | 1 + src/catch2/reporters/catch_reporter_helpers.cpp | 13 + src/catch2/reporters/catch_reporter_helpers.hpp | 13 +- src/catch2/reporters/catch_reporter_json.cpp | 2 + src/catch2/reporters/catch_reporter_junit.cpp | 4 +- src/catch2/reporters/catch_reporter_sonarqube.cpp | 10 + src/catch2/reporters/catch_reporter_sonarqube.hpp | 9 +- src/catch2/reporters/catch_reporter_tap.cpp | 8 + src/catch2/reporters/catch_reporter_tap.hpp | 6 +- src/catch2/reporters/catch_reporter_teamcity.cpp | 8 + src/catch2/reporters/catch_reporter_teamcity.hpp | 8 +- src/catch2/reporters/catch_reporter_xml.cpp | 2 + tests/ExtraTests/CMakeLists.txt | 35 ++ tests/ExtraTests/X38-ThrowingReporter.cpp | 79 ++++ .../IntrospectiveTests/Reporters.tests.cpp | 14 + xmake.lua | 2 +- 42 files changed, 840 insertions(+), 455 deletions(-) diff --git a/.github/workflows/mac-builds.yml b/.github/workflows/mac-builds.yml index 7ba8dac5..7162646b 100644 --- a/.github/workflows/mac-builds.yml +++ b/.github/workflows/mac-builds.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - image: [macos-14, macos-15, macos-15-intel] + image: [macos-15, macos-15-intel] build_type: [Debug, Release] std: [14, 17] diff --git a/CMakeLists.txt b/CMakeLists.txt index 291af7c5..a8f1da6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ if(CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) endif() project(Catch2 - VERSION 3.16.0 # CML version placeholder, don't delete + VERSION 3.16.1 # CML version placeholder, don't delete LANGUAGES CXX HOMEPAGE_URL "https://github.com/catchorg/Catch2" DESCRIPTION "A modern, C++-native, unit test framework." diff --git a/README.md b/README.md index fae1e037..d1ef75e0 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ owned C++20 library for property checks, snapshots, structured results, and XML serialization. > Catch3 is not an official Catch2 release and is not endorsed by Catch2's -> maintainers. Its current compatibility engine is based on Catch2 3.16.0; +> maintainers. Its current compatibility engine is based on Catch2 3.16.1; > upstream-originated files keep their upstream license. Progmasoft-authored > additions have their own licensing terms. See [the project scope and > compatibility notes](docs/why-catch3.md). diff --git a/docs/deprecations.md b/docs/deprecations.md index 3915ddbc..82f044cc 100644 --- a/docs/deprecations.md +++ b/docs/deprecations.md @@ -62,6 +62,16 @@ specified the filters through the new `-p/--path-filter` parameter, which means that generators are taken into account. +### Reporter event handling functions without noexcept + +> Deprecated in Catch2 3.16.1 + +Catch2 does not try to support reporters throwing from arbitrary +`fooStarting` or `fooEnded` test running event. For legacy reasons, the +event listener interface is declared without `noexcept`, but in the future +it will be changed to use `noexcept` to make this limitation explicit. + + ### Generator interfaces #### Defaulted `UntypedGeneratorBase::isFinite()` diff --git a/docs/release-notes.md b/docs/release-notes.md index 3a2c7734..2ba3b663 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -5,6 +5,7 @@ # Release notes **Contents**<br> +[3.16.1](#3161)<br> [3.16.0](#3160)<br> [3.15.3](#3153)<br> [3.15.2](#3152)<br> @@ -82,6 +83,40 @@ [Even Older versions](#even-older-versions)<br> +## 3.16.1 + +### Fixes +* Fixed optimizations being disabled after including benchmarking headers under MSVC. (#3212) + * This was introduced in 3.16.0's fix for the deopt implementation for MSVC never being used. +* Fixed Console reporter replacing `\0xFF`s in user-provided strings. +* Added missing `<cassert>` include to `catch_generators_range.hpp`. (#3205) + +### Improvements +* Workaround for GCC's quadratic scaling for release builds with number of test cases. + * In practice the improvement is only noticeable with unity builds and/or template product tests. +* Changed how decomposer operators are defined for better compilation times. + * For Catch2's `SelfTest` binary, this provides ~2% build time improvement. +* Changed how decomposer handles stringification for smaller binary sizes. + * For the `SelfTest` binary this is about ~8%. +* Improved compilation costs of multiple assertions in one test cases for release builds. +* Optimized initial test registration at process start: + * Fewer allocations for test cases with many tags. + * Faster processing for test cases with <=2 tags. + * Faster tag validation. + * Faster checking for duplicated test cases. +* Optimized test selection and filtering: +* Optimized filtering tests by test name. + * Running `SelfTest` with `abc*` as the filter pattern is ~10% faster. + +### Miscellaneous +* `catch_discover_tests` registers individual tests with `--order decl` for faster startup. + * For `SelfTest`, this provides 20% improvement in Debug and 5% in Release builds. +* `CMakeConfig::` family of tests use the main build's toolchain file. (#3202) +* Deprecated throwing exceptions from the reporter interface functions. + * Previously, throwing from e.g. `assertionEnded` might, or might not, have worked depending on which assertion has ended. As it is not practical for Catch2 to guard against the reporters failing randomly, and marking so with `noexcept` can provide compilation throughput improvements, I decided to fully forbid it. + * Reporter constructors are allowed to throw and the test binary will report the error nicely before exiting. + * Due to C++ limitations, this deprecation cannot be marked inside the code, only in docs. + ## 3.16.0 diff --git a/docs/upstream-status.md b/docs/upstream-status.md index 99182ad7..ac87b654 100644 --- a/docs/upstream-status.md +++ b/docs/upstream-status.md @@ -13,6 +13,33 @@ licenses or compiler options: - `222e233903d287cda4713fb08e1a959e8d71fd6d`: isolate Clang's pointer-to-bool diagnostic at the upstream reusable-string-stream compatibility boundary. +The branch was reviewed again through +`08092139210881f01a34e60c5f2fd6afa0ea4024`, the commit of Catch2 3.16.1, on +2026-10-11. The eleven changes since the previous reviewed revision were all +backported, in upstream order, and the compatibility engine is now 3.16.1: + +- `d7683484`: a better message when a reporter cannot be initialized. +- `ea5020f0`: every first-party reporter validates its custom arguments. +- `aa01559e`: the throwing-reporter test also throws in `assertionEnded`. +- `45cd3987`: `handleUnexpectedInflightException` is `noexcept`. +- `6adbd539`: MSVC optimizations are enabled again after the benchmark + headers (upstream issue 3212). +- `e87f2882`: test registration with non-type template parameters no longer + scales quadratically. +- `73d546cd`: `REQUIRE` expands its arguments fewer times under GCC and + Clang. +- `b710039a`: the decomposer operators are plain functions and no longer + hidden friends. +- `f899467b`: streaming of reconstructed expressions is type-erased. +- `8f0e70ca`: the retired `macos-14` runner is dropped from the macOS builds. +- `08092139`: version 3.16.1, with its release notes. + +Each applied without a conflict in the sources. The amalgamated distribution +under `extras/` did conflict, because it is generated; it was generated again +from the sources of this repository with `generateAmalgamatedFiles.py`, which +also brought it up to date with fork changes it had not yet picked up. No +fork-owned file under `src/Progmasoft` needed a change. + These are compatibility fixes, not a claim that every future upstream change is automatically incorporated. The existing Windows/macOS build and test workflows remain the verification gate for the fork. diff --git a/docs/why-catch3.md b/docs/why-catch3.md index 7fbeef44..8c900ac7 100644 --- a/docs/why-catch3.md +++ b/docs/why-catch3.md @@ -7,7 +7,7 @@ namespace, build-target, and licensing boundary. The name does **not** mean that this is an official Catch2 v3 release, a new upstream version, or a project endorsed by Catch2's maintainers. The current -compatibility engine is based on Catch2's 3.16.0 development line. The project +compatibility engine is based on Catch2's 3.16.1 development line. The project is still early-stage and does not yet replace or independently reimplement the Catch2 runner. @@ -35,7 +35,7 @@ Catch2's `Catch` namespace, established headers, macros, command-line behavior, reporters, and coding style remain the compatibility surface. Progmasoft features should be added under `Progmasoft::Catch3` and must not require users to rename existing Catch2 tests. The first compatibility goal is source-level -continuity with the included Catch2 3.16.0 base; this is not a promise that every +continuity with the included Catch2 3.16.1 base; this is not a promise that every future Catch2 upstream change will be adopted unchanged or that all downstream projects have already been validated. diff --git a/extras/catch_amalgamated.cpp b/extras/catch_amalgamated.cpp index c6a8de36..230849f1 100644 --- a/extras/catch_amalgamated.cpp +++ b/extras/catch_amalgamated.cpp @@ -6,8 +6,8 @@ // SPDX-License-Identifier: BSL-1.0 -// Catch v3.16.0 -// Generated: 2026-09-28 00:12:28.744397 +// Catch v3.16.1 +// Generated: 2026-10-11 02:09:25.622664 // ---------------------------------------------------------- // This file is an amalgamation of multiple different files. // You probably shouldn't edit it directly. @@ -1458,7 +1458,9 @@ namespace Catch { int err = 0; std::FILE* const file = openGuardFile( guardFilePath, err ); if ( file == nullptr ) { +#if defined( _MSC_VER ) char msgBuffer[100] = {}; +#endif std::string errMsg; #if defined( _MSC_VER ) if ( !strerror_s( msgBuffer, err ) ) { @@ -1677,7 +1679,17 @@ namespace Catch { getCurrentMutableContext().setConfig(m_config.get()); // Create reporter(s) so we can route listings through them - auto reporter = prepareReporters(m_config.get()); + IEventListenerPtr reporter; + CATCH_TRY { + auto reporter_ = prepareReporters( m_config.get() ); + swap( reporter, reporter_ ); + } +#if !defined( CATCH_CONFIG_DISABLE_EXCEPTIONS ) + catch ( std::exception& ex ) { + Catch::cerr() << "Error when creating reporter: " << ex.what() << '\n' << std::flush; + return ReporterConstructionErrorExitCode; + } +#endif auto const& invalidSpecs = m_config->testSpec().getInvalidSpecs(); if ( !invalidSpecs.empty() ) { @@ -2586,7 +2598,7 @@ namespace Catch { } Version const& libraryVersion() { - static Version version( 3, 16, 0, "", 0 ); + static Version version( 3, 16, 1, "", 0 ); return version; } @@ -2922,7 +2934,7 @@ namespace Catch { } } - void AssertionHandler::handleUnexpectedInflightException() { + void AssertionHandler::handleUnexpectedInflightException() noexcept { m_resultCapture.handleUnexpectedInflightException( m_assertionInfo, Catch::translateActiveException(), m_reaction ); } @@ -4234,13 +4246,6 @@ namespace Catch { namespace Catch { - void ITransientExpression::streamReconstructedExpression( - std::ostream& os ) const { - // We can't make this function pure virtual to keep ITransientExpression - // constexpr, so we write error message instead - os << "Some class derived from ITransientExpression without overriding streamReconstructedExpression"; - } - void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ) { if( lhs.size() + rhs.size() < 40 && lhs.find('\n') == std::string::npos && @@ -6056,6 +6061,7 @@ ReporterSpec::ReporterSpec( #include <cstdio> +#include <list> #include <sstream> #include <tuple> #include <vector> @@ -6065,7 +6071,10 @@ namespace Catch { // This class encapsulates the idea of a pool of ostringstreams that can be reused. struct StringStreams { std::vector<Detail::unique_ptr<std::ostringstream>> m_streams; - std::vector<std::size_t> m_unused; + // The library can be linked into ASan-instrumented clients without + // instrumenting Catch3 itself. A list avoids mixed-mode libc++ vector + // container annotations for the reusable-index pool. + std::list<std::size_t> m_unused; std::ostringstream m_referenceStream; // Used for copy state/ flags from Detail::Mutex m_mutex; @@ -9530,12 +9539,20 @@ namespace Catch { + #include <ostream> namespace Catch { AutomakeReporter::~AutomakeReporter() = default; + AutomakeReporter::AutomakeReporter( ReporterConfig&& _config ): + StreamingReporterBase( CATCH_MOVE( _config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldReportAllAssertionStarts = false; + } + void AutomakeReporter::testCaseEnded(TestCaseStats const& _testCaseStats) { // Possible values to emit are PASS, XFAIL, SKIP, FAIL, XPASS and ERROR. m_stream << ":test-result: "; @@ -9837,6 +9854,13 @@ private: } CompactReporter::~CompactReporter() = default; + CompactReporter::CompactReporter( ReporterConfig&& _config ): + StreamingReporterBase( CATCH_MOVE( _config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldReportAllAssertionStarts = false; + } + } // end namespace Catch @@ -10224,6 +10248,7 @@ ConsoleReporter::ConsoleReporter(ReporterConfig&& config): }; } }())) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); m_preferences.shouldReportAllAssertionStarts = false; } @@ -11037,6 +11062,18 @@ namespace Catch { printSummaryRow( stream, streamColour, "assertions"_sr, columns, 1 ); } + void rejectSuperfluousConfigKeys( + std::map<std::string, std::string> const& options, + std::initializer_list<StringRef> knownKeys ) { + for ( auto const& option : options ) { + StringRef key( option.first ); + CATCH_ENFORCE( std::find( knownKeys.begin(), + knownKeys.end(), + key ) != knownKeys.end(), + "Unknown reporter argument: " << key ); + } + } + } // namespace Catch @@ -11075,6 +11112,7 @@ namespace Catch { JsonReporter::JsonReporter( ReporterConfig&& config ): StreamingReporterBase{ CATCH_MOVE( config ) } { + rejectSuperfluousConfigKeys( m_customOptions, {} ); m_preferences.shouldRedirectStdOut = true; // TBD: Do we want to report all assertions? XML reporter does @@ -11480,11 +11518,13 @@ namespace Catch { : CumulativeReporterBase( CATCH_MOVE(_config) ), xml( m_stream ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + m_preferences.shouldRedirectStdOut = true; m_preferences.shouldReportAllAssertions = false; m_preferences.shouldReportAllAssertionStarts = false; m_shouldStoreSuccesfulAssertions = false; - } + } std::string JunitReporter::getDescription() { return "Reports test results in an XML format that looks like Ant's junitreport target"; @@ -11947,6 +11987,16 @@ namespace Catch { } } + SonarQubeReporter::SonarQubeReporter( ReporterConfig&& config ): + CumulativeReporterBase( CATCH_MOVE( config ) ), xml( m_stream ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldRedirectStdOut = true; + m_preferences.shouldReportAllAssertions = false; + m_preferences.shouldReportAllAssertionStarts = false; + m_shouldStoreSuccesfulAssertions = false; + } + void SonarQubeReporter::testRunStarting(TestRunInfo const& testRunInfo) { CumulativeReporterBase::testRunStarting(testRunInfo); @@ -12275,6 +12325,14 @@ namespace Catch { } // End anonymous namespace + TAPReporter::TAPReporter( ReporterConfig&& config ): + StreamingReporterBase( CATCH_MOVE( config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldReportAllAssertions = true; + m_preferences.shouldReportAllAssertionStarts = false; + } + void TAPReporter::testRunStarting( TestRunInfo const& ) { if ( m_config->testSpec().hasFilters() ) { m_stream << "# filters: " << m_config->testSpec() << '\n'; @@ -12346,6 +12404,14 @@ namespace Catch { } // end anonymous namespace + TeamCityReporter::TeamCityReporter( ReporterConfig&& _config ): + StreamingReporterBase( CATCH_MOVE( _config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldRedirectStdOut = true; + m_preferences.shouldReportAllAssertionStarts = false; + } + TeamCityReporter::~TeamCityReporter() = default; void TeamCityReporter::testRunStarting( TestRunInfo const& runInfo ) { @@ -12492,6 +12558,8 @@ namespace Catch { : StreamingReporterBase( CATCH_MOVE(_config) ), m_xml(m_stream) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + m_preferences.shouldRedirectStdOut = true; m_preferences.shouldReportAllAssertions = true; m_preferences.shouldReportAllAssertionStarts = false; diff --git a/extras/catch_amalgamated.hpp b/extras/catch_amalgamated.hpp index 29279da0..25207b85 100644 --- a/extras/catch_amalgamated.hpp +++ b/extras/catch_amalgamated.hpp @@ -6,8 +6,8 @@ // SPDX-License-Identifier: BSL-1.0 -// Catch v3.16.0 -// Generated: 2026-09-28 00:12:28.337654 +// Catch v3.16.1 +// Generated: 2026-10-11 02:09:25.452537 // ---------------------------------------------------------- // This file is an amalgamation of multiple different files. // You probably shouldn't edit it directly. @@ -1520,7 +1520,7 @@ namespace Catch { *reinterpret_cast<char volatile*>(p) = *reinterpret_cast<char const volatile*>(p); } // TODO equivalent keep_memory() -#if defined(_MSVC_VER) +#if defined(_MSC_VER) #pragma optimize("", on) #endif @@ -2513,6 +2513,9 @@ namespace Catch { // be null... #pragma GCC diagnostic ignored "-Waddress" #pragma GCC diagnostic ignored "-Wnonnull-compare" +#elif defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpointer-bool-conversion" #endif template<typename T> @@ -2523,6 +2526,8 @@ namespace Catch { #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop +#elif defined(__clang__) +#pragma clang diagnostic pop #endif auto get() -> std::ostream& { return *m_oss; } }; @@ -5130,6 +5135,7 @@ namespace Catch { constexpr int UnmatchedTestSpecExitCode = 3; constexpr int AllTestsSkippedExitCode = 4; constexpr int InvalidTestSpecExitCode = 5; + constexpr int ReporterConstructionErrorExitCode = 6; constexpr int TestFailureExitCode = 42; class Session : Detail::NonCopyable { @@ -5531,28 +5537,27 @@ namespace Catch { struct always_false : std::false_type {}; class ITransientExpression { + //! Type-erased streaming function for the derived expression + using StreamFn = void ( * )( void const*, std::ostream& ); + + StreamFn m_streamFn; bool m_isBinaryExpression; bool m_result; - protected: - ~ITransientExpression() = default; - public: constexpr auto isBinaryExpression() const -> bool { return m_isBinaryExpression; } constexpr auto getResult() const -> bool { return m_result; } - //! This function **has** to be overridden by the derived class. - virtual void streamReconstructedExpression( std::ostream& os ) const; - constexpr ITransientExpression( bool isBinaryExpression, bool result ) - : m_isBinaryExpression( isBinaryExpression ), - m_result( result ) - {} - - constexpr ITransientExpression( ITransientExpression const& ) = default; - constexpr ITransientExpression& operator=( ITransientExpression const& ) = default; + constexpr ITransientExpression( StreamFn streamFn, + bool isBinaryExpression, + bool result ): + m_streamFn( streamFn ), + m_isBinaryExpression( isBinaryExpression ), + m_result( result ) {} - friend std::ostream& operator<<(std::ostream& out, ITransientExpression const& expr) { - expr.streamReconstructedExpression(out); + friend std::ostream& operator<<( std::ostream& out, + ITransientExpression const& expr ) { + expr.m_streamFn( &expr, out ); return out; } }; @@ -5565,87 +5570,67 @@ namespace Catch { StringRef m_op; RhsT m_rhs; - void streamReconstructedExpression( std::ostream &os ) const override { - formatReconstructedExpression - ( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) ); + static void streamFn( void const* self, std::ostream& out ) { + auto const& expr = *static_cast<BinaryExpr const*>( + static_cast<ITransientExpression const*>( self ) ); + formatReconstructedExpression( + out, + Catch::Detail::stringify( expr.m_lhs ), + expr.m_op, + Catch::Detail::stringify( expr.m_rhs ) ); } public: constexpr BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs ) - : ITransientExpression{ true, comparisonResult }, + : ITransientExpression{ &streamFn, true, comparisonResult }, m_lhs( lhs ), m_op( op ), m_rhs( rhs ) {} + }; - template<typename T> - auto operator && ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator || ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator == ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator != ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator > ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator < ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } +#define CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( op ) \ + template <typename LhsT, typename RhsT, typename T> \ + auto operator op( BinaryExpr<LhsT, RhsT> const&, T ) \ + ->BinaryExpr<LhsT, RhsT const&> const { \ + static_assert( always_false<T>::value, \ + "chained comparisons are not supported inside " \ + "assertions, wrap the expression inside " \ + "parentheses, or decompose it" ); \ + } - template<typename T> - auto operator >= ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } + /* + * The operators are **intentionally** not hidden friends. + * + * If they were hidden friends, all of them would get stamped out when + * a `BinaryExpr` is instantiated with a new type. In practice, only + * few operators are used for each different `BinaryExpr<LhsT, RhsT>`, + * so this avoids bunch of wasted work. + */ + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( && ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( || ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( == ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( != ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( > ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( < ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( >= ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( <= ) - template<typename T> - auto operator <= ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - }; +#undef CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR template<typename LhsT> class UnaryExpr final : public ITransientExpression { LhsT m_lhs; - void streamReconstructedExpression( std::ostream &os ) const override { - os << Catch::Detail::stringify( m_lhs ); + static void streamFn( void const* self, std::ostream& os ) { + auto const& expr = *static_cast<UnaryExpr const*>( + static_cast<ITransientExpression const*>( self ) ); + os << Catch::Detail::stringify( expr.m_lhs ); } public: explicit constexpr UnaryExpr( LhsT lhs ) - : ITransientExpression{ false, static_cast<bool>(lhs) }, + : ITransientExpression{ &streamFn, false, static_cast<bool>(lhs) }, m_lhs( lhs ) {} }; @@ -5657,153 +5642,186 @@ namespace Catch { public: explicit constexpr ExprLhs( LhsT lhs ) : m_lhs( lhs ) {} + // We rely on reference collapsing if LhsT is already a reference. + // Used in the out-of-class decomposition operators + constexpr LhsT& getLhs() { return m_lhs; } + + constexpr auto makeUnaryExpr() const -> UnaryExpr<LhsT> { + return UnaryExpr<LhsT>{ m_lhs }; + } + }; + +/* + * The comparison operators are **intentionally** not hidden friends. + * + * If they were hidden friends, all of them would get stamped out when + * a `ExprLhs` is instantiated with a new type. In practice, only few + * operators are used for each different `ExprLhs<T>`, so this avoids + * a lot of wasted work. + */ #define CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( id, op ) \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT&& rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT&& rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ Detail::negation<capture_by_value< \ Detail::RemoveCVRef_t<RhsT>>>>::value, \ BinaryExpr<LhsT, RhsT const&>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ capture_by_value<RhsT>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ - Detail::is_eq_0_comparable<LhsT>, \ - /* We allow long because we want `ptr op NULL` to be accepted */ \ + Detail::is_eq_0_comparable<LhsT>, /* We allow long because we \ + want `ptr op NULL` to be \ + accepted */ \ Detail::disjunction<std::is_same<RhsT, int>, \ std::is_same<RhsT, long>>>::value, \ BinaryExpr<LhsT, RhsT>> { \ if ( rhs != 0 ) { throw_test_failure_exception(); } \ - return { \ - static_cast<bool>( lhs.m_lhs op 0 ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op 0 ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ - Detail::is_eq_0_comparable<RhsT>, \ - /* We allow long because we want `ptr op NULL` to be accepted */ \ + Detail::is_eq_0_comparable<RhsT>, /* We allow long because we \ + want `ptr op NULL` to be \ + accepted */ \ Detail::disjunction<std::is_same<LhsT, int>, \ std::is_same<LhsT, long>>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - if ( lhs.m_lhs != 0 ) { throw_test_failure_exception(); } \ - return { static_cast<bool>( 0 op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \ + return { static_cast<bool>( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \ } - CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == ) - CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( ne, != ) - - #undef CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR + CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == ) + CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( ne, != ) +#undef CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR #define CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( id, op ) \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT&& rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT&& rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ Detail::negation<capture_by_value< \ Detail::RemoveCVRef_t<RhsT>>>>::value, \ BinaryExpr<LhsT, RhsT const&>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ capture_by_value<RhsT>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ Detail::is_##id##_0_comparable<LhsT>, \ std::is_same<RhsT, int>>::value, \ BinaryExpr<LhsT, RhsT>> { \ if ( rhs != 0 ) { throw_test_failure_exception(); } \ - return { \ - static_cast<bool>( lhs.m_lhs op 0 ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op 0 ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ Detail::is_##id##_0_comparable<RhsT>, \ std::is_same<LhsT, int>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - if ( lhs.m_lhs != 0 ) { throw_test_failure_exception(); } \ - return { static_cast<bool>( 0 op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \ + return { static_cast<bool>( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \ } - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( lt, < ) - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( le, <= ) - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( gt, > ) - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( ge, >= ) - - #undef CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR - - -#define CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( op ) \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT&& rhs ) \ - -> std::enable_if_t< \ - !capture_by_value<Detail::RemoveCVRef_t<RhsT>>::value, \ - BinaryExpr<LhsT, RhsT const&>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ - } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t<capture_by_value<RhsT>::value, \ - BinaryExpr<LhsT, RhsT>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( lt, < ) + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( le, <= ) + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( gt, > ) + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( ge, >= ) + +#undef CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR + +#define CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( op ) \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT&& rhs ) \ + ->std::enable_if_t< \ + !capture_by_value<Detail::RemoveCVRef_t<RhsT>>::value, \ + BinaryExpr<LhsT, RhsT const&>> { \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ + } \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t<capture_by_value<RhsT>::value, \ + BinaryExpr<LhsT, RhsT>> { \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } - CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR(|) - CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR(&) - CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR(^) + CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( | ) + CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( & ) + CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( ^) - #undef CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR +#undef CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR - template<typename RhsT> - friend auto operator && ( ExprLhs &&, RhsT && ) -> BinaryExpr<LhsT, RhsT const&> { - static_assert(always_false<RhsT>::value, + template <typename LhsT, typename RhsT> + auto operator&&( ExprLhs<LhsT>&&, RhsT&& ) + -> BinaryExpr<LhsT, RhsT const&> { + static_assert( + always_false<RhsT>::value, "operator&& is not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } + "wrap the expression inside parentheses, or decompose it" ); + } - template<typename RhsT> - friend auto operator || ( ExprLhs &&, RhsT && ) -> BinaryExpr<LhsT, RhsT const&> { - static_assert(always_false<RhsT>::value, + template <typename LhsT, typename RhsT> + auto operator||( ExprLhs<LhsT>&&, RhsT&& ) + -> BinaryExpr<LhsT, RhsT const&> { + static_assert( + always_false<RhsT>::value, "operator|| is not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } + "wrap the expression inside parentheses, or decompose it" ); + } - constexpr auto makeUnaryExpr() const -> UnaryExpr<LhsT> { - return UnaryExpr<LhsT>{ m_lhs }; - } - }; struct Decomposer { template <typename T, @@ -5881,7 +5899,20 @@ namespace Catch { void handleUnexpectedExceptionNotThrown(); void handleExceptionNotThrownAsExpected(); void handleThrowingCallSkipped(); - void handleUnexpectedInflightException(); + + // Marking this as `noexcept` gives us measurable improvement + // in optimized compilation times. Making it `noexcept` changes + // what happens if the underlying code throws, **but** the only + // way this can throw is if the reporter itself throws from + // `assertionEnded`. However, in such case the process is going + // to abort anyway, because we will enter `assertionEnded` again + // from the (noexcept) destructor of `AssertionHandler` above. + // In the future, all reporter event handlers will be marked + // `noexcept` to make this limitation explicit. + void handleUnexpectedInflightException() noexcept; + // Ideally we could extend the logic above to more of the handle* + // function, but right now it would worsen the behaviour of + // preexisting custom reporters that throw from event handlers. void complete(); @@ -5938,6 +5969,19 @@ namespace Catch { #endif +#if defined( CATCH_CONFIG_USE_BUILTIN_CONSTANT_P ) && \ + !defined( CATCH_CONFIG_NO_USE_BUILTIN_CONSTANT_P ) + // CATCH_INTERNAL_IGNORE_BUT_WARN already checks the expression on these + // compilers, so avoid checking it a third time in the loop condition. + #define INTERNAL_CATCH_TEST_LOOP_CONDITION( ... ) false +#else + // * The double negation silences MSVC's C4800 warning. + // * We keep the expression to allow for warnings in the original context + // * The static_cast forces short-circuit evaluation if the type has overloaded &&. + #define INTERNAL_CATCH_TEST_LOOP_CONDITION( ... ) \ + (void)0, (false) && static_cast<const bool&>( !!(__VA_ARGS__) ) +#endif + /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \ do { /* NOLINT(bugprone-infinite-loop) */ \ @@ -5951,8 +5995,7 @@ namespace Catch { CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ catchAssertionHandler.complete(); \ - } while( (void)0, (false) && static_cast<const bool&>( !!(__VA_ARGS__) ) ) // the expression 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 &&. + } while( INTERNAL_CATCH_TEST_LOOP_CONDITION( __VA_ARGS__ ) ) // the expression here is never evaluated at runtime, but is shown to some compilers /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \ @@ -6946,7 +6989,7 @@ namespace Catch { template<typename... Ts>\ void reg_test(TypeList<Ts...>, Catch::NameAndTags nameAndTags)\ {\ - Catch::AutoReg( Catch::makeTestInvoker(&TestFunc<Ts...>), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ + Catch::AutoReg( &TestFunc<Ts...>, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ } #define INTERNAL_CATCH_NTTP_REGISTER0(TestFunc, signature, ...) @@ -6954,7 +6997,7 @@ namespace Catch { template<INTERNAL_CATCH_REMOVE_PARENS(signature)>\ void reg_test(Nttp<__VA_ARGS__>, Catch::NameAndTags nameAndTags)\ {\ - Catch::AutoReg( Catch::makeTestInvoker(&TestFunc<__VA_ARGS__>), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ + Catch::AutoReg( &TestFunc<__VA_ARGS__>, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ } #define INTERNAL_CATCH_NTTP_REGISTER_METHOD0(TestName, signature, ...)\ @@ -7748,7 +7791,7 @@ namespace Catch { #define CATCH_VERSION_MAJOR 3 #define CATCH_VERSION_MINOR 16 -#define CATCH_VERSION_PATCH 0 +#define CATCH_VERSION_PATCH 1 #endif // CATCH_VERSION_MACROS_HPP_INCLUDED @@ -9186,6 +9229,7 @@ random(T a, T b) { #define CATCH_GENERATORS_RANGE_HPP_INCLUDED +#include <cassert> #include <iterator> #include <type_traits> @@ -12127,18 +12171,21 @@ namespace Catch { class MatchExpr final : public ITransientExpression { ArgT && m_arg; MatcherT const& m_matcher; + + static void streamFn( void const* self, std::ostream& os ) { + auto const& expr = *static_cast<MatchExpr const*>( + static_cast<ITransientExpression const*>( self ) ); + os << Catch::Detail::stringify( expr.m_arg ) + << ' ' + << expr.m_matcher.toString(); + } + public: constexpr MatchExpr( ArgT && arg, MatcherT const& matcher ) - : ITransientExpression{ true, matcher.match( arg ) }, // not forwarding arg here on purpose + : ITransientExpression{ &streamFn, true, matcher.match( arg ) }, // not forwarding arg here on purpose m_arg( CATCH_FORWARD(arg) ), m_matcher( matcher ) {} - - void streamReconstructedExpression( std::ostream& os ) const override { - os << Catch::Detail::stringify( m_arg ) - << ' ' - << m_matcher.toString(); - } }; #ifdef __clang__ @@ -13923,11 +13970,7 @@ namespace Catch { public: // GCC5 compat: we cannot use inherited constructor, because it // doesn't implement backport of P0136 - AutomakeReporter( ReporterConfig&& _config ): - StreamingReporterBase( CATCH_MOVE( _config ) ) { - m_preferences.shouldReportAllAssertionStarts = false; - } - + AutomakeReporter( ReporterConfig&& _config ); ~AutomakeReporter() override; static std::string getDescription() { @@ -13954,11 +13997,7 @@ namespace Catch { class CompactReporter final : public StreamingReporterBase { public: - CompactReporter( ReporterConfig&& _config ): - StreamingReporterBase( CATCH_MOVE( _config ) ) { - m_preferences.shouldReportAllAssertionStarts = false; - } - + CompactReporter( ReporterConfig&& _config ); ~CompactReporter() override; static std::string getDescription(); @@ -14238,11 +14277,13 @@ namespace Catch { #ifndef CATCH_REPORTER_HELPERS_HPP_INCLUDED #define CATCH_REPORTER_HELPERS_HPP_INCLUDED + + #include <iosfwd> +#include <map> #include <string> #include <vector> - namespace Catch { class IConfig; @@ -14321,6 +14362,11 @@ namespace Catch { ColourImpl& streamColour, Totals const& totals ); + //! Throws an exception if options contain unknown keys + void rejectSuperfluousConfigKeys( + std::map<std::string, std::string> const& options, + std::initializer_list<StringRef> knownKeys ); + } // end namespace Catch #endif // CATCH_REPORTER_HELPERS_HPP_INCLUDED @@ -14659,14 +14705,7 @@ namespace Catch { class SonarQubeReporter final : public CumulativeReporterBase { public: - SonarQubeReporter(ReporterConfig&& config) - : CumulativeReporterBase(CATCH_MOVE(config)) - , xml(m_stream) { - m_preferences.shouldRedirectStdOut = true; - m_preferences.shouldReportAllAssertions = false; - m_preferences.shouldReportAllAssertionStarts = false; - m_shouldStoreSuccesfulAssertions = false; - } + SonarQubeReporter( ReporterConfig&& config ); static std::string getDescription() { using namespace std::string_literals; @@ -14710,11 +14749,7 @@ namespace Catch { class TAPReporter final : public StreamingReporterBase { public: - TAPReporter( ReporterConfig&& config ): - StreamingReporterBase( CATCH_MOVE(config) ) { - m_preferences.shouldReportAllAssertions = true; - m_preferences.shouldReportAllAssertionStarts = false; - } + TAPReporter( ReporterConfig&& config ); static std::string getDescription() { using namespace std::string_literals; @@ -14753,13 +14788,7 @@ namespace Catch { class TeamCityReporter final : public StreamingReporterBase { public: - TeamCityReporter( ReporterConfig&& _config ) - : StreamingReporterBase( CATCH_MOVE(_config) ) - { - m_preferences.shouldRedirectStdOut = true; - m_preferences.shouldReportAllAssertionStarts = false; - } - + TeamCityReporter( ReporterConfig&& _config ); ~TeamCityReporter() override; static std::string getDescription() { diff --git a/meson.build b/meson.build index 467ebb61..466b8027 100644 --- a/meson.build +++ b/meson.build @@ -8,7 +8,7 @@ project( 'catch2', 'cpp', - version: '3.16.0', # CML version placeholder, don't delete + version: '3.16.1', # CML version placeholder, don't delete license: 'BSL-1.0', # Meson 1.1 added clang-cl support for the C++20 mode used by Progmasoft tests. meson_version: '>=1.1.0', diff --git a/src/catch2/benchmark/catch_optimizer.hpp b/src/catch2/benchmark/catch_optimizer.hpp index 5535b2a2..281256a3 100644 --- a/src/catch2/benchmark/catch_optimizer.hpp +++ b/src/catch2/benchmark/catch_optimizer.hpp @@ -46,7 +46,7 @@ namespace Catch { *reinterpret_cast<char volatile*>(p) = *reinterpret_cast<char const volatile*>(p); } // TODO equivalent keep_memory() -#if defined(_MSVC_VER) +#if defined(_MSC_VER) #pragma optimize("", on) #endif diff --git a/src/catch2/catch_session.cpp b/src/catch2/catch_session.cpp index 1c993ce1..34fd2809 100644 --- a/src/catch2/catch_session.cpp +++ b/src/catch2/catch_session.cpp @@ -458,7 +458,17 @@ namespace Catch { getCurrentMutableContext().setConfig(m_config.get()); // Create reporter(s) so we can route listings through them - auto reporter = prepareReporters(m_config.get()); + IEventListenerPtr reporter; + CATCH_TRY { + auto reporter_ = prepareReporters( m_config.get() ); + swap( reporter, reporter_ ); + } +#if !defined( CATCH_CONFIG_DISABLE_EXCEPTIONS ) + catch ( std::exception& ex ) { + Catch::cerr() << "Error when creating reporter: " << ex.what() << '\n' << std::flush; + return ReporterConstructionErrorExitCode; + } +#endif auto const& invalidSpecs = m_config->testSpec().getInvalidSpecs(); if ( !invalidSpecs.empty() ) { diff --git a/src/catch2/catch_session.hpp b/src/catch2/catch_session.hpp index 23fc6f80..4da7c66f 100644 --- a/src/catch2/catch_session.hpp +++ b/src/catch2/catch_session.hpp @@ -22,6 +22,7 @@ namespace Catch { constexpr int UnmatchedTestSpecExitCode = 3; constexpr int AllTestsSkippedExitCode = 4; constexpr int InvalidTestSpecExitCode = 5; + constexpr int ReporterConstructionErrorExitCode = 6; constexpr int TestFailureExitCode = 42; class Session : Detail::NonCopyable { diff --git a/src/catch2/catch_version.cpp b/src/catch2/catch_version.cpp index d0b44d72..cc332bc2 100644 --- a/src/catch2/catch_version.cpp +++ b/src/catch2/catch_version.cpp @@ -36,7 +36,7 @@ namespace Catch { } Version const& libraryVersion() { - static Version version( 3, 16, 0, "", 0 ); + static Version version( 3, 16, 1, "", 0 ); return version; } diff --git a/src/catch2/catch_version_macros.hpp b/src/catch2/catch_version_macros.hpp index 3219afb2..241928ab 100644 --- a/src/catch2/catch_version_macros.hpp +++ b/src/catch2/catch_version_macros.hpp @@ -10,6 +10,6 @@ #define CATCH_VERSION_MAJOR 3 #define CATCH_VERSION_MINOR 16 -#define CATCH_VERSION_PATCH 0 +#define CATCH_VERSION_PATCH 1 #endif // CATCH_VERSION_MACROS_HPP_INCLUDED diff --git a/src/catch2/internal/catch_assertion_handler.cpp b/src/catch2/internal/catch_assertion_handler.cpp index 79d5dcd8..e028c371 100644 --- a/src/catch2/internal/catch_assertion_handler.cpp +++ b/src/catch2/internal/catch_assertion_handler.cpp @@ -61,7 +61,7 @@ namespace Catch { } } - void AssertionHandler::handleUnexpectedInflightException() { + void AssertionHandler::handleUnexpectedInflightException() noexcept { m_resultCapture.handleUnexpectedInflightException( m_assertionInfo, Catch::translateActiveException(), m_reaction ); } diff --git a/src/catch2/internal/catch_assertion_handler.hpp b/src/catch2/internal/catch_assertion_handler.hpp index 93a70b1a..4b7607ad 100644 --- a/src/catch2/internal/catch_assertion_handler.hpp +++ b/src/catch2/internal/catch_assertion_handler.hpp @@ -59,7 +59,20 @@ namespace Catch { void handleUnexpectedExceptionNotThrown(); void handleExceptionNotThrownAsExpected(); void handleThrowingCallSkipped(); - void handleUnexpectedInflightException(); + + // Marking this as `noexcept` gives us measurable improvement + // in optimized compilation times. Making it `noexcept` changes + // what happens if the underlying code throws, **but** the only + // way this can throw is if the reporter itself throws from + // `assertionEnded`. However, in such case the process is going + // to abort anyway, because we will enter `assertionEnded` again + // from the (noexcept) destructor of `AssertionHandler` above. + // In the future, all reporter event handlers will be marked + // `noexcept` to make this limitation explicit. + void handleUnexpectedInflightException() noexcept; + // Ideally we could extend the logic above to more of the handle* + // function, but right now it would worsen the behaviour of + // preexisting custom reporters that throw from event handlers. void complete(); diff --git a/src/catch2/internal/catch_decomposer.cpp b/src/catch2/internal/catch_decomposer.cpp index 17a7bc95..e702c745 100644 --- a/src/catch2/internal/catch_decomposer.cpp +++ b/src/catch2/internal/catch_decomposer.cpp @@ -10,13 +10,6 @@ namespace Catch { - void ITransientExpression::streamReconstructedExpression( - std::ostream& os ) const { - // We can't make this function pure virtual to keep ITransientExpression - // constexpr, so we write error message instead - os << "Some class derived from ITransientExpression without overriding streamReconstructedExpression"; - } - void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ) { if( lhs.size() + rhs.size() < 40 && lhs.find('\n') == std::string::npos && diff --git a/src/catch2/internal/catch_decomposer.hpp b/src/catch2/internal/catch_decomposer.hpp index ff628482..9c004b02 100644 --- a/src/catch2/internal/catch_decomposer.hpp +++ b/src/catch2/internal/catch_decomposer.hpp @@ -164,28 +164,27 @@ namespace Catch { struct always_false : std::false_type {}; class ITransientExpression { + //! Type-erased streaming function for the derived expression + using StreamFn = void ( * )( void const*, std::ostream& ); + + StreamFn m_streamFn; bool m_isBinaryExpression; bool m_result; - protected: - ~ITransientExpression() = default; - public: constexpr auto isBinaryExpression() const -> bool { return m_isBinaryExpression; } constexpr auto getResult() const -> bool { return m_result; } - //! This function **has** to be overridden by the derived class. - virtual void streamReconstructedExpression( std::ostream& os ) const; - - constexpr ITransientExpression( bool isBinaryExpression, bool result ) - : m_isBinaryExpression( isBinaryExpression ), - m_result( result ) - {} - constexpr ITransientExpression( ITransientExpression const& ) = default; - constexpr ITransientExpression& operator=( ITransientExpression const& ) = default; + constexpr ITransientExpression( StreamFn streamFn, + bool isBinaryExpression, + bool result ): + m_streamFn( streamFn ), + m_isBinaryExpression( isBinaryExpression ), + m_result( result ) {} - friend std::ostream& operator<<(std::ostream& out, ITransientExpression const& expr) { - expr.streamReconstructedExpression(out); + friend std::ostream& operator<<( std::ostream& out, + ITransientExpression const& expr ) { + expr.m_streamFn( &expr, out ); return out; } }; @@ -198,87 +197,67 @@ namespace Catch { StringRef m_op; RhsT m_rhs; - void streamReconstructedExpression( std::ostream &os ) const override { - formatReconstructedExpression - ( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) ); + static void streamFn( void const* self, std::ostream& out ) { + auto const& expr = *static_cast<BinaryExpr const*>( + static_cast<ITransientExpression const*>( self ) ); + formatReconstructedExpression( + out, + Catch::Detail::stringify( expr.m_lhs ), + expr.m_op, + Catch::Detail::stringify( expr.m_rhs ) ); } public: constexpr BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs ) - : ITransientExpression{ true, comparisonResult }, + : ITransientExpression{ &streamFn, true, comparisonResult }, m_lhs( lhs ), m_op( op ), m_rhs( rhs ) {} + }; - template<typename T> - auto operator && ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator || ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator == ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator != ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator > ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator < ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - - template<typename T> - auto operator >= ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } +#define CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( op ) \ + template <typename LhsT, typename RhsT, typename T> \ + auto operator op( BinaryExpr<LhsT, RhsT> const&, T ) \ + ->BinaryExpr<LhsT, RhsT const&> const { \ + static_assert( always_false<T>::value, \ + "chained comparisons are not supported inside " \ + "assertions, wrap the expression inside " \ + "parentheses, or decompose it" ); \ + } - template<typename T> - auto operator <= ( T ) const -> BinaryExpr<LhsT, RhsT const&> const { - static_assert(always_false<T>::value, - "chained comparisons are not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } - }; + /* + * The operators are **intentionally** not hidden friends. + * + * If they were hidden friends, all of them would get stamped out when + * a `BinaryExpr` is instantiated with a new type. In practice, only + * few operators are used for each different `BinaryExpr<LhsT, RhsT>`, + * so this avoids bunch of wasted work. + */ + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( && ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( || ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( == ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( != ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( > ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( < ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( >= ) + CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR( <= ) + +#undef CATCH_INTERNAL_DEFINE_BINARY_EXPR_OPERATOR template<typename LhsT> class UnaryExpr final : public ITransientExpression { LhsT m_lhs; - void streamReconstructedExpression( std::ostream &os ) const override { - os << Catch::Detail::stringify( m_lhs ); + static void streamFn( void const* self, std::ostream& os ) { + auto const& expr = *static_cast<UnaryExpr const*>( + static_cast<ITransientExpression const*>( self ) ); + os << Catch::Detail::stringify( expr.m_lhs ); } public: explicit constexpr UnaryExpr( LhsT lhs ) - : ITransientExpression{ false, static_cast<bool>(lhs) }, + : ITransientExpression{ &streamFn, false, static_cast<bool>(lhs) }, m_lhs( lhs ) {} }; @@ -290,153 +269,186 @@ namespace Catch { public: explicit constexpr ExprLhs( LhsT lhs ) : m_lhs( lhs ) {} + // We rely on reference collapsing if LhsT is already a reference. + // Used in the out-of-class decomposition operators + constexpr LhsT& getLhs() { return m_lhs; } + + constexpr auto makeUnaryExpr() const -> UnaryExpr<LhsT> { + return UnaryExpr<LhsT>{ m_lhs }; + } + }; + +/* + * The comparison operators are **intentionally** not hidden friends. + * + * If they were hidden friends, all of them would get stamped out when + * a `ExprLhs` is instantiated with a new type. In practice, only few + * operators are used for each different `ExprLhs<T>`, so this avoids + * a lot of wasted work. + */ #define CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( id, op ) \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT&& rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT&& rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ Detail::negation<capture_by_value< \ Detail::RemoveCVRef_t<RhsT>>>>::value, \ BinaryExpr<LhsT, RhsT const&>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ capture_by_value<RhsT>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ - Detail::is_eq_0_comparable<LhsT>, \ - /* We allow long because we want `ptr op NULL` to be accepted */ \ + Detail::is_eq_0_comparable<LhsT>, /* We allow long because we \ + want `ptr op NULL` to be \ + accepted */ \ Detail::disjunction<std::is_same<RhsT, int>, \ std::is_same<RhsT, long>>>::value, \ BinaryExpr<LhsT, RhsT>> { \ if ( rhs != 0 ) { throw_test_failure_exception(); } \ - return { \ - static_cast<bool>( lhs.m_lhs op 0 ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op 0 ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ - Detail::is_eq_0_comparable<RhsT>, \ - /* We allow long because we want `ptr op NULL` to be accepted */ \ + Detail::is_eq_0_comparable<RhsT>, /* We allow long because we \ + want `ptr op NULL` to be \ + accepted */ \ Detail::disjunction<std::is_same<LhsT, int>, \ std::is_same<LhsT, long>>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - if ( lhs.m_lhs != 0 ) { throw_test_failure_exception(); } \ - return { static_cast<bool>( 0 op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \ + return { static_cast<bool>( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \ } - CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == ) - CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( ne, != ) - - #undef CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR + CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == ) + CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( ne, != ) +#undef CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR #define CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( id, op ) \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT&& rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT&& rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ Detail::negation<capture_by_value< \ Detail::RemoveCVRef_t<RhsT>>>>::value, \ BinaryExpr<LhsT, RhsT const&>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction<Detail::is_##id##_comparable<LhsT, RhsT>, \ capture_by_value<RhsT>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ Detail::is_##id##_0_comparable<LhsT>, \ std::is_same<RhsT, int>>::value, \ BinaryExpr<LhsT, RhsT>> { \ if ( rhs != 0 ) { throw_test_failure_exception(); } \ - return { \ - static_cast<bool>( lhs.m_lhs op 0 ), lhs.m_lhs, #op##_sr, rhs }; \ + return { static_cast<bool>( lhs.getLhs() op 0 ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t< \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t< \ Detail::conjunction< \ Detail::negation<Detail::is_##id##_comparable<LhsT, RhsT>>, \ Detail::is_##id##_0_comparable<RhsT>, \ std::is_same<LhsT, int>>::value, \ BinaryExpr<LhsT, RhsT>> { \ - if ( lhs.m_lhs != 0 ) { throw_test_failure_exception(); } \ - return { static_cast<bool>( 0 op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \ + return { static_cast<bool>( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \ } - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( lt, < ) - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( le, <= ) - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( gt, > ) - CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( ge, >= ) - - #undef CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR - - -#define CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( op ) \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT&& rhs ) \ - -> std::enable_if_t< \ - !capture_by_value<Detail::RemoveCVRef_t<RhsT>>::value, \ - BinaryExpr<LhsT, RhsT const&>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ - } \ - template <typename RhsT> \ - constexpr friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ - -> std::enable_if_t<capture_by_value<RhsT>::value, \ - BinaryExpr<LhsT, RhsT>> { \ - return { \ - static_cast<bool>( lhs.m_lhs op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( lt, < ) + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( le, <= ) + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( gt, > ) + CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( ge, >= ) + +#undef CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR + +#define CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( op ) \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT&& rhs ) \ + ->std::enable_if_t< \ + !capture_by_value<Detail::RemoveCVRef_t<RhsT>>::value, \ + BinaryExpr<LhsT, RhsT const&>> { \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ + } \ + template <typename LhsT, typename RhsT> \ + constexpr auto operator op( ExprLhs<LhsT>&& lhs, RhsT rhs ) \ + ->std::enable_if_t<capture_by_value<RhsT>::value, \ + BinaryExpr<LhsT, RhsT>> { \ + return { static_cast<bool>( lhs.getLhs() op rhs ), \ + lhs.getLhs(), \ + #op##_sr, \ + rhs }; \ } - CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR(|) - CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR(&) - CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR(^) + CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( | ) + CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( & ) + CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR( ^) - #undef CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR +#undef CATCH_INTERNAL_DEFINE_EXPRESSION_OPERATOR - template<typename RhsT> - friend auto operator && ( ExprLhs &&, RhsT && ) -> BinaryExpr<LhsT, RhsT const&> { - static_assert(always_false<RhsT>::value, + template <typename LhsT, typename RhsT> + auto operator&&( ExprLhs<LhsT>&&, RhsT&& ) + -> BinaryExpr<LhsT, RhsT const&> { + static_assert( + always_false<RhsT>::value, "operator&& is not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } + "wrap the expression inside parentheses, or decompose it" ); + } - template<typename RhsT> - friend auto operator || ( ExprLhs &&, RhsT && ) -> BinaryExpr<LhsT, RhsT const&> { - static_assert(always_false<RhsT>::value, + template <typename LhsT, typename RhsT> + auto operator||( ExprLhs<LhsT>&&, RhsT&& ) + -> BinaryExpr<LhsT, RhsT const&> { + static_assert( + always_false<RhsT>::value, "operator|| is not supported inside assertions, " - "wrap the expression inside parentheses, or decompose it"); - } + "wrap the expression inside parentheses, or decompose it" ); + } - constexpr auto makeUnaryExpr() const -> UnaryExpr<LhsT> { - return UnaryExpr<LhsT>{ m_lhs }; - } - }; struct Decomposer { template <typename T, diff --git a/src/catch2/internal/catch_preprocessor.hpp b/src/catch2/internal/catch_preprocessor.hpp index b7336c47..82868640 100644 --- a/src/catch2/internal/catch_preprocessor.hpp +++ b/src/catch2/internal/catch_preprocessor.hpp @@ -218,7 +218,7 @@ namespace Catch { template<typename... Ts>\ void reg_test(TypeList<Ts...>, Catch::NameAndTags nameAndTags)\ {\ - Catch::AutoReg( Catch::makeTestInvoker(&TestFunc<Ts...>), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ + Catch::AutoReg( &TestFunc<Ts...>, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ } #define INTERNAL_CATCH_NTTP_REGISTER0(TestFunc, signature, ...) @@ -226,7 +226,7 @@ namespace Catch { template<INTERNAL_CATCH_REMOVE_PARENS(signature)>\ void reg_test(Nttp<__VA_ARGS__>, Catch::NameAndTags nameAndTags)\ {\ - Catch::AutoReg( Catch::makeTestInvoker(&TestFunc<__VA_ARGS__>), CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ + Catch::AutoReg( &TestFunc<__VA_ARGS__>, CATCH_INTERNAL_LINEINFO, Catch::StringRef(), nameAndTags);\ } #define INTERNAL_CATCH_NTTP_REGISTER_METHOD0(TestName, signature, ...)\ diff --git a/src/catch2/internal/catch_test_macro_impl.hpp b/src/catch2/internal/catch_test_macro_impl.hpp index f38ec606..6263e57d 100644 --- a/src/catch2/internal/catch_test_macro_impl.hpp +++ b/src/catch2/internal/catch_test_macro_impl.hpp @@ -44,6 +44,19 @@ namespace Catch { #endif +#if defined( CATCH_CONFIG_USE_BUILTIN_CONSTANT_P ) && \ + !defined( CATCH_CONFIG_NO_USE_BUILTIN_CONSTANT_P ) + // CATCH_INTERNAL_IGNORE_BUT_WARN already checks the expression on these + // compilers, so avoid checking it a third time in the loop condition. + #define INTERNAL_CATCH_TEST_LOOP_CONDITION( ... ) false +#else + // * The double negation silences MSVC's C4800 warning. + // * We keep the expression to allow for warnings in the original context + // * The static_cast forces short-circuit evaluation if the type has overloaded &&. + #define INTERNAL_CATCH_TEST_LOOP_CONDITION( ... ) \ + (void)0, (false) && static_cast<const bool&>( !!(__VA_ARGS__) ) +#endif + /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \ do { /* NOLINT(bugprone-infinite-loop) */ \ @@ -57,8 +70,7 @@ namespace Catch { CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ catchAssertionHandler.complete(); \ - } while( (void)0, (false) && static_cast<const bool&>( !!(__VA_ARGS__) ) ) // the expression 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 &&. + } while( INTERNAL_CATCH_TEST_LOOP_CONDITION( __VA_ARGS__ ) ) // the expression here is never evaluated at runtime, but is shown to some compilers /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \ diff --git a/src/catch2/matchers/internal/catch_matchers_impl.hpp b/src/catch2/matchers/internal/catch_matchers_impl.hpp index 1aa50385..1e5431fe 100644 --- a/src/catch2/matchers/internal/catch_matchers_impl.hpp +++ b/src/catch2/matchers/internal/catch_matchers_impl.hpp @@ -32,18 +32,21 @@ namespace Catch { class MatchExpr final : public ITransientExpression { ArgT && m_arg; MatcherT const& m_matcher; + + static void streamFn( void const* self, std::ostream& os ) { + auto const& expr = *static_cast<MatchExpr const*>( + static_cast<ITransientExpression const*>( self ) ); + os << Catch::Detail::stringify( expr.m_arg ) + << ' ' + << expr.m_matcher.toString(); + } + public: constexpr MatchExpr( ArgT && arg, MatcherT const& matcher ) - : ITransientExpression{ true, matcher.match( arg ) }, // not forwarding arg here on purpose + : ITransientExpression{ &streamFn, true, matcher.match( arg ) }, // not forwarding arg here on purpose m_arg( CATCH_FORWARD(arg) ), m_matcher( matcher ) {} - - void streamReconstructedExpression( std::ostream& os ) const override { - os << Catch::Detail::stringify( m_arg ) - << ' ' - << m_matcher.toString(); - } }; #ifdef __clang__ diff --git a/src/catch2/reporters/catch_reporter_automake.cpp b/src/catch2/reporters/catch_reporter_automake.cpp index 5e506a6b..6df6bc95 100644 --- a/src/catch2/reporters/catch_reporter_automake.cpp +++ b/src/catch2/reporters/catch_reporter_automake.cpp @@ -5,8 +5,10 @@ // https://www.boost.org/LICENSE_1_0.txt) // SPDX-License-Identifier: BSL-1.0 + #include <catch2/reporters/catch_reporter_automake.hpp> #include <catch2/catch_test_case_info.hpp> +#include <catch2/reporters/catch_reporter_helpers.hpp> #include <ostream> @@ -14,6 +16,13 @@ namespace Catch { AutomakeReporter::~AutomakeReporter() = default; + AutomakeReporter::AutomakeReporter( ReporterConfig&& _config ): + StreamingReporterBase( CATCH_MOVE( _config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldReportAllAssertionStarts = false; + } + void AutomakeReporter::testCaseEnded(TestCaseStats const& _testCaseStats) { // Possible values to emit are PASS, XFAIL, SKIP, FAIL, XPASS and ERROR. m_stream << ":test-result: "; diff --git a/src/catch2/reporters/catch_reporter_automake.hpp b/src/catch2/reporters/catch_reporter_automake.hpp index d920adf6..8200cdc3 100644 --- a/src/catch2/reporters/catch_reporter_automake.hpp +++ b/src/catch2/reporters/catch_reporter_automake.hpp @@ -19,11 +19,7 @@ namespace Catch { public: // GCC5 compat: we cannot use inherited constructor, because it // doesn't implement backport of P0136 - AutomakeReporter( ReporterConfig&& _config ): - StreamingReporterBase( CATCH_MOVE( _config ) ) { - m_preferences.shouldReportAllAssertionStarts = false; - } - + AutomakeReporter( ReporterConfig&& _config ); ~AutomakeReporter() override; static std::string getDescription() { diff --git a/src/catch2/reporters/catch_reporter_compact.cpp b/src/catch2/reporters/catch_reporter_compact.cpp index 6d2c8655..b46fcf38 100644 --- a/src/catch2/reporters/catch_reporter_compact.cpp +++ b/src/catch2/reporters/catch_reporter_compact.cpp @@ -251,5 +251,12 @@ private: } CompactReporter::~CompactReporter() = default; + CompactReporter::CompactReporter( ReporterConfig&& _config ): + StreamingReporterBase( CATCH_MOVE( _config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldReportAllAssertionStarts = false; + } + } // end namespace Catch diff --git a/src/catch2/reporters/catch_reporter_compact.hpp b/src/catch2/reporters/catch_reporter_compact.hpp index a6437c68..07e90c02 100644 --- a/src/catch2/reporters/catch_reporter_compact.hpp +++ b/src/catch2/reporters/catch_reporter_compact.hpp @@ -16,11 +16,7 @@ namespace Catch { class CompactReporter final : public StreamingReporterBase { public: - CompactReporter( ReporterConfig&& _config ): - StreamingReporterBase( CATCH_MOVE( _config ) ) { - m_preferences.shouldReportAllAssertionStarts = false; - } - + CompactReporter( ReporterConfig&& _config ); ~CompactReporter() override; static std::string getDescription(); diff --git a/src/catch2/reporters/catch_reporter_console.cpp b/src/catch2/reporters/catch_reporter_console.cpp index acb4ae8e..0ed300f0 100644 --- a/src/catch2/reporters/catch_reporter_console.cpp +++ b/src/catch2/reporters/catch_reporter_console.cpp @@ -402,6 +402,7 @@ ConsoleReporter::ConsoleReporter(ReporterConfig&& config): }; } }())) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); m_preferences.shouldReportAllAssertionStarts = false; } diff --git a/src/catch2/reporters/catch_reporter_helpers.cpp b/src/catch2/reporters/catch_reporter_helpers.cpp index cfb2219d..596fec5d 100644 --- a/src/catch2/reporters/catch_reporter_helpers.cpp +++ b/src/catch2/reporters/catch_reporter_helpers.cpp @@ -9,6 +9,7 @@ #include <catch2/reporters/catch_reporter_helpers.hpp> #include <catch2/interfaces/catch_interfaces_config.hpp> #include <catch2/internal/catch_console_width.hpp> +#include <catch2/internal/catch_enforce.hpp> #include <catch2/internal/catch_errno_guard.hpp> #include <catch2/internal/catch_textflow.hpp> #include <catch2/internal/catch_reusable_string_stream.hpp> @@ -377,4 +378,16 @@ namespace Catch { printSummaryRow( stream, streamColour, "assertions"_sr, columns, 1 ); } + void rejectSuperfluousConfigKeys( + std::map<std::string, std::string> const& options, + std::initializer_list<StringRef> knownKeys ) { + for ( auto const& option : options ) { + StringRef key( option.first ); + CATCH_ENFORCE( std::find( knownKeys.begin(), + knownKeys.end(), + key ) != knownKeys.end(), + "Unknown reporter argument: " << key ); + } + } + } // namespace Catch diff --git a/src/catch2/reporters/catch_reporter_helpers.hpp b/src/catch2/reporters/catch_reporter_helpers.hpp index 46988888..bbe8fb88 100644 --- a/src/catch2/reporters/catch_reporter_helpers.hpp +++ b/src/catch2/reporters/catch_reporter_helpers.hpp @@ -8,14 +8,16 @@ #ifndef CATCH_REPORTER_HELPERS_HPP_INCLUDED #define CATCH_REPORTER_HELPERS_HPP_INCLUDED -#include <iosfwd> -#include <string> -#include <vector> #include <catch2/internal/catch_list.hpp> #include <catch2/interfaces/catch_interfaces_config.hpp> #include <catch2/catch_totals.hpp> +#include <iosfwd> +#include <map> +#include <string> +#include <vector> + namespace Catch { class IConfig; @@ -94,6 +96,11 @@ namespace Catch { ColourImpl& streamColour, Totals const& totals ); + //! Throws an exception if options contain unknown keys + void rejectSuperfluousConfigKeys( + std::map<std::string, std::string> const& options, + std::initializer_list<StringRef> knownKeys ); + } // end namespace Catch #endif // CATCH_REPORTER_HELPERS_HPP_INCLUDED diff --git a/src/catch2/reporters/catch_reporter_json.cpp b/src/catch2/reporters/catch_reporter_json.cpp index 39a2105d..b4376990 100644 --- a/src/catch2/reporters/catch_reporter_json.cpp +++ b/src/catch2/reporters/catch_reporter_json.cpp @@ -12,6 +12,7 @@ #include <catch2/interfaces/catch_interfaces_config.hpp> #include <catch2/internal/catch_list.hpp> #include <catch2/internal/catch_string_manip.hpp> +#include <catch2/reporters/catch_reporter_helpers.hpp> #include <catch2/reporters/catch_reporter_json.hpp> namespace Catch { @@ -47,6 +48,7 @@ namespace Catch { JsonReporter::JsonReporter( ReporterConfig&& config ): StreamingReporterBase{ CATCH_MOVE( config ) } { + rejectSuperfluousConfigKeys( m_customOptions, {} ); m_preferences.shouldRedirectStdOut = true; // TBD: Do we want to report all assertions? XML reporter does diff --git a/src/catch2/reporters/catch_reporter_junit.cpp b/src/catch2/reporters/catch_reporter_junit.cpp index 00b45876..a5893a6d 100644 --- a/src/catch2/reporters/catch_reporter_junit.cpp +++ b/src/catch2/reporters/catch_reporter_junit.cpp @@ -87,11 +87,13 @@ namespace Catch { : CumulativeReporterBase( CATCH_MOVE(_config) ), xml( m_stream ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + m_preferences.shouldRedirectStdOut = true; m_preferences.shouldReportAllAssertions = false; m_preferences.shouldReportAllAssertionStarts = false; m_shouldStoreSuccesfulAssertions = false; - } + } std::string JunitReporter::getDescription() { return "Reports test results in an XML format that looks like Ant's junitreport target"; diff --git a/src/catch2/reporters/catch_reporter_sonarqube.cpp b/src/catch2/reporters/catch_reporter_sonarqube.cpp index 2c3eb1cd..c3520d2d 100644 --- a/src/catch2/reporters/catch_reporter_sonarqube.cpp +++ b/src/catch2/reporters/catch_reporter_sonarqube.cpp @@ -31,6 +31,16 @@ namespace Catch { } } + SonarQubeReporter::SonarQubeReporter( ReporterConfig&& config ): + CumulativeReporterBase( CATCH_MOVE( config ) ), xml( m_stream ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldRedirectStdOut = true; + m_preferences.shouldReportAllAssertions = false; + m_preferences.shouldReportAllAssertionStarts = false; + m_shouldStoreSuccesfulAssertions = false; + } + void SonarQubeReporter::testRunStarting(TestRunInfo const& testRunInfo) { CumulativeReporterBase::testRunStarting(testRunInfo); diff --git a/src/catch2/reporters/catch_reporter_sonarqube.hpp b/src/catch2/reporters/catch_reporter_sonarqube.hpp index 98803a2a..d2f6bbb8 100644 --- a/src/catch2/reporters/catch_reporter_sonarqube.hpp +++ b/src/catch2/reporters/catch_reporter_sonarqube.hpp @@ -17,14 +17,7 @@ namespace Catch { class SonarQubeReporter final : public CumulativeReporterBase { public: - SonarQubeReporter(ReporterConfig&& config) - : CumulativeReporterBase(CATCH_MOVE(config)) - , xml(m_stream) { - m_preferences.shouldRedirectStdOut = true; - m_preferences.shouldReportAllAssertions = false; - m_preferences.shouldReportAllAssertionStarts = false; - m_shouldStoreSuccesfulAssertions = false; - } + SonarQubeReporter( ReporterConfig&& config ); static std::string getDescription() { using namespace std::string_literals; diff --git a/src/catch2/reporters/catch_reporter_tap.cpp b/src/catch2/reporters/catch_reporter_tap.cpp index d7e68bfd..5b8031c8 100644 --- a/src/catch2/reporters/catch_reporter_tap.cpp +++ b/src/catch2/reporters/catch_reporter_tap.cpp @@ -192,6 +192,14 @@ namespace Catch { } // End anonymous namespace + TAPReporter::TAPReporter( ReporterConfig&& config ): + StreamingReporterBase( CATCH_MOVE( config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldReportAllAssertions = true; + m_preferences.shouldReportAllAssertionStarts = false; + } + void TAPReporter::testRunStarting( TestRunInfo const& ) { if ( m_config->testSpec().hasFilters() ) { m_stream << "# filters: " << m_config->testSpec() << '\n'; diff --git a/src/catch2/reporters/catch_reporter_tap.hpp b/src/catch2/reporters/catch_reporter_tap.hpp index 4664928f..36c303a5 100644 --- a/src/catch2/reporters/catch_reporter_tap.hpp +++ b/src/catch2/reporters/catch_reporter_tap.hpp @@ -15,11 +15,7 @@ namespace Catch { class TAPReporter final : public StreamingReporterBase { public: - TAPReporter( ReporterConfig&& config ): - StreamingReporterBase( CATCH_MOVE(config) ) { - m_preferences.shouldReportAllAssertions = true; - m_preferences.shouldReportAllAssertionStarts = false; - } + TAPReporter( ReporterConfig&& config ); static std::string getDescription() { using namespace std::string_literals; diff --git a/src/catch2/reporters/catch_reporter_teamcity.cpp b/src/catch2/reporters/catch_reporter_teamcity.cpp index 38aa55a6..88d9386b 100644 --- a/src/catch2/reporters/catch_reporter_teamcity.cpp +++ b/src/catch2/reporters/catch_reporter_teamcity.cpp @@ -45,6 +45,14 @@ namespace Catch { } // end anonymous namespace + TeamCityReporter::TeamCityReporter( ReporterConfig&& _config ): + StreamingReporterBase( CATCH_MOVE( _config ) ) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + + m_preferences.shouldRedirectStdOut = true; + m_preferences.shouldReportAllAssertionStarts = false; + } + TeamCityReporter::~TeamCityReporter() = default; void TeamCityReporter::testRunStarting( TestRunInfo const& runInfo ) { diff --git a/src/catch2/reporters/catch_reporter_teamcity.hpp b/src/catch2/reporters/catch_reporter_teamcity.hpp index 4adb0e37..970a843f 100644 --- a/src/catch2/reporters/catch_reporter_teamcity.hpp +++ b/src/catch2/reporters/catch_reporter_teamcity.hpp @@ -22,13 +22,7 @@ namespace Catch { class TeamCityReporter final : public StreamingReporterBase { public: - TeamCityReporter( ReporterConfig&& _config ) - : StreamingReporterBase( CATCH_MOVE(_config) ) - { - m_preferences.shouldRedirectStdOut = true; - m_preferences.shouldReportAllAssertionStarts = false; - } - + TeamCityReporter( ReporterConfig&& _config ); ~TeamCityReporter() override; static std::string getDescription() { diff --git a/src/catch2/reporters/catch_reporter_xml.cpp b/src/catch2/reporters/catch_reporter_xml.cpp index 8b7b5a70..9b3175ad 100644 --- a/src/catch2/reporters/catch_reporter_xml.cpp +++ b/src/catch2/reporters/catch_reporter_xml.cpp @@ -28,6 +28,8 @@ namespace Catch { : StreamingReporterBase( CATCH_MOVE(_config) ), m_xml(m_stream) { + rejectSuperfluousConfigKeys( m_customOptions, {} ); + m_preferences.shouldRedirectStdOut = true; m_preferences.shouldReportAllAssertions = true; m_preferences.shouldReportAllAssertionStarts = false; diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt index 168b739d..98f050d6 100644 --- a/tests/ExtraTests/CMakeLists.txt +++ b/tests/ExtraTests/CMakeLists.txt @@ -482,6 +482,41 @@ set_tests_properties(Validation::TestCaseTags ) +add_executable(ThrowingReporter ${TESTS_DIR}/X38-ThrowingReporter.cpp) +target_link_libraries(ThrowingReporter PRIVATE Catch2::Catch2WithMain) +add_test( + NAME Reporters::ThrowInConstructor + COMMAND + $<TARGET_FILE:ThrowingReporter> + --reporter ThrowingReporter::Xthrow-in-constructor=true +) +set_tests_properties(Reporters::ThrowInConstructor + PROPERTIES + PASS_REGULAR_EXPRESSION "Error when creating reporter: Exception in constructor" +) + +foreach(reporterName "Automake" + "compact" + "console" + "JUnit" + "SonarQube" + "TAP" + "TeamCity" + "XML" + "JSON") + add_test(NAME "Reporters::RejectUnknownArgs::${reporterName}" + COMMAND + $<TARGET_FILE:SelfTest> + --reporter ${reporterName}::Xfoo=bar + "[comparisons]" # Limit selection to avoid running full test binary if the error check doesn't trigger + ) + set_tests_properties("Reporters::RejectUnknownArgs::${reporterName}" + PROPERTIES + PASS_REGULAR_EXPRESSION "Error when creating reporter: Unknown reporter argument: Xfoo" + ) +endforeach() + + add_executable(QuickExitInTest ${TESTS_DIR}/X40-QuickExit.cpp) target_link_libraries(QuickExitInTest PRIVATE Catch2::Catch2WithMain) add_test( diff --git a/tests/ExtraTests/X38-ThrowingReporter.cpp b/tests/ExtraTests/X38-ThrowingReporter.cpp new file mode 100644 index 00000000..5f660322 --- /dev/null +++ b/tests/ExtraTests/X38-ThrowingReporter.cpp @@ -0,0 +1,79 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +/**\file + * Runs simple test cases with reporter that likes to throw. + * + * Used to analyze how Catch2 currently treats throwing reporters/listeners, + * and what changes make sense for the future. + */ + +#include <catch2/catch_test_macros.hpp> +#include <catch2/reporters/catch_reporter_helpers.hpp> +#include <catch2/reporters/catch_reporter_registrars.hpp> +#include <catch2/reporters/catch_reporter_streaming_base.hpp> + +namespace { + + class ThrowingReporter final : public Catch::StreamingReporterBase { + bool m_throwInConstructor = false; + bool m_throwInAssertionEnded = false; + + void processOptions() { + if ( m_customOptions.find( "Xthrow-in-constructor" ) != + m_customOptions.end() ) { + m_throwInConstructor = true; + } + if ( m_customOptions.find( "Xthrow-in-assertion-ended" ) != + m_customOptions.end() ) { + m_throwInAssertionEnded = true; + } + } + + public: + ThrowingReporter( Catch::ReporterConfig&& config ): + StreamingReporterBase( CATCH_MOVE( config ) ) { + m_preferences.shouldReportAllAssertions = true; + m_preferences.shouldReportAllAssertionStarts = false; + + Catch::rejectSuperfluousConfigKeys( + m_customOptions, + { "Xthrow-in-constructor", "Xthrow-in-assertion-ended" } ); + processOptions(); + + if ( m_throwInConstructor ) { + throw std::runtime_error( "Exception in constructor" ); + } + } + + void assertionEnded( Catch::AssertionStats const& stats ) override { + m_stream << "assertionEnded\n"; + if ( m_throwInAssertionEnded ) { + throw std::runtime_error( "Exception in assertionEnded" ); + } + } + + static std::string getDescription() { + using namespace std::string_literals; + return "Throws for different reporter events"s; + } + }; + + CATCH_REGISTER_REPORTER( "ThrowingReporter", ThrowingReporter ) + +} // namespace + +TEST_CASE( "REQUIRE - Pass" ) { REQUIRE( true ); } + +TEST_CASE( "REQUIRE - Fail" ) { REQUIRE( false ); } + +TEST_CASE( "REQUIRE_THROWS - Pass" ) { + REQUIRE_THROWS( [] { + throw std::runtime_error( "expected" ); + }() ); +} diff --git a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp index 4ff793f0..7abfbb7f 100644 --- a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp @@ -330,3 +330,17 @@ TEST_CASE("Registering multiple reporters with the same name fails", Catch::Detail::make_unique<TestReporterFactory>() ), "reporter using 'some-reporter-name' as name was already registered" ); } + +TEST_CASE( "rejectSuperfluousConfigKeys helper", + "[reporters][reporter-helpers][approvals]" ) { + using Catch::rejectSuperfluousConfigKeys; using Catch::Matchers::ContainsSubstring; + + std::map<std::string, std::string> options { { "Xfoo", "1" }, { "Xbar", "2" } }; + REQUIRE_THROWS_WITH( rejectSuperfluousConfigKeys( options, {} ), + ContainsSubstring( "Unknown reporter argument:" ) ); + REQUIRE_THROWS_WITH( + rejectSuperfluousConfigKeys( options, { "Xfoo", "Xbaz" } ), + ContainsSubstring( "Unknown reporter argument: Xbar" ) ); + REQUIRE_NOTHROW( + Catch::rejectSuperfluousConfigKeys( options, { "Xfoo", "Xbaz", "Xbar" } ) ); +} diff --git a/xmake.lua b/xmake.lua index a865f98d..385cf47d 100644 --- a/xmake.lua +++ b/xmake.lua @@ -2,7 +2,7 @@ -- SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1 set_project("Catch3") -set_version("3.16.0") +set_version("3.16.1") set_languages("cxx14") set_warnings("all")