Skip to content

Commit

Permalink
Outline GeneratorException from generators header
Browse files Browse the repository at this point in the history
  • Loading branch information
horenmar committed Feb 14, 2020
1 parent b2a6523 commit db1a046
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/301-Gen-MapTypeConversion.cpp
Expand Up @@ -18,7 +18,7 @@ class LineGenerator : public Catch::Generators::IGenerator<std::string> {
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");
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/catch2/catch_approx.h
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions 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 <catch2/catch_generator_exception.hpp>

namespace Catch {

const char* GeneratorException::what() const noexcept {
return m_msg;
}

} // end namespace Catch
29 changes: 29 additions & 0 deletions 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 <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
15 changes: 10 additions & 5 deletions src/catch2/catch_generators.cpp
Expand Up @@ -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>
Expand All @@ -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& {
Expand Down
17 changes: 6 additions & 11 deletions src/catch2/catch_generators.hpp
Expand Up @@ -9,29 +9,24 @@

#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 = "";
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<typename T>
struct IGenerator : GeneratorUntypedBase {
Expand Down
4 changes: 2 additions & 2 deletions src/catch2/catch_generators_generic.hpp
Expand Up @@ -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");
}
}
}
Expand Down Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/catch_generators_specific.hpp
Expand Up @@ -140,7 +140,7 @@ class IteratorGenerator final : public IGenerator<T> {
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");
}
}

Expand Down
1 change: 0 additions & 1 deletion src/catch2/catch_output_redirect.cpp
Expand Up @@ -12,7 +12,6 @@
#include <cstring>
#include <fstream>
#include <sstream>
#include <stdexcept>

#if defined(CATCH_CONFIG_NEW_CAPTURE)
#if defined(_MSC_VER)
Expand Down
1 change: 0 additions & 1 deletion src/catch2/catch_test_case_tracker.cpp
Expand Up @@ -12,7 +12,6 @@

#include <algorithm>
#include <cassert>
#include <stdexcept>
#include <memory>
#include <sstream>

Expand Down
1 change: 1 addition & 0 deletions tests/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp
@@ -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>

Expand Down
1 change: 1 addition & 0 deletions tests/SelfTest/UsageTests/Generators.tests.cpp
@@ -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>

Expand Down

0 comments on commit db1a046

Please sign in to comment.