diff options
| author | Martin Hořeňovský | 2022-05-16 16:34:06 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-05-16 16:34:06 +0200 |
| commit | 48f322697434292206db570e8fbe1e3ac8728d0b (patch) | |
| tree | b19176f822bad7fb0eb3fd4b1ee50b762c654db5 /src/catch2/interfaces | |
| parent | af8b54ecd59f8174aaaa97682e3544f2820a0900 (diff) | |
| download | catch3-48f322697434292206db570e8fbe1e3ac8728d0b.tar.gz | |
Add support for retrieving generator's element as string
Diffstat (limited to 'src/catch2/interfaces')
| -rw-r--r-- | src/catch2/interfaces/catch_interfaces_generatortracker.cpp | 32 | ||||
| -rw-r--r-- | src/catch2/interfaces/catch_interfaces_generatortracker.hpp | 33 |
2 files changed, 58 insertions, 7 deletions
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>; |