diff options
| author | Martin Hořeňovský | 2026-05-12 09:25:50 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2026-05-12 11:05:31 +0200 |
| commit | c267b6eb4de6d9ccdde238334c1248c7b60b8c0b (patch) | |
| tree | a16676b23daa11fe15d3dfdddfab7e9dc7601325 /tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp | |
| parent | 3cdae5faf08b7a3861be5e7bf6f1968e9229f460 (diff) | |
| download | catch3-c267b6eb4de6d9ccdde238334c1248c7b60b8c0b.tar.gz | |
Constexpr matching support in the quantifier matchers
Diffstat (limited to 'tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp')
| -rw-r--r-- | tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp | 54 |
1 files changed, 49 insertions, 5 deletions
diff --git a/tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp b/tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp index 0d910daf..66fe7c8f 100644 --- a/tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp +++ b/tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp @@ -6,14 +6,17 @@ // SPDX-License-Identifier: BSL-1.0 +#include <catch2/internal/catch_compiler_capabilities.hpp> + +#if defined( CATCH_INTERNAL_CONSTEXPR_MATCHERS_ENABLED ) + #include <catch2/catch_test_macros.hpp> #include <catch2/matchers/catch_matchers_templated.hpp> #include <catch2/matchers/catch_matchers_container_properties.hpp> +#include <catch2/matchers/catch_matchers_quantifiers.hpp> #include <array> -#if defined( CATCH_INTERNAL_CONSTEXPR_MATCHERS_ENABLED ) - namespace { struct MatchAllMatcher final : public Catch::Matchers::MatcherGenericBase { public: @@ -30,6 +33,21 @@ namespace { constexpr MatchAllMatcher MatchAll() { return MatchAllMatcher(); } + struct MatchTrueMatcher final : public Catch::Matchers::MatcherGenericBase { + public: + constexpr bool match( bool b ) const { + return b; + } + + std::string describe() const override { + using namespace std::string_literals; + return "Matches true"s; + } + }; + + constexpr MatchTrueMatcher MatchTrue() { return MatchTrueMatcher(); } + + } // namespace TEST_CASE( "Constexpr support for matchers", "[constexpr][matchers][approvals]" ) { @@ -39,11 +57,37 @@ TEST_CASE( "Constexpr support for matchers", "[constexpr][matchers][approvals]" TEST_CASE( "IsEmpty and HasSize matchers can be used in constexpr contexts", "[constexpr][matchers][approvals]" ){ + using namespace Catch::Matchers; static constexpr std::array<int, 0> empty{}; - STATIC_REQUIRE_THAT( empty, Catch::Matchers::IsEmpty() ); + STATIC_REQUIRE_THAT( empty, IsEmpty() ); static constexpr int arr[1] = { 2 }; - STATIC_REQUIRE_THAT( arr, Catch::Matchers::SizeIs( 1 ) ); - STATIC_REQUIRE_THAT( arr, Catch::Matchers::SizeIs( MatchAll() ) ); + STATIC_REQUIRE_THAT( arr, SizeIs( 1 ) ); + STATIC_REQUIRE_THAT( arr, SizeIs( MatchAll() ) ); +} + +constexpr std::array<bool, 3> compute_bools( int type ) { + switch ( type ) { + case 0: + return { true, true, true }; + case 1: + return { false, true, false }; + case 2: + return { false, false, false }; + default: + return { false, false, false }; + } +} + +TEST_CASE( "Quantifier matchers can be used in constexpr contexts", + "[constexpr][matchers][approvals]" ) { + using namespace Catch::Matchers; + STATIC_REQUIRE_THAT( compute_bools( 0 ), AllTrue() ); + STATIC_REQUIRE_THAT( compute_bools( 1 ), AnyTrue() ); + STATIC_REQUIRE_THAT( compute_bools( 2 ), NoneTrue() ); + + STATIC_REQUIRE_THAT( compute_bools( 0 ), AllMatch( MatchTrue() ) ); + STATIC_REQUIRE_THAT( compute_bools( 1 ), AnyMatch( MatchTrue() ) ); + STATIC_REQUIRE_THAT( compute_bools( 2 ), NoneMatch( MatchTrue() ) ); } // Combining matchers needs C++26 and P2738, so they are in separate preprocessor block |