Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/SelfTest/UsageTests/MatchersConstexpr.tests.cpp
blob: 66fe7c8f4512db048f00655afcf38e9a489a2391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

//              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/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>

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(); }

    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]" ) {
    STATIC_REQUIRE( MatchAll().match( 1 ) );
    STATIC_REQUIRE_THAT( 1, MatchAll() );
}

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, IsEmpty() );
    static constexpr int arr[1] = { 2 };
    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
#    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