diff options
| author | Martin Hořeňovský | 2025-10-06 11:47:38 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2026-01-12 16:58:15 +0100 |
| commit | 9eca713a1f4555b3095564781d77654958ee3dc1 (patch) | |
| tree | e500e9b38c705f7f08ca412af16cb843cb80088e /src | |
| parent | 44c597f0742b491399994afa00070b99d3ef2885 (diff) | |
| download | catch3-9eca713a1f4555b3095564781d77654958ee3dc1.tar.gz | |
Add option to skip forward to the generator interface
Diffstat (limited to 'src')
| -rw-r--r-- | src/catch2/interfaces/catch_interfaces_generatortracker.cpp | 27 | ||||
| -rw-r--r-- | src/catch2/interfaces/catch_interfaces_generatortracker.hpp | 16 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/catch2/interfaces/catch_interfaces_generatortracker.cpp b/src/catch2/interfaces/catch_interfaces_generatortracker.cpp index e9fa5dda..4ac23ddb 100644 --- a/src/catch2/interfaces/catch_interfaces_generatortracker.cpp +++ b/src/catch2/interfaces/catch_interfaces_generatortracker.cpp @@ -7,6 +7,8 @@ // SPDX-License-Identifier: BSL-1.0 #include <catch2/interfaces/catch_interfaces_generatortracker.hpp> +#include <catch2/generators/catch_generators.hpp> + #include <string> namespace Catch { @@ -21,6 +23,31 @@ namespace Catch { return ret; } + void GeneratorUntypedBase::skipToNthElementImpl( std::size_t n ) { + for ( size_t i = m_currentElementIndex; i < n; ++i ) { + bool isValid = next(); + if ( !isValid ) { + Detail::throw_generator_exception( + "Coud not jump to Nth element: not enough elements" ); + } + } + } + + void GeneratorUntypedBase::skipToNthElement( std::size_t n ) { + if ( n < m_currentElementIndex ) { + Detail::throw_generator_exception( + "Tried to jump generator backwards" ); + } + if ( n == m_currentElementIndex ) { return; } + + skipToNthElementImpl(n); + // Fixup tracking after moving the generator forward + // * Ensure that the correct element index is set after skipping + // * Invalidate cache + m_currentElementIndex = n; + m_stringReprCache.clear(); + } + StringRef GeneratorUntypedBase::currentElementAsString() const { if ( m_stringReprCache.empty() ) { m_stringReprCache = stringifyImpl(); diff --git a/src/catch2/interfaces/catch_interfaces_generatortracker.hpp b/src/catch2/interfaces/catch_interfaces_generatortracker.hpp index d70cb593..eb3871bc 100644 --- a/src/catch2/interfaces/catch_interfaces_generatortracker.hpp +++ b/src/catch2/interfaces/catch_interfaces_generatortracker.hpp @@ -35,6 +35,15 @@ namespace Catch { //! Customization point for `currentElementAsString` virtual std::string stringifyImpl() const = 0; + /** + * Customization point for skipping to the n-th element + * + * Defaults to successively calling `countedNext`. If there + * are not enough elements to reach the nth one, will throw + * an error. + */ + virtual void skipToNthElementImpl( std::size_t n ); + public: GeneratorUntypedBase() = default; // Generation of copy ops is deprecated (and Clang will complain) @@ -59,6 +68,13 @@ namespace Catch { std::size_t currentElementIndex() const { return m_currentElementIndex; } /** + * Moves the generator forward **to** the n-th element + * + * Cannot move backwards. Can stay in place. + */ + void skipToNthElement( std::size_t n ); + + /** * Returns generator's current element as user-friendly string. * * By default returns string equivalent to calling |