Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Replace most naked throws with macros from catch_enforce.h

This is a first step towards support a no-exceptions mode
Commit
86da2846af20666d85effcecd4c90077477c1ce6
Author
Martin Hořeňovský ·
Parents
ef9150fe6f3c

Files · Download .tar.gz

Changes

 include/internal/catch_approx.cpp             | 21 +++++++--------------
 include/internal/catch_enforce.h              |  2 ++
 include/internal/catch_generators.hpp         |  5 +++--
 include/internal/catch_matchers_floating.cpp  | 14 ++++++--------
 include/internal/catch_output_redirect.cpp    | 11 +++++------
 include/reporters/catch_reporter_bases.hpp    |  5 +++--
 include/reporters/catch_reporter_teamcity.hpp |  4 ++--
 7 files changed, 28 insertions(+), 34 deletions(-)

diff --git a/include/internal/catch_approx.cpp b/include/internal/catch_approx.cpp
index 13cc77bb..a7d7111c 100644
--- a/include/internal/catch_approx.cpp
+++ b/include/internal/catch_approx.cpp
@@ -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;
     }
 
diff --git a/include/internal/catch_enforce.h b/include/internal/catch_enforce.h
index 513dcf1c..58cbe134 100644
--- a/include/internal/catch_enforce.h
+++ b/include/internal/catch_enforce.h
@@ -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)
 
diff --git a/include/internal/catch_generators.hpp b/include/internal/catch_generators.hpp
index 74fa5922..4a446be1 100644
--- a/include/internal/catch_generators.hpp
+++ b/include/internal/catch_generators.hpp
@@ -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 << ')');
         }
     };
 
diff --git a/include/internal/catch_matchers_floating.cpp b/include/internal/catch_matchers_floating.cpp
index 455361eb..31206986 100644
--- a/include/internal/catch_matchers_floating.cpp
+++ b/include/internal/catch_matchers_floating.cpp
@@ -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" );
         }
     }
 
diff --git a/include/internal/catch_output_redirect.cpp b/include/internal/catch_output_redirect.cpp
index 5965b7fc..86b0f6a0 100644
--- a/include/internal/catch_output_redirect.cpp
+++ b/include/internal/catch_output_redirect.cpp
@@ -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.");
         }
     }
 
diff --git a/include/reporters/catch_reporter_bases.hpp b/include/reporters/catch_reporter_bases.hpp
index f198d214..ae1618fe 100644
--- a/include/reporters/catch_reporter_bases.hpp
+++ b/include/reporters/catch_reporter_bases.hpp
@@ -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;
 
diff --git a/include/reporters/catch_reporter_teamcity.hpp b/include/reporters/catch_reporter_teamcity.hpp
index dbd0db53..eca2885c 100644
--- a/include/reporters/catch_reporter_teamcity.hpp
+++ b/include/reporters/catch_reporter_teamcity.hpp
@@ -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:";