diff options
| author | Uriel García Rivas | 2020-12-07 17:23:54 -0600 |
|---|---|---|
| committer | Martin Hořeňovský | 2020-12-27 20:20:55 +0100 |
| commit | 552af8920df24e16738dbb1cb6b46dd52de7804c (patch) | |
| tree | c336ff726945350d6943488dbf1102be43246533 /src | |
| parent | ce54ec185f69e32661efdcb4659f892415316b0e (diff) | |
| download | catch3-552af8920df24e16738dbb1cb6b46dd52de7804c.tar.gz | |
Added AnyMatch, AllMatch and NoneMatch
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/catch2/matchers/catch_matchers_all.hpp | 1 | ||||
| -rw-r--r-- | src/catch2/matchers/catch_matchers_quantifiers.hpp | 109 |
3 files changed, 111 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e19bd639..15fe1e7e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -88,6 +88,7 @@ set(INTERNAL_HEADERS ${SOURCES_DIR}/matchers/catch_matchers_exception.hpp ${SOURCES_DIR}/matchers/catch_matchers_floating_point.hpp ${SOURCES_DIR}/matchers/catch_matchers_predicate.hpp + ${SOURCES_DIR}/matchers/catch_matchers_quantifiers.hpp ${SOURCES_DIR}/matchers/catch_matchers_string.hpp ${SOURCES_DIR}/matchers/catch_matchers_templated.hpp ${SOURCES_DIR}/matchers/catch_matchers_vector.hpp diff --git a/src/catch2/matchers/catch_matchers_all.hpp b/src/catch2/matchers/catch_matchers_all.hpp index 6466b8ab..3c0bb9f5 100644 --- a/src/catch2/matchers/catch_matchers_all.hpp +++ b/src/catch2/matchers/catch_matchers_all.hpp @@ -26,6 +26,7 @@ #include <catch2/matchers/catch_matchers_exception.hpp> #include <catch2/matchers/catch_matchers_floating_point.hpp> #include <catch2/matchers/catch_matchers_predicate.hpp> +#include <catch2/matchers/catch_matchers_quantifiers.hpp> #include <catch2/matchers/catch_matchers_string.hpp> #include <catch2/matchers/catch_matchers_templated.hpp> #include <catch2/matchers/catch_matchers_vector.hpp> diff --git a/src/catch2/matchers/catch_matchers_quantifiers.hpp b/src/catch2/matchers/catch_matchers_quantifiers.hpp new file mode 100644 index 00000000..09a8c128 --- /dev/null +++ b/src/catch2/matchers/catch_matchers_quantifiers.hpp @@ -0,0 +1,109 @@ + +// 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 +#ifndef CATCH_MATCHERS_QUANTIFIERS_HPP_INCLUDED +#define CATCH_MATCHERS_QUANTIFIERS_HPP_INCLUDED + +#include <catch2/matchers/catch_matchers_templated.hpp> + +#include <utility> + +namespace Catch { + namespace Matchers { + // Matcher for checking that all elements in range matches a given matcher. + template <typename Matcher> + class AllMatchMatcher final : public MatcherGenericBase { + Matcher m_matcher; + public: + AllMatchMatcher(Matcher matcher): + m_matcher(std::move(matcher)) + {} + + std::string describe() const override { + return "all match " + m_matcher.describe(); + } + + template <typename RangeLike> + bool match(RangeLike&& rng) const { + for (auto&& elem : rng) { + if (!m_matcher.match(elem)) { + return false; + } + } + return true; + } + }; + + // Matcher for checking that no element in range matches a given matcher. + template <typename Matcher> + class NoneMatchMatcher final : public MatcherGenericBase { + Matcher m_matcher; + public: + NoneMatchMatcher(Matcher matcher): + m_matcher(std::move(matcher)) + {} + + std::string describe() const override { + return "none match " + m_matcher.describe(); + } + + template <typename RangeLike> + bool match(RangeLike&& rng) const { + for (auto&& elem : rng) { + if (m_matcher.match(elem)) { + return false; + } + } + return true; + } + }; + + // Matcher for checking that at least one element in range matches a given matcher. + template <typename Matcher> + class AnyMatchMatcher final : public MatcherGenericBase { + Matcher m_matcher; + public: + AnyMatchMatcher(Matcher matcher): + m_matcher(std::move(matcher)) + {} + + std::string describe() const override { + return "any match " + m_matcher.describe(); + } + + template <typename RangeLike> + bool match(RangeLike&& rng) const { + for (auto&& elem : rng) { + if (m_matcher.match(elem)) { + return true; + } + } + return false; + } + }; + + // Creates a matcher that checks whether a range contains element matching a matcher + template <typename Matcher> + AllMatchMatcher<Matcher> AllMatch(Matcher&& matcher) { + return { std::forward<Matcher>(matcher) }; + } + + // Creates a matcher that checks whether no element in a range matches a matcher. + template <typename Matcher> + NoneMatchMatcher<Matcher> NoneMatch(Matcher&& matcher) { + return { std::forward<Matcher>(matcher) }; + } + + // Creates a matcher that checks whether any element in a range matches a matcher. + template <typename Matcher> + AnyMatchMatcher<Matcher> AnyMatch(Matcher&& matcher) { + return { std::forward<Matcher>(matcher) }; + } + } +} + +#endif // CATCH_MATCHERS_QUANTIFIERS_HPP_INCLUDED |