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

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2022-05-16 16:34:06 +0200
committerMartin Hořeňovský2022-05-16 16:34:06 +0200
commit48f322697434292206db570e8fbe1e3ac8728d0b (patch)
treeb19176f822bad7fb0eb3fd4b1ee50b762c654db5 /src
parentaf8b54ecd59f8174aaaa97682e3544f2820a0900 (diff)
downloadcatch3-48f322697434292206db570e8fbe1e3ac8728d0b.tar.gz
Add support for retrieving generator's element as string
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/catch2/generators/catch_generators.hpp5
-rw-r--r--src/catch2/interfaces/catch_interfaces_generatortracker.cpp32
-rw-r--r--src/catch2/interfaces/catch_interfaces_generatortracker.hpp33
4 files changed, 64 insertions, 7 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 705b31d0..f69d3166 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -184,6 +184,7 @@ set(IMPL_SOURCES
${SOURCES_DIR}/internal/catch_istream.cpp
${SOURCES_DIR}/generators/internal/catch_generators_combined_tu.cpp
${SOURCES_DIR}/interfaces/catch_interfaces_combined_tu.cpp
+ ${SOURCES_DIR}/interfaces/catch_interfaces_generatortracker.cpp
${SOURCES_DIR}/interfaces/catch_interfaces_reporter.cpp
${SOURCES_DIR}/internal/catch_list.cpp
${SOURCES_DIR}/matchers/catch_matchers_floating_point.cpp
diff --git a/src/catch2/generators/catch_generators.hpp b/src/catch2/generators/catch_generators.hpp
index 24af63d6..1de04e3b 100644
--- a/src/catch2/generators/catch_generators.hpp
+++ b/src/catch2/generators/catch_generators.hpp
@@ -8,6 +8,7 @@
#ifndef CATCH_GENERATORS_HPP_INCLUDED
#define CATCH_GENERATORS_HPP_INCLUDED
+#include <catch2/catch_tostring.hpp>
#include <catch2/interfaces/catch_interfaces_generatortracker.hpp>
#include <catch2/internal/catch_source_line_info.hpp>
#include <catch2/internal/catch_stringref.hpp>
@@ -32,6 +33,10 @@ namespace Detail {
template<typename T>
class IGenerator : public GeneratorUntypedBase {
+ std::string stringifyImpl() const override {
+ return ::Catch::Detail::stringify( get() );
+ }
+
public:
~IGenerator() override = default;
IGenerator() = default;
diff --git a/src/catch2/interfaces/catch_interfaces_generatortracker.cpp b/src/catch2/interfaces/catch_interfaces_generatortracker.cpp
new file mode 100644
index 00000000..e8c67b54
--- /dev/null
+++ b/src/catch2/interfaces/catch_interfaces_generatortracker.cpp
@@ -0,0 +1,32 @@
+
+// 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/interfaces/catch_interfaces_generatortracker.hpp>
+#include <string>
+
+namespace Catch {
+ namespace Generators {
+
+ bool GeneratorUntypedBase::countedNext() {
+ auto ret = next();
+ if ( ret ) {
+ m_stringReprCache.clear();
+ ++m_currentElementIndex;
+ }
+ return ret;
+ }
+
+ StringRef GeneratorUntypedBase::currentElementAsString() const {
+ if ( m_stringReprCache.empty() ) {
+ m_stringReprCache = stringifyImpl();
+ }
+ return m_stringReprCache;
+ }
+
+ } // namespace Generators
+} // namespace Catch
diff --git a/src/catch2/interfaces/catch_interfaces_generatortracker.hpp b/src/catch2/interfaces/catch_interfaces_generatortracker.hpp
index 2efc4a58..83e3d783 100644
--- a/src/catch2/interfaces/catch_interfaces_generatortracker.hpp
+++ b/src/catch2/interfaces/catch_interfaces_generatortracker.hpp
@@ -9,11 +9,18 @@
#define CATCH_INTERFACES_GENERATORTRACKER_HPP_INCLUDED
#include <catch2/internal/catch_unique_ptr.hpp>
+#include <catch2/internal/catch_stringref.hpp>
+
+#include <string>
namespace Catch {
namespace Generators {
class GeneratorUntypedBase {
+ // Caches result from `toStringImpl`, assume that when it is an
+ // empty string, the cache is invalidated.
+ mutable std::string m_stringReprCache;
+
// Counts based on `next` returning true
std::size_t m_currentElementIndex = 0;
@@ -25,6 +32,9 @@ namespace Catch {
*/
virtual bool next() = 0;
+ //! Customization point for `currentElementAsString`
+ virtual std::string stringifyImpl() const = 0;
+
public:
GeneratorUntypedBase() = default;
// Generation of copy ops is deprecated (and Clang will complain)
@@ -44,15 +54,24 @@ namespace Catch {
* As with `next`, returns true iff the move succeeded and
* the generator has new valid element to provide.
*/
- bool countedNext() {
- auto ret = next();
- if ( ret ) {
- ++m_currentElementIndex;
- }
- return ret;
- }
+ bool countedNext();
std::size_t currentElementIndex() const { return m_currentElementIndex; }
+
+ /**
+ * Returns generator's current element as user-friendly string.
+ *
+ * By default returns string equivalent to calling
+ * `Catch::Detail::stringify` on the current element, but generators
+ * can customize their implementation as needed.
+ *
+ * Not thread-safe due to internal caching.
+ *
+ * The returned ref is valid only until the generator instance
+ * is destructed, or it moves onto the next element, whichever
+ * comes first.
+ */
+ StringRef currentElementAsString() const;
};
using GeneratorBasePtr = Catch::Detail::unique_ptr<GeneratorUntypedBase>;