/* * Created by Phil Nash on 15/6/2018. * * 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_GENERATORS_HPP_INCLUDED #define TWOBLUECUBES_CATCH_GENERATORS_HPP_INCLUDED #include "catch_interfaces_generatortracker.h" #include "catch_common.h" #include "catch_enforce.h" #include #include #include #include #include #include namespace Catch { class GeneratorException : public std::exception { const char* const m_msg = ""; public: GeneratorException(const char* msg): m_msg(msg) {} const char* what() const noexcept override final; }; namespace Generators { // !TBD move this into its own location? namespace pf{ template std::unique_ptr make_unique( Args&&... args ) { return std::unique_ptr(new T(std::forward(args)...)); } } template struct IGenerator : GeneratorUntypedBase { virtual ~IGenerator() = default; // Returns the current element of the generator // // \Precondition The generator is either freshly constructed, // or the last call to `next()` returned true virtual T const& get() const = 0; using type = T; }; template class SingleValueGenerator final : public IGenerator { T m_value; public: SingleValueGenerator(T const& value) : m_value( value ) {} SingleValueGenerator(T&& value) : m_value(std::move(value)) {} T const& get() const override { return m_value; } bool next() override { return false; } }; template class FixedValuesGenerator final : public IGenerator { static_assert(!std::is_same::value, "FixedValuesGenerator does not support bools because of std::vector" "specialization, use SingleValue Generator instead."); std::vector m_values; size_t m_idx = 0; public: FixedValuesGenerator( std::initializer_list values ) : m_values( values ) {} T const& get() const override { return m_values[m_idx]; } bool next() override { ++m_idx; return m_idx < m_values.size(); } }; template class GeneratorWrapper final { std::unique_ptr> m_generator; public: GeneratorWrapper(std::unique_ptr> generator): m_generator(std::move(generator)) {} T const& get() const { return m_generator->get(); } bool next() { return m_generator->next(); } }; template GeneratorWrapper value(T&& value) { return GeneratorWrapper(pf::make_unique>(std::forward(value))); } template GeneratorWrapper values(std::initializer_list values) { return GeneratorWrapper(pf::make_unique>(values)); } template class Generators : public IGenerator { std::vector> m_generators; size_t m_current = 0; void populate(GeneratorWrapper&& generator) { m_generators.emplace_back(std::move(generator)); } void populate(T&& val) { m_generators.emplace_back(value(std::move(val))); } template void populate(U&& val) { populate(T(std::move(val))); } template void populate(U&& valueOrGenerator, Gs... moreGenerators) { populate(std::forward(valueOrGenerator)); populate(std::forward(moreGenerators)...); } public: template Generators(Gs... moreGenerators) { m_generators.reserve(sizeof...(Gs)); populate(std::forward(moreGenerators)...); } T const& get() const override { return m_generators[m_current].get(); } bool next() override { if (m_current >= m_generators.size()) { return false; } const bool current_status = m_generators[m_current].next(); if (!current_status) { ++m_current; } return m_current < m_generators.size(); } }; template GeneratorWrapper> table( std::initializer_list::type...>> tuples ) { return values>( tuples ); } // Tag type to signal that a generator sequence should convert arguments to a specific type template struct as {}; template auto makeGenerators( GeneratorWrapper&& generator, Gs... moreGenerators ) -> Generators { return Generators(std::move(generator), std::forward(moreGenerators)...); } template auto makeGenerators( GeneratorWrapper&& generator ) -> Generators { return Generators(std::move(generator)); } template auto makeGenerators( T&& val, Gs... moreGenerators ) -> Generators { return makeGenerators( value( std::forward( val ) ), std::forward( moreGenerators )... ); } template auto makeGenerators( as, U&& val, Gs... moreGenerators ) -> Generators { return makeGenerators( value( T( std::forward( val ) ) ), std::forward( moreGenerators )... ); } auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker&; template // Note: The type after -> is weird, because VS2015 cannot parse // the expression used in the typedef inside, when it is in // return type. Yeah. auto generate( SourceLineInfo const& lineInfo, L const& generatorExpression ) -> decltype(std::declval().get()) { using UnderlyingType = typename decltype(generatorExpression())::type; IGeneratorTracker& tracker = acquireGeneratorTracker( lineInfo ); if (!tracker.hasGenerator()) { tracker.setGenerator(pf::make_unique>(generatorExpression())); } auto const& generator = static_cast const&>( *tracker.getGenerator() ); return generator.get(); } } // namespace Generators } // namespace Catch #define GENERATE( ... ) \ Catch::Generators::generate( CATCH_INTERNAL_LINEINFO, [ ]{ using namespace Catch::Generators; return makeGenerators( __VA_ARGS__ ); } ) #define GENERATE_COPY( ... ) \ Catch::Generators::generate( CATCH_INTERNAL_LINEINFO, [=]{ using namespace Catch::Generators; return makeGenerators( __VA_ARGS__ ); } ) #define GENERATE_REF( ... ) \ Catch::Generators::generate( CATCH_INTERNAL_LINEINFO, [&]{ using namespace Catch::Generators; return makeGenerators( __VA_ARGS__ ); } ) #endif // TWOBLUECUBES_CATCH_GENERATORS_HPP_INCLUDED