Support for constexpr matchers in C++20 (P0784)
To make this all work, I had to remove the stringification cache from matchers. In theory, this can cause performance penalty in cases where single matcher instance is stringified multiple times, but in practice this does not happen much, and the difference is surprisingly small anyway, because the performance of stringification is already horrible and full of allocating strings just to throw them away. The matcher combinators need P2738 from C++26 to be `constexpr`. Closes #3091
This commit is contained in:
14 files changed
+254
-49
No files matched your search
@@ -62,6 +62,7 @@ cc_test(
|
||||
"SelfTest/UsageTests/Exception.tests.cpp",
|
||||
"SelfTest/UsageTests/Generators.tests.cpp",
|
||||
"SelfTest/UsageTests/Matchers.tests.cpp",
|
||||
"SelfTest/UsageTests/MatchersConstexpr.tests.cpp",
|
||||
"SelfTest/UsageTests/MatchersRanges.tests.cpp",
|
||||
"SelfTest/UsageTests/Message.tests.cpp",
|
||||
"SelfTest/UsageTests/Misc.tests.cpp",
|
||||
|
||||
@@ -124,8 +124,9 @@ set(TEST_SOURCES
|
||||
${SELF_TEST_DIR}/UsageTests/ToStringWhich.tests.cpp
|
||||
${SELF_TEST_DIR}/UsageTests/Tricky.tests.cpp
|
||||
${SELF_TEST_DIR}/UsageTests/VariadicMacros.tests.cpp
|
||||
${SELF_TEST_DIR}/UsageTests/MatchersRanges.tests.cpp
|
||||
${SELF_TEST_DIR}/UsageTests/Matchers.tests.cpp
|
||||
${SELF_TEST_DIR}/UsageTests/MatchersConstexpr.tests.cpp
|
||||
${SELF_TEST_DIR}/UsageTests/MatchersRanges.tests.cpp
|
||||
)
|
||||
|
||||
set(TEST_HEADERS
|
||||
|
||||
@@ -174,7 +174,7 @@ target_compile_definitions(DeferredStaticChecks PRIVATE "CATCH_CONFIG_RUNTIME_ST
|
||||
add_test(NAME DeferredStaticChecks COMMAND DeferredStaticChecks -r compact)
|
||||
set_tests_properties(DeferredStaticChecks
|
||||
PROPERTIES
|
||||
PASS_REGULAR_EXPRESSION "test cases: 1 \\| 1 failed\nassertions: 3 \\| 3 failed"
|
||||
PASS_REGULAR_EXPRESSION "test cases: 1 \\| 1 failed\nassertions: 3 \\| 3 failed;test cases: 1 \\| 1 failed\nassertions: 4 \\| 4 failed"
|
||||
)
|
||||
|
||||
add_executable(MixingClearedAndUnclearedMessages ${TESTS_DIR}/X06-MixingClearedAndUnclearedMessages.cpp)
|
||||
|
||||
@@ -53,6 +53,8 @@ TEST_CASE( "Disabled Macros" ) {
|
||||
REQUIRE_THAT( 1,
|
||||
Catch::Matchers::Predicate( []( int ) { return false; } ) );
|
||||
BENCHMARK( "Disabled benchmark" ) { REQUIRE( 1 == 2 ); };
|
||||
|
||||
STATIC_REQUIRE_THAT( 1, Catch::Matchers::Predicate( []( int ) { return false; } ) );
|
||||
}
|
||||
|
||||
struct DisabledFixture {};
|
||||
|
||||
@@ -12,10 +12,36 @@
|
||||
*/
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/matchers/catch_matchers_templated.hpp>
|
||||
|
||||
#if defined( CATCH_INTERNAL_CONSTEXPR_MATCHERS_ENABLED )
|
||||
|
||||
namespace {
|
||||
struct MatchNoneMatcher final : public Catch::Matchers::MatcherGenericBase {
|
||||
public:
|
||||
template <typename Any>
|
||||
constexpr bool match( Any&& ) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string describe() const override {
|
||||
using namespace std::string_literals;
|
||||
return "Matches anything"s;
|
||||
}
|
||||
};
|
||||
|
||||
constexpr MatchNoneMatcher MatchNone() { return MatchNoneMatcher(); }
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
|
||||
TEST_CASE("Deferred static checks") {
|
||||
STATIC_CHECK(1 == 2);
|
||||
STATIC_CHECK_FALSE(1 != 2);
|
||||
#if defined(CATCH_INTERNAL_CONSTEXPR_MATCHERS_ENABLED)
|
||||
STATIC_CHECK_THAT(1, MatchNone());
|
||||
#endif
|
||||
// This last assertion must be executed too
|
||||
CHECK(1 == 2);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
// 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
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/matchers/catch_matchers_templated.hpp>
|
||||
|
||||
#if defined( CATCH_INTERNAL_CONSTEXPR_MATCHERS_ENABLED )
|
||||
|
||||
namespace {
|
||||
struct MatchAllMatcher final : public Catch::Matchers::MatcherGenericBase {
|
||||
public:
|
||||
template <typename Any>
|
||||
constexpr bool match( Any&& ) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string describe() const override {
|
||||
using namespace std::string_literals;
|
||||
return "Matches anything"s;
|
||||
}
|
||||
};
|
||||
|
||||
constexpr MatchAllMatcher MatchAll() { return MatchAllMatcher(); }
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE( "Constexpr support for matchers", "[constexpr][matchers][approvals]" ) {
|
||||
STATIC_REQUIRE( MatchAll().match( 1 ) );
|
||||
STATIC_REQUIRE_THAT( 1, MatchAll() );
|
||||
}
|
||||
|
||||
// Combining matchers needs C++26 and P2738, so they are in separate preprocessor block
|
||||
# if __cpp_constexpr >= 202306L
|
||||
|
||||
TEST_CASE("Constexpr support for combining matchers",
|
||||
"[constexpr][matchers][approvals]") {
|
||||
STATIC_REQUIRE( ( MatchAll() && MatchAll() ).match( 1 ) );
|
||||
STATIC_REQUIRE( ( MatchAll() || MatchAll() ).match( 1 ) );
|
||||
STATIC_REQUIRE( ( !!MatchAll() ).match( 1 ) );
|
||||
STATIC_REQUIRE_THAT( 1, MatchAll() && MatchAll() );
|
||||
STATIC_REQUIRE_THAT( 1, MatchAll() || MatchAll() );
|
||||
STATIC_REQUIRE_THAT( 1, !!MatchAll() );
|
||||
}
|
||||
|
||||
#endif // __cpp_constexpr >= 202306L
|
||||
|
||||
#endif
|
||||
@@ -49,6 +49,7 @@ self_test_sources = files(
|
||||
'SelfTest/UsageTests/Exception.tests.cpp',
|
||||
'SelfTest/UsageTests/Generators.tests.cpp',
|
||||
'SelfTest/UsageTests/Matchers.tests.cpp',
|
||||
'SelfTest/UsageTests/MatchersConstexpr.tests.cpp',
|
||||
'SelfTest/UsageTests/MatchersRanges.tests.cpp',
|
||||
'SelfTest/UsageTests/Message.tests.cpp',
|
||||
'SelfTest/UsageTests/Misc.tests.cpp',
|
||||
|
||||
Reference in new issue
Block a user