mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
Replace most naked throws with macros from catch_enforce.h
This is a first step towards support a no-exceptions mode
This commit is contained in:
parent
ef9150fe6f
commit
86da2846af
@ -7,10 +7,10 @@
|
||||
*/
|
||||
|
||||
#include "catch_approx.h"
|
||||
#include "catch_enforce.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace {
|
||||
|
||||
@ -56,23 +56,16 @@ namespace Detail {
|
||||
}
|
||||
|
||||
void Approx::setMargin(double margin) {
|
||||
if (margin < 0) {
|
||||
throw std::domain_error
|
||||
("Invalid Approx::margin: " +
|
||||
Catch::Detail::stringify(margin) +
|
||||
", Approx::Margin has to be non-negative.");
|
||||
|
||||
}
|
||||
CATCH_ENFORCE(margin >= 0,
|
||||
"Invalid Approx::margin: " << margin << '.'
|
||||
<< " Approx::Margin has to be non-negative.");
|
||||
m_margin = margin;
|
||||
}
|
||||
|
||||
void Approx::setEpsilon(double epsilon) {
|
||||
if (epsilon < 0 || epsilon > 1.0) {
|
||||
throw std::domain_error
|
||||
("Invalid Approx::epsilon: " +
|
||||
Catch::Detail::stringify(epsilon) +
|
||||
", Approx::epsilon has to be between 0 and 1");
|
||||
}
|
||||
CATCH_ENFORCE(epsilon >= 0 && epsilon <= 1.0,
|
||||
"Invalid Approx::epsilon: " << epsilon << '.'
|
||||
<< " Approx::epsilon has to be in [0, 1]");
|
||||
m_epsilon = epsilon;
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,8 @@
|
||||
throw CATCH_PREPARE_EXCEPTION( std::logic_error, CATCH_INTERNAL_LINEINFO << ": Internal Catch error: " << msg);
|
||||
#define CATCH_ERROR( msg ) \
|
||||
throw CATCH_PREPARE_EXCEPTION( std::domain_error, msg )
|
||||
#define CATCH_RUNTIME_ERROR( msg ) \
|
||||
throw CATCH_PREPARE_EXCEPTION( std::runtime_error, msg )
|
||||
#define CATCH_ENFORCE( condition, msg ) \
|
||||
do{ if( !(condition) ) CATCH_ERROR( msg ); } while(false)
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include "catch_interfaces_generatortracker.h"
|
||||
#include "catch_common.h"
|
||||
#include "catch_enforce.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
@ -76,7 +77,7 @@ namespace Generators {
|
||||
template<typename T>
|
||||
struct NullGenerator : IGenerator<T> {
|
||||
auto get( size_t ) const -> T override {
|
||||
throw std::logic_error("A Null Generator should always be empty" );
|
||||
CATCH_INTERNAL_ERROR("A Null Generator is always empty");
|
||||
}
|
||||
};
|
||||
|
||||
@ -198,7 +199,7 @@ namespace Generators {
|
||||
if( index < sizes )
|
||||
return gen[localIndex];
|
||||
}
|
||||
throw std::out_of_range("index out of range");
|
||||
CATCH_INTERNAL_ERROR("Index '" << index << "' is out of range (" << sizes << ')');
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -6,13 +6,13 @@
|
||||
*/
|
||||
|
||||
#include "catch_matchers_floating.h"
|
||||
#include "catch_enforce.h"
|
||||
#include "catch_to_string.hpp"
|
||||
#include "catch_tostring.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Catch {
|
||||
namespace Matchers {
|
||||
@ -81,9 +81,8 @@ namespace Matchers {
|
||||
namespace Floating {
|
||||
WithinAbsMatcher::WithinAbsMatcher(double target, double margin)
|
||||
:m_target{ target }, m_margin{ margin } {
|
||||
if (m_margin < 0) {
|
||||
throw std::domain_error("Allowed margin difference has to be >= 0");
|
||||
}
|
||||
CATCH_ENFORCE(margin >= 0, "Invalid margin: " << margin << '.'
|
||||
<< " Margin has to be non-negative.");
|
||||
}
|
||||
|
||||
// Performs equivalent check of std::fabs(lhs - rhs) <= margin
|
||||
@ -99,9 +98,8 @@ namespace Floating {
|
||||
|
||||
WithinUlpsMatcher::WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType)
|
||||
:m_target{ target }, m_ulps{ ulps }, m_type{ baseType } {
|
||||
if (m_ulps < 0) {
|
||||
throw std::domain_error("Allowed ulp difference has to be >= 0");
|
||||
}
|
||||
CATCH_ENFORCE(ulps >= 0, "Invalid ULP setting: " << ulps << '.'
|
||||
<< " ULPs have to be non-negative.");
|
||||
}
|
||||
|
||||
#if defined(__clang__)
|
||||
@ -117,7 +115,7 @@ namespace Floating {
|
||||
case FloatingPointKind::Double:
|
||||
return almostEqualUlps<double>(matchee, m_target, m_ulps);
|
||||
default:
|
||||
throw std::domain_error("Unknown FloatingPointKind value");
|
||||
CATCH_INTERNAL_ERROR( "Unknown FloatingPointKind value" );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "catch_output_redirect.h"
|
||||
|
||||
|
||||
#include "catch_enforce.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
@ -56,21 +55,21 @@ namespace Catch {
|
||||
#if defined(_MSC_VER)
|
||||
TempFile::TempFile() {
|
||||
if (tmpnam_s(m_buffer)) {
|
||||
throw std::runtime_error("Could not get a temp filename");
|
||||
CATCH_RUNTIME_ERROR("Could not get a temp filename");
|
||||
}
|
||||
if (fopen_s(&m_file, m_buffer, "w")) {
|
||||
char buffer[100];
|
||||
if (strerror_s(buffer, errno)) {
|
||||
throw std::runtime_error("Could not translate errno to string");
|
||||
CATCH_RUNTIME_ERROR("Could not translate errno to a string");
|
||||
}
|
||||
throw std::runtime_error("Could not open the temp file: " + std::string(m_buffer) + buffer);
|
||||
CATCH_RUNTIME_ERROR("Coul dnot open the temp file: '" << m_buffer << "' because: " << buffer);
|
||||
}
|
||||
}
|
||||
#else
|
||||
TempFile::TempFile() {
|
||||
m_file = std::tmpfile();
|
||||
if (!m_file) {
|
||||
throw std::runtime_error("Could not create a temp file.");
|
||||
CATCH_RUNTIME_ERROR("Could not create a temp file.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#ifndef TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
|
||||
|
||||
#include "../internal/catch_enforce.h"
|
||||
#include "../internal/catch_interfaces_reporter.h"
|
||||
|
||||
#include <algorithm>
|
||||
@ -33,7 +34,7 @@ namespace Catch {
|
||||
{
|
||||
m_reporterPrefs.shouldRedirectStdOut = false;
|
||||
if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) )
|
||||
throw std::domain_error( "Verbosity level not supported by this reporter" );
|
||||
CATCH_ERROR( "Verbosity level not supported by this reporter" );
|
||||
}
|
||||
|
||||
ReporterPreferences getPreferences() const override {
|
||||
@ -148,7 +149,7 @@ namespace Catch {
|
||||
{
|
||||
m_reporterPrefs.shouldRedirectStdOut = false;
|
||||
if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) )
|
||||
throw std::domain_error( "Verbosity level not supported by this reporter" );
|
||||
CATCH_ERROR( "Verbosity level not supported by this reporter" );
|
||||
}
|
||||
~CumulativeReporterBase() override = default;
|
||||
|
||||
|
@ -97,12 +97,12 @@ namespace Catch {
|
||||
case ResultWas::Ok:
|
||||
case ResultWas::Info:
|
||||
case ResultWas::Warning:
|
||||
throw std::domain_error( "Internal error in TeamCity reporter" );
|
||||
CATCH_ERROR( "Internal error in TeamCity reporter" );
|
||||
// These cases are here to prevent compiler warnings
|
||||
case ResultWas::Unknown:
|
||||
case ResultWas::FailureBit:
|
||||
case ResultWas::Exception:
|
||||
throw std::domain_error( "Not implemented" );
|
||||
CATCH_ERROR( "Not implemented" );
|
||||
}
|
||||
if( assertionStats.infoMessages.size() == 1 )
|
||||
msg << " with message:";
|
||||
|
Loading…
Reference in New Issue
Block a user