diff options
| author | Raphael Schaller | 2021-11-14 17:05:31 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-07-16 16:16:05 +0200 |
| commit | 1bd233866c5ba1d94fbea34b260685b4b4ecb796 (patch) | |
| tree | f12a6ec27174d50af450a537ed49f006de505301 /src | |
| parent | f993b702c694211690a01de19e6d9eaa9912cae8 (diff) | |
| download | catch3-1bd233866c5ba1d94fbea34b260685b4b4ecb796.tar.gz | |
Add AllTrue, AnyTrue, NoneTrue matchers
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/catch2/matchers/catch_matchers_quantifiers.cpp | 24 | ||||
| -rw-r--r-- | src/catch2/matchers/catch_matchers_quantifiers.hpp | 59 |
3 files changed, 83 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2aa67b8f..b3dc5570 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -187,6 +187,7 @@ set(IMPL_SOURCES ${SOURCES_DIR}/interfaces/catch_interfaces_reporter.cpp ${SOURCES_DIR}/internal/catch_list.cpp ${SOURCES_DIR}/matchers/catch_matchers_floating_point.cpp + ${SOURCES_DIR}/matchers/catch_matchers_quantifiers.cpp ${SOURCES_DIR}/matchers/catch_matchers_string.cpp ${SOURCES_DIR}/matchers/catch_matchers_templated.cpp ${SOURCES_DIR}/catch_message.cpp diff --git a/src/catch2/matchers/catch_matchers_quantifiers.cpp b/src/catch2/matchers/catch_matchers_quantifiers.cpp new file mode 100644 index 00000000..1f2e48d8 --- /dev/null +++ b/src/catch2/matchers/catch_matchers_quantifiers.cpp @@ -0,0 +1,24 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 +#include <catch2/matchers/catch_matchers_quantifiers.hpp> + +namespace Catch { + namespace Matchers { + std::string AllTrueMatcher::describe() const { return "contains only true"; } + + AllTrueMatcher AllTrue() { return AllTrueMatcher{}; } + + std::string NoneTrueMatcher::describe() const { return "contains no true"; } + + NoneTrueMatcher NoneTrue() { return NoneTrueMatcher{}; } + + std::string AnyTrueMatcher::describe() const { return "contains at least one true"; } + + AnyTrueMatcher AnyTrue() { return AnyTrueMatcher{}; } + } // namespace Matchers +} // namespace Catch diff --git a/src/catch2/matchers/catch_matchers_quantifiers.hpp b/src/catch2/matchers/catch_matchers_quantifiers.hpp index e0e1a62d..8833440f 100644 --- a/src/catch2/matchers/catch_matchers_quantifiers.hpp +++ b/src/catch2/matchers/catch_matchers_quantifiers.hpp @@ -85,7 +85,55 @@ namespace Catch { } }; - // Creates a matcher that checks whether a range contains element matching a matcher + // Matcher for checking that all elements in range are true. + class AllTrueMatcher final : public MatcherGenericBase { + public: + std::string describe() const override; + + template <typename RangeLike> + bool match(RangeLike&& rng) const { + for (auto&& elem : rng) { + if (!elem) { + return false; + } + } + return true; + } + }; + + // Matcher for checking that no element in range is true. + class NoneTrueMatcher final : public MatcherGenericBase { + public: + std::string describe() const override; + + template <typename RangeLike> + bool match(RangeLike&& rng) const { + for (auto&& elem : rng) { + if (elem) { + return false; + } + } + return true; + } + }; + + // Matcher for checking that any element in range is true. + class AnyTrueMatcher final : public MatcherGenericBase { + public: + std::string describe() const override; + + template <typename RangeLike> + bool match(RangeLike&& rng) const { + for (auto&& elem : rng) { + if (elem) { + return true; + } + } + return false; + } + }; + + // Creates a matcher that checks whether all elements in a range match a matcher template <typename Matcher> AllMatchMatcher<Matcher> AllMatch(Matcher&& matcher) { return { CATCH_FORWARD(matcher) }; @@ -102,6 +150,15 @@ namespace Catch { AnyMatchMatcher<Matcher> AnyMatch(Matcher&& matcher) { return { CATCH_FORWARD(matcher) }; } + + // Creates a matcher that checks whether all elements in a range are true + AllTrueMatcher AllTrue(); + + // Creates a matcher that checks whether no element in a range is true + NoneTrueMatcher NoneTrue(); + + // Creates a matcher that checks whether any element in a range is true + AnyTrueMatcher AnyTrue(); } } |