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() {
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");
}
}

View File

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

View File

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

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_random_number_generator.h>
#include <catch2/catch_enforce.h>
#include <catch2/catch_generator_exception.hpp>
#include <catch2/catch_interfaces_capture.h>
#include <limits>
@ -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& {

View File

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

View File

@ -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());
}

View File

@ -140,7 +140,7 @@ public:
template <typename InputIterator, typename InputSentinel>
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");
}
}

View File

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

View File

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

View File

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

View File

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