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

Moved all Catch::Main()s into new Session class

- renamed them run()
- moved cleanUp call into destructor
Commit
c1196b6245f12237b441f6018562353e9c095977
Author
Phil Nash ·
Parents
c57ebc84b270

Files · Download .tar.gz

Changes

 include/catch_runner.hpp                | 127 +++++++++++++++++---------------
 include/internal/catch_default_main.hpp |   6 +-
 2 files changed, 71 insertions(+), 62 deletions(-)

diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp
index e431e50f..6d6c9f47 100644
--- a/include/catch_runner.hpp
+++ b/include/catch_runner.hpp
@@ -108,30 +108,6 @@ namespace Catch {
         std::set<TestCase> m_testsAlreadyRun;
     };
 
-    inline int Main( Ptr<Config> const& config ) {
-        int result = 0;
-        try
-        {
-            Runner runner( config );
-
-            // Handle list request
-            if( list( config ) ) {
-                Catch::cleanUp();
-                return 0;
-            }
-
-            result = static_cast<int>( runner.runTests().assertions.failed );
-
-        }
-        catch( std::exception& ex ) {
-            std::cerr << ex.what() << std::endl;
-            result = (std::numeric_limits<int>::max)();
-        }
-
-        Catch::cleanUp();
-        return result;
-    }
-
     inline void showHelp( std::string const& processName ) {
         Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
 
@@ -145,47 +121,80 @@ namespace Catch {
         cli.usage( std::cout, processName );
         std::cout << "\nFor more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line\n" << std::endl;
     }
-    inline Ptr<Config> processConfig( int argc, char* const argv[], ConfigData& configData ) {
-        Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
-        std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData );
-        if( !unused.empty() ) {
-            std::vector<Clara::Parser::Token>::const_iterator
-                it = unused.begin(),
-                itEnd = unused.end();
-            std::string msg;
-            for(; it != itEnd; ++it )
-                msg += "  unrecognised option: " + it->data + "\n";
-            throw std::runtime_error( msg.substr( 0, msg.size()-1 ) );
-        }
-        Ptr<Config> config = new Config( configData );
-        return config;        
-    }
-    inline Ptr<Config> processConfig( int argc, char* const argv[] ) {
-        ConfigData configData;
-        return processConfig( argc, argv, configData );
-    }
 
-    inline int Main( int argc, char* const argv[], ConfigData configData = ConfigData() ) {
+    class Session {
+        static bool alreadyInstantiated;
+        
+    public:
+        Session() {
+            if( alreadyInstantiated ) {
+                std::string msg = "Only one instance of Catch::Session can ever be used";
+                std::cerr << msg << std::endl;
+                throw std::logic_error( msg );
+            }
+            alreadyInstantiated = true;
+        }
+        ~Session() {
+            Catch::cleanUp();
+        }
+        
+        int run( Ptr<Config> const& config ) {
+            try
+            {
+                Runner runner( config );
 
-        Ptr<Config> config;
+                // Handle list request
+                if( list( config ) )
+                    return 0;
 
-        try {
-            config = processConfig( argc, argv, configData );
-            if( config->showHelp() ) {
-                showHelp( config->getProcessName() );
-                Catch::cleanUp();        
-                return 0;
+                return static_cast<int>( runner.runTests().assertions.failed );
+            }
+            catch( std::exception& ex ) {
+                std::cerr << ex.what() << std::endl;
+                return (std::numeric_limits<int>::max)();
             }
         }
-        catch( std::exception& ex ) {
-            std::cerr   << "\nError in input:\n"
-                        << "  " << ex.what() << "\n\n";
-            makeCommandLineParser().usage( std::cout, configData.processName );
-            Catch::cleanUp();
-            return (std::numeric_limits<int>::max)();
+        
+        Ptr<Config> processConfig( int argc, char* const argv[], ConfigData& configData ) {
+            Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
+            std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData );
+            if( !unused.empty() ) {
+                std::vector<Clara::Parser::Token>::const_iterator
+                    it = unused.begin(),
+                    itEnd = unused.end();
+                std::string msg;
+                for(; it != itEnd; ++it )
+                    msg += "  unrecognised option: " + it->data + "\n";
+                throw std::runtime_error( msg.substr( 0, msg.size()-1 ) );
+            }
+            Ptr<Config> config = new Config( configData );
+            return config;        
         }
-        return Main( config );
-    }
+        Ptr<Config> processConfig( int argc, char* const argv[] ) {
+            ConfigData configData;
+            return processConfig( argc, argv, configData );
+        }        
+        int run( int argc, char* const argv[], ConfigData configData = ConfigData() ) {
+
+            Ptr<Config> config;
+
+            try {
+                config = processConfig( argc, argv, configData );
+                if( config->showHelp() ) {
+                    showHelp( config->getProcessName() );
+                    return 0;
+                }
+            }
+            catch( std::exception& ex ) {
+                std::cerr   << "\nError in input:\n"
+                            << "  " << ex.what() << "\n\n";
+                makeCommandLineParser().usage( std::cout, configData.processName );
+                return (std::numeric_limits<int>::max)();
+            }
+            return run( config );
+        }
+    };
+    bool Session::alreadyInstantiated = false;
     
 } // end namespace Catch
 
diff --git a/include/internal/catch_default_main.hpp b/include/internal/catch_default_main.hpp
index 3d7e423a..5844f350 100644
--- a/include/internal/catch_default_main.hpp
+++ b/include/internal/catch_default_main.hpp
@@ -11,8 +11,8 @@
 #ifndef __OBJC__
 
 // Standard C/C++ main entry point
-int main (int argc, char * const argv[]) {
-    return Catch::Main( argc, argv );    
+int main (int argc, char * const argv[]) {    
+    return Catch::Session().run( argc, argv );
 }
 
 #else // __OBJC__
@@ -24,7 +24,7 @@ int main (int argc, char * const argv[]) {
 #endif
     
     Catch::registerTestMethods();    
-    int result = Catch::Main( argc, (char* const*)argv );
+    int result = Catch::Session().run( argc, (char* const*)argv );
     
 #if !CATCH_ARC_ENABLED
     [pool drain];