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

Made everything used in test case registration noexcept

- this prevents warnings about startup-time exceptions
Commit
e01ed48a70925a6b18f70882f0065a42a560782b
Author
Phil Nash ·
Parents
989222eceb34

Files · Download .tar.gz

Changes

 include/internal/catch_common.cpp                     | 10 +++++-----
 include/internal/catch_common.h                       | 10 +++++-----
 include/internal/catch_interfaces_registry_hub.h      |  2 +-
 include/internal/catch_registry_hub.cpp               |  2 +-
 include/internal/catch_startup_exception_registry.cpp | 12 +++++++++---
 include/internal/catch_startup_exception_registry.h   |  4 ++--
 include/internal/catch_test_case_registry_impl.hpp    | 14 ++++++--------
 include/internal/catch_test_registry.hpp              | 12 ++++++------
 8 files changed, 35 insertions(+), 31 deletions(-)

diff --git a/include/internal/catch_common.cpp b/include/internal/catch_common.cpp
index 6cd1b46d..a35ef93a 100644
--- a/include/internal/catch_common.cpp
+++ b/include/internal/catch_common.cpp
@@ -75,18 +75,18 @@ namespace Catch {
         return os;
     }
 
-    SourceLineInfo::SourceLineInfo() : file(""), line( 0 ){}
-    SourceLineInfo::SourceLineInfo( char const* _file, std::size_t _line )
+    SourceLineInfo::SourceLineInfo() noexcept : file(""), line( 0 ){}
+    SourceLineInfo::SourceLineInfo( char const* _file, std::size_t _line ) noexcept
     :   file( _file ),
         line( _line )
     {}
-    bool SourceLineInfo::empty() const {
+    bool SourceLineInfo::empty() const noexcept {
         return file[0] == '\0';
     }
-    bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const {
+    bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const noexcept {
         return line == other.line && (file == other.file || std::strcmp(file, other.file) == 0);
     }
-    bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const {
+    bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const noexcept {
         return line < other.line || ( line == other.line && (std::strcmp(file, other.file) < 0));
     }
 
diff --git a/include/internal/catch_common.h b/include/internal/catch_common.h
index f86af403..6f037c71 100644
--- a/include/internal/catch_common.h
+++ b/include/internal/catch_common.h
@@ -77,17 +77,17 @@ namespace Catch {
 
     struct SourceLineInfo {
 
-        SourceLineInfo();
-        SourceLineInfo( char const* _file, std::size_t _line );
+        SourceLineInfo() noexcept;
+        SourceLineInfo( char const* _file, std::size_t _line ) noexcept;
 
         SourceLineInfo(SourceLineInfo const& other)          = default;
         SourceLineInfo( SourceLineInfo && )                  = default;
         SourceLineInfo& operator = ( SourceLineInfo const& ) = default;
         SourceLineInfo& operator = ( SourceLineInfo && )     = default;
 
-        bool empty() const;
-        bool operator == ( SourceLineInfo const& other ) const;
-        bool operator < ( SourceLineInfo const& other ) const;
+        bool empty() const noexcept;
+        bool operator == ( SourceLineInfo const& other ) const noexcept;
+        bool operator < ( SourceLineInfo const& other ) const noexcept;
 
         char const* file;
         std::size_t line;
diff --git a/include/internal/catch_interfaces_registry_hub.h b/include/internal/catch_interfaces_registry_hub.h
index 8934d008..f71178be 100644
--- a/include/internal/catch_interfaces_registry_hub.h
+++ b/include/internal/catch_interfaces_registry_hub.h
@@ -46,7 +46,7 @@ namespace Catch {
         virtual void registerTest( TestCase const& testInfo ) = 0;
         virtual void registerTranslator( const IExceptionTranslator* translator ) = 0;
         virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0;
-        virtual void registerStartupException( std::exception_ptr const& exception ) = 0;
+        virtual void registerStartupException( std::exception_ptr const& exception ) noexcept = 0;
     };
 
     IRegistryHub& getRegistryHub();
diff --git a/include/internal/catch_registry_hub.cpp b/include/internal/catch_registry_hub.cpp
index 4d34311c..12af6e38 100644
--- a/include/internal/catch_registry_hub.cpp
+++ b/include/internal/catch_registry_hub.cpp
@@ -56,7 +56,7 @@ namespace Catch {
             virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) override {
                 m_tagAliasRegistry.add( alias, tag, lineInfo );
             }
-            virtual void registerStartupException( std::exception_ptr const& exception ) override {
+            virtual void registerStartupException( std::exception_ptr const& exception ) noexcept override {
                 m_exceptionRegistry.add(exception);
             }
 
diff --git a/include/internal/catch_startup_exception_registry.cpp b/include/internal/catch_startup_exception_registry.cpp
index 088b6446..d58d18a5 100644
--- a/include/internal/catch_startup_exception_registry.cpp
+++ b/include/internal/catch_startup_exception_registry.cpp
@@ -9,11 +9,17 @@
 #include "catch_startup_exception_registry.h"
 
 namespace Catch {
-    void StartupExceptionRegistry::add( std::exception_ptr const& exception ) {
-        m_exceptions.push_back(exception);
+    void StartupExceptionRegistry::add( std::exception_ptr const& exception ) noexcept {
+        try {
+            m_exceptions.push_back(exception);
+        }
+        catch(...) {
+            // If we run out of memory during start-up there's really not a lot more we can do about it
+            std::terminate();
+        }
     }
 
-    std::vector<std::exception_ptr> const& StartupExceptionRegistry::getExceptions() const {
+    std::vector<std::exception_ptr> const& StartupExceptionRegistry::getExceptions() const noexcept {
         return m_exceptions;
     }
 
diff --git a/include/internal/catch_startup_exception_registry.h b/include/internal/catch_startup_exception_registry.h
index ece5e6ca..feb56601 100644
--- a/include/internal/catch_startup_exception_registry.h
+++ b/include/internal/catch_startup_exception_registry.h
@@ -16,8 +16,8 @@ namespace Catch {
 
     class StartupExceptionRegistry {
     public:
-        void add(std::exception_ptr const& exception);
-        std::vector<std::exception_ptr> const& getExceptions() const;
+        void add(std::exception_ptr const& exception) noexcept;
+        std::vector<std::exception_ptr> const& getExceptions() const noexcept;
     private:
         std::vector<std::exception_ptr> m_exceptions;
     };
diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp
index 6874107c..5bb32f5e 100644
--- a/include/internal/catch_test_case_registry_impl.hpp
+++ b/include/internal/catch_test_case_registry_impl.hpp
@@ -46,10 +46,8 @@ namespace Catch {
                 std::sort( sorted.begin(), sorted.end() );
                 break;
             case RunTests::InRandomOrder:
-                {
-                    seedRng( config );
-                    RandomNumberGenerator::shuffle( sorted );
-                }
+                seedRng( config );
+                RandomNumberGenerator::shuffle( sorted );
                 break;
             case RunTests::InDeclarationOrder:
                 // already in declaration order
@@ -128,14 +126,14 @@ namespace Catch {
     class TestInvokerAsFunction : public ITestInvoker {
         void(*m_testAsFunction)();
     public:
-        TestInvokerAsFunction( void(*testAsFunction)() ) : m_testAsFunction( testAsFunction ) {}
+        TestInvokerAsFunction( void(*testAsFunction)() ) noexcept : m_testAsFunction( testAsFunction ) {}
 
         void invoke() const override {
             m_testAsFunction();
         }
     };
-    auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker* {
-        return new TestInvokerAsFunction( testAsFunction );
+    auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker* {
+        return new(std::nothrow) TestInvokerAsFunction( testAsFunction );
     }
 
 
@@ -154,7 +152,7 @@ namespace Catch {
 
     ///////////////////////////////////////////////////////////////////////////
 
-    AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags )
+    AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags ) noexcept
     {
         try {
             getMutableRegistryHub()
diff --git a/include/internal/catch_test_registry.hpp b/include/internal/catch_test_registry.hpp
index da0fbc2e..c8538a2c 100644
--- a/include/internal/catch_test_registry.hpp
+++ b/include/internal/catch_test_registry.hpp
@@ -19,7 +19,7 @@ template<typename C>
 class TestInvokerAsMethod : public ITestInvoker {
     void (C::*m_testAsMethod)();
 public:
-    TestInvokerAsMethod( void (C::*testAsMethod)() ) : m_testAsMethod( testAsMethod ) {}
+    TestInvokerAsMethod( void (C::*testAsMethod)() ) noexcept : m_testAsMethod( testAsMethod ) {}
 
     void invoke() const override {
         C obj;
@@ -27,23 +27,23 @@ public:
     }
 };
 
-auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker*;
+auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker*;
 
 template<typename C>
-auto makeTestInvoker( void (C::*testAsMethod)() ) -> ITestInvoker* {
-    return new TestInvokerAsMethod<C>( testAsMethod );
+auto makeTestInvoker( void (C::*testAsMethod)() ) noexcept -> ITestInvoker* {
+    return new(std::nothrow) TestInvokerAsMethod<C>( testAsMethod );
 }
 
 struct NameAndTags {
 
-    NameAndTags( StringRef name_ = "", StringRef tags_ = "" ) : name( name_ ), tags( tags_ ) {}
+    NameAndTags( StringRef name_ = "", StringRef tags_ = "" ) noexcept : name( name_ ), tags( tags_ ) {}
 
     StringRef name;
     StringRef tags;
 };
 
 struct AutoReg : NonCopyable {
-    AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags );
+    AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags ) noexcept;
     ~AutoReg();
 };