From db1a0465dc39e8aed36852984c14072192750b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Fri, 14 Feb 2020 10:32:30 +0100 Subject: [PATCH] Outline GeneratorException from generators header --- examples/301-Gen-MapTypeConversion.cpp | 2 +- src/CMakeLists.txt | 2 ++ src/catch2/catch_approx.h | 6 ++-- src/catch2/catch_generator_exception.cpp | 14 +++++++++ src/catch2/catch_generator_exception.hpp | 29 +++++++++++++++++++ src/catch2/catch_generators.cpp | 15 ++++++---- src/catch2/catch_generators.hpp | 17 ++++------- src/catch2/catch_generators_generic.hpp | 4 +-- src/catch2/catch_generators_specific.hpp | 2 +- src/catch2/catch_output_redirect.cpp | 1 - src/catch2/catch_test_case_tracker.cpp | 1 - .../GeneratorsImpl.tests.cpp | 1 + .../SelfTest/UsageTests/Generators.tests.cpp | 1 + 13 files changed, 69 insertions(+), 26 deletions(-) create mode 100644 src/catch2/catch_generator_exception.cpp create mode 100644 src/catch2/catch_generator_exception.hpp diff --git a/examples/301-Gen-MapTypeConversion.cpp b/examples/301-Gen-MapTypeConversion.cpp index 66f582d613..56434a0df7 100644 --- a/examples/301-Gen-MapTypeConversion.cpp +++ b/examples/301-Gen-MapTypeConversion.cpp @@ -18,7 +18,7 @@ class LineGenerator : public Catch::Generators::IGenerator { LineGenerator() { m_stream.str("1\n2\n3\n4\n"); if (!next()) { - throw Catch::GeneratorException("Couldn't read a single line"); + Catch::Generators::Detail::throw_generator_exception("Couldn't read a single line"); } } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d1cde1b741..a3e5b53dd9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -56,6 +56,7 @@ set(INTERNAL_HEADERS ${SOURCES_DIR}/catch_errno_guard.h ${SOURCES_DIR}/catch_exception_translator_registry.h ${SOURCES_DIR}/catch_fatal_condition.h + ${SOURCES_DIR}/catch_generator_exception.hpp ${SOURCES_DIR}/catch_generators.hpp ${SOURCES_DIR}/catch_generators_generic.hpp ${SOURCES_DIR}/catch_generators_specific.hpp @@ -142,6 +143,7 @@ set(IMPL_SOURCES ${SOURCES_DIR}/catch_errno_guard.cpp ${SOURCES_DIR}/catch_exception_translator_registry.cpp ${SOURCES_DIR}/catch_fatal_condition.cpp + ${SOURCES_DIR}/catch_generator_exception.cpp ${SOURCES_DIR}/catch_generators.cpp ${SOURCES_DIR}/catch_interfaces_capture.cpp ${SOURCES_DIR}/catch_interfaces_config.cpp diff --git a/src/catch2/catch_approx.h b/src/catch2/catch_approx.h index 9dc9f292b5..4f3a69584e 100644 --- a/src/catch2/catch_approx.h +++ b/src/catch2/catch_approx.h @@ -17,11 +17,9 @@ namespace Catch { class Approx { private: bool equalityComparisonImpl(double other) const; - // Validates the new margin (margin >= 0) - // out-of-line to avoid including stdexcept in the header + //! Sets and validates the new margin (margin >= 0) void setMargin(double margin); - // Validates the new epsilon (0 < epsilon < 1) - // out-of-line to avoid including stdexcept in the header + //! Sets and validates the new epsilon (0 < epsilon < 1) void setEpsilon(double epsilon); public: diff --git a/src/catch2/catch_generator_exception.cpp b/src/catch2/catch_generator_exception.cpp new file mode 100644 index 0000000000..9ec5d55b2c --- /dev/null +++ b/src/catch2/catch_generator_exception.cpp @@ -0,0 +1,14 @@ +/* + * 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) + */ + +#include + +namespace Catch { + + const char* GeneratorException::what() const noexcept { + return m_msg; + } + +} // end namespace Catch diff --git a/src/catch2/catch_generator_exception.hpp b/src/catch2/catch_generator_exception.hpp new file mode 100644 index 0000000000..ff84c4d8cf --- /dev/null +++ b/src/catch2/catch_generator_exception.hpp @@ -0,0 +1,29 @@ +/* + * 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_GENERATOR_EXCEPTION_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_GENERATOR_EXCEPTION_HPP_INCLUDED + +#include + +namespace Catch { + + // Exception type to be thrown when a Generator runs into an error, + // e.g. it cannot initialize the first return value based on + // runtime information + 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; + }; + +} // end namespace Catch + +#endif // TWOBLUECUBES_CATCH_GENERATOR_EXCEPTION_HPP_INCLUDED diff --git a/src/catch2/catch_generators.cpp b/src/catch2/catch_generators.cpp index 0cf55a8df1..48d98b929d 100644 --- a/src/catch2/catch_generators.cpp +++ b/src/catch2/catch_generators.cpp @@ -6,7 +6,8 @@ */ #include -#include +#include +#include #include #include @@ -16,12 +17,16 @@ namespace Catch { IGeneratorTracker::~IGeneratorTracker() {} -const char* GeneratorException::what() const noexcept { - return m_msg; -} - namespace Generators { +namespace Detail { + + [[noreturn]] + void throw_generator_exception(char const* msg) { + Catch::throw_exception(GeneratorException{ msg }); + } +} // end namespace Detail + GeneratorUntypedBase::~GeneratorUntypedBase() {} auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker& { diff --git a/src/catch2/catch_generators.hpp b/src/catch2/catch_generators.hpp index 86f020068a..d49ae2e222 100644 --- a/src/catch2/catch_generators.hpp +++ b/src/catch2/catch_generators.hpp @@ -9,29 +9,24 @@ #include #include -#include #include #include #include #include #include -#include namespace Catch { -class GeneratorException : public std::exception { - const char* const m_msg = ""; +namespace Generators { -public: - GeneratorException(const char* msg): - m_msg(msg) - {} +namespace Detail { - const char* what() const noexcept override final; -}; + //! Throws GeneratorException with the provided message + [[noreturn]] + void throw_generator_exception(char const * msg); -namespace Generators { +} // end namespace detail template struct IGenerator : GeneratorUntypedBase { diff --git a/src/catch2/catch_generators_generic.hpp b/src/catch2/catch_generators_generic.hpp index 5a8708dcce..954150b9e5 100644 --- a/src/catch2/catch_generators_generic.hpp +++ b/src/catch2/catch_generators_generic.hpp @@ -65,7 +65,7 @@ namespace Generators { // filter. In that case we throw an exception. auto has_initial_value = next(); if (!has_initial_value) { - Catch::throw_exception(GeneratorException("No valid value found in filtered generator")); + Detail::throw_generator_exception("No valid value found in filtered generator"); } } } @@ -202,7 +202,7 @@ namespace Generators { m_chunk.push_back(m_generator.get()); for (size_t i = 1; i < m_chunk_size; ++i) { if (!m_generator.next()) { - Catch::throw_exception(GeneratorException("Not enough values to initialize the first chunk")); + Detail::throw_generator_exception("Not enough values to initialize the first chunk"); } m_chunk.push_back(m_generator.get()); } diff --git a/src/catch2/catch_generators_specific.hpp b/src/catch2/catch_generators_specific.hpp index b4ca536c62..f61a2be2a1 100644 --- a/src/catch2/catch_generators_specific.hpp +++ b/src/catch2/catch_generators_specific.hpp @@ -140,7 +140,7 @@ class IteratorGenerator final : public IGenerator { template IteratorGenerator(InputIterator first, InputSentinel last):m_elems(first, last) { if (m_elems.empty()) { - Catch::throw_exception(GeneratorException("IteratorGenerator received no valid values")); + Detail::throw_generator_exception("IteratorGenerator received no valid values"); } } diff --git a/src/catch2/catch_output_redirect.cpp b/src/catch2/catch_output_redirect.cpp index 10ecfe22bf..5ff7abd0c7 100644 --- a/src/catch2/catch_output_redirect.cpp +++ b/src/catch2/catch_output_redirect.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #if defined(CATCH_CONFIG_NEW_CAPTURE) #if defined(_MSC_VER) diff --git a/src/catch2/catch_test_case_tracker.cpp b/src/catch2/catch_test_case_tracker.cpp index 1781a8db3a..51aa56ffa7 100644 --- a/src/catch2/catch_test_case_tracker.cpp +++ b/src/catch2/catch_test_case_tracker.cpp @@ -12,7 +12,6 @@ #include #include -#include #include #include diff --git a/tests/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp b/tests/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp index df5e036e15..459485bcb8 100644 --- a/tests/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include diff --git a/tests/SelfTest/UsageTests/Generators.tests.cpp b/tests/SelfTest/UsageTests/Generators.tests.cpp index ca03f026ca..0b4511c6f6 100644 --- a/tests/SelfTest/UsageTests/Generators.tests.cpp +++ b/tests/SelfTest/UsageTests/Generators.tests.cpp @@ -1,4 +1,5 @@ #include +#include #include #include