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

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal
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/internal
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/internal')
-rw-r--r--src/catch2/internal/catch_container_nonmembers.hpp67
1 files changed, 67 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