Outline GeneratorException from generators header

This commit is contained in:
Martin Hořeňovský 2020-02-14 10:32:30 +01:00
parent b2a6523d85
commit db1a0465dc
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
13 changed files with 71 additions and 28 deletions

View File

@ -18,7 +18,7 @@ public:
LineGenerator() { LineGenerator() {
m_stream.str("1\n2\n3\n4\n"); m_stream.str("1\n2\n3\n4\n");
if (!next()) { if (!next()) {
throw Catch::GeneratorException("Couldn't read a single line"); Catch::Generators::Detail::throw_generator_exception("Couldn't read a single line");
} }
} }

View File

@ -56,6 +56,7 @@ set(INTERNAL_HEADERS
${SOURCES_DIR}/catch_errno_guard.h ${SOURCES_DIR}/catch_errno_guard.h
${SOURCES_DIR}/catch_exception_translator_registry.h ${SOURCES_DIR}/catch_exception_translator_registry.h
${SOURCES_DIR}/catch_fatal_condition.h ${SOURCES_DIR}/catch_fatal_condition.h
${SOURCES_DIR}/catch_generator_exception.hpp
${SOURCES_DIR}/catch_generators.hpp ${SOURCES_DIR}/catch_generators.hpp
${SOURCES_DIR}/catch_generators_generic.hpp ${SOURCES_DIR}/catch_generators_generic.hpp
${SOURCES_DIR}/catch_generators_specific.hpp ${SOURCES_DIR}/catch_generators_specific.hpp
@ -142,6 +143,7 @@ set(IMPL_SOURCES
${SOURCES_DIR}/catch_errno_guard.cpp ${SOURCES_DIR}/catch_errno_guard.cpp
${SOURCES_DIR}/catch_exception_translator_registry.cpp ${SOURCES_DIR}/catch_exception_translator_registry.cpp
${SOURCES_DIR}/catch_fatal_condition.cpp ${SOURCES_DIR}/catch_fatal_condition.cpp
${SOURCES_DIR}/catch_generator_exception.cpp
${SOURCES_DIR}/catch_generators.cpp ${SOURCES_DIR}/catch_generators.cpp
${SOURCES_DIR}/catch_interfaces_capture.cpp ${SOURCES_DIR}/catch_interfaces_capture.cpp
${SOURCES_DIR}/catch_interfaces_config.cpp ${SOURCES_DIR}/catch_interfaces_config.cpp

View File

@ -17,11 +17,9 @@ namespace Catch {
class Approx { class Approx {
private: private:
bool equalityComparisonImpl(double other) const; bool equalityComparisonImpl(double other) const;
// Validates the new margin (margin >= 0) //! Sets and validates the new margin (margin >= 0)
// out-of-line to avoid including stdexcept in the header
void setMargin(double margin); void setMargin(double margin);
// Validates the new epsilon (0 < epsilon < 1) //! Sets and validates the new epsilon (0 < epsilon < 1)
// out-of-line to avoid including stdexcept in the header
void setEpsilon(double epsilon); void setEpsilon(double epsilon);
public: public:

View File

@ -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 <catch2/catch_generator_exception.hpp>
namespace Catch {
const char* GeneratorException::what() const noexcept {
return m_msg;
}
} // end namespace Catch

View File

@ -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 <exception>
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

View File

@ -6,7 +6,8 @@
*/ */
#include <catch2/catch_generators.hpp> #include <catch2/catch_generators.hpp>
#include <catch2/catch_random_number_generator.h> #include <catch2/catch_enforce.h>
#include <catch2/catch_generator_exception.hpp>
#include <catch2/catch_interfaces_capture.h> #include <catch2/catch_interfaces_capture.h>
#include <limits> #include <limits>
@ -16,12 +17,16 @@ namespace Catch {
IGeneratorTracker::~IGeneratorTracker() {} IGeneratorTracker::~IGeneratorTracker() {}
const char* GeneratorException::what() const noexcept {
return m_msg;
}
namespace Generators { namespace Generators {
namespace Detail {
[[noreturn]]
void throw_generator_exception(char const* msg) {
Catch::throw_exception(GeneratorException{ msg });
}
} // end namespace Detail
GeneratorUntypedBase::~GeneratorUntypedBase() {} GeneratorUntypedBase::~GeneratorUntypedBase() {}
auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker& { auto acquireGeneratorTracker( SourceLineInfo const& lineInfo ) -> IGeneratorTracker& {

View File

@ -9,30 +9,25 @@
#include <catch2/catch_interfaces_generatortracker.h> #include <catch2/catch_interfaces_generatortracker.h>
#include <catch2/catch_common.h> #include <catch2/catch_common.h>
#include <catch2/catch_enforce.h>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <cassert> #include <cassert>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
#include <exception>
namespace Catch { 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 { namespace Generators {
namespace Detail {
//! Throws GeneratorException with the provided message
[[noreturn]]
void throw_generator_exception(char const * msg);
} // end namespace detail
template<typename T> template<typename T>
struct IGenerator : GeneratorUntypedBase { struct IGenerator : GeneratorUntypedBase {
virtual ~IGenerator() = default; virtual ~IGenerator() = default;

View File

@ -65,7 +65,7 @@ namespace Generators {
// filter. In that case we throw an exception. // filter. In that case we throw an exception.
auto has_initial_value = next(); auto has_initial_value = next();
if (!has_initial_value) { 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()); m_chunk.push_back(m_generator.get());
for (size_t i = 1; i < m_chunk_size; ++i) { for (size_t i = 1; i < m_chunk_size; ++i) {
if (!m_generator.next()) { 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()); m_chunk.push_back(m_generator.get());
} }

View File

@ -140,7 +140,7 @@ public:
template <typename InputIterator, typename InputSentinel> template <typename InputIterator, typename InputSentinel>
IteratorGenerator(InputIterator first, InputSentinel last):m_elems(first, last) { IteratorGenerator(InputIterator first, InputSentinel last):m_elems(first, last) {
if (m_elems.empty()) { if (m_elems.empty()) {
Catch::throw_exception(GeneratorException("IteratorGenerator received no valid values")); Detail::throw_generator_exception("IteratorGenerator received no valid values");
} }
} }

View File

@ -12,7 +12,6 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <stdexcept>
#if defined(CATCH_CONFIG_NEW_CAPTURE) #if defined(CATCH_CONFIG_NEW_CAPTURE)
#if defined(_MSC_VER) #if defined(_MSC_VER)

View File

@ -12,7 +12,6 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <stdexcept>
#include <memory> #include <memory>
#include <sstream> #include <sstream>

View File

@ -1,5 +1,6 @@
#include <catch2/catch_approx.h> #include <catch2/catch_approx.h>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/catch_generator_exception.hpp>
#include <catch2/catch_generators_generic.hpp> #include <catch2/catch_generators_generic.hpp>
#include <catch2/catch_generators_specific.hpp> #include <catch2/catch_generators_specific.hpp>

View File

@ -1,4 +1,5 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/catch_generator_exception.hpp>
#include <catch2/catch_generators_generic.hpp> #include <catch2/catch_generators_generic.hpp>
#include <catch2/catch_generators_specific.hpp> #include <catch2/catch_generators_specific.hpp>