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

Added support for Objective-C exceptions

Commit
35b31fa78a74abc4f9e3ef31dd140b03935ca20c
Author
Phil Nash ·
Parents
371db8b42fb2

Files · Download .tar.gz

Changes

 include/internal/catch_exception_translator_registry.hpp | 14 ++++++++++++++
 projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm         | 12 ++++++++++++
 single_include/catch.hpp                                 | 14 ++++++++++++++
 3 files changed, 40 insertions(+)

diff --git a/include/internal/catch_exception_translator_registry.hpp b/include/internal/catch_exception_translator_registry.hpp
index e80142f8..55a20713 100644
--- a/include/internal/catch_exception_translator_registry.hpp
+++ b/include/internal/catch_exception_translator_registry.hpp
@@ -10,6 +10,10 @@
 
 #include "catch_interfaces_exception.h"
 
+#ifdef __OBJC__
+#import "Foundation/Foundation.h"
+#endif
+
 namespace Catch {
 
     class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
@@ -24,7 +28,17 @@ namespace Catch {
         
         virtual std::string translateActiveException() const {
             try {
+#ifdef __OBJC__
+                // In Objective-C try objective-c exceptions first
+                @try {
+                    throw;
+                }
+                @catch (NSException *exception) {
+                    return toString( [exception description] );
+                }
+#else
                 throw;
+#endif
             }
             catch( std::exception& ex ) {
                 return ex.what();
diff --git a/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm b/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm
index 149e51ff..ed84a5be 100644
--- a/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm
+++ b/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm
@@ -41,6 +41,18 @@ OC_TEST_CASE( "OCTest/test2", "This is another test case" )
     
     REQUIRE( obj.int_val == 2 );
 }
+
+OC_TEST_CASE( "OCTest/throws/objc", "throws an Objective-C exception" )
+{
+    @throw [[NSException alloc] initWithName: NSGenericException
+                                      reason: @"Objective-C exception"
+                                    userInfo: nil];
+}
+OC_TEST_CASE( "OCTest/throws/stdc++", "throws a std c++ exception" )
+{
+    throw std::domain_error( "std C++ exception" );
+}
+
 ///////////////////////////////////////////////////////////////////////////
 template<typename T>
 void useObject( const T& object ){}
diff --git a/single_include/catch.hpp b/single_include/catch.hpp
index 471b76fb..0f3a68de 100644
--- a/single_include/catch.hpp
+++ b/single_include/catch.hpp
@@ -3157,6 +3157,10 @@ namespace Catch {
 
 // #included from: catch_exception_translator_registry.hpp
 
+#ifdef __OBJC__
+#import "Foundation/Foundation.h"
+#endif
+
 namespace Catch {
 
     class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
@@ -3171,7 +3175,17 @@ namespace Catch {
 
         virtual std::string translateActiveException() const {
             try {
+#ifdef __OBJC__
+                // In Objective-C try objective-c exceptions first
+                @try {
+                    throw;
+                }
+                @catch (NSException *exception) {
+                    return toString( [exception description] );
+                }
+#else
                 throw;
+#endif
             }
             catch( std::exception& ex ) {
                 return ex.what();