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

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2
diff options
context:
space:
mode:
authorMartin Hořeňovský2020-03-09 11:13:07 +0100
committerMartin Hořeňovský2020-03-27 10:24:08 +0100
commit3a3efebd163f19dc8b9d8bbed4ab5309fc752867 (patch)
treeffc252431075ae2357b53d75caf2b27f96d03fbb /src/catch2
parentf52a58e857967124bedf5370b5b03f799c1db0b2 (diff)
downloadcatch3-3a3efebd163f19dc8b9d8bbed4ab5309fc752867.tar.gz
Add IsEmpty and SizeIs matchers for ranges/containers
`SizeIs` can accept both `size_t` and a matcher. In the first case, it checks whether the size of the range is equal to specified size. In the second case, it checks whether the provided matcher accepts the size of the range.
Diffstat (limited to 'src/catch2')
-rw-r--r--src/catch2/internal/catch_container_nonmembers.hpp67
-rw-r--r--src/catch2/matchers/catch_matchers_container_properties.cpp30
-rw-r--r--src/catch2/matchers/catch_matchers_container_properties.hpp86
3 files changed, 183 insertions, 0 deletions
diff --git a/src/catch2/internal/catch_container_nonmembers.hpp b/src/catch2/internal/catch_container_nonmembers.hpp
new file mode 100644
index 00000000..781b3dc4
--- /dev/null
+++ b/src/catch2/internal/catch_container_nonmembers.hpp
@@ -0,0 +1,67 @@
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef TWOBLUECUBES_CATCH_CONTAINER_NONMEMBERS_HPP_INCLUDED
+#define TWOBLUECUBES_CATCH_CONTAINER_NONMEMBERS_HPP_INCLUDED
+
+#include <catch2/catch_compiler_capabilities.h>
+
+
+// We want a simple polyfill over `std::empty`, `std::size` and so on
+// for C++14 or C++ libraries with incomplete support.
+// We also have to handle that MSVC std lib will happily provide these
+// under older standards.
+#if defined(CATCH_CPP17_OR_GREATER) || defined(_MSC_VER)
+
+// We are already using this header either way, so there shouldn't
+// be much additional overhead in including it to get the feature
+// test macros
+#include <string>
+
+# if !defined(__cpp_lib_nonmember_container_access)
+# define CATCH_CONFIG_POLYFILL_NONMEMBER_CONTAINER_ACCESS
+# endif
+
+#else
+#define CATCH_CONFIG_POLYFILL_NONMEMBER_CONTAINER_ACCESS
+#endif
+
+
+
+namespace Catch {
+namespace Detail {
+
+#if defined(CATCH_CONFIG_POLYFILL_NONMEMBER_CONTAINER_ACCESS)
+ template <typename Container>
+ constexpr auto empty(Container const& cont) -> decltype(cont.empty()) {
+ return cont.empty();
+ }
+ template <typename T, std::size_t N>
+ constexpr bool empty(const T (&)[N]) noexcept {
+ // GCC < 7 does not support the const T(&)[] parameter syntax
+ // so we have to ignore the length explicitly
+ (void)N;
+ return false;
+ }
+ template <typename T>
+ constexpr bool empty(std::initializer_list<T> list) noexcept {
+ return list.size() > 0;
+ }
+
+
+ template <typename Container>
+ constexpr auto size(Container const& cont) -> decltype(cont.size()) {
+ return cont.size();
+ }
+ template <typename T, std::size_t N>
+ constexpr std::size_t size(const T(&)[N]) noexcept {
+ return N;
+ }
+#endif // CATCH_CONFIG_POLYFILL_NONMEMBER_CONTAINER_ACCESS
+
+} // end namespace Detail
+} // end namespace Catch
+
+
+
+#endif // TWOBLUECUBES_CATCH_CONTAINER_NONMEMBERS_HPP_INCLUDED
diff --git a/src/catch2/matchers/catch_matchers_container_properties.cpp b/src/catch2/matchers/catch_matchers_container_properties.cpp
new file mode 100644
index 00000000..d4da71bf
--- /dev/null
+++ b/src/catch2/matchers/catch_matchers_container_properties.cpp
@@ -0,0 +1,30 @@
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#include <catch2/matchers/catch_matchers_container_properties.hpp>
+
+#include <catch2/catch_stream.h>
+
+namespace Catch {
+namespace Matchers {
+
+ std::string IsEmptyMatcher::describe() const {
+ return "is empty";
+ }
+
+ std::string HasSizeMatcher::describe() const {
+ ReusableStringStream sstr;
+ sstr << "has size == " << m_target_size;
+ return sstr.str();
+ }
+
+ IsEmptyMatcher IsEmpty() {
+ return {};
+ }
+
+ HasSizeMatcher SizeIs(std::size_t sz) {
+ return HasSizeMatcher{ sz };
+ }
+
+} // end namespace Matchers
+} // end namespace Catch
diff --git a/src/catch2/matchers/catch_matchers_container_properties.hpp b/src/catch2/matchers/catch_matchers_container_properties.hpp
new file mode 100644
index 00000000..e2d49ffd
--- /dev/null
+++ b/src/catch2/matchers/catch_matchers_container_properties.hpp
@@ -0,0 +1,86 @@
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef TWOBLUECUBES_CATCH_MATCHERS_CONTAINER_PROPERTIES_HPP_INCLUDED
+#define TWOBLUECUBES_CATCH_MATCHERS_CONTAINER_PROPERTIES_HPP_INCLUDED
+
+#include <catch2/matchers/catch_matchers_templates.hpp>
+#include <catch2/internal/catch_container_nonmembers.hpp>
+
+namespace Catch {
+ namespace Matchers {
+
+ class IsEmptyMatcher final : public MatcherGenericBase {
+ public:
+ // todo: Use polyfills
+ template <typename RangeLike>
+ bool match(RangeLike&& rng) const {
+#if defined(CATCH_CONFIG_POLYFILL_NONMEMBER_CONTAINER_ACCESS)
+ using Catch::Detail::empty;
+#else
+ using std::empty;
+#endif
+ return empty(rng);
+ }
+
+ std::string describe() const override;
+ };
+
+ class HasSizeMatcher final : public MatcherGenericBase {
+ std::size_t m_target_size;
+ public:
+ explicit HasSizeMatcher(std::size_t target_size):
+ m_target_size(target_size)
+ {}
+
+ template <typename RangeLike>
+ bool match(RangeLike&& rng) const {
+#if defined(CATCH_CONFIG_POLYFILL_NONMEMBER_CONTAINER_ACCESS)
+ using Catch::Detail::size;
+#else
+ using std::size;
+#endif
+ return size(rng) == m_target_size;
+ }
+
+ std::string describe() const override;
+ };
+
+ template <typename Matcher>
+ class SizeMatchesMatcher final : public MatcherGenericBase {
+ Matcher m_matcher;
+ public:
+ explicit SizeMatchesMatcher(Matcher m):
+ m_matcher(std::move(m))
+ {}
+
+ template <typename RangeLike>
+ bool match(RangeLike&& rng) const {
+#if defined(CATCH_CONFIG_POLYFILL_NONMEMBER_CONTAINER_ACCESS)
+ using Catch::Detail::size;
+#else
+ using std::size;
+#endif
+ return m_matcher.match(size(rng));
+ }
+
+ std::string describe() const override {
+ return "size matches " + m_matcher.describe();
+ }
+ };
+
+
+ //! Creates a matcher that accepts empty ranges/containers
+ IsEmptyMatcher IsEmpty();
+ //! Creates a matcher that accepts ranges/containers with specific size
+ HasSizeMatcher SizeIs(std::size_t sz);
+ template <typename Matcher>
+ std::enable_if_t<Detail::is_matcher<Matcher>::value,
+ SizeMatchesMatcher<Matcher>> SizeIs(Matcher&& m) {
+ return SizeMatchesMatcher<Matcher>{std::forward<Matcher>(m)};
+ }
+
+ } // end namespace Matchers
+} // end namespace Catch
+
+#endif // TWOBLUECUBES_CATCH_MATCHERS_CONTAINER_PROPERTIES_HPP_INCLUDED