mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-03 13:55:39 +02:00
Add AllTrue, AnyTrue, NoneTrue matchers
This commit is contained in:

committed by
Martin Hořeňovský

parent
f993b702c6
commit
1bd233866c
@@ -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
|
||||
|
24
src/catch2/matchers/catch_matchers_quantifiers.cpp
Normal file
24
src/catch2/matchers/catch_matchers_quantifiers.cpp
Normal file
@@ -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
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user