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
First cut of new filtering mechanism
- Commit
56d5c429121cfa6770961d22a9ef8191a30867ce- Author
- Phil Nash ·
- Parents
b354da9ab9f4
Changes
include/catch_runner.hpp | 174 +- include/internal/catch_commandline.hpp | 29 +- include/internal/catch_common.h | 4 + include/internal/catch_config.hpp | 15 +- include/internal/catch_impl.hpp | 8 + include/internal/catch_interfaces_runner.h | 12 - include/internal/catch_interfaces_testcase.h | 6 +- include/internal/catch_list.hpp | 30 +- include/internal/catch_ptr.hpp | 13 +- include/internal/catch_runner_impl.hpp | 42 +- include/internal/catch_test_case_registry_impl.hpp | 16 +- include/internal/catch_test_spec.h | 79 +- projects/SelfTest/TestMain.cpp | 127 +- single_include/catch.hpp | 4439 ++++++++++---------- 14 files changed, 2582 insertions(+), 2412 deletions(-) diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp index 6a4dca07..5bda2c9d 100644 --- a/include/catch_runner.hpp +++ b/include/catch_runner.hpp @@ -11,6 +11,7 @@ #include "internal/catch_commandline.hpp" #include "internal/catch_list.hpp" #include "internal/catch_runner_impl.hpp" +#include "internal/catch_test_spec.h" #include <fstream> #include <stdlib.h> @@ -18,87 +19,136 @@ namespace Catch { - inline int resolveStream( std::ofstream& ofs, Config& configWrapper ) { - const ConfigData& config = configWrapper.data(); - - if( !config.stream.empty() ) { - if( config.stream[0] == '%' ) - configWrapper.useStream( config.stream.substr( 1 ) ); - else - configWrapper.setFilename( config.stream ); + class Runner2 { // This will become Runner when Runner becomes Context + + public: + Runner2( Config& configWrapper ) + : m_configWrapper( configWrapper ), + m_config( configWrapper.data() ) + { + resolveStream(); + makeReporter(); } - // Open output file, if specified - if( !config.outputFilename.empty() ) { - ofs.open( config.outputFilename.c_str() ); - if( ofs.fail() ) { - std::cerr << "Unable to open file: '" << config.outputFilename << "'" << std::endl; - return (std::numeric_limits<int>::max)(); + + Totals runTests() { + + std::vector<TestCaseFilters> filterGroups = m_config.filters; + if( filterGroups.empty() ) { + TestCaseFilters filterGroup( "" ); + filterGroup.addFilter( TestCaseFilter( "./*", IfFilterMatches::ExcludeTests ) ); + filterGroups.push_back( filterGroup ); } - configWrapper.setStreamBuf( ofs.rdbuf() ); - } - return 0; - } - - inline Ptr<IReporter> makeReporter( Config& configWrapper ) { - const ConfigData& config = configWrapper.data(); - - std::string reporterName = config.reporter.empty() - ? "basic" - : config.reporter; - ReporterConfig reporterConfig( config.name, configWrapper.stream(), config.includeWhichResults == Include::SuccessfulResults ); + Runner context( m_configWrapper, m_reporter ); // This Runner will be renamed Context + Totals totals; - Ptr<IReporter> reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig ); - if( !reporter ) - std::cerr << "No reporter registered with name: '" << reporterName << "'" << std::endl; - return reporter; - } + std::vector<TestCaseFilters>::const_iterator it = filterGroups.begin(); + std::vector<TestCaseFilters>::const_iterator itEnd = filterGroups.end(); + for(; it != itEnd; ++it ) { + m_reporter->StartGroup( it->getName() ); + runTestsForGroup( context, *it ); + if( context.aborting() ) + m_reporter->Aborted(); + m_reporter->EndGroup( it->getName(), totals ); + } + return totals; + } - inline int Main( Config& configWrapper ) { + Totals runTestsForGroup( Runner& context, const TestCaseFilters& filterGroup ) { + Totals totals; + std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end(); + int testsRunForGroup = 0; + for(; it != itEnd; ++it ) { + if( filterGroup.shouldInclude( *it ) ) { + testsRunForGroup++; + if( m_testsAlreadyRun.find( *it ) == m_testsAlreadyRun.end() ) { + + if( context.aborting() ) + break; + + totals += context.runTest( *it ); + m_testsAlreadyRun.insert( *it ); + } + } + } + if( testsRunForGroup == 0 ) + std::cerr << "\n[No test cases matched with: " << filterGroup.getName() << "]" << std::endl; + return totals; + + } + + private: + void resolveStream() { + if( !m_config.stream.empty() ) { + if( m_config.stream[0] == '%' ) + m_configWrapper.useStream( m_config.stream.substr( 1 ) ); + else + m_configWrapper.setFilename( m_config.stream ); + } + // Open output file, if specified + if( !m_config.outputFilename.empty() ) { + m_ofs.open( m_config.outputFilename.c_str() ); + if( m_ofs.fail() ) { + std::ostringstream oss; + oss << "Unable to open file: '" << m_config.outputFilename << "'"; + throw std::domain_error( oss.str() ); + } + m_configWrapper.setStreamBuf( m_ofs.rdbuf() ); + } + } + void makeReporter() { + std::string reporterName = m_config.reporter.empty() + ? "basic" + : m_config.reporter; - std::ofstream ofs; - int result = resolveStream( ofs, configWrapper ); - if( result != 0 ) - return result; + ReporterConfig reporterConfig( m_config.name, m_configWrapper.stream(), m_config.includeWhichResults == Include::SuccessfulResults ); - Ptr<IReporter> reporter = makeReporter( configWrapper ); - if( !reporter ) - return (std::numeric_limits<int>::max)(); + m_reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig ); + if( !m_reporter ) { + std::ostringstream oss; + oss << "No reporter registered with name: '" << reporterName << "'"; + throw std::domain_error( oss.str() ); + } + } - const ConfigData& config = configWrapper.data(); + private: + Config& m_configWrapper; + const ConfigData& m_config; + std::ofstream m_ofs; + Ptr<IReporter> m_reporter; + std::set<TestCaseInfo> m_testsAlreadyRun; + }; - // Handle list request - if( config.listSpec != List::None ) - return List( config ); - - // Scope here for the Runner so it can use the context before it is cleaned-up + inline int Main( Config& configWrapper ) { + int result = 0; + try { - Runner runner( configWrapper, reporter ); + Runner2 runner( configWrapper ); - Totals totals; - // Run test specs specified on the command line - or default to all - if( config.testSpecs.empty() ) { - totals = runner.runAllNonHidden(); - } - else { - std::vector<std::string>::const_iterator it = config.testSpecs.begin(); - std::vector<std::string>::const_iterator itEnd = config.testSpecs.end(); - for(; it != itEnd; ++it ) { - Totals groupTotals = runner.runMatching( *it ); - if( groupTotals.testCases.total() == 0 ) - std::cerr << "\n[No test cases matched with: " << *it << "]" << std::endl; - totals += groupTotals; - } + const ConfigData& config = configWrapper.data(); + + // Handle list request + if( config.listSpec != List::None ) { + List( config ); + return 0; } - result = static_cast<int>( totals.assertions.failed ); + + 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 showUsage( std::ostream& os ) { os << "\t-?, -h, --help\n" - << "\t-l, --list <tests | reporters> [xml]\n" + << "\t-l, --list [all | tests | reporters [xml]]\n" << "\t-t, --test <testspec> [<testspec>...]\n" << "\t-r, --reporter <reporter name>\n" << "\t-o, --out <file name>|<%stream name>\n" diff --git a/include/internal/catch_commandline.hpp b/include/internal/catch_commandline.hpp index d1b39a82..3b6c9f1f 100644 --- a/include/internal/catch_commandline.hpp +++ b/include/internal/catch_commandline.hpp @@ -9,6 +9,7 @@ #define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED #include "catch_config.hpp" +#include "catch_common.h" namespace Catch { @@ -95,9 +96,11 @@ namespace Catch { if( cmd.argsCount() > 2 ) cmd.raiseError( "Expected upto 2 arguments" ); - List::What listSpec = List::All; + config.listSpec = List::TestNames; if( cmd.argsCount() >= 1 ) { - if( cmd[0] == "tests" ) + if( cmd[0] == "all" ) + config.listSpec = List::All; + else if( cmd[0] == "tests" ) config.listSpec = List::Tests; else if( cmd[0] == "reporters" ) config.listSpec = List::Reports; @@ -106,9 +109,9 @@ namespace Catch { } if( cmd.argsCount() >= 2 ) { if( cmd[1] == "xml" ) - config.listSpec = static_cast<List::What>( listSpec | List::AsXml ); + config.listSpec = static_cast<List::What>( config.listSpec | List::AsXml ); else if( cmd[1] == "text" ) - config.listSpec = static_cast<List::What>( listSpec | List::AsText ); + config.listSpec = static_cast<List::What>( config.listSpec | List::AsText ); else cmd.raiseError( "Expected [xml] or [text]" ); } @@ -117,10 +120,22 @@ namespace Catch { if( Command cmd = parser.find( "-t", "--test" ) ) { if( cmd.argsCount() == 0 ) cmd.raiseError( "Expected at least one argument" ); - for( std::size_t i = 0; i < cmd.argsCount(); ++i ) - config.testSpecs.push_back( cmd[i] ); + std::string groupName; + for( std::size_t i = 0; i < cmd.argsCount(); ++i ) { + if( i != 0 ) + groupName += " "; + groupName += cmd[i]; + } + TestCaseFilters filters( groupName ); + for( std::size_t i = 0; i < cmd.argsCount(); ++i ) { + if( startsWith( cmd[i], "exclude:" ) ) + filters.addFilter( TestCaseFilter( cmd[i].substr( 8 ), IfFilterMatches::ExcludeTests ) ); + else + filters.addFilter( TestCaseFilter( cmd[i] ) ); + } + config.filters.push_back( filters ); } - + if( Command cmd = parser.find( "-r", "--reporter" ) ) { if( cmd.argsCount() != 1 ) cmd.raiseError( "Expected one argument" ); diff --git a/include/internal/catch_common.h b/include/internal/catch_common.h index ee4a565b..4a3567e5 100644 --- a/include/internal/catch_common.h +++ b/include/internal/catch_common.h @@ -74,6 +74,10 @@ namespace Catch { inline void forEach( const ContainerT& container, Function function ) { std::for_each( container.begin(), container.end(), function ); } + + inline bool startsWith( const std::string& s, const std::string& prefix ) { + return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix; + } struct SourceLineInfo { diff --git a/include/internal/catch_config.hpp b/include/internal/catch_config.hpp index fa488095..f859f004 100644 --- a/include/internal/catch_config.hpp +++ b/include/internal/catch_config.hpp @@ -8,6 +8,7 @@ #ifndef TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED #define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED +#include "catch_test_spec.h" #include "catch_interfaces_reporter.h" #include "catch_context.h" @@ -29,7 +30,9 @@ namespace Catch { Reports = 1, Tests = 2, All = 3, - + + TestNames = 6, + WhatMask = 0xf, AsText = 0x10, @@ -50,7 +53,7 @@ namespace Catch { std::string reporter; std::string outputFilename; List::What listSpec; - std::vector<std::string> testSpecs; + std::vector<TestCaseFilters> filters; bool shouldDebugBreak; std::string stream; Include::WhichResults includeWhichResults; @@ -87,14 +90,6 @@ namespace Catch { m_data.outputFilename = filename; } - bool testsSpecified() const { - return !m_data.testSpecs.empty(); - } - - const std::vector<std::string>& getTestSpecs() const { - return m_data.testSpecs; - } - List::What getListSpec( void ) const { return m_data.listSpec; } diff --git a/include/internal/catch_impl.hpp b/include/internal/catch_impl.hpp index 73aa5e49..9815f271 100644 --- a/include/internal/catch_impl.hpp +++ b/include/internal/catch_impl.hpp @@ -24,6 +24,10 @@ #include "catch_resultinfo_builder.hpp" #include "catch_test_case_info.hpp" +#include "../reporters/catch_reporter_basic.hpp" +#include "../reporters/catch_reporter_xml.hpp" +#include "../reporters/catch_reporter_junit.hpp" + namespace Catch { NonCopyable::~NonCopyable() {} IShared::~IShared() {} @@ -52,6 +56,10 @@ namespace Catch { void Config::dummy() {} + INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter ) + INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter ) + INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter ) + } #ifdef __clang__ diff --git a/include/internal/catch_interfaces_runner.h b/include/internal/catch_interfaces_runner.h index f3825626..874907b6 100644 --- a/include/internal/catch_interfaces_runner.h +++ b/include/internal/catch_interfaces_runner.h @@ -17,18 +17,6 @@ namespace Catch { struct IRunner { virtual ~IRunner(); - - /// Runs all tests, even if hidden - virtual Totals runAll() = 0; - - /// Runs all tests unless 'hidden' by ./ prefix - virtual Totals runAllNonHidden() = 0; - - /// Runs all test that match the spec string - virtual Totals runMatching( const std::string& rawTestSpec ) = 0; - - /// Runs all the tests passed in - virtual Totals runTests( const std::string& groupName, const std::vector<TestCaseInfo>& testCases ) = 0; }; } diff --git a/include/internal/catch_interfaces_testcase.h b/include/internal/catch_interfaces_testcase.h index 992b71d3..7d5192aa 100644 --- a/include/internal/catch_interfaces_testcase.h +++ b/include/internal/catch_interfaces_testcase.h @@ -11,6 +11,9 @@ #include <vector> namespace Catch { + + class TestCaseFilters; + struct ITestCase : IShared { virtual void invoke () const = 0; protected: @@ -22,10 +25,7 @@ namespace Catch { struct ITestCaseRegistry { virtual ~ITestCaseRegistry(); virtual const std::vector<TestCaseInfo>& getAllTests() const = 0; - virtual const std::vector<TestCaseInfo>& getAllNonHiddenTests() const = 0; - virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const = 0; - virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const = 0; }; } diff --git a/include/internal/catch_list.hpp b/include/internal/catch_list.hpp index 747939bc..d9c1200a 100644 --- a/include/internal/catch_list.hpp +++ b/include/internal/catch_list.hpp @@ -12,7 +12,15 @@ #include <limits> namespace Catch { - inline int List( const ConfigData& config ) { + inline bool matchesFilters( const std::vector<TestCaseFilters>& filters, const TestCaseInfo& testCase ) { + std::vector<TestCaseFilters>::const_iterator it = filters.begin(); + std::vector<TestCaseFilters>::const_iterator itEnd = filters.end(); + for(; it != itEnd; ++it ) + if( !it->shouldInclude( testCase ) ) + return false; + return true; + } + inline void List( const ConfigData& config ) { if( config.listSpec & List::Reports ) { std::cout << "Available reports:\n"; @@ -26,22 +34,28 @@ namespace Catch { } if( config.listSpec & List::Tests ) { - std::cout << "Available tests:\n"; + if( config.filters.empty() ) + std::cout << "All available tests:\n"; + else + std::cout << "Matching tests:\n"; std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin(); std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end(); for(; it != itEnd; ++it ) { - // !TBD: consider listAs() - std::cout << "\t" << it->getName() << "\n\t\t '" << it->getDescription() << "'\n"; + if( matchesFilters( config.filters, *it ) ) { + // !TBD: consider listAs() + std::cout << "\t" << it->getName() << "\n"; + if( ( config.listSpec & List::TestNames ) != List::TestNames ) + std::cout << "\t\t '" << it->getDescription() << "'\n"; + } } std::cout << std::endl; } if( ( config.listSpec & List::All ) == 0 ) { - std::cerr << "Unknown list type" << std::endl; - return (std::numeric_limits<int>::max)(); + std::ostringstream oss; + oss << "Unknown list type"; + throw std::domain_error( oss.str() ); } - - return 0; } } // end namespace Catch diff --git a/include/internal/catch_ptr.hpp b/include/internal/catch_ptr.hpp index 3976b7e9..2b8caf6f 100644 --- a/include/internal/catch_ptr.hpp +++ b/include/internal/catch_ptr.hpp @@ -52,19 +52,14 @@ namespace Catch { return m_p; } - T& operator*(){ + T& operator*() const { return *m_p; } - const T& operator*() const{ - return *m_p; - } - - T* operator->(){ - return m_p; - } - const T* operator->() const{ + + T* operator->() const { return m_p; } + bool operator !() const { return m_p == NULL; } diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 7a22b3b9..fc438b00 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -56,7 +56,7 @@ namespace Catch { public: - explicit Runner( Config& config, const Ptr<IReporter>& reporter ) + explicit Runner( const Config& config, const Ptr<IReporter>& reporter ) : m_context( getCurrentMutableContext() ), m_runningTest( NULL ), m_config( config ), @@ -78,34 +78,22 @@ namespace Catch { m_context.setResultCapture( m_prevResultCapture ); m_context.setConfig( m_prevConfig ); } - - virtual Totals runAll() { - return runTests( "", getRegistryHub().getTestCaseRegistry().getAllTests() ); - } - virtual Totals runAllNonHidden() { - return runTests( "", getRegistryHub().getTestCaseRegistry().getAllNonHiddenTests() ); - } + Totals runMatching( const std::string& testSpec ) { - virtual Totals runMatching( const std::string& rawTestSpec ) { + std::vector<TestCaseInfo> matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( testSpec ); - const std::vector<TestCaseInfo>& matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( rawTestSpec ); - return runTests( rawTestSpec, matchingTests ); - } + Totals totals; - virtual Totals runTests( const std::string& groupName, const std::vector<TestCaseInfo>& testCases ) { + m_reporter->StartGroup( testSpec ); - Totals totals; - m_reporter->StartGroup( groupName ); + std::vector<TestCaseInfo>::const_iterator it = matchingTests.begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = matchingTests.end(); + for(; it != itEnd; ++it ) + totals += runTest( *it ); + // !TBD use std::accumulate? - for( std::size_t i=0; i < testCases.size(); ++i ) { - if( aborting() ) { - m_reporter->Aborted(); - break; - } - totals += runTest( testCases[i] ); - } - m_reporter->EndGroup( groupName, totals ); + m_reporter->EndGroup( testSpec, totals ); return totals; } @@ -228,13 +216,15 @@ namespace Catch { virtual const ResultInfo* getLastResult() const { return &m_lastResult; } - - private: + public: + // !TBD We need to do this another way! bool aborting() const { return m_totals.assertions.failed == static_cast<std::size_t>( m_config.getCutoff() ); } - + + private: + ResultAction::Value actOnCurrentResult() { testEnded( m_currentResult ); m_lastResult = m_currentResult; diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp index a17e6033..ade5878d 100644 --- a/include/internal/catch_test_case_registry_impl.hpp +++ b/include/internal/catch_test_case_registry_impl.hpp @@ -52,25 +52,33 @@ namespace Catch { return m_nonHiddenFunctions; } + // !TBD deprecated virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const { - TestSpec testSpec( rawTestSpec ); - std::vector<TestCaseInfo> matchingTests; getMatchingTestCases( rawTestSpec, matchingTests ); return matchingTests; } + // !TBD deprecated virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const { - TestSpec testSpec( rawTestSpec ); + TestCaseFilter filter( rawTestSpec ); std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin(); std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end(); for(; it != itEnd; ++it ) { - if( testSpec.matches( it->getName() ) ) { + if( filter.shouldInclude( *it ) ) { matchingTestsOut.push_back( *it ); } } } + virtual void getMatchingTestCases( const TestCaseFilters& filters, std::vector<TestCaseInfo>& matchingTestsOut ) const { + std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end(); + // !TBD: replace with algorithm + for(; it != itEnd; ++it ) + if( filters.shouldInclude( *it ) ) + matchingTestsOut.push_back( *it ); + } private: diff --git a/include/internal/catch_test_spec.h b/include/internal/catch_test_spec.h index 90013248..909db165 100644 --- a/include/internal/catch_test_spec.h +++ b/include/internal/catch_test_spec.h @@ -11,30 +11,85 @@ #include <string> namespace Catch { + + struct IfFilterMatches{ enum DoWhat { + IncludeTests, + ExcludeTests + }; }; - class TestSpec { + class TestCaseFilter { public: - TestSpec( const std::string& rawSpec ) - : m_rawSpec( rawSpec ), - m_isWildcarded( false ) { - - if( m_rawSpec[m_rawSpec.size()-1] == '*' ) { - m_rawSpec = m_rawSpec.substr( 0, m_rawSpec.size()-1 ); + TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat matchBehaviour = IfFilterMatches::IncludeTests ) + : m_testSpec( testSpec ), + m_filterType( matchBehaviour ), + m_isWildcarded( false ) + { + if( m_testSpec[m_testSpec.size()-1] == '*' ) { + m_testSpec = m_testSpec.substr( 0, m_testSpec.size()-1 ); m_isWildcarded = true; } } - bool matches ( const std::string& testName ) const { + IfFilterMatches::DoWhat getFilterType() const { + return m_filterType; + } + + bool shouldInclude( const TestCaseInfo& testCase ) const { + return isMatch( testCase ) == (m_filterType == IfFilterMatches::IncludeTests); + } + private: + + bool isMatch( const TestCaseInfo& testCase ) const { + const std::string& name = testCase.getName(); if( !m_isWildcarded ) - return m_rawSpec == testName; + return m_testSpec == name; else - return testName.size() >= m_rawSpec.size() && testName.substr( 0, m_rawSpec.size() ) == m_rawSpec; + return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec; } - private: - std::string m_rawSpec; + std::string m_testSpec; + IfFilterMatches::DoWhat m_filterType; bool m_isWildcarded; }; + + class TestCaseFilters { + public: + TestCaseFilters( const std::string& name ) : m_name( name ) {} + + std::string getName() const { + return m_name; + } + + void addFilter( const TestCaseFilter& filter ) { + if( filter.getFilterType() == IfFilterMatches::ExcludeTests ) + m_exclusionFilters.push_back( filter ); + else + m_inclusionFilters.push_back( filter ); + } + + bool shouldInclude( const TestCaseInfo& testCase ) const { + if( !m_inclusionFilters.empty() ) { + std::vector<TestCaseFilter>::const_iterator it = m_inclusionFilters.begin(); + std::vector<TestCaseFilter>::const_iterator itEnd = m_inclusionFilters.end(); + for(; it != itEnd; ++it ) + if( it->shouldInclude( testCase ) ) + break; + if( it == itEnd ) + return false; + } + std::vector<TestCaseFilter>::const_iterator it = m_exclusionFilters.begin(); + std::vector<TestCaseFilter>::const_iterator itEnd = m_exclusionFilters.end(); + for(; it != itEnd; ++it ) + if( !it->shouldInclude( testCase ) ) + return false; + return true; + } + private: + std::vector<TestCaseFilter> m_inclusionFilters; + std::vector<TestCaseFilter> m_exclusionFilters; + std::string m_name; + }; + } #endif // TWOBLUECUBES_CATCH_TESTSPEC_H_INCLUDED diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp index 10bf32a6..42185757 100644 --- a/projects/SelfTest/TestMain.cpp +++ b/projects/SelfTest/TestMain.cpp @@ -63,6 +63,7 @@ TEST_CASE( "meta/Misc/Sections", "looped tests" ) { #endif #include "../../include/internal/catch_commandline.hpp" +#include "../../include/internal/catch_test_spec.h" #include "../../include/reporters/catch_reporter_basic.hpp" #include "../../include/reporters/catch_reporter_xml.hpp" #include "../../include/reporters/catch_reporter_junit.hpp" @@ -84,6 +85,8 @@ std::string parseIntoConfigAndReturnError( const char * (&argv)[size], Catch::Co return ""; } +inline Catch::TestCaseInfo makeTestCase( const char* name ){ return Catch::TestCaseInfo( NULL, name, "", CATCH_INTERNAL_LINEINFO ); } + TEST_CASE( "selftest/parser/2", "ConfigData" ) { Catch::ConfigData config; @@ -92,7 +95,6 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) { const char* argv[] = { "test" }; CHECK_NOTHROW( parseIntoConfig( argv, config ) ); - CHECK( config.testSpecs.empty() ); CHECK( config.shouldDebugBreak == false ); CHECK( config.cutoff == -1 ); CHECK( config.allowThrows == true ); @@ -103,26 +105,46 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) { SECTION( "-t/1", "Specify one test case using -t" ) { const char* argv[] = { "test", "-t", "test1" }; CHECK_NOTHROW( parseIntoConfig( argv, config ) ); - - REQUIRE( config.testSpecs.size() == 1 ); - REQUIRE( config.testSpecs[0] == "test1" ); + + REQUIRE( config.filters.size() == 1 ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "notIncluded" ) ) == false ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) ); + } + SECTION( "-t/exclude:1", "Specify one test case exclusion using -t exclude:" ) { + const char* argv[] = { "test", "-t", "exclude:test1" }; + CHECK_NOTHROW( parseIntoConfig( argv, config ) ); + + REQUIRE( config.filters.size() == 1 ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) == false ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "alwaysIncluded" ) ) ); } SECTION( "--test/1", "Specify one test case using --test" ) { const char* argv[] = { "test", "--test", "test1" }; CHECK_NOTHROW( parseIntoConfig( argv, config ) ); - REQUIRE( config.testSpecs.size() == 1 ); - REQUIRE( config.testSpecs[0] == "test1" ); + REQUIRE( config.filters.size() == 1 ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "notIncluded" ) ) == false ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) ); + } + + SECTION( "--test/exclude:1", "Specify one test case exclusion using --test exclude:" ) { + const char* argv[] = { "test", "--test", "exclude:test1" }; + CHECK_NOTHROW( parseIntoConfig( argv, config ) ); + + REQUIRE( config.filters.size() == 1 ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) == false ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "alwaysIncluded" ) ) ); } SECTION( "-t/2", "Specify two test cases using -t" ) { const char* argv[] = { "test", "-t", "test1", "test2" }; CHECK_NOTHROW( parseIntoConfig( argv, config ) ); - REQUIRE( config.testSpecs.size() == 2 ); - REQUIRE( config.testSpecs[0] == "test1" ); - REQUIRE( config.testSpecs[1] == "test2" ); + REQUIRE( config.filters.size() == 1 ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "notIncluded" ) ) == false ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) ); + REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test2" ) ) ); } SECTION( "-t/0", "When no test names are supplied it is an error" ) { @@ -229,91 +251,6 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) { } } -class TestSpec { -public: - TestSpec( const std::string& rawSpec ) - : m_rawSpec( rawSpec ), - m_isWildcarded( false ) { - - if( m_rawSpec[m_rawSpec.size()-1] == '*' ) { - m_rawSpec = m_rawSpec.substr( 0, m_rawSpec.size()-1 ); - m_isWildcarded = true; - } - } - - bool matches ( const std::string& testName ) const { - if( !m_isWildcarded ) - return m_rawSpec == testName; - else - return testName.size() >= m_rawSpec.size() && testName.substr( 0, m_rawSpec.size() ) == m_rawSpec; - } - -private: - std::string m_rawSpec; - bool m_isWildcarded; -}; - - -namespace Catch{ // - -struct IfFilterMatches{ enum DoWhat { - IncludeTests, - ExcludeTests -}; }; - -class TestCaseFilter { -public: - TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat filterType = IfFilterMatches::IncludeTests ) - : m_testSpec( testSpec ), - m_filterType( filterType ), - m_isWildcarded( false ) - { - if( m_testSpec[m_testSpec.size()-1] == '*' ) { - m_testSpec = m_testSpec.substr( 0, m_testSpec.size()-1 ); - m_isWildcarded = true; - } - } - - bool shouldInclude( const TestCaseInfo& testCase ) const { - return isMatch( testCase ) == (m_filterType == IfFilterMatches::IncludeTests); - } -private: - - bool isMatch( const TestCaseInfo& testCase ) const { - const std::string& name = testCase.getName(); - if( !m_isWildcarded ) - return m_testSpec == name; - else - return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec; - } - - std::string m_testSpec; - IfFilterMatches::DoWhat m_filterType; - bool m_isWildcarded; -}; - -class TestCaseFilters { -public: - void addFilter( const TestCaseFilter& filter ) { - m_filters.push_back( filter ); - } - - bool shouldInclude( const TestCaseInfo& testCase ) const { - std::vector<TestCaseFilter>::const_iterator it = m_filters.begin(); - std::vector<TestCaseFilter>::const_iterator itEnd = m_filters.end(); - for(; it != itEnd; ++it ) - if( !it->shouldInclude( testCase ) ) - return false; - return true; - } -private: - std::vector<TestCaseFilter> m_filters; -}; - -} - -inline Catch::TestCaseInfo makeTestCase( const char* name ){ return Catch::TestCaseInfo( NULL, name, "", CATCH_INTERNAL_LINEINFO ); } - TEST_CASE( "selftest/test filter", "Groups of tests can be selected" ) { Catch::TestCaseFilter matchAny( "*" ); @@ -335,7 +272,7 @@ TEST_CASE( "selftest/test filters", "Groups of tests can be selected" ) { Catch::TestCaseFilter matchHidden( "./*" ); Catch::TestCaseFilter dontMatchA( "./a*", Catch::IfFilterMatches::ExcludeTests ); - Catch::TestCaseFilters filters; + Catch::TestCaseFilters filters( "" ); filters.addFilter( matchHidden ); filters.addFilter( dontMatchA ); diff --git a/single_include/catch.hpp b/single_include/catch.hpp index ba0259f6..b4933e93 100644 --- a/single_include/catch.hpp +++ b/single_include/catch.hpp @@ -1,5 +1,5 @@ /* - * Generated: 2012-08-23 19:48:39.069512 + * Generated: 2012-08-23 20:06:52.538506 * ---------------------------------------------------------- * This file has been merged from multiple headers. Please don't edit it directly * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. @@ -92,6 +92,10 @@ namespace Catch { std::for_each( container.begin(), container.end(), function ); } + inline bool startsWith( const std::string& s, const std::string& prefix ) { + return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix; + } + struct SourceLineInfo { SourceLineInfo() : line( 0 ){} @@ -269,19 +273,14 @@ namespace Catch { return m_p; } - T& operator*(){ - return *m_p; - } - const T& operator*() const{ + T& operator*() const { return *m_p; } - T* operator->(){ - return m_p; - } - const T* operator->() const{ + T* operator->() const { return m_p; } + bool operator !() const { return m_p == NULL; } @@ -463,6 +462,9 @@ namespace Catch { #include <vector> namespace Catch { + + class TestCaseFilters; + struct ITestCase : IShared { virtual void invoke () const = 0; protected: @@ -474,10 +476,7 @@ namespace Catch { struct ITestCaseRegistry { virtual ~ITestCaseRegistry(); virtual const std::vector<TestCaseInfo>& getAllTests() const = 0; - virtual const std::vector<TestCaseInfo>& getAllNonHiddenTests() const = 0; - virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const = 0; - virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const = 0; }; } @@ -1985,18 +1984,6 @@ namespace Catch { struct IRunner { virtual ~IRunner(); - - /// Runs all tests, even if hidden - virtual Totals runAll() = 0; - - /// Runs all tests unless 'hidden' by ./ prefix - virtual Totals runAllNonHidden() = 0; - - /// Runs all test that match the spec string - virtual Totals runMatching( const std::string& rawTestSpec ) = 0; - - /// Runs all the tests passed in - virtual Totals runTests( const std::string& groupName, const std::vector<TestCaseInfo>& testCases ) = 0; }; } @@ -2210,2778 +2197,2898 @@ return @ desc; \ // #included from: catch_runner.hpp #define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED -// #included from: reporters/catch_reporter_basic.hpp -#define TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED - -// #included from: ../internal/catch_reporter_registrars.hpp -#define TWOBLUECUBES_CATCH_REPORTER_REGISTRARS_HPP_INCLUDED +// #included from: internal/catch_commandline.hpp +#define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED -namespace Catch { +// #included from: catch_config.hpp +#define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED - template<typename T> - class ReporterRegistrar { +// #included from: catch_test_spec.h +#define TWOBLUECUBES_CATCH_TESTSPEC_H_INCLUDED - class ReporterFactory : public IReporterFactory { +#include <string> - virtual IReporter* create( const ReporterConfig& config ) const { - return new T( config ); - } +namespace Catch { - virtual std::string getDescription() const { - return T::getDescription(); - } - }; + struct IfFilterMatches{ enum DoWhat { + IncludeTests, + ExcludeTests + }; }; + class TestCaseFilter { public: - - ReporterRegistrar( const std::string& name ) { - getMutableRegistryHub().registerReporter( name, new ReporterFactory() ); + TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat matchBehaviour = IfFilterMatches::IncludeTests ) + : m_testSpec( testSpec ), + m_filterType( matchBehaviour ), + m_isWildcarded( false ) + { + if( m_testSpec[m_testSpec.size()-1] == '*' ) { + m_testSpec = m_testSpec.substr( 0, m_testSpec.size()-1 ); + m_isWildcarded = true; + } } - }; -} -#define INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) \ - Catch::ReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name ); + IfFilterMatches::DoWhat getFilterType() const { + return m_filterType; + } -// #included from: ../internal/catch_console_colour.hpp -#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED + bool shouldInclude( const TestCaseInfo& testCase ) const { + return isMatch( testCase ) == (m_filterType == IfFilterMatches::IncludeTests); + } + private: -namespace Catch { + bool isMatch( const TestCaseInfo& testCase ) const { + const std::string& name = testCase.getName(); + if( !m_isWildcarded ) + return m_testSpec == name; + else + return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec; + } - struct ConsoleColourImpl; + std::string m_testSpec; + IfFilterMatches::DoWhat m_filterType; + bool m_isWildcarded; + }; - class TextColour : NonCopyable { + class TestCaseFilters { public: + TestCaseFilters( const std::string& name ) : m_name( name ) {} - enum Colours { - None, - - FileName, - ResultError, - ResultSuccess, - - Error, - Success, - - OriginalExpression, - ReconstructedExpression - }; + std::string getName() const { + return m_name; + } - TextColour( Colours colour = None ); - void set( Colours colour ); - ~TextColour(); + void addFilter( const TestCaseFilter& filter ) { + if( filter.getFilterType() == IfFilterMatches::ExcludeTests ) + m_exclusionFilters.push_back( filter ); + else + m_inclusionFilters.push_back( filter ); + } + bool shouldInclude( const TestCaseInfo& testCase ) const { + if( !m_inclusionFilters.empty() ) { + std::vector<TestCaseFilter>::const_iterator it = m_inclusionFilters.begin(); + std::vector<TestCaseFilter>::const_iterator itEnd = m_inclusionFilters.end(); + for(; it != itEnd; ++it ) + if( it->shouldInclude( testCase ) ) + break; + if( it == itEnd ) + return false; + } + std::vector<TestCaseFilter>::const_iterator it = m_exclusionFilters.begin(); + std::vector<TestCaseFilter>::const_iterator itEnd = m_exclusionFilters.end(); + for(; it != itEnd; ++it ) + if( !it->shouldInclude( testCase ) ) + return false; + return true; + } private: - ConsoleColourImpl* m_impl; + std::vector<TestCaseFilter> m_inclusionFilters; + std::vector<TestCaseFilter> m_exclusionFilters; + std::string m_name; }; -} // end namespace Catch +} + +#include <memory> +#include <vector> +#include <string> +#include <iostream> namespace Catch { - struct pluralise { - pluralise( std::size_t count, const std::string& label ) - : m_count( count ), - m_label( label ) - {} + struct Include { enum WhichResults { + FailedOnly, + SuccessfulResults + }; }; - friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) { - os << pluraliser.m_count << " " << pluraliser.m_label; - if( pluraliser.m_count != 1 ) - os << "s"; - return os; - } + struct List{ enum What { + None = 0, - std::size_t m_count; - std::string m_label; - }; + Reports = 1, + Tests = 2, + All = 3, - class BasicReporter : public SharedImpl<IReporter> { + TestNames = 6, - struct SpanInfo { + WhatMask = 0xf, - SpanInfo() - : emitted( false ) - {} + AsText = 0x10, + AsXml = 0x11, - SpanInfo( const std::string& spanName ) - : name( spanName ), - emitted( false ) - {} + AsMask = 0xf0 + }; }; - SpanInfo( const SpanInfo& other ) - : name( other.name ), - emitted( other.emitted ) - {} + struct ConfigData { + ConfigData() + : listSpec( List::None ), + shouldDebugBreak( false ), + includeWhichResults( Include::FailedOnly ), + cutoff( -1 ), + allowThrows( true ) + {} - std::string name; - bool emitted; - }; + std::string reporter; + std::string outputFilename; + List::What listSpec; + std::vector<TestCaseFilters> filters; + bool shouldDebugBreak; + std::string stream; + Include::WhichResults includeWhichResults; + std::string name; + int cutoff; + bool allowThrows; + }; + class Config : public IConfig { + private: + Config( const Config& other ); + Config& operator = ( const Config& other ); + virtual void dummy(); public: - BasicReporter( const ReporterConfig& config ) - : m_config( config ), - m_firstSectionInTestCase( true ), - m_aborted( false ) + + Config() + : m_streambuf( NULL ), + m_os( std::cout.rdbuf() ) {} - virtual ~BasicReporter(); + Config( const ConfigData& data ) + : m_data( data ), + m_streambuf( NULL ), + m_os( std::cout.rdbuf() ) + {} - static std::string getDescription() { - return "Reports test results as lines of text"; + virtual ~Config() { + m_os.rdbuf( std::cout.rdbuf() ); + delete m_streambuf; } - private: - - void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) { - if( counts.passed ) - m_config.stream << counts.failed << " of " << counts.total() << " " << label << "s failed"; - else - m_config.stream << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed"; + void setFilename( const std::string& filename ) { + m_data.outputFilename = filename; } - void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) { - if( totals.assertions.total() == 0 ) { - m_config.stream << "No tests ran"; - } - else if( totals.assertions.failed ) { - TextColour colour( TextColour::ResultError ); - ReportCounts( "test case", totals.testCases, allPrefix ); - if( totals.testCases.failed > 0 ) { - m_config.stream << " ("; - ReportCounts( "assertion", totals.assertions, allPrefix ); - m_config.stream << ")"; - } - } - else { - TextColour colour( TextColour::ResultSuccess ); - m_config.stream << allPrefix << "tests passed (" - << pluralise( totals.assertions.passed, "assertion" ) << " in " - << pluralise( totals.testCases.passed, "test case" ) << ")"; - } + List::What getListSpec( void ) const { + return m_data.listSpec; } - private: // IReporter - - virtual bool shouldRedirectStdout() const { - return false; + const std::string& getFilename() const { + return m_data.outputFilename ; } - virtual void StartTesting() { - m_testingSpan = SpanInfo(); + List::What listWhat() const { + return static_cast<List::What>( m_data.listSpec & List::WhatMask ); } - virtual void Aborted() { - m_aborted = true; + List::What listAs() const { + return static_cast<List::What>( m_data.listSpec & List::AsMask ); } - virtual void EndTesting( const Totals& totals ) { - // Output the overall test results even if "Started Testing" was not emitted - if( m_aborted ) { - m_config.stream << "\n[Testing aborted. "; - ReportCounts( totals, "The first " ); - } - else { - m_config.stream << "\n[Testing completed. "; - ReportCounts( totals ); - } - m_config.stream << "]\n" << std::endl; + std::string getName() const { + return m_data.name; } - virtual void StartGroup( const std::string& groupName ) { - m_groupSpan = groupName; + bool shouldDebugBreak() const { + return m_data.shouldDebugBreak; } - virtual void EndGroup( const std::string& groupName, const Totals& totals ) { - if( m_groupSpan.emitted && !groupName.empty() ) { - m_config.stream << "[End of group: '" << groupName << "'. "; - ReportCounts( totals ); - m_config.stream << "]\n" << std::endl; - m_groupSpan = SpanInfo(); - } + virtual std::ostream& stream() const { + return m_os; } - virtual void StartTestCase( const TestCaseInfo& testInfo ) { - m_testSpan = testInfo.getName(); + void setStreamBuf( std::streambuf* buf ) { + m_os.rdbuf( buf ? buf : std::cout.rdbuf() ); } - virtual void StartSection( const std::string& sectionName, const std::string& ) { - m_sectionSpans.push_back( SpanInfo( sectionName ) ); + void useStream( const std::string& streamName ) { + std::streambuf* newBuf = createStreamBuf( streamName ); + setStreamBuf( newBuf ); + delete m_streambuf; + m_streambuf = newBuf; } - virtual void EndSection( const std::string& sectionName, const Counts& assertions ) { - SpanInfo& sectionSpan = m_sectionSpans.back(); - if( sectionSpan.emitted && !sectionSpan.name.empty() ) { - m_config.stream << "[End of section: '" << sectionName << "' "; + virtual bool includeSuccessfulResults() const { + return m_data.includeWhichResults == Include::SuccessfulResults; + } - if( assertions.failed ) { - TextColour colour( TextColour::ResultError ); - ReportCounts( "assertion", assertions); - } - else { - TextColour colour( TextColour::ResultSuccess ); - m_config.stream << ( assertions.passed > 1 ? "All " : "" ) - << pluralise( assertions.passed, "assertion" ) << " passed" ; - } - m_config.stream << "]\n" << std::endl; - } - m_sectionSpans.pop_back(); + int getCutoff() const { + return m_data.cutoff; } - virtual void Result( const ResultInfo& resultInfo ) { - if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok ) - return; + virtual bool allowThrows() const { + return m_data.allowThrows; + } - StartSpansLazily(); + ConfigData& data() { + return m_data; + } - if( !resultInfo.getFilename().empty() ) { - TextColour colour( TextColour::FileName ); - m_config.stream << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() ); - } + private: + ConfigData m_data; - if( resultInfo.hasExpression() ) { - TextColour colour( TextColour::OriginalExpression ); - m_config.stream << resultInfo.getExpression(); - if( resultInfo.ok() ) { - TextColour successColour( TextColour::Success ); - m_config.stream << " succeeded"; - } - else { - TextColour errorColour( TextColour::Error ); - m_config.stream << " failed"; - } - } - switch( resultInfo.getResultType() ) { - case ResultWas::ThrewException: - { - TextColour colour( TextColour::Error ); - if( resultInfo.hasExpression() ) - m_config.stream << " with unexpected"; - else - m_config.stream << "Unexpected"; - m_config.stream << " exception with message: '" << resultInfo.getMessage() << "'"; - } - break; - case ResultWas::DidntThrowException: - { - TextColour colour( TextColour::Error ); - if( resultInfo.hasExpression() ) - m_config.stream << " because no exception was thrown where one was expected"; - else - m_config.stream << "No exception thrown where one was expected"; - } - break; - case ResultWas::Info: - streamVariableLengthText( "info", resultInfo.getMessage() ); - break; - case ResultWas::Warning: - m_config.stream << "warning:\n'" << resultInfo.getMessage() << "'"; - break; - case ResultWas::ExplicitFailure: - { - TextColour colour( TextColour::Error ); - m_config.stream << "failed with message: '" << resultInfo.getMessage() << "'"; - } - break; - case ResultWas::Unknown: // These cases are here to prevent compiler warnings - case ResultWas::Ok: - case ResultWas::FailureBit: - case ResultWas::ExpressionFailed: - case ResultWas::Exception: - if( !resultInfo.hasExpression() ) { - if( resultInfo.ok() ) { - TextColour colour( TextColour::Success ); - m_config.stream << " succeeded"; - } - else { - TextColour colour( TextColour::Error ); - m_config.stream << " failed"; - } - } - break; - } + // !TBD Move these out of here + std::streambuf* m_streambuf; + mutable std::ostream m_os; + }; - if( resultInfo.hasExpandedExpression() ) { - m_config.stream << " for: "; - TextColour colour( TextColour::ReconstructedExpression ); - m_config.stream << resultInfo.getExpandedExpression(); - } - m_config.stream << std::endl; +} // end namespace Catch + +namespace Catch { + + class Command { + public: + Command(){} + + explicit Command( const std::string& name ) : m_name( name ) {} + + Command& operator += ( const std::string& arg ) { + m_args.push_back( arg ); + return *this; + } + Command& operator += ( const Command& other ) { + std::copy( other.m_args.begin(), other.m_args.end(), std::back_inserter( m_args ) ); + if( m_name.empty() ) + m_name = other.m_name; + return *this; + } + Command operator + ( const Command& other ) { + Command newCommand( *this ); + newCommand += other; + return newCommand; } - virtual void EndTestCase( const TestCaseInfo& testInfo, - const Totals& totals, - const std::string& stdOut, - const std::string& stdErr ) { - if( !stdOut.empty() ) { - StartSpansLazily(); - streamVariableLengthText( "stdout", stdOut ); - } + operator SafeBool::type() const { + return SafeBool::makeSafe( !m_name.empty() ); + } - if( !stdErr.empty() ) { - StartSpansLazily(); - streamVariableLengthText( "stderr", stdErr ); - } + std::string name() const { return m_name; } + std::string operator[]( std::size_t i ) const { return m_args[i]; } + std::size_t argsCount() const { return m_args.size(); } - if( m_testSpan.emitted ) { - m_config.stream << "[Finished: '" << testInfo.getName() << "' "; - ReportCounts( totals ); - m_config.stream << "]" << std::endl; - } + CATCH_ATTRIBUTE_NORETURN + void raiseError( const std::string& message ) const { + std::ostringstream oss; + oss << "Error while parsing " << m_name << ". " << message << "."; + if( m_args.size() > 0 ) + oss << " Arguments where:"; + for( std::size_t i = 0; i < m_args.size(); ++i ) + oss << " " << m_args[i]; + throw std::domain_error( oss.str() ); } - private: // helpers + private: - void StartSpansLazily() { - if( !m_testingSpan.emitted ) { - if( m_config.name.empty() ) - m_config.stream << "[Started testing]" << std::endl; + std::string m_name; + std::vector<std::string> m_args; + }; + + class CommandParser { + public: + CommandParser( int argc, char const * const * argv ) : m_argc( static_cast<std::size_t>( argc ) ), m_argv( argv ) {} + + Command find( const std::string& arg1, const std::string& arg2, const std::string& arg3 ) const { + return find( arg1 ) + find( arg2 ) + find( arg3 ); + } + + Command find( const std::string& shortArg, const std::string& longArg ) const { + return find( shortArg ) + find( longArg ); + } + Command find( const std::string& arg ) const { + for( std::size_t i = 0; i < m_argc; ++i ) + if( m_argv[i] == arg ) + return getArgs( i ); + return Command(); + } + + private: + Command getArgs( std::size_t from ) const { + Command command( m_argv[from] ); + for( std::size_t i = from+1; i < m_argc && m_argv[i][0] != '-'; ++i ) + command += m_argv[i]; + return command; + } + + std::size_t m_argc; + char const * const * m_argv; + }; + + inline void parseIntoConfig( const CommandParser& parser, ConfigData& config ) { + + if( Command cmd = parser.find( "-l", "--list" ) ) { + if( cmd.argsCount() > 2 ) + cmd.raiseError( "Expected upto 2 arguments" ); + + config.listSpec = List::TestNames; + if( cmd.argsCount() >= 1 ) { + if( cmd[0] == "all" ) + config.listSpec = List::All; + else if( cmd[0] == "tests" ) + config.listSpec = List::Tests; + else if( cmd[0] == "reporters" ) + config.listSpec = List::Reports; else - m_config.stream << "[Started testing: " << m_config.name << "]" << std::endl; - m_testingSpan.emitted = true; + cmd.raiseError( "Expected [tests] or [reporters]" ); + } + if( cmd.argsCount() >= 2 ) { + if( cmd[1] == "xml" ) + config.listSpec = static_cast<List::What>( config.listSpec | List::AsXml ); + else if( cmd[1] == "text" ) + config.listSpec = static_cast<List::What>( config.listSpec | List::AsText ); + else + cmd.raiseError( "Expected [xml] or [text]" ); } + } - if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) { - m_config.stream << "[Started group: '" << m_groupSpan.name << "']" << std::endl; - m_groupSpan.emitted = true; + if( Command cmd = parser.find( "-t", "--test" ) ) { + if( cmd.argsCount() == 0 ) + cmd.raiseError( "Expected at least one argument" ); + std::string groupName; + for( std::size_t i = 0; i < cmd.argsCount(); ++i ) { + if( i != 0 ) + groupName += " "; + groupName += cmd[i]; } + TestCaseFilters filters( groupName ); + for( std::size_t i = 0; i < cmd.argsCount(); ++i ) { + if( startsWith( cmd[i], "exclude:" ) ) + filters.addFilter( TestCaseFilter( cmd[i].substr( 8 ), IfFilterMatches::ExcludeTests ) ); + else + filters.addFilter( TestCaseFilter( cmd[i] ) ); + } + config.filters.push_back( filters ); + } - if( !m_testSpan.emitted ) { - m_config.stream << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl; - m_testSpan.emitted = true; + if( Command cmd = parser.find( "-r", "--reporter" ) ) { + if( cmd.argsCount() != 1 ) + cmd.raiseError( "Expected one argument" ); + config.reporter = cmd[0]; + } + + if( Command cmd = parser.find( "-o", "--out" ) ) { + if( cmd.argsCount() == 0 ) + cmd.raiseError( "Expected filename" ); + if( cmd[0][0] == '%' ) + config.stream = cmd[0].substr( 1 ); + else + config.outputFilename = cmd[0]; + } + + if( Command cmd = parser.find( "-s", "--success" ) ) { + if( cmd.argsCount() != 0 ) + cmd.raiseError( "Does not accept arguments" ); + config.includeWhichResults = Include::SuccessfulResults; + } + + if( Command cmd = parser.find( "-b", "--break" ) ) { + if( cmd.argsCount() != 0 ) + cmd.raiseError( "Does not accept arguments" ); + config.shouldDebugBreak = true; + } + + if( Command cmd = parser.find( "-n", "--name" ) ) { + if( cmd.argsCount() != 1 ) + cmd.raiseError( "Expected a name" ); + config.name = cmd[0]; + } + + if( Command cmd = parser.find( "-a", "--abort" ) ) { + if( cmd.argsCount() > 1 ) + cmd.raiseError( "Only accepts 0-1 arguments" ); + int threshold = 1; + if( cmd.argsCount() == 1 ) { + std::stringstream ss; + ss << cmd[0]; + ss >> threshold; + if( ss.fail() || threshold <= 0 ) + cmd.raiseError( "threshold must be a number greater than zero" ); } + config.cutoff = threshold; + } - if( !m_sectionSpans.empty() ) { - SpanInfo& sectionSpan = m_sectionSpans.back(); - if( !sectionSpan.emitted && !sectionSpan.name.empty() ) { - if( m_firstSectionInTestCase ) { - m_config.stream << "\n"; - m_firstSectionInTestCase = false; - } - std::vector<SpanInfo>::iterator it = m_sectionSpans.begin(); - std::vector<SpanInfo>::iterator itEnd = m_sectionSpans.end(); - for(; it != itEnd; ++it ) { - SpanInfo& prevSpan = *it; - if( !prevSpan.emitted && !prevSpan.name.empty() ) { - m_config.stream << "[Started section: '" << prevSpan.name << "']" << std::endl; - prevSpan.emitted = true; - } - } - } + if( Command cmd = parser.find( "-nt", "--nothrow" ) ) { + if( cmd.argsCount() != 0 ) + cmd.raiseError( "Does not accept arguments" ); + config.allowThrows = false; + } + + } + +} // end namespace Catch + +// #included from: internal/catch_list.hpp +#define TWOBLUECUBES_CATCH_LIST_HPP_INCLUDED + +#include <limits> + +namespace Catch { + inline bool matchesFilters( const std::vector<TestCaseFilters>& filters, const TestCaseInfo& testCase ) { + std::vector<TestCaseFilters>::const_iterator it = filters.begin(); + std::vector<TestCaseFilters>::const_iterator itEnd = filters.end(); + for(; it != itEnd; ++it ) + if( !it->shouldInclude( testCase ) ) + return false; + return true; + } + inline void List( const ConfigData& config ) { + + if( config.listSpec & List::Reports ) { + std::cout << "Available reports:\n"; + IReporterRegistry::FactoryMap::const_iterator it = getRegistryHub().getReporterRegistry().getFactories().begin(); + IReporterRegistry::FactoryMap::const_iterator itEnd = getRegistryHub().getReporterRegistry().getFactories().end(); + for(; it != itEnd; ++it ) { + // !TBD: consider listAs() + std::cout << "\t" << it->first << "\n\t\t'" << it->second->getDescription() << "'\n"; } + std::cout << std::endl; } - void streamVariableLengthText( const std::string& prefix, const std::string& text ) { - std::string trimmed = trim( text ); - if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) { - m_config.stream << "[" << prefix << ": " << trimmed << "]\n"; - } - else { - m_config.stream << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed - << "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n"; + if( config.listSpec & List::Tests ) { + if( config.filters.empty() ) + std::cout << "All available tests:\n"; + else + std::cout << "Matching tests:\n"; + std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end(); + for(; it != itEnd; ++it ) { + if( matchesFilters( config.filters, *it ) ) { + // !TBD: consider listAs() + std::cout << "\t" << it->getName() << "\n"; + if( ( config.listSpec & List::TestNames ) != List::TestNames ) + std::cout << "\t\t '" << it->getDescription() << "'\n"; + } } + std::cout << std::endl; } - private: - ReporterConfig m_config; - bool m_firstSectionInTestCase; - - SpanInfo m_testingSpan; - SpanInfo m_groupSpan; - SpanInfo m_testSpan; - std::vector<SpanInfo> m_sectionSpans; - bool m_aborted; - }; + if( ( config.listSpec & List::All ) == 0 ) { + std::ostringstream oss; + oss << "Unknown list type"; + throw std::domain_error( oss.str() ); + } + } } // end namespace Catch -// #included from: reporters/catch_reporter_xml.hpp -#define TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED +// #included from: internal/catch_runner_impl.hpp +#define TWOBLUECUBES_INTERNAL_CATCH_RUNNER_HPP_INCLUDED -// #included from: ../internal/catch_xmlwriter.hpp -#define TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED +// #included from: catch_running_test.hpp +#define TWOBLUECUBES_INTERNAL_CATCH_RUNNING_TEST_HPP_INCLUDED -#include <sstream> +// #included from: catch_section_info.hpp +#define TWOBLUECUBES_INTERNAL_CATCH_SECTION_INFO_HPP_INCLUDED + +#include <map> #include <string> -#include <vector> namespace Catch { - class XmlWriter { + class SectionInfo { public: - class ScopedElement { - public: - ScopedElement( XmlWriter* writer ) - : m_writer( writer ) - {} - - ScopedElement( const ScopedElement& other ) - : m_writer( other.m_writer ){ - other.m_writer = NULL; - } - - ~ScopedElement() { - if( m_writer ) - m_writer->endElement(); - } - - ScopedElement& writeText( const std::string& text ) { - m_writer->writeText( text ); - return *this; - } - - template<typename T> - ScopedElement& writeAttribute( const std::string& name, const T& attribute ) { - m_writer->writeAttribute( name, attribute ); - return *this; - } - - private: - mutable XmlWriter* m_writer; + enum Status { + Root, + Unknown, + Branch, + TestedBranch, + TestedLeaf }; - XmlWriter() - : m_tagIsOpen( false ), - m_needsNewline( false ), - m_os( &std::cout ) + SectionInfo( SectionInfo* parent ) + : m_status( Unknown ), + m_parent( parent ) {} - XmlWriter( std::ostream& os ) - : m_tagIsOpen( false ), - m_needsNewline( false ), - m_os( &os ) + SectionInfo() + : m_status( Root ), + m_parent( NULL ) {} - ~XmlWriter() { - while( !m_tags.empty() ) - endElement(); + ~SectionInfo() { + deleteAllValues( m_subSections ); } - XmlWriter& operator = ( const XmlWriter& other ) { - XmlWriter temp( other ); - swap( temp ); - return *this; + bool shouldRun() const { + return m_status < TestedBranch; } - void swap( XmlWriter& other ) { - std::swap( m_tagIsOpen, other.m_tagIsOpen ); - std::swap( m_needsNewline, other.m_needsNewline ); - std::swap( m_tags, other.m_tags ); - std::swap( m_indent, other.m_indent ); - std::swap( m_os, other.m_os ); + bool ran() { + if( m_status < Branch ) { + m_status = TestedLeaf; + return true; + } + return false; } - XmlWriter& startElement( const std::string& name ) { - ensureTagClosed(); - newlineIfNecessary(); - stream() << m_indent << "<" << name; - m_tags.push_back( name ); - m_indent += " "; - m_tagIsOpen = true; - return *this; + void ranToCompletion() { + if( m_status == Branch && !hasUntestedSections() ) + m_status = TestedBranch; } - ScopedElement scopedElement( const std::string& name ) { - ScopedElement scoped( this ); - startElement( name ); - return scoped; + SectionInfo* findSubSection( const std::string& name ) { + std::map<std::string, SectionInfo*>::const_iterator it = m_subSections.find( name ); + return it != m_subSections.end() + ? it->second + : NULL; } - XmlWriter& endElement() { - newlineIfNecessary(); - m_indent = m_indent.substr( 0, m_indent.size()-2 ); - if( m_tagIsOpen ) { - stream() << "/>\n"; - m_tagIsOpen = false; - } - else { - stream() << m_indent << "</" << m_tags.back() << ">\n"; - } - m_tags.pop_back(); - return *this; + SectionInfo* addSubSection( const std::string& name ) { + SectionInfo* subSection = new SectionInfo( this ); + m_subSections.insert( std::make_pair( name, subSection ) ); + m_status = Branch; + return subSection; } - XmlWriter& writeAttribute( const std::string& name, const std::string& attribute ) { - if( !name.empty() && !attribute.empty() ) { - stream() << " " << name << "=\""; - writeEncodedText( attribute ); - stream() << "\""; - } - return *this; + SectionInfo* getParent() { + return m_parent; } - XmlWriter& writeAttribute( const std::string& name, bool attribute ) { - stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\""; - return *this; - } + bool hasUntestedSections() const { + if( m_status == Unknown ) + return true; - template<typename T> - XmlWriter& writeAttribute( const std::string& name, const T& attribute ) { - if( !name.empty() ) - stream() << " " << name << "=\"" << attribute << "\""; - return *this; + std::map<std::string, SectionInfo*>::const_iterator it = m_subSections.begin(); + std::map<std::string, SectionInfo*>::const_iterator itEnd = m_subSections.end(); + for(; it != itEnd; ++it ) { + if( it->second->hasUntestedSections() ) + return true; + } + return false; } - XmlWriter& writeText( const std::string& text ) { - if( !text.empty() ){ - bool tagWasOpen = m_tagIsOpen; - ensureTagClosed(); - if( tagWasOpen ) - stream() << m_indent; - writeEncodedText( text ); - m_needsNewline = true; - } - return *this; + private: + Status m_status; + std::map<std::string, SectionInfo*> m_subSections; + SectionInfo* m_parent; + }; +} + +namespace Catch { + + class RunningTest { + + enum RunStatus { + NothingRun, + EncounteredASection, + RanAtLeastOneSection, + RanToCompletionWithSections, + RanToCompletionWithNoSections + }; + + public: + explicit RunningTest( const TestCaseInfo* info = NULL ) + : m_info( info ), + m_runStatus( RanAtLeastOneSection ), + m_currentSection( &m_rootSection ), + m_changed( false ) + {} + + bool wasSectionSeen() const { + return m_runStatus == RanAtLeastOneSection || + m_runStatus == RanToCompletionWithSections; } - XmlWriter& writeComment( const std::string& text ) { - ensureTagClosed(); - stream() << m_indent << "<!--" << text << "-->"; - m_needsNewline = true; - return *this; + void reset() { + m_runStatus = NothingRun; + m_changed = false; + m_lastSectionToRun = NULL; } - XmlWriter& writeBlankLine() { - ensureTagClosed(); - stream() << "\n"; - return *this; + void ranToCompletion() { + if( m_runStatus == RanAtLeastOneSection || + m_runStatus == EncounteredASection ) { + m_runStatus = RanToCompletionWithSections; + if( m_lastSectionToRun ) { + m_lastSectionToRun->ranToCompletion(); + m_changed = true; + } + } + else { + m_runStatus = RanToCompletionWithNoSections; + } } - private: + bool addSection( const std::string& name ) { + if( m_runStatus == NothingRun ) + m_runStatus = EncounteredASection; - std::ostream& stream() { - return *m_os; - } + SectionInfo* thisSection = m_currentSection->findSubSection( name ); + if( !thisSection ) { + thisSection = m_currentSection->addSubSection( name ); + m_changed = true; + } - void ensureTagClosed() { - if( m_tagIsOpen ) { - stream() << ">\n"; - m_tagIsOpen = false; + if( !wasSectionSeen() && thisSection->shouldRun() ) { + m_currentSection = thisSection; + m_lastSectionToRun = NULL; + return true; } + return false; } - void newlineIfNecessary() { - if( m_needsNewline ) { - stream() << "\n"; - m_needsNewline = false; + void endSection( const std::string& ) { + if( m_currentSection->ran() ) { + m_runStatus = RanAtLeastOneSection; + m_changed = true; + } + else if( m_runStatus == EncounteredASection ) { + m_runStatus = RanAtLeastOneSection; + m_lastSectionToRun = m_currentSection; } + m_currentSection = m_currentSection->getParent(); } - void writeEncodedText( const std::string& text ) { - static const char* charsToEncode = "<&\""; - std::string mtext = text; - std::string::size_type pos = mtext.find_first_of( charsToEncode ); - while( pos != std::string::npos ) { - stream() << mtext.substr( 0, pos ); + const TestCaseInfo& getTestCaseInfo() const { + return *m_info; + } - switch( mtext[pos] ) { - case '<': - stream() << "<"; - break; - case '&': - stream() << "&"; - break; - case '\"': - stream() << """; - break; - } - mtext = mtext.substr( pos+1 ); - pos = mtext.find_first_of( charsToEncode ); - } - stream() << mtext; + bool hasUntestedSections() const { + return m_runStatus == RanAtLeastOneSection || + ( m_rootSection.hasUntestedSections() && m_changed ); } - bool m_tagIsOpen; - bool m_needsNewline; - std::vector<std::string> m_tags; - std::string m_indent; - std::ostream* m_os; + private: + const TestCaseInfo* m_info; + RunStatus m_runStatus; + SectionInfo m_rootSection; + SectionInfo* m_currentSection; + SectionInfo* m_lastSectionToRun; + bool m_changed; }; - } + +#include <set> +#include <string> + namespace Catch { - class XmlReporter : public SharedImpl<IReporter> { + + class StreamRedirect { + public: - XmlReporter( const ReporterConfig& config ) : m_config( config ) {} + StreamRedirect( std::ostream& stream, std::string& targetString ) + : m_stream( stream ), + m_prevBuf( stream.rdbuf() ), + m_targetString( targetString ) + { + stream.rdbuf( m_oss.rdbuf() ); + } - static std::string getDescription() { - return "Reports test results as an XML document"; + ~StreamRedirect() { + m_targetString += m_oss.str(); + m_stream.rdbuf( m_prevBuf ); } - virtual ~XmlReporter(); - private: // IReporter + private: + std::ostream& m_stream; + std::streambuf* m_prevBuf; + std::ostringstream m_oss; + std::string& m_targetString; + }; - virtual bool shouldRedirectStdout() const { - return true; - } + /////////////////////////////////////////////////////////////////////////// - virtual void StartTesting() { - m_xml = XmlWriter( m_config.stream ); - m_xml.startElement( "Catch" ); - if( !m_config.name.empty() ) - m_xml.writeAttribute( "name", m_config.name ); - } + class Runner : public IResultCapture, public IRunner { - virtual void EndTesting( const Totals& totals ) { - m_xml.scopedElement( "OverallResults" ) - .writeAttribute( "successes", totals.assertions.passed ) - .writeAttribute( "failures", totals.assertions.failed ); - m_xml.endElement(); - } + Runner( const Runner& ); + void operator =( const Runner& ); - virtual void StartGroup( const std::string& groupName ) { - m_xml.startElement( "Group" ) - .writeAttribute( "name", groupName ); - } + public: - virtual void EndGroup( const std::string&, const Totals& totals ) { - m_xml.scopedElement( "OverallResults" ) - .writeAttribute( "successes", totals.assertions.passed ) - .writeAttribute( "failures", totals.assertions.failed ); - m_xml.endElement(); + explicit Runner( const Config& config, const Ptr<IReporter>& reporter ) + : m_context( getCurrentMutableContext() ), + m_runningTest( NULL ), + m_config( config ), + m_reporter( reporter ), + m_prevRunner( &m_context.getRunner() ), + m_prevResultCapture( &m_context.getResultCapture() ), + m_prevConfig( m_context.getConfig() ) + { + m_context.setRunner( this ); + m_context.setConfig( &m_config ); + m_context.setResultCapture( this ); + m_reporter->StartTesting(); } - virtual void StartSection( const std::string& sectionName, const std::string& description ) { - m_xml.startElement( "Section" ) - .writeAttribute( "name", sectionName ) - .writeAttribute( "description", description ); + virtual ~Runner() { + m_reporter->EndTesting( m_totals ); + m_context.setRunner( m_prevRunner ); + m_context.setConfig( NULL ); + m_context.setResultCapture( m_prevResultCapture ); + m_context.setConfig( m_prevConfig ); } - virtual void EndSection( const std::string& /*sectionName*/, const Counts& assertions ) { - m_xml.scopedElement( "OverallResults" ) - .writeAttribute( "successes", assertions.passed ) - .writeAttribute( "failures", assertions.failed ); - m_xml.endElement(); - } + Totals runMatching( const std::string& testSpec ) { - virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) { - m_xml.startElement( "TestCase" ).writeAttribute( "name", testInfo.getName() ); - m_currentTestSuccess = true; + std::vector<TestCaseInfo> matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( testSpec ); + + Totals totals; + + m_reporter->StartGroup( testSpec ); + + std::vector<TestCaseInfo>::const_iterator it = matchingTests.begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = matchingTests.end(); + for(; it != itEnd; ++it ) + totals += runTest( *it ); + // !TBD use std::accumulate? + + m_reporter->EndGroup( testSpec, totals ); + return totals; } - virtual void Result( const Catch::ResultInfo& resultInfo ) { - if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok ) - return; + Totals runTest( const TestCaseInfo& testInfo ) { + Totals prevTotals = m_totals; - if( resultInfo.hasExpression() ) { - m_xml.startElement( "Expression" ) - .writeAttribute( "success", resultInfo.ok() ) - .writeAttribute( "filename", resultInfo.getFilename() ) - .writeAttribute( "line", resultInfo.getLine() ); + std::string redirectedCout; + std::string redirectedCerr; - m_xml.scopedElement( "Original" ) - .writeText( resultInfo.getExpression() ); - m_xml.scopedElement( "Expanded" ) - .writeText( resultInfo.getExpandedExpression() ); - m_currentTestSuccess &= resultInfo.ok(); - } + m_reporter->StartTestCase( testInfo ); - switch( resultInfo.getResultType() ) { - case ResultWas::ThrewException: - m_xml.scopedElement( "Exception" ) - .writeAttribute( "filename", resultInfo.getFilename() ) - .writeAttribute( "line", resultInfo.getLine() ) - .writeText( resultInfo.getMessage() ); - m_currentTestSuccess = false; - break; - case ResultWas::Info: - m_xml.scopedElement( "Info" ) - .writeText( resultInfo.getMessage() ); - break; - case ResultWas::Warning: - m_xml.scopedElement( "Warning" ) - .writeText( resultInfo.getMessage() ); - break; - case ResultWas::ExplicitFailure: - m_xml.scopedElement( "Failure" ) - .writeText( resultInfo.getMessage() ); - m_currentTestSuccess = false; - break; - case ResultWas::Unknown: - case ResultWas::Ok: - case ResultWas::FailureBit: - case ResultWas::ExpressionFailed: - case ResultWas::Exception: - case ResultWas::DidntThrowException: - break; + m_runningTest = new RunningTest( &testInfo ); + + do { + do { + m_currentResult.setLineInfo( m_runningTest->getTestCaseInfo().getLineInfo() ); + runCurrentTest( redirectedCout, redirectedCerr ); + } + while( m_runningTest->hasUntestedSections() && !aborting() ); } - if( resultInfo.hasExpression() ) - m_xml.endElement(); - } + while( getCurrentContext().advanceGeneratorsForCurrentTest() && !aborting() ); - virtual void Aborted() { - // !TBD - } + delete m_runningTest; + m_runningTest = NULL; - virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string&, const std::string& ) { - m_xml.scopedElement( "OverallResult" ).writeAttribute( "success", m_currentTestSuccess ); - m_xml.endElement(); + Totals deltaTotals = m_totals.delta( prevTotals ); + m_totals.testCases += deltaTotals.testCases; + m_reporter->EndTestCase( testInfo, deltaTotals, redirectedCout, redirectedCerr ); + return deltaTotals; } - private: - ReporterConfig m_config; - bool m_currentTestSuccess; - XmlWriter m_xml; - }; + const Config& config() const { + return m_config; + } -} // end namespace Catch + private: // IResultCapture -// #included from: reporters/catch_reporter_junit.hpp -#define TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED + virtual ResultAction::Value acceptResult( bool result ) { + return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed ); + } -namespace Catch { + virtual ResultAction::Value acceptResult( ResultWas::OfType result ) { + m_currentResult.setResultType( result ); + return actOnCurrentResult(); + } - class JunitReporter : public SharedImpl<IReporter> { + virtual ResultAction::Value acceptExpression( const ResultInfoBuilder& resultInfo ) { + m_currentResult = resultInfo; + return actOnCurrentResult(); + } - struct TestStats { - std::string m_element; - std::string m_resultType; - std::string m_message; - std::string m_content; - }; + virtual void acceptMessage( const std::string& msg ) { + m_currentResult.setMessage( msg ); + } - struct TestCaseStats { + virtual void testEnded( const ResultInfo& result ) { + if( result.getResultType() == ResultWas::Ok ) { + m_totals.assertions.passed++; + } + else if( !result.ok() ) { + m_totals.assertions.failed++; - TestCaseStats( const std::string& name = std::string() ) :m_name( name ){} + std::vector<ResultInfo>::const_iterator it = m_info.begin(); + std::vector<ResultInfo>::const_iterator itEnd = m_info.end(); + for(; it != itEnd; ++it ) + m_reporter->Result( *it ); + m_info.clear(); + } - double m_timeInSeconds; - std::string m_status; - std::string m_className; - std::string m_name; - std::vector<TestStats> m_testStats; - }; + if( result.getResultType() == ResultWas::Info ) + m_info.push_back( result ); + else + m_reporter->Result( result ); + } - struct Stats { + virtual bool sectionStarted ( + const std::string& name, + const std::string& description, + const SourceLineInfo& lineInfo, + Counts& assertions + ) + { + std::ostringstream oss; + oss << name << "@" << lineInfo; - Stats( const std::string& name = std::string() ) - : m_testsCount( 0 ), - m_failuresCount( 0 ), - m_disabledCount( 0 ), - m_errorsCount( 0 ), - m_timeInSeconds( 0 ), - m_name( name ) - {} + if( !m_runningTest->addSection( oss.str() ) ) + return false; - std::size_t m_testsCount; - std::size_t m_failuresCount; - std::size_t m_disabledCount; - std::size_t m_errorsCount; - double m_timeInSeconds; - std::string m_name; + m_currentResult.setLineInfo( lineInfo ); + m_reporter->StartSection( name, description ); + assertions = m_totals.assertions; - std::vector<TestCaseStats> m_testCaseStats; - }; + return true; + } - public: - JunitReporter( const ReporterConfig& config ) - : m_config( config ), - m_testSuiteStats( "AllTests" ), - m_currentStats( &m_testSuiteStats ) - {} - virtual ~JunitReporter(); + virtual void sectionEnded( const std::string& name, const Counts& prevAssertions ) { + m_runningTest->endSection( name ); + m_reporter->EndSection( name, m_totals.assertions - prevAssertions ); + } - static std::string getDescription() { - return "Reports test results in an XML format that looks like Ant's junitreport target"; + virtual void pushScopedInfo( ScopedInfo* scopedInfo ) { + m_scopedInfos.push_back( scopedInfo ); } - private: // IReporter + virtual void popScopedInfo( ScopedInfo* scopedInfo ) { + if( m_scopedInfos.back() == scopedInfo ) + m_scopedInfos.pop_back(); + } - virtual bool shouldRedirectStdout() const { - return true; + virtual bool shouldDebugBreak() const { + return m_config.shouldDebugBreak(); } - virtual void StartTesting(){} + virtual std::string getCurrentTestName() const { + return m_runningTest + ? m_runningTest->getTestCaseInfo().getName() + : ""; + } - virtual void StartGroup( const std::string& groupName ) { - m_statsForSuites.push_back( Stats( groupName ) ); - m_currentStats = &m_statsForSuites.back(); + virtual const ResultInfo* getLastResult() const { + return &m_lastResult; } - virtual void EndGroup( const std::string&, const Totals& totals ) { - m_currentStats->m_testsCount = totals.assertions.total(); - m_currentStats = &m_testSuiteStats; + public: + // !TBD We need to do this another way! + bool aborting() const { + return m_totals.assertions.failed == static_cast<std::size_t>( m_config.getCutoff() ); } - virtual void StartSection( const std::string&, const std::string& ){} + private: - virtual void EndSection( const std::string&, const Counts& ){} + ResultAction::Value actOnCurrentResult() { + testEnded( m_currentResult ); + m_lastResult = m_currentResult; - virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) { - m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getName() ) ); - } + m_currentResult = ResultInfoBuilder(); - virtual void Result( const Catch::ResultInfo& resultInfo ) { - if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults ) { - TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back(); - TestStats stats; - std::ostringstream oss; - if( !resultInfo.getMessage().empty() ) - oss << resultInfo.getMessage() << " at "; - oss << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() ); - stats.m_content = oss.str(); - stats.m_message = resultInfo.getExpandedExpression(); - stats.m_resultType = resultInfo.getTestMacroName(); + ResultAction::Value action = ResultAction::None; - switch( resultInfo.getResultType() ) { - case ResultWas::ThrewException: - stats.m_element = "error"; - m_currentStats->m_errorsCount++; - break; - case ResultWas::Info: - stats.m_element = "info"; // !TBD ? - break; - case ResultWas::Warning: - stats.m_element = "warning"; // !TBD ? - break; - case ResultWas::ExplicitFailure: - stats.m_element = "failure"; - m_currentStats->m_failuresCount++; - break; - case ResultWas::ExpressionFailed: - stats.m_element = "failure"; - m_currentStats->m_failuresCount++; - break; - case ResultWas::Ok: - stats.m_element = "success"; - break; - case ResultWas::Unknown: - case ResultWas::FailureBit: - case ResultWas::Exception: - case ResultWas::DidntThrowException: - break; - } - testCaseStats.m_testStats.push_back( stats ); + if( !m_lastResult.ok() ) { + action = ResultAction::Failed; + if( shouldDebugBreak() ) + action = (ResultAction::Value)( action | ResultAction::Debug ); + if( aborting() ) + action = (ResultAction::Value)( action | ResultAction::Abort ); } + return action; } - virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string& stdOut, const std::string& stdErr ) { - if( !stdOut.empty() ) - m_stdOut << stdOut << "\n"; - if( !stdErr.empty() ) - m_stdErr << stdErr << "\n"; + void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ) { + try { + m_runningTest->reset(); + if( m_reporter->shouldRedirectStdout() ) { + StreamRedirect coutRedir( std::cout, redirectedCout ); + StreamRedirect cerrRedir( std::cerr, redirectedCerr ); + m_runningTest->getTestCaseInfo().invoke(); + } + else { + m_runningTest->getTestCaseInfo().invoke(); + } + m_runningTest->ranToCompletion(); + } + catch( TestFailureException& ) { + // This just means the test was aborted due to failure + } + catch(...) { + acceptMessage( getRegistryHub().getExceptionTranslatorRegistry().translateActiveException() ); + acceptResult( ResultWas::ThrewException ); + } + m_info.clear(); } - virtual void Aborted() { - // !TBD - } + private: + IMutableContext& m_context; + RunningTest* m_runningTest; + ResultInfoBuilder m_currentResult; + ResultInfo m_lastResult; - virtual void EndTesting( const Totals& ) { - std::ostream& str = m_config.stream; - { - XmlWriter xml( str ); + const Config& m_config; + Totals m_totals; + Ptr<IReporter> m_reporter; + std::vector<ScopedInfo*> m_scopedInfos; + std::vector<ResultInfo> m_info; + IRunner* m_prevRunner; + IResultCapture* m_prevResultCapture; + const IConfig* m_prevConfig; + }; - if( m_statsForSuites.size() > 0 ) - xml.startElement( "testsuites" ); +} // end namespace Catch - std::vector<Stats>::const_iterator it = m_statsForSuites.begin(); - std::vector<Stats>::const_iterator itEnd = m_statsForSuites.end(); +#include <fstream> +#include <stdlib.h> +#include <limits> - for(; it != itEnd; ++it ) { - XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" ); - xml.writeAttribute( "name", it->m_name ); - xml.writeAttribute( "errors", it->m_errorsCount ); - xml.writeAttribute( "failures", it->m_failuresCount ); - xml.writeAttribute( "tests", it->m_testsCount ); - xml.writeAttribute( "hostname", "tbd" ); - xml.writeAttribute( "time", "tbd" ); - xml.writeAttribute( "timestamp", "tbd" ); +namespace Catch { - OutputTestCases( xml, *it ); - } + class Runner2 { // This will become Runner when Runner becomes Context - xml.scopedElement( "system-out" ).writeText( trim( m_stdOut.str() ) ); - xml.scopedElement( "system-err" ).writeText( trim( m_stdErr.str() ) ); - } + public: + Runner2( Config& configWrapper ) + : m_configWrapper( configWrapper ), + m_config( configWrapper.data() ) + { + resolveStream(); + makeReporter(); } - void OutputTestCases( XmlWriter& xml, const Stats& stats ) { - std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin(); - std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end(); - for(; it != itEnd; ++it ) { - xml.writeBlankLine(); - xml.writeComment( "Test case" ); + Totals runTests() { + + std::vector<TestCaseFilters> filterGroups = m_config.filters; + if( filterGroups.empty() ) { + TestCaseFilters filterGroup( "" ); + filterGroup.addFilter( TestCaseFilter( "./*", IfFilterMatches::ExcludeTests ) ); + filterGroups.push_back( filterGroup ); + } - XmlWriter::ScopedElement e = xml.scopedElement( "testcase" ); - xml.writeAttribute( "classname", it->m_className ); - xml.writeAttribute( "name", it->m_name ); - xml.writeAttribute( "time", "tbd" ); + Runner context( m_configWrapper, m_reporter ); // This Runner will be renamed Context + Totals totals; - OutputTestResult( xml, *it ); + std::vector<TestCaseFilters>::const_iterator it = filterGroups.begin(); + std::vector<TestCaseFilters>::const_iterator itEnd = filterGroups.end(); + for(; it != itEnd; ++it ) { + m_reporter->StartGroup( it->getName() ); + runTestsForGroup( context, *it ); + if( context.aborting() ) + m_reporter->Aborted(); + m_reporter->EndGroup( it->getName(), totals ); } + return totals; } - void OutputTestResult( XmlWriter& xml, const TestCaseStats& stats ) { - std::vector<TestStats>::const_iterator it = stats.m_testStats.begin(); - std::vector<TestStats>::const_iterator itEnd = stats.m_testStats.end(); + Totals runTestsForGroup( Runner& context, const TestCaseFilters& filterGroup ) { + Totals totals; + std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end(); + int testsRunForGroup = 0; for(; it != itEnd; ++it ) { - if( it->m_element != "success" ) { - XmlWriter::ScopedElement e = xml.scopedElement( it->m_element ); + if( filterGroup.shouldInclude( *it ) ) { + testsRunForGroup++; + if( m_testsAlreadyRun.find( *it ) == m_testsAlreadyRun.end() ) { - xml.writeAttribute( "message", it->m_message ); - xml.writeAttribute( "type", it->m_resultType ); - if( !it->m_content.empty() ) - xml.writeText( it->m_content ); + if( context.aborting() ) + break; + + totals += context.runTest( *it ); + m_testsAlreadyRun.insert( *it ); + } } } + if( testsRunForGroup == 0 ) + std::cerr << "\n[No test cases matched with: " << filterGroup.getName() << "]" << std::endl; + return totals; + } private: - ReporterConfig m_config; - bool m_currentTestSuccess; + void resolveStream() { + if( !m_config.stream.empty() ) { + if( m_config.stream[0] == '%' ) + m_configWrapper.useStream( m_config.stream.substr( 1 ) ); + else + m_configWrapper.setFilename( m_config.stream ); + } + // Open output file, if specified + if( !m_config.outputFilename.empty() ) { + m_ofs.open( m_config.outputFilename.c_str() ); + if( m_ofs.fail() ) { + std::ostringstream oss; + oss << "Unable to open file: '" << m_config.outputFilename << "'"; + throw std::domain_error( oss.str() ); + } + m_configWrapper.setStreamBuf( m_ofs.rdbuf() ); + } + } + void makeReporter() { + std::string reporterName = m_config.reporter.empty() + ? "basic" + : m_config.reporter; - Stats m_testSuiteStats; - Stats* m_currentStats; - std::vector<Stats> m_statsForSuites; - std::ostringstream m_stdOut; - std::ostringstream m_stdErr; + ReporterConfig reporterConfig( m_config.name, m_configWrapper.stream(), m_config.includeWhichResults == Include::SuccessfulResults ); + + m_reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig ); + if( !m_reporter ) { + std::ostringstream oss; + oss << "No reporter registered with name: '" << reporterName << "'"; + throw std::domain_error( oss.str() ); + } + } + + private: + Config& m_configWrapper; + const ConfigData& m_config; + std::ofstream m_ofs; + Ptr<IReporter> m_reporter; + std::set<TestCaseInfo> m_testsAlreadyRun; }; -} // end namespace Catch + inline int Main( Config& configWrapper ) { + int result = 0; + try + { + Runner2 runner( configWrapper ); -// #included from: internal/catch_commandline.hpp -#define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED + const ConfigData& config = configWrapper.data(); -// #included from: catch_config.hpp -#define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED + // Handle list request + if( config.listSpec != List::None ) { + List( config ); + return 0; + } -#include <memory> -#include <vector> -#include <string> -#include <iostream> + result = static_cast<int>( runner.runTests().assertions.failed ); -namespace Catch { + } + catch( std::exception& ex ) { + std::cerr << ex.what() << std::endl; + result = (std::numeric_limits<int>::max)(); + } - struct Include { enum WhichResults { - FailedOnly, - SuccessfulResults - }; }; + Catch::cleanUp(); + return result; + } - struct List{ enum What { - None = 0, + inline void showUsage( std::ostream& os ) { + os << "\t-?, -h, --help\n" + << "\t-l, --list [all | tests | reporters [xml]]\n" + << "\t-t, --test <testspec> [<testspec>...]\n" + << "\t-r, --reporter <reporter name>\n" + << "\t-o, --out <file name>|<%stream name>\n" + << "\t-s, --success\n" + << "\t-b, --break\n" + << "\t-n, --name <name>\n" + << "\t-a, --abort [#]\n" + << "\t-nt, --nothrow\n\n" + << "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl; + } + inline void showHelp( std::string exeName ) { + std::string::size_type pos = exeName.find_last_of( "/\\" ); + if( pos != std::string::npos ) { + exeName = exeName.substr( pos+1 ); + } - Reports = 1, - Tests = 2, - All = 3, + std::cout << exeName << " is a CATCH host application. Options are as follows:\n\n"; + showUsage( std::cout ); + } - WhatMask = 0xf, + inline int Main( int argc, char* const argv[], Config& config ) { - AsText = 0x10, - AsXml = 0x11, + try { + CommandParser parser( argc, argv ); - AsMask = 0xf0 - }; }; + if( Command cmd = parser.find( "-h", "-?", "--help" ) ) { + if( cmd.argsCount() != 0 ) + cmd.raiseError( "Does not accept arguments" ); - struct ConfigData { - ConfigData() - : listSpec( List::None ), - shouldDebugBreak( false ), - includeWhichResults( Include::FailedOnly ), - cutoff( -1 ), - allowThrows( true ) - {} + showHelp( argv[0] ); + Catch::cleanUp(); + return 0; + } - std::string reporter; - std::string outputFilename; - List::What listSpec; - std::vector<std::string> testSpecs; - bool shouldDebugBreak; - std::string stream; - Include::WhichResults includeWhichResults; - std::string name; - int cutoff; - bool allowThrows; - }; + parseIntoConfig( parser, config.data() ); + } + catch( std::exception& ex ) { + std::cerr << ex.what() << "\n\nUsage: ...\n\n"; + showUsage( std::cerr ); + Catch::cleanUp(); + return (std::numeric_limits<int>::max)(); + } - class Config : public IConfig { - private: - Config( const Config& other ); - Config& operator = ( const Config& other ); - virtual void dummy(); - public: + return Main( config ); + } - Config() - : m_streambuf( NULL ), - m_os( std::cout.rdbuf() ) - {} + inline int Main( int argc, char* const argv[] ) { + Config config; +// !TBD: This doesn't always work, for some reason +// if( isDebuggerActive() ) +// config.useStream( "debug" ); + return Main( argc, argv, config ); + } - Config( const ConfigData& data ) - : m_data( data ), - m_streambuf( NULL ), - m_os( std::cout.rdbuf() ) - {} +} // end namespace Catch - virtual ~Config() { - m_os.rdbuf( std::cout.rdbuf() ); - delete m_streambuf; - } +// #included from: catch_registry_hub.hpp - void setFilename( const std::string& filename ) { - m_data.outputFilename = filename; - } +// #included from: catch_test_case_registry_impl.hpp - bool testsSpecified() const { - return !m_data.testSpecs.empty(); - } +#include <vector> +#include <set> +#include <sstream> +#include <iostream> - const std::vector<std::string>& getTestSpecs() const { - return m_data.testSpecs; - } +namespace Catch { - List::What getListSpec( void ) const { - return m_data.listSpec; - } + class TestRegistry : public ITestCaseRegistry { + public: + TestRegistry() : m_unnamedCount( 0 ) {} + virtual ~TestRegistry(); - const std::string& getFilename() const { - return m_data.outputFilename ; - } + virtual void registerTest( const TestCaseInfo& testInfo ) { + if( testInfo.getName() == "" ) { + std::ostringstream oss; + oss << testInfo.getName() << "unnamed/" << ++m_unnamedCount; + return registerTest( TestCaseInfo( testInfo, oss.str() ) ); + } - List::What listWhat() const { - return static_cast<List::What>( m_data.listSpec & List::WhatMask ); + if( m_functions.find( testInfo ) == m_functions.end() ) { + m_functions.insert( testInfo ); + m_functionsInOrder.push_back( testInfo ); + if( !testInfo.isHidden() ) + m_nonHiddenFunctions.push_back( testInfo ); + } + else { + const TestCaseInfo& prev = *m_functions.find( testInfo ); + std::cerr << "error: TEST_CASE( \"" << testInfo.getName() << "\" ) already defined.\n" + << "\tFirst seen at " << SourceLineInfo( prev.getLineInfo() ) << "\n" + << "\tRedefined at " << SourceLineInfo( testInfo.getLineInfo() ) << std::endl; + exit(1); + } } - List::What listAs() const { - return static_cast<List::What>( m_data.listSpec & List::AsMask ); + virtual const std::vector<TestCaseInfo>& getAllTests() const { + return m_functionsInOrder; } - std::string getName() const { - return m_data.name; + virtual const std::vector<TestCaseInfo>& getAllNonHiddenTests() const { + return m_nonHiddenFunctions; } - bool shouldDebugBreak() const { - return m_data.shouldDebugBreak; + // !TBD deprecated + virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const { + std::vector<TestCaseInfo> matchingTests; + getMatchingTestCases( rawTestSpec, matchingTests ); + return matchingTests; } - virtual std::ostream& stream() const { - return m_os; - } + // !TBD deprecated + virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const { + TestCaseFilter filter( rawTestSpec ); - void setStreamBuf( std::streambuf* buf ) { - m_os.rdbuf( buf ? buf : std::cout.rdbuf() ); + std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end(); + for(; it != itEnd; ++it ) { + if( filter.shouldInclude( *it ) ) { + matchingTestsOut.push_back( *it ); + } + } } - - void useStream( const std::string& streamName ) { - std::streambuf* newBuf = createStreamBuf( streamName ); - setStreamBuf( newBuf ); - delete m_streambuf; - m_streambuf = newBuf; + virtual void getMatchingTestCases( const TestCaseFilters& filters, std::vector<TestCaseInfo>& matchingTestsOut ) const { + std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin(); + std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end(); + // !TBD: replace with algorithm + for(; it != itEnd; ++it ) + if( filters.shouldInclude( *it ) ) + matchingTestsOut.push_back( *it ); } - virtual bool includeSuccessfulResults() const { - return m_data.includeWhichResults == Include::SuccessfulResults; - } + private: - int getCutoff() const { - return m_data.cutoff; - } + std::set<TestCaseInfo> m_functions; + std::vector<TestCaseInfo> m_functionsInOrder; + std::vector<TestCaseInfo> m_nonHiddenFunctions; + size_t m_unnamedCount; + }; + + /////////////////////////////////////////////////////////////////////////// - virtual bool allowThrows() const { - return m_data.allowThrows; - } + class FreeFunctionTestCase : public SharedImpl<ITestCase> { + public: - ConfigData& data() { - return m_data; + FreeFunctionTestCase( TestFunction fun ) : m_fun( fun ) {} + + virtual void invoke() const { + m_fun(); } private: - ConfigData m_data; + virtual ~FreeFunctionTestCase(); - // !TBD Move these out of here - std::streambuf* m_streambuf; - mutable std::ostream m_os; + TestFunction m_fun; }; + /////////////////////////////////////////////////////////////////////////// + + AutoReg::AutoReg( TestFunction function, + const char* name, + const char* description, + const SourceLineInfo& lineInfo ) { + registerTestCase( new FreeFunctionTestCase( function ), name, description, lineInfo ); + } + + AutoReg::~AutoReg() {} + + void AutoReg::registerTestCase( ITestCase* testCase, + const char* name, + const char* description, + const SourceLineInfo& lineInfo ) { + getMutableRegistryHub().registerTest( TestCaseInfo( testCase, name, description, lineInfo ) ); + } + } // end namespace Catch +// #included from: catch_reporter_registry.hpp +#define TWOBLUECUBES_CATCH_REPORTER_REGISTRY_HPP_INCLUDED + +#include <map> + namespace Catch { - class Command { - public: - Command(){} + class ReporterRegistry : public IReporterRegistry { - explicit Command( const std::string& name ) : m_name( name ) {} + public: - Command& operator += ( const std::string& arg ) { - m_args.push_back( arg ); - return *this; - } - Command& operator += ( const Command& other ) { - std::copy( other.m_args.begin(), other.m_args.end(), std::back_inserter( m_args ) ); - if( m_name.empty() ) - m_name = other.m_name; - return *this; - } - Command operator + ( const Command& other ) { - Command newCommand( *this ); - newCommand += other; - return newCommand; + virtual ~ReporterRegistry() { + deleteAllValues( m_factories ); } - operator SafeBool::type() const { - return SafeBool::makeSafe( !m_name.empty() ); + virtual IReporter* create( const std::string& name, const ReporterConfig& config ) const { + FactoryMap::const_iterator it = m_factories.find( name ); + if( it == m_factories.end() ) + return NULL; + return it->second->create( config ); } - std::string name() const { return m_name; } - std::string operator[]( std::size_t i ) const { return m_args[i]; } - std::size_t argsCount() const { return m_args.size(); } + void registerReporter( const std::string& name, IReporterFactory* factory ) { + m_factories.insert( std::make_pair( name, factory ) ); + } - CATCH_ATTRIBUTE_NORETURN - void raiseError( const std::string& message ) const { - std::ostringstream oss; - oss << "Error while parsing " << m_name << ". " << message << "."; - if( m_args.size() > 0 ) - oss << " Arguments where:"; - for( std::size_t i = 0; i < m_args.size(); ++i ) - oss << " " << m_args[i]; - throw std::domain_error( oss.str() ); + const FactoryMap& getFactories() const { + return m_factories; } private: - - std::string m_name; - std::vector<std::string> m_args; + FactoryMap m_factories; }; +} - class CommandParser { - public: - CommandParser( int argc, char const * const * argv ) : m_argc( static_cast<std::size_t>( argc ) ), m_argv( argv ) {} +// #included from: catch_exception_translator_registry.hpp +#define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_HPP_INCLUDED - Command find( const std::string& arg1, const std::string& arg2, const std::string& arg3 ) const { - return find( arg1 ) + find( arg2 ) + find( arg3 ); - } +#ifdef __OBJC__ +#import "Foundation/Foundation.h" +#endif - Command find( const std::string& shortArg, const std::string& longArg ) const { - return find( shortArg ) + find( longArg ); - } - Command find( const std::string& arg ) const { - for( std::size_t i = 0; i < m_argc; ++i ) - if( m_argv[i] == arg ) - return getArgs( i ); - return Command(); - } +namespace Catch { - private: - Command getArgs( std::size_t from ) const { - Command command( m_argv[from] ); - for( std::size_t i = from+1; i < m_argc && m_argv[i][0] != '-'; ++i ) - command += m_argv[i]; - return command; + class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { + public: + ~ExceptionTranslatorRegistry() { + deleteAll( m_translators ); } - std::size_t m_argc; - char const * const * m_argv; - }; + virtual void registerTranslator( const IExceptionTranslator* translator ) { + m_translators.push_back( translator ); + } - inline void parseIntoConfig( const CommandParser& parser, ConfigData& config ) { + 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(); + } + catch( std::string& msg ) { + return msg; + } + catch( const char* msg ) { + return msg; + } + catch(...) { + return tryTranslators( m_translators.begin() ); + } + } - if( Command cmd = parser.find( "-l", "--list" ) ) { - if( cmd.argsCount() > 2 ) - cmd.raiseError( "Expected upto 2 arguments" ); + std::string tryTranslators( std::vector<const IExceptionTranslator*>::const_iterator it ) const { + if( it == m_translators.end() ) + return "Unknown exception"; - List::What listSpec = List::All; - if( cmd.argsCount() >= 1 ) { - if( cmd[0] == "tests" ) - config.listSpec = List::Tests; - else if( cmd[0] == "reporters" ) - config.listSpec = List::Reports; - else - cmd.raiseError( "Expected [tests] or [reporters]" ); + try { + return (*it)->translate(); } - if( cmd.argsCount() >= 2 ) { - if( cmd[1] == "xml" ) - config.listSpec = static_cast<List::What>( listSpec | List::AsXml ); - else if( cmd[1] == "text" ) - config.listSpec = static_cast<List::What>( listSpec | List::AsText ); - else - cmd.raiseError( "Expected [xml] or [text]" ); + catch(...) { + return tryTranslators( it+1 ); } } - if( Command cmd = parser.find( "-t", "--test" ) ) { - if( cmd.argsCount() == 0 ) - cmd.raiseError( "Expected at least one argument" ); - for( std::size_t i = 0; i < cmd.argsCount(); ++i ) - config.testSpecs.push_back( cmd[i] ); - } + private: + std::vector<const IExceptionTranslator*> m_translators; + }; +} - if( Command cmd = parser.find( "-r", "--reporter" ) ) { - if( cmd.argsCount() != 1 ) - cmd.raiseError( "Expected one argument" ); - config.reporter = cmd[0]; - } +namespace Catch { - if( Command cmd = parser.find( "-o", "--out" ) ) { - if( cmd.argsCount() == 0 ) - cmd.raiseError( "Expected filename" ); - if( cmd[0][0] == '%' ) - config.stream = cmd[0].substr( 1 ); - else - config.outputFilename = cmd[0]; - } + namespace { - if( Command cmd = parser.find( "-s", "--success" ) ) { - if( cmd.argsCount() != 0 ) - cmd.raiseError( "Does not accept arguments" ); - config.includeWhichResults = Include::SuccessfulResults; - } + class RegistryHub : public IRegistryHub, public IMutableRegistryHub { - if( Command cmd = parser.find( "-b", "--break" ) ) { - if( cmd.argsCount() != 0 ) - cmd.raiseError( "Does not accept arguments" ); - config.shouldDebugBreak = true; - } + RegistryHub( const RegistryHub& ); + void operator=( const RegistryHub& ); - if( Command cmd = parser.find( "-n", "--name" ) ) { - if( cmd.argsCount() != 1 ) - cmd.raiseError( "Expected a name" ); - config.name = cmd[0]; - } + public: // IRegistryHub + RegistryHub() { + } + virtual const IReporterRegistry& getReporterRegistry() const { + return m_reporterRegistry; + } + virtual const ITestCaseRegistry& getTestCaseRegistry() const { + return m_testCaseRegistry; + } + virtual IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() { + return m_exceptionTranslatorRegistry; + } - if( Command cmd = parser.find( "-a", "--abort" ) ) { - if( cmd.argsCount() > 1 ) - cmd.raiseError( "Only accepts 0-1 arguments" ); - int threshold = 1; - if( cmd.argsCount() == 1 ) { - std::stringstream ss; - ss << cmd[0]; - ss >> threshold; - if( ss.fail() || threshold <= 0 ) - cmd.raiseError( "threshold must be a number greater than zero" ); + public: // IMutableRegistryHub + virtual void registerReporter( const std::string& name, IReporterFactory* factory ) { + m_reporterRegistry.registerReporter( name, factory ); } - config.cutoff = threshold; - } + virtual void registerTest( const TestCaseInfo& testInfo ) { + m_testCaseRegistry.registerTest( testInfo ); + } + virtual void registerTranslator( const IExceptionTranslator* translator ) { + m_exceptionTranslatorRegistry.registerTranslator( translator ); + } + + private: + TestRegistry m_testCaseRegistry; + ReporterRegistry m_reporterRegistry; + ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; + }; - if( Command cmd = parser.find( "-nt", "--nothrow" ) ) { - if( cmd.argsCount() != 0 ) - cmd.raiseError( "Does not accept arguments" ); - config.allowThrows = false; + // Single, global, instance + inline RegistryHub*& getTheRegistryHub() { + static RegistryHub* theRegistryHub = NULL; + if( !theRegistryHub ) + theRegistryHub = new RegistryHub(); + return theRegistryHub; } + } + IRegistryHub& getRegistryHub() { + return *getTheRegistryHub(); + } + IMutableRegistryHub& getMutableRegistryHub() { + return *getTheRegistryHub(); + } + void cleanUp() { + delete getTheRegistryHub(); + getTheRegistryHub() = NULL; + cleanUpContext(); } } // end namespace Catch +// #included from: catch_notimplemented_exception.hpp +#define TWOBLUECUBES_CATCH_NOTIMPLEMENTED_EXCEPTION_HPP_INCLUDED -// #included from: internal/catch_list.hpp -#define TWOBLUECUBES_CATCH_LIST_HPP_INCLUDED - -#include <limits> +#include <ostream> namespace Catch { - inline int List( const ConfigData& config ) { - - if( config.listSpec & List::Reports ) { - std::cout << "Available reports:\n"; - IReporterRegistry::FactoryMap::const_iterator it = getRegistryHub().getReporterRegistry().getFactories().begin(); - IReporterRegistry::FactoryMap::const_iterator itEnd = getRegistryHub().getReporterRegistry().getFactories().end(); - for(; it != itEnd; ++it ) { - // !TBD: consider listAs() - std::cout << "\t" << it->first << "\n\t\t'" << it->second->getDescription() << "'\n"; - } - std::cout << std::endl; - } - - if( config.listSpec & List::Tests ) { - std::cout << "Available tests:\n"; - std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin(); - std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end(); - for(; it != itEnd; ++it ) { - // !TBD: consider listAs() - std::cout << "\t" << it->getName() << "\n\t\t '" << it->getDescription() << "'\n"; - } - std::cout << std::endl; - } - if( ( config.listSpec & List::All ) == 0 ) { - std::cerr << "Unknown list type" << std::endl; - return (std::numeric_limits<int>::max)(); - } + NotImplementedException::NotImplementedException( const SourceLineInfo& lineInfo ) + : m_lineInfo( lineInfo ) { + std::ostringstream oss; + oss << lineInfo << "function "; + if( !lineInfo.function.empty() ) + oss << lineInfo.function << " "; + oss << "not implemented"; + m_what = oss.str(); + } - return 0; + const char* NotImplementedException::what() const throw() { + return m_what.c_str(); } } // end namespace Catch -// #included from: internal/catch_runner_impl.hpp -#define TWOBLUECUBES_INTERNAL_CATCH_RUNNER_HPP_INCLUDED - -// #included from: catch_running_test.hpp -#define TWOBLUECUBES_INTERNAL_CATCH_RUNNING_TEST_HPP_INCLUDED +// #included from: catch_context_impl.hpp -// #included from: catch_section_info.hpp -#define TWOBLUECUBES_INTERNAL_CATCH_SECTION_INFO_HPP_INCLUDED +// #included from: catch_stream.hpp +#define TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED -#include <map> -#include <string> +#include <stdexcept> +#include <cstdio> namespace Catch { - class SectionInfo { - public: - - enum Status { - Root, - Unknown, - Branch, - TestedBranch, - TestedLeaf - }; - - SectionInfo( SectionInfo* parent ) - : m_status( Unknown ), - m_parent( parent ) - {} - - SectionInfo() - : m_status( Root ), - m_parent( NULL ) - {} - - ~SectionInfo() { - deleteAllValues( m_subSections ); - } + template<typename WriterF, size_t bufferSize=256> + class StreamBufImpl : public StreamBufBase { + char data[bufferSize]; + WriterF m_writer; - bool shouldRun() const { - return m_status < TestedBranch; + public: + StreamBufImpl() { + setp( data, data + sizeof(data) ); } - bool ran() { - if( m_status < Branch ) { - m_status = TestedLeaf; - return true; - } - return false; + ~StreamBufImpl() { + sync(); } - void ranToCompletion() { - if( m_status == Branch && !hasUntestedSections() ) - m_status = TestedBranch; - } + private: + int overflow( int c ) { + sync(); - SectionInfo* findSubSection( const std::string& name ) { - std::map<std::string, SectionInfo*>::const_iterator it = m_subSections.find( name ); - return it != m_subSections.end() - ? it->second - : NULL; + if( c != EOF ) { + if( pbase() == epptr() ) + m_writer( std::string( 1, static_cast<char>( c ) ) ); + else + sputc( static_cast<char>( c ) ); + } + return 0; } - SectionInfo* addSubSection( const std::string& name ) { - SectionInfo* subSection = new SectionInfo( this ); - m_subSections.insert( std::make_pair( name, subSection ) ); - m_status = Branch; - return subSection; + int sync() { + if( pbase() != pptr() ) { + m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) ); + setp( pbase(), epptr() ); + } + return 0; } + }; - SectionInfo* getParent() { - return m_parent; - } + /////////////////////////////////////////////////////////////////////////// - bool hasUntestedSections() const { - if( m_status == Unknown ) - return true; + struct OutputDebugWriter { - std::map<std::string, SectionInfo*>::const_iterator it = m_subSections.begin(); - std::map<std::string, SectionInfo*>::const_iterator itEnd = m_subSections.end(); - for(; it != itEnd; ++it ) { - if( it->second->hasUntestedSections() ) - return true; - } - return false; + void operator()( const std::string &str ) { + writeToDebugConsole( str ); } - - private: - Status m_status; - std::map<std::string, SectionInfo*> m_subSections; - SectionInfo* m_parent; }; } namespace Catch { - class RunningTest { - - enum RunStatus { - NothingRun, - EncounteredASection, - RanAtLeastOneSection, - RanToCompletionWithSections, - RanToCompletionWithNoSections - }; - - public: - explicit RunningTest( const TestCaseInfo* info = NULL ) - : m_info( info ), - m_runStatus( RanAtLeastOneSection ), - m_currentSection( &m_rootSection ), - m_changed( false ) - {} - - bool wasSectionSeen() const { - return m_runStatus == RanAtLeastOneSection || - m_runStatus == RanToCompletionWithSections; - } + class Context : public IMutableContext { - void reset() { - m_runStatus = NothingRun; - m_changed = false; - m_lastSectionToRun = NULL; - } + Context() : m_config( NULL ) {} + Context( const Context& ); + void operator=( const Context& ); - void ranToCompletion() { - if( m_runStatus == RanAtLeastOneSection || - m_runStatus == EncounteredASection ) { - m_runStatus = RanToCompletionWithSections; - if( m_lastSectionToRun ) { - m_lastSectionToRun->ranToCompletion(); - m_changed = true; - } - } - else { - m_runStatus = RanToCompletionWithNoSections; - } + public: // IContext + virtual IResultCapture& getResultCapture() { + return *m_resultCapture; } - - bool addSection( const std::string& name ) { - if( m_runStatus == NothingRun ) - m_runStatus = EncounteredASection; - - SectionInfo* thisSection = m_currentSection->findSubSection( name ); - if( !thisSection ) { - thisSection = m_currentSection->addSubSection( name ); - m_changed = true; - } - - if( !wasSectionSeen() && thisSection->shouldRun() ) { - m_currentSection = thisSection; - m_lastSectionToRun = NULL; - return true; - } - return false; + virtual IRunner& getRunner() { + return *m_runner; } - - void endSection( const std::string& ) { - if( m_currentSection->ran() ) { - m_runStatus = RanAtLeastOneSection; - m_changed = true; - } - else if( m_runStatus == EncounteredASection ) { - m_runStatus = RanAtLeastOneSection; - m_lastSectionToRun = m_currentSection; - } - m_currentSection = m_currentSection->getParent(); + virtual size_t getGeneratorIndex( const std::string& fileInfo, size_t totalSize ) { + return getGeneratorsForCurrentTest() + .getGeneratorInfo( fileInfo, totalSize ) + .getCurrentIndex(); } - - const TestCaseInfo& getTestCaseInfo() const { - return *m_info; + virtual bool advanceGeneratorsForCurrentTest() { + IGeneratorsForTest* generators = findGeneratorsForCurrentTest(); + return generators && generators->moveNext(); } - bool hasUntestedSections() const { - return m_runStatus == RanAtLeastOneSection || - ( m_rootSection.hasUntestedSections() && m_changed ); + virtual const IConfig* getConfig() const { + return m_config; } - private: - const TestCaseInfo* m_info; - RunStatus m_runStatus; - SectionInfo m_rootSection; - SectionInfo* m_currentSection; - SectionInfo* m_lastSectionToRun; - bool m_changed; - }; -} - -// #included from: catch_test_spec.h -#define TWOBLUECUBES_CATCH_TESTSPEC_H_INCLUDED - -#include <string> + public: // IMutableContext + virtual void setResultCapture( IResultCapture* resultCapture ) { + m_resultCapture = resultCapture; + } + virtual void setRunner( IRunner* runner ) { + m_runner = runner; + } + virtual void setConfig( const IConfig* config ) { + m_config = config; + } -namespace Catch { + friend IMutableContext& getCurrentMutableContext(); - class TestSpec { - public: - TestSpec( const std::string& rawSpec ) - : m_rawSpec( rawSpec ), - m_isWildcarded( false ) { + private: + IGeneratorsForTest* findGeneratorsForCurrentTest() { + std::string testName = getResultCapture().getCurrentTestName(); - if( m_rawSpec[m_rawSpec.size()-1] == '*' ) { - m_rawSpec = m_rawSpec.substr( 0, m_rawSpec.size()-1 ); - m_isWildcarded = true; - } + std::map<std::string, IGeneratorsForTest*>::const_iterator it = + m_generatorsByTestName.find( testName ); + return it != m_generatorsByTestName.end() + ? it->second + : NULL; } - bool matches ( const std::string& testName ) const { - if( !m_isWildcarded ) - return m_rawSpec == testName; - else - return testName.size() >= m_rawSpec.size() && testName.substr( 0, m_rawSpec.size() ) == m_rawSpec; + IGeneratorsForTest& getGeneratorsForCurrentTest() { + IGeneratorsForTest* generators = findGeneratorsForCurrentTest(); + if( !generators ) { + std::string testName = getResultCapture().getCurrentTestName(); + generators = createGeneratorsForTest(); + m_generatorsByTestName.insert( std::make_pair( testName, generators ) ); + } + return *generators; } private: - std::string m_rawSpec; - bool m_isWildcarded; + IRunner* m_runner; + IResultCapture* m_resultCapture; + const IConfig* m_config; + std::map<std::string, IGeneratorsForTest*> m_generatorsByTestName; }; -} -#include <set> -#include <string> + namespace { + Context* currentContext = NULL; + } + IMutableContext& getCurrentMutableContext() { + if( !currentContext ) + currentContext = new Context(); + return *currentContext; + } + IContext& getCurrentContext() { + return getCurrentMutableContext(); + } -namespace Catch { + std::streambuf* createStreamBuf( const std::string& streamName ) { + if( streamName == "stdout" ) return std::cout.rdbuf(); + if( streamName == "stderr" ) return std::cerr.rdbuf(); + if( streamName == "debug" ) return new StreamBufImpl<OutputDebugWriter>; - class StreamRedirect { + throw std::domain_error( "Unknown stream: " + streamName ); + } - public: - StreamRedirect( std::ostream& stream, std::string& targetString ) - : m_stream( stream ), - m_prevBuf( stream.rdbuf() ), - m_targetString( targetString ) - { - stream.rdbuf( m_oss.rdbuf() ); - } + void cleanUpContext() { + delete currentContext; + currentContext = NULL; + } +} +// #included from: catch_console_colour_impl.hpp +#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED - ~StreamRedirect() { - m_targetString += m_oss.str(); - m_stream.rdbuf( m_prevBuf ); - } +// #included from: catch_console_colour.hpp +#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED - private: - std::ostream& m_stream; - std::streambuf* m_prevBuf; - std::ostringstream m_oss; - std::string& m_targetString; - }; +namespace Catch { - /////////////////////////////////////////////////////////////////////////// + struct ConsoleColourImpl; - class Runner : public IResultCapture, public IRunner { + class TextColour : NonCopyable { + public: - Runner( const Runner& ); - void operator =( const Runner& ); + enum Colours { + None, - public: + FileName, + ResultError, + ResultSuccess, - explicit Runner( Config& config, const Ptr<IReporter>& reporter ) - : m_context( getCurrentMutableContext() ), - m_runningTest( NULL ), - m_config( config ), - m_reporter( reporter ), - m_prevRunner( &m_context.getRunner() ), - m_prevResultCapture( &m_context.getResultCapture() ), - m_prevConfig( m_context.getConfig() ) - { - m_context.setRunner( this ); - m_context.setConfig( &m_config ); - m_context.setResultCapture( this ); - m_reporter->StartTesting(); - } + Error, + Success, - virtual ~Runner() { - m_reporter->EndTesting( m_totals ); - m_context.setRunner( m_prevRunner ); - m_context.setConfig( NULL ); - m_context.setResultCapture( m_prevResultCapture ); - m_context.setConfig( m_prevConfig ); - } + OriginalExpression, + ReconstructedExpression + }; - virtual Totals runAll() { - return runTests( "", getRegistryHub().getTestCaseRegistry().getAllTests() ); - } + TextColour( Colours colour = None ); + void set( Colours colour ); + ~TextColour(); - virtual Totals runAllNonHidden() { - return runTests( "", getRegistryHub().getTestCaseRegistry().getAllNonHiddenTests() ); - } + private: + ConsoleColourImpl* m_impl; + }; - virtual Totals runMatching( const std::string& rawTestSpec ) { +} // end namespace Catch - const std::vector<TestCaseInfo>& matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( rawTestSpec ); - return runTests( rawTestSpec, matchingTests ); - } +#ifdef CATCH_PLATFORM_WINDOWS - virtual Totals runTests( const std::string& groupName, const std::vector<TestCaseInfo>& testCases ) { +#include <windows.h> - Totals totals; - m_reporter->StartGroup( groupName ); +namespace Catch { - for( std::size_t i=0; i < testCases.size(); ++i ) { - if( aborting() ) { - m_reporter->Aborted(); - break; - } - totals += runTest( testCases[i] ); + namespace { + + WORD mapConsoleColour( TextColour::Colours colour ) { + switch( colour ) { + case TextColour::FileName: + return FOREGROUND_INTENSITY; // greyed out + case TextColour::ResultError: + return FOREGROUND_RED | FOREGROUND_INTENSITY; // bright red + case TextColour::ResultSuccess: + return FOREGROUND_GREEN | FOREGROUND_INTENSITY; // bright green + case TextColour::Error: + return FOREGROUND_RED; // dark red + case TextColour::Success: + return FOREGROUND_GREEN; // dark green + case TextColour::OriginalExpression: + return FOREGROUND_BLUE | FOREGROUND_GREEN; // turquoise + case TextColour::ReconstructedExpression: + return FOREGROUND_RED | FOREGROUND_GREEN; // greeny-yellow + default: return 0; } - m_reporter->EndGroup( groupName, totals ); - return totals; } + } - Totals runTest( const TestCaseInfo& testInfo ) { - Totals prevTotals = m_totals; - - std::string redirectedCout; - std::string redirectedCerr; + struct ConsoleColourImpl { - m_reporter->StartTestCase( testInfo ); + ConsoleColourImpl() + : hStdout( GetStdHandle(STD_OUTPUT_HANDLE) ), + wOldColorAttrs( 0 ) + { + GetConsoleScreenBufferInfo( hStdout, &csbiInfo ); + wOldColorAttrs = csbiInfo.wAttributes; + } - m_runningTest = new RunningTest( &testInfo ); + ~ConsoleColourImpl() { + SetConsoleTextAttribute( hStdout, wOldColorAttrs ); + } - do { - do { - m_currentResult.setLineInfo( m_runningTest->getTestCaseInfo().getLineInfo() ); - runCurrentTest( redirectedCout, redirectedCerr ); - } - while( m_runningTest->hasUntestedSections() && !aborting() ); - } - while( getCurrentContext().advanceGeneratorsForCurrentTest() && !aborting() ); + void set( TextColour::Colours colour ) { + WORD consoleColour = mapConsoleColour( colour ); + if( consoleColour > 0 ) + SetConsoleTextAttribute( hStdout, consoleColour ); + } - delete m_runningTest; - m_runningTest = NULL; + HANDLE hStdout; + CONSOLE_SCREEN_BUFFER_INFO csbiInfo; + WORD wOldColorAttrs; + }; - Totals deltaTotals = m_totals.delta( prevTotals ); - m_totals.testCases += deltaTotals.testCases; - m_reporter->EndTestCase( testInfo, deltaTotals, redirectedCout, redirectedCerr ); - return deltaTotals; - } + TextColour::TextColour( Colours colour ) + : m_impl( new ConsoleColourImpl() ) + { + if( colour ) + m_impl->set( colour ); + } - const Config& config() const { - return m_config; - } + TextColour::~TextColour() { + delete m_impl; + } - private: // IResultCapture + void TextColour::set( Colours colour ) { + m_impl->set( colour ); + } - virtual ResultAction::Value acceptResult( bool result ) { - return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed ); - } +} // end namespace Catch - virtual ResultAction::Value acceptResult( ResultWas::OfType result ) { - m_currentResult.setResultType( result ); - return actOnCurrentResult(); - } +#else - virtual ResultAction::Value acceptExpression( const ResultInfoBuilder& resultInfo ) { - m_currentResult = resultInfo; - return actOnCurrentResult(); - } +namespace Catch { + TextColour::TextColour( Colours ){} + TextColour::~TextColour(){} + void TextColour::set( Colours ){} - virtual void acceptMessage( const std::string& msg ) { - m_currentResult.setMessage( msg ); - } +} // end namespace Catch - virtual void testEnded( const ResultInfo& result ) { - if( result.getResultType() == ResultWas::Ok ) { - m_totals.assertions.passed++; - } - else if( !result.ok() ) { - m_totals.assertions.failed++; +#endif - std::vector<ResultInfo>::const_iterator it = m_info.begin(); - std::vector<ResultInfo>::const_iterator itEnd = m_info.end(); - for(; it != itEnd; ++it ) - m_reporter->Result( *it ); - m_info.clear(); - } +// #included from: catch_generators_impl.hpp +#define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED - if( result.getResultType() == ResultWas::Info ) - m_info.push_back( result ); - else - m_reporter->Result( result ); - } +#include <vector> +#include <string> +#include <map> - virtual bool sectionStarted ( - const std::string& name, - const std::string& description, - const SourceLineInfo& lineInfo, - Counts& assertions - ) - { - std::ostringstream oss; - oss << name << "@" << lineInfo; +namespace Catch { - if( !m_runningTest->addSection( oss.str() ) ) - return false; + struct GeneratorInfo : IGeneratorInfo { - m_currentResult.setLineInfo( lineInfo ); - m_reporter->StartSection( name, description ); - assertions = m_totals.assertions; + GeneratorInfo( std::size_t size ) + : m_size( size ), + m_currentIndex( 0 ) + {} + bool moveNext() { + if( ++m_currentIndex == m_size ) { + m_currentIndex = 0; + return false; + } return true; } - virtual void sectionEnded( const std::string& name, const Counts& prevAssertions ) { - m_runningTest->endSection( name ); - m_reporter->EndSection( name, m_totals.assertions - prevAssertions ); + std::size_t getCurrentIndex() const { + return m_currentIndex; } - virtual void pushScopedInfo( ScopedInfo* scopedInfo ) { - m_scopedInfos.push_back( scopedInfo ); - } + std::size_t m_size; + std::size_t m_currentIndex; + }; - virtual void popScopedInfo( ScopedInfo* scopedInfo ) { - if( m_scopedInfos.back() == scopedInfo ) - m_scopedInfos.pop_back(); - } + /////////////////////////////////////////////////////////////////////////// - virtual bool shouldDebugBreak() const { - return m_config.shouldDebugBreak(); + class GeneratorsForTest : public IGeneratorsForTest { + + public: + ~GeneratorsForTest() { + deleteAll( m_generatorsInOrder ); } - virtual std::string getCurrentTestName() const { - return m_runningTest - ? m_runningTest->getTestCaseInfo().getName() - : ""; + IGeneratorInfo& getGeneratorInfo( const std::string& fileInfo, std::size_t size ) { + std::map<std::string, IGeneratorInfo*>::const_iterator it = m_generatorsByName.find( fileInfo ); + if( it == m_generatorsByName.end() ) { + IGeneratorInfo* info = new GeneratorInfo( size ); + m_generatorsByName.insert( std::make_pair( fileInfo, info ) ); + m_generatorsInOrder.push_back( info ); + return *info; + } + return *it->second; } - virtual const ResultInfo* getLastResult() const { - return &m_lastResult; + bool moveNext() { + std::vector<IGeneratorInfo*>::const_iterator it = m_generatorsInOrder.begin(); + std::vector<IGeneratorInfo*>::const_iterator itEnd = m_generatorsInOrder.end(); + for(; it != itEnd; ++it ) { + if( (*it)->moveNext() ) + return true; + } + return false; } private: + std::map<std::string, IGeneratorInfo*> m_generatorsByName; + std::vector<IGeneratorInfo*> m_generatorsInOrder; + }; - bool aborting() const { - return m_totals.assertions.failed == static_cast<std::size_t>( m_config.getCutoff() ); - } + IGeneratorsForTest* createGeneratorsForTest() + { + return new GeneratorsForTest(); + } - ResultAction::Value actOnCurrentResult() { - testEnded( m_currentResult ); - m_lastResult = m_currentResult; +} // end namespace Catch - m_currentResult = ResultInfoBuilder(); +// #included from: catch_resultinfo.hpp +#define TWOBLUECUBES_CATCH_RESULT_INFO_HPP_INCLUDED - ResultAction::Value action = ResultAction::None; +namespace Catch { - if( !m_lastResult.ok() ) { - action = ResultAction::Failed; - if( shouldDebugBreak() ) - action = (ResultAction::Value)( action | ResultAction::Debug ); - if( aborting() ) - action = (ResultAction::Value)( action | ResultAction::Abort ); - } - return action; - } + ResultInfo::ResultInfo() + : m_macroName(), + m_expr(), + m_lhs(), + m_rhs(), + m_op(), + m_message(), + m_result( ResultWas::Unknown ), + m_isNot( false ) + {} - void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ) { - try { - m_runningTest->reset(); - if( m_reporter->shouldRedirectStdout() ) { - StreamRedirect coutRedir( std::cout, redirectedCout ); - StreamRedirect cerrRedir( std::cerr, redirectedCerr ); - m_runningTest->getTestCaseInfo().invoke(); - } - else { - m_runningTest->getTestCaseInfo().invoke(); - } - m_runningTest->ranToCompletion(); - } - catch( TestFailureException& ) { - // This just means the test was aborted due to failure - } - catch(...) { - acceptMessage( getRegistryHub().getExceptionTranslatorRegistry().translateActiveException() ); - acceptResult( ResultWas::ThrewException ); - } - m_info.clear(); + ResultInfo::ResultInfo(const char* expr, + ResultWas::OfType result, + bool isNot, + const SourceLineInfo& lineInfo, + const char* macroName, + const char* message ) + : m_macroName( macroName ), + m_lineInfo( lineInfo ), + m_expr( expr ), + m_lhs(), + m_rhs(), + m_op( isNotExpression( expr ) ? "!" : "" ), + m_message( message ), + m_result( result ), + m_isNot( isNot ) + { + if( isNot ) + m_expr = "!" + m_expr; } - private: - IMutableContext& m_context; - RunningTest* m_runningTest; - ResultInfoBuilder m_currentResult; - ResultInfo m_lastResult; + ResultInfo::~ResultInfo() {} - const Config& m_config; - Totals m_totals; - Ptr<IReporter> m_reporter; - std::vector<ScopedInfo*> m_scopedInfos; - std::vector<ResultInfo> m_info; - IRunner* m_prevRunner; - IResultCapture* m_prevResultCapture; - const IConfig* m_prevConfig; - }; + bool ResultInfo::ok() const { + return ( m_result & ResultWas::FailureBit ) != ResultWas::FailureBit; + } -} // end namespace Catch + ResultWas::OfType ResultInfo::getResultType() const { + return m_result; + } -#include <fstream> -#include <stdlib.h> -#include <limits> + bool ResultInfo::hasExpression() const { + return !m_expr.empty(); + } -namespace Catch { + bool ResultInfo::hasMessage() const { + return !m_message.empty(); + } - INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter ) - INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter ) - INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter ) + std::string ResultInfo::getExpression() const { + return m_expr; + } - inline int resolveStream( Config& configWrapper ) { - const ConfigData& config = configWrapper.data(); + bool ResultInfo::hasExpandedExpression() const { + return hasExpression() && getExpandedExpressionInternal() != m_expr; + } - if( !config.stream.empty() ) { - if( config.stream[0] == '%' ) - configWrapper.useStream( config.stream.substr( 1 ) ); - else - configWrapper.setFilename( config.stream ); - } - // Open output file, if specified - std::ofstream ofs; - if( !config.outputFilename.empty() ) { - ofs.open( config.outputFilename.c_str() ); - if( ofs.fail() ) { - std::cerr << "Unable to open file: '" << config.outputFilename << "'" << std::endl; - return (std::numeric_limits<int>::max)(); - } - configWrapper.setStreamBuf( ofs.rdbuf() ); - } - return 0; + std::string ResultInfo::getExpandedExpression() const { + return hasExpression() ? getExpandedExpressionInternal() : ""; + } + + std::string ResultInfo::getMessage() const { + return m_message; } - inline Ptr<IReporter> makeReporter( Config& configWrapper ) { - const ConfigData& config = configWrapper.data(); + std::string ResultInfo::getFilename() const { + return m_lineInfo.file; + } - std::string reporterName = config.reporter.empty() - ? "basic" - : config.reporter; + std::size_t ResultInfo::getLine() const { + return m_lineInfo.line; + } + + std::string ResultInfo::getTestMacroName() const { + return m_macroName; + } - ReporterConfig reporterConfig( config.name, configWrapper.stream(), config.includeWhichResults == Include::SuccessfulResults ); + std::string ResultInfo::getExpandedExpressionInternal() const { + if( m_op == "" || m_isNot ) + return m_lhs.empty() ? m_expr : m_op + m_lhs; + else if( m_op == "matches" ) + return m_lhs + " " + m_rhs; + else if( m_op != "!" ) + { + if( m_lhs.size() + m_rhs.size() < 30 ) + return m_lhs + " " + m_op + " " + m_rhs; + else if( m_lhs.size() < 70 && m_rhs.size() < 70 ) + return "\n\t" + m_lhs + "\n\t" + m_op + "\n\t" + m_rhs; + else + return "\n" + m_lhs + "\n" + m_op + "\n" + m_rhs + "\n\n"; + } + else + return "{can't expand - use " + m_macroName + "_FALSE( " + m_expr.substr(1) + " ) instead of " + m_macroName + "( " + m_expr + " ) for better diagnostics}"; + } - Ptr<IReporter> reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig ); - if( !reporter ) - std::cerr << "No reporter registered with name: '" << reporterName << "'" << std::endl; - return reporter; + bool ResultInfo::isNotExpression( const char* expr ) { + return expr && expr[0] == '!'; } - inline int Main( Config& configWrapper ) { - - int result = resolveStream( configWrapper ); - if( result != 0 ) - return result; +} // end namespace Catch - Ptr<IReporter> reporter = makeReporter( configWrapper ); - if( !reporter ) - return (std::numeric_limits<int>::max)(); +// #included from: catch_resultinfo_builder.hpp +#define TWOBLUECUBES_CATCH_RESULTINFO_BUILDER_HPP_INCLUDED - const ConfigData& config = configWrapper.data(); +namespace Catch { - // Handle list request - if( config.listSpec != List::None ) - return List( config ); + ResultInfoBuilder::ResultInfoBuilder() {} - // Scope here for the Runner so it can use the context before it is cleaned-up - { - Runner runner( configWrapper, reporter ); + ResultInfoBuilder::ResultInfoBuilder( const char* expr, + bool isNot, + const SourceLineInfo& lineInfo, + const char* macroName, + const char* message ) + : ResultInfo( expr, ResultWas::Unknown, isNot, lineInfo, macroName, message ) + {} - Totals totals; - // Run test specs specified on the command line - or default to all - if( config.testSpecs.empty() ) { - totals = runner.runAllNonHidden(); - } - else { - std::vector<std::string>::const_iterator it = config.testSpecs.begin(); - std::vector<std::string>::const_iterator itEnd = config.testSpecs.end(); - for(; it != itEnd; ++it ) { - Totals groupTotals = runner.runMatching( *it ); - if( groupTotals.testCases.total() == 0 ) - std::cerr << "\n[No test cases matched with: " << *it << "]" << std::endl; - totals += groupTotals; - } - } - result = static_cast<int>( totals.assertions.failed ); - } - Catch::cleanUp(); - return result; + void ResultInfoBuilder::setResultType( ResultWas::OfType result ) { + // Flip bool results if isNot is set + if( m_isNot && result == ResultWas::Ok ) + m_result = ResultWas::ExpressionFailed; + else if( m_isNot && result == ResultWas::ExpressionFailed ) + m_result = ResultWas::Ok; + else + m_result = result; } - inline void showUsage( std::ostream& os ) { - os << "\t-?, -h, --help\n" - << "\t-l, --list <tests | reporters> [xml]\n" - << "\t-t, --test <testspec> [<testspec>...]\n" - << "\t-r, --reporter <reporter name>\n" - << "\t-o, --out <file name>|<%stream name>\n" - << "\t-s, --success\n" - << "\t-b, --break\n" - << "\t-n, --name <name>\n" - << "\t-a, --abort [#]\n" - << "\t-nt, --nothrow\n\n" - << "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl; + void ResultInfoBuilder::setMessage( const std::string& message ) { + m_message = message; } - inline void showHelp( std::string exeName ) { - std::string::size_type pos = exeName.find_last_of( "/\\" ); - if( pos != std::string::npos ) { - exeName = exeName.substr( pos+1 ); - } - std::cout << exeName << " is a CATCH host application. Options are as follows:\n\n"; - showUsage( std::cout ); + void ResultInfoBuilder::setLineInfo( const SourceLineInfo& lineInfo ) { + m_lineInfo = lineInfo; } - inline int Main( int argc, char* const argv[], Config& config ) { - - try { - CommandParser parser( argc, argv ); - - if( Command cmd = parser.find( "-h", "-?", "--help" ) ) { - if( cmd.argsCount() != 0 ) - cmd.raiseError( "Does not accept arguments" ); - - showHelp( argv[0] ); - Catch::cleanUp(); - return 0; - } + void ResultInfoBuilder::setLhs( const std::string& lhs ) { + m_lhs = lhs; + } - parseIntoConfig( parser, config.data() ); - } - catch( std::exception& ex ) { - std::cerr << ex.what() << "\n\nUsage: ...\n\n"; - showUsage( std::cerr ); - Catch::cleanUp(); - return (std::numeric_limits<int>::max)(); - } + void ResultInfoBuilder::setRhs( const std::string& rhs ) { + m_rhs = rhs; + } - return Main( config ); + void ResultInfoBuilder::setOp( const std::string& op ) { + m_op = op; } - inline int Main( int argc, char* const argv[] ) { - Config config; -// !TBD: This doesn't always work, for some reason -// if( isDebuggerActive() ) -// config.useStream( "debug" ); - return Main( argc, argv, config ); + ResultInfoBuilder& ResultInfoBuilder::captureBoolExpression( bool result ) { + m_lhs = Catch::toString( result ); + m_op = m_isNot ? "!" : ""; + setResultType( result ? ResultWas::Ok : ResultWas::ExpressionFailed ); + return *this; } } // end namespace Catch -// #included from: catch_registry_hub.hpp - -// #included from: catch_test_case_registry_impl.hpp - -#include <vector> -#include <set> -#include <sstream> -#include <iostream> +// #included from: catch_test_case_info.hpp +#define TWOBLUECUBES_CATCH_TESTCASEINFO_HPP_INCLUDED namespace Catch { - class TestRegistry : public ITestCaseRegistry { - public: - TestRegistry() : m_unnamedCount( 0 ) {} - virtual ~TestRegistry(); - - virtual void registerTest( const TestCaseInfo& testInfo ) { - if( testInfo.getName() == "" ) { - std::ostringstream oss; - oss << testInfo.getName() << "unnamed/" << ++m_unnamedCount; - return registerTest( TestCaseInfo( testInfo, oss.str() ) ); - } - - if( m_functions.find( testInfo ) == m_functions.end() ) { - m_functions.insert( testInfo ); - m_functionsInOrder.push_back( testInfo ); - if( !testInfo.isHidden() ) - m_nonHiddenFunctions.push_back( testInfo ); - } - else { - const TestCaseInfo& prev = *m_functions.find( testInfo ); - std::cerr << "error: TEST_CASE( \"" << testInfo.getName() << "\" ) already defined.\n" - << "\tFirst seen at " << SourceLineInfo( prev.getLineInfo() ) << "\n" - << "\tRedefined at " << SourceLineInfo( testInfo.getLineInfo() ) << std::endl; - exit(1); - } - } + TestCaseInfo::TestCaseInfo( ITestCase* testCase, + const char* name, + const char* description, + const SourceLineInfo& lineInfo ) + : m_test( testCase ), + m_name( name ), + m_description( description ), + m_lineInfo( lineInfo ) + {} - virtual const std::vector<TestCaseInfo>& getAllTests() const { - return m_functionsInOrder; - } + TestCaseInfo::TestCaseInfo() + : m_test( NULL ), + m_name(), + m_description() + {} - virtual const std::vector<TestCaseInfo>& getAllNonHiddenTests() const { - return m_nonHiddenFunctions; - } + TestCaseInfo::TestCaseInfo( const TestCaseInfo& other, const std::string& name ) + : m_test( other.m_test ), + m_name( name ), + m_description( other.m_description ), + m_lineInfo( other.m_lineInfo ) + {} - virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const { - TestSpec testSpec( rawTestSpec ); + TestCaseInfo::TestCaseInfo( const TestCaseInfo& other ) + : m_test( other.m_test ), + m_name( other.m_name ), + m_description( other.m_description ), + m_lineInfo( other.m_lineInfo ) + {} - std::vector<TestCaseInfo> matchingTests; - getMatchingTestCases( rawTestSpec, matchingTests ); - return matchingTests; - } + void TestCaseInfo::invoke() const { + m_test->invoke(); + } - virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const { - TestSpec testSpec( rawTestSpec ); + const std::string& TestCaseInfo::getName() const { + return m_name; + } - std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin(); - std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end(); - for(; it != itEnd; ++it ) { - if( testSpec.matches( it->getName() ) ) { - matchingTestsOut.push_back( *it ); - } - } - } + const std::string& TestCaseInfo::getDescription() const { + return m_description; + } - private: + const SourceLineInfo& TestCaseInfo::getLineInfo() const { + return m_lineInfo; + } - std::set<TestCaseInfo> m_functions; - std::vector<TestCaseInfo> m_functionsInOrder; - std::vector<TestCaseInfo> m_nonHiddenFunctions; - size_t m_unnamedCount; - }; + bool TestCaseInfo::isHidden() const { + return m_name.size() >= 2 && m_name[0] == '.' && m_name[1] == '/'; + } - /////////////////////////////////////////////////////////////////////////// + void TestCaseInfo::swap( TestCaseInfo& other ) { + m_test.swap( other.m_test ); + m_name.swap( other.m_name ); + m_description.swap( other.m_description ); + m_lineInfo.swap( other.m_lineInfo ); + } - class FreeFunctionTestCase : public SharedImpl<ITestCase> { - public: + bool TestCaseInfo::operator == ( const TestCaseInfo& other ) const { + return m_test.get() == other.m_test.get() && m_name == other.m_name; + } - FreeFunctionTestCase( TestFunction fun ) : m_fun( fun ) {} + bool TestCaseInfo::operator < ( const TestCaseInfo& other ) const { + return m_name < other.m_name; + } + TestCaseInfo& TestCaseInfo::operator = ( const TestCaseInfo& other ) { + TestCaseInfo temp( other ); + swap( temp ); + return *this; + } +} - virtual void invoke() const { - m_fun(); - } +// #included from: ../reporters/catch_reporter_basic.hpp +#define TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED - private: - virtual ~FreeFunctionTestCase(); +// #included from: ../internal/catch_reporter_registrars.hpp +#define TWOBLUECUBES_CATCH_REPORTER_REGISTRARS_HPP_INCLUDED - TestFunction m_fun; - }; +namespace Catch { - /////////////////////////////////////////////////////////////////////////// + template<typename T> + class ReporterRegistrar { - AutoReg::AutoReg( TestFunction function, - const char* name, - const char* description, - const SourceLineInfo& lineInfo ) { - registerTestCase( new FreeFunctionTestCase( function ), name, description, lineInfo ); - } + class ReporterFactory : public IReporterFactory { - AutoReg::~AutoReg() {} + virtual IReporter* create( const ReporterConfig& config ) const { + return new T( config ); + } - void AutoReg::registerTestCase( ITestCase* testCase, - const char* name, - const char* description, - const SourceLineInfo& lineInfo ) { - getMutableRegistryHub().registerTest( TestCaseInfo( testCase, name, description, lineInfo ) ); - } + virtual std::string getDescription() const { + return T::getDescription(); + } + }; -} // end namespace Catch + public: -// #included from: catch_reporter_registry.hpp -#define TWOBLUECUBES_CATCH_REPORTER_REGISTRY_HPP_INCLUDED + ReporterRegistrar( const std::string& name ) { + getMutableRegistryHub().registerReporter( name, new ReporterFactory() ); + } + }; +} -#include <map> +#define INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) \ + Catch::ReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name ); namespace Catch { - class ReporterRegistry : public IReporterRegistry { - - public: + struct pluralise { + pluralise( std::size_t count, const std::string& label ) + : m_count( count ), + m_label( label ) + {} - virtual ~ReporterRegistry() { - deleteAllValues( m_factories ); + friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) { + os << pluraliser.m_count << " " << pluraliser.m_label; + if( pluraliser.m_count != 1 ) + os << "s"; + return os; } - virtual IReporter* create( const std::string& name, const ReporterConfig& config ) const { - FactoryMap::const_iterator it = m_factories.find( name ); - if( it == m_factories.end() ) - return NULL; - return it->second->create( config ); - } + std::size_t m_count; + std::string m_label; + }; - void registerReporter( const std::string& name, IReporterFactory* factory ) { - m_factories.insert( std::make_pair( name, factory ) ); - } + class BasicReporter : public SharedImpl<IReporter> { - const FactoryMap& getFactories() const { - return m_factories; - } + struct SpanInfo { - private: - FactoryMap m_factories; - }; -} + SpanInfo() + : emitted( false ) + {} -// #included from: catch_exception_translator_registry.hpp -#define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_HPP_INCLUDED + SpanInfo( const std::string& spanName ) + : name( spanName ), + emitted( false ) + {} -#ifdef __OBJC__ -#import "Foundation/Foundation.h" -#endif + SpanInfo( const SpanInfo& other ) + : name( other.name ), + emitted( other.emitted ) + {} -namespace Catch { + std::string name; + bool emitted; + }; - class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { public: - ~ExceptionTranslatorRegistry() { - deleteAll( m_translators ); + BasicReporter( const ReporterConfig& config ) + : m_config( config ), + m_firstSectionInTestCase( true ), + m_aborted( false ) + {} + + virtual ~BasicReporter(); + + static std::string getDescription() { + return "Reports test results as lines of text"; } - virtual void registerTranslator( const IExceptionTranslator* translator ) { - m_translators.push_back( translator ); + private: + + void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) { + if( counts.passed ) + m_config.stream << counts.failed << " of " << counts.total() << " " << label << "s failed"; + else + m_config.stream << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed"; } - 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(); - } - catch( std::string& msg ) { - return msg; + void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) { + if( totals.assertions.total() == 0 ) { + m_config.stream << "No tests ran"; } - catch( const char* msg ) { - return msg; + else if( totals.assertions.failed ) { + TextColour colour( TextColour::ResultError ); + ReportCounts( "test case", totals.testCases, allPrefix ); + if( totals.testCases.failed > 0 ) { + m_config.stream << " ("; + ReportCounts( "assertion", totals.assertions, allPrefix ); + m_config.stream << ")"; + } } - catch(...) { - return tryTranslators( m_translators.begin() ); + else { + TextColour colour( TextColour::ResultSuccess ); + m_config.stream << allPrefix << "tests passed (" + << pluralise( totals.assertions.passed, "assertion" ) << " in " + << pluralise( totals.testCases.passed, "test case" ) << ")"; } } - std::string tryTranslators( std::vector<const IExceptionTranslator*>::const_iterator it ) const { - if( it == m_translators.end() ) - return "Unknown exception"; + private: // IReporter - try { - return (*it)->translate(); - } - catch(...) { - return tryTranslators( it+1 ); - } + virtual bool shouldRedirectStdout() const { + return false; } - private: - std::vector<const IExceptionTranslator*> m_translators; - }; -} - -namespace Catch { - - namespace { - - class RegistryHub : public IRegistryHub, public IMutableRegistryHub { - - RegistryHub( const RegistryHub& ); - void operator=( const RegistryHub& ); + virtual void StartTesting() { + m_testingSpan = SpanInfo(); + } - public: // IRegistryHub - RegistryHub() { - } - virtual const IReporterRegistry& getReporterRegistry() const { - return m_reporterRegistry; - } - virtual const ITestCaseRegistry& getTestCaseRegistry() const { - return m_testCaseRegistry; - } - virtual IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() { - return m_exceptionTranslatorRegistry; - } + virtual void Aborted() { + m_aborted = true; + } - public: // IMutableRegistryHub - virtual void registerReporter( const std::string& name, IReporterFactory* factory ) { - m_reporterRegistry.registerReporter( name, factory ); - } - virtual void registerTest( const TestCaseInfo& testInfo ) { - m_testCaseRegistry.registerTest( testInfo ); + virtual void EndTesting( const Totals& totals ) { + // Output the overall test results even if "Started Testing" was not emitted + if( m_aborted ) { + m_config.stream << "\n[Testing aborted. "; + ReportCounts( totals, "The first " ); } - virtual void registerTranslator( const IExceptionTranslator* translator ) { - m_exceptionTranslatorRegistry.registerTranslator( translator ); + else { + m_config.stream << "\n[Testing completed. "; + ReportCounts( totals ); } - - private: - TestRegistry m_testCaseRegistry; - ReporterRegistry m_reporterRegistry; - ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; - }; - - // Single, global, instance - inline RegistryHub*& getTheRegistryHub() { - static RegistryHub* theRegistryHub = NULL; - if( !theRegistryHub ) - theRegistryHub = new RegistryHub(); - return theRegistryHub; + m_config.stream << "]\n" << std::endl; } - } - IRegistryHub& getRegistryHub() { - return *getTheRegistryHub(); - } - IMutableRegistryHub& getMutableRegistryHub() { - return *getTheRegistryHub(); - } - void cleanUp() { - delete getTheRegistryHub(); - getTheRegistryHub() = NULL; - cleanUpContext(); - } + virtual void StartGroup( const std::string& groupName ) { + m_groupSpan = groupName; + } -} // end namespace Catch -// #included from: catch_notimplemented_exception.hpp -#define TWOBLUECUBES_CATCH_NOTIMPLEMENTED_EXCEPTION_HPP_INCLUDED + virtual void EndGroup( const std::string& groupName, const Totals& totals ) { + if( m_groupSpan.emitted && !groupName.empty() ) { + m_config.stream << "[End of group: '" << groupName << "'. "; + ReportCounts( totals ); + m_config.stream << "]\n" << std::endl; + m_groupSpan = SpanInfo(); + } + } -#include <ostream> + virtual void StartTestCase( const TestCaseInfo& testInfo ) { + m_testSpan = testInfo.getName(); + } -namespace Catch { + virtual void StartSection( const std::string& sectionName, const std::string& ) { + m_sectionSpans.push_back( SpanInfo( sectionName ) ); + } - NotImplementedException::NotImplementedException( const SourceLineInfo& lineInfo ) - : m_lineInfo( lineInfo ) { - std::ostringstream oss; - oss << lineInfo << "function "; - if( !lineInfo.function.empty() ) - oss << lineInfo.function << " "; - oss << "not implemented"; - m_what = oss.str(); - } + virtual void EndSection( const std::string& sectionName, const Counts& assertions ) { + SpanInfo& sectionSpan = m_sectionSpans.back(); + if( sectionSpan.emitted && !sectionSpan.name.empty() ) { + m_config.stream << "[End of section: '" << sectionName << "' "; - const char* NotImplementedException::what() const throw() { - return m_what.c_str(); - } + if( assertions.failed ) { + TextColour colour( TextColour::ResultError ); + ReportCounts( "assertion", assertions); + } + else { + TextColour colour( TextColour::ResultSuccess ); + m_config.stream << ( assertions.passed > 1 ? "All " : "" ) + << pluralise( assertions.passed, "assertion" ) << " passed" ; + } + m_config.stream << "]\n" << std::endl; + } + m_sectionSpans.pop_back(); + } -} // end namespace Catch + virtual void Result( const ResultInfo& resultInfo ) { + if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok ) + return; -// #included from: catch_context_impl.hpp + StartSpansLazily(); -// #included from: catch_stream.hpp -#define TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED + if( !resultInfo.getFilename().empty() ) { + TextColour colour( TextColour::FileName ); + m_config.stream << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() ); + } -#include <stdexcept> -#include <cstdio> + if( resultInfo.hasExpression() ) { + TextColour colour( TextColour::OriginalExpression ); + m_config.stream << resultInfo.getExpression(); + if( resultInfo.ok() ) { + TextColour successColour( TextColour::Success ); + m_config.stream << " succeeded"; + } + else { + TextColour errorColour( TextColour::Error ); + m_config.stream << " failed"; + } + } + switch( resultInfo.getResultType() ) { + case ResultWas::ThrewException: + { + TextColour colour( TextColour::Error ); + if( resultInfo.hasExpression() ) + m_config.stream << " with unexpected"; + else + m_config.stream << "Unexpected"; + m_config.stream << " exception with message: '" << resultInfo.getMessage() << "'"; + } + break; + case ResultWas::DidntThrowException: + { + TextColour colour( TextColour::Error ); + if( resultInfo.hasExpression() ) + m_config.stream << " because no exception was thrown where one was expected"; + else + m_config.stream << "No exception thrown where one was expected"; + } + break; + case ResultWas::Info: + streamVariableLengthText( "info", resultInfo.getMessage() ); + break; + case ResultWas::Warning: + m_config.stream << "warning:\n'" << resultInfo.getMessage() << "'"; + break; + case ResultWas::ExplicitFailure: + { + TextColour colour( TextColour::Error ); + m_config.stream << "failed with message: '" << resultInfo.getMessage() << "'"; + } + break; + case ResultWas::Unknown: // These cases are here to prevent compiler warnings + case ResultWas::Ok: + case ResultWas::FailureBit: + case ResultWas::ExpressionFailed: + case ResultWas::Exception: + if( !resultInfo.hasExpression() ) { + if( resultInfo.ok() ) { + TextColour colour( TextColour::Success ); + m_config.stream << " succeeded"; + } + else { + TextColour colour( TextColour::Error ); + m_config.stream << " failed"; + } + } + break; + } -namespace Catch { + if( resultInfo.hasExpandedExpression() ) { + m_config.stream << " for: "; + TextColour colour( TextColour::ReconstructedExpression ); + m_config.stream << resultInfo.getExpandedExpression(); + } + m_config.stream << std::endl; + } - template<typename WriterF, size_t bufferSize=256> - class StreamBufImpl : public StreamBufBase { - char data[bufferSize]; - WriterF m_writer; + virtual void EndTestCase( const TestCaseInfo& testInfo, + const Totals& totals, + const std::string& stdOut, + const std::string& stdErr ) { + if( !stdOut.empty() ) { + StartSpansLazily(); + streamVariableLengthText( "stdout", stdOut ); + } - public: - StreamBufImpl() { - setp( data, data + sizeof(data) ); - } + if( !stdErr.empty() ) { + StartSpansLazily(); + streamVariableLengthText( "stderr", stdErr ); + } - ~StreamBufImpl() { - sync(); + if( m_testSpan.emitted ) { + m_config.stream << "[Finished: '" << testInfo.getName() << "' "; + ReportCounts( totals ); + m_config.stream << "]" << std::endl; + } } - private: - int overflow( int c ) { - sync(); + private: // helpers - if( c != EOF ) { - if( pbase() == epptr() ) - m_writer( std::string( 1, static_cast<char>( c ) ) ); + void StartSpansLazily() { + if( !m_testingSpan.emitted ) { + if( m_config.name.empty() ) + m_config.stream << "[Started testing]" << std::endl; else - sputc( static_cast<char>( c ) ); + m_config.stream << "[Started testing: " << m_config.name << "]" << std::endl; + m_testingSpan.emitted = true; } - return 0; - } - int sync() { - if( pbase() != pptr() ) { - m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) ); - setp( pbase(), epptr() ); + if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) { + m_config.stream << "[Started group: '" << m_groupSpan.name << "']" << std::endl; + m_groupSpan.emitted = true; } - return 0; - } - }; - - /////////////////////////////////////////////////////////////////////////// - struct OutputDebugWriter { + if( !m_testSpan.emitted ) { + m_config.stream << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl; + m_testSpan.emitted = true; + } - void operator()( const std::string &str ) { - writeToDebugConsole( str ); + if( !m_sectionSpans.empty() ) { + SpanInfo& sectionSpan = m_sectionSpans.back(); + if( !sectionSpan.emitted && !sectionSpan.name.empty() ) { + if( m_firstSectionInTestCase ) { + m_config.stream << "\n"; + m_firstSectionInTestCase = false; + } + std::vector<SpanInfo>::iterator it = m_sectionSpans.begin(); + std::vector<SpanInfo>::iterator itEnd = m_sectionSpans.end(); + for(; it != itEnd; ++it ) { + SpanInfo& prevSpan = *it; + if( !prevSpan.emitted && !prevSpan.name.empty() ) { + m_config.stream << "[Started section: '" << prevSpan.name << "']" << std::endl; + prevSpan.emitted = true; + } + } + } + } } - }; -} -namespace Catch { + void streamVariableLengthText( const std::string& prefix, const std::string& text ) { + std::string trimmed = trim( text ); + if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) { + m_config.stream << "[" << prefix << ": " << trimmed << "]\n"; + } + else { + m_config.stream << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed + << "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n"; + } + } - class Context : public IMutableContext { + private: + ReporterConfig m_config; + bool m_firstSectionInTestCase; - Context() : m_config( NULL ) {} - Context( const Context& ); - void operator=( const Context& ); + SpanInfo m_testingSpan; + SpanInfo m_groupSpan; + SpanInfo m_testSpan; + std::vector<SpanInfo> m_sectionSpans; + bool m_aborted; + }; - public: // IContext - virtual IResultCapture& getResultCapture() { - return *m_resultCapture; - } - virtual IRunner& getRunner() { - return *m_runner; - } - virtual size_t getGeneratorIndex( const std::string& fileInfo, size_t totalSize ) { - return getGeneratorsForCurrentTest() - .getGeneratorInfo( fileInfo, totalSize ) - .getCurrentIndex(); - } - virtual bool advanceGeneratorsForCurrentTest() { - IGeneratorsForTest* generators = findGeneratorsForCurrentTest(); - return generators && generators->moveNext(); - } +} // end namespace Catch - virtual const IConfig* getConfig() const { - return m_config; - } +// #included from: ../reporters/catch_reporter_xml.hpp +#define TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED - public: // IMutableContext - virtual void setResultCapture( IResultCapture* resultCapture ) { - m_resultCapture = resultCapture; - } - virtual void setRunner( IRunner* runner ) { - m_runner = runner; - } - virtual void setConfig( const IConfig* config ) { - m_config = config; - } +// #included from: ../internal/catch_xmlwriter.hpp +#define TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED - friend IMutableContext& getCurrentMutableContext(); +#include <sstream> +#include <string> +#include <vector> - private: - IGeneratorsForTest* findGeneratorsForCurrentTest() { - std::string testName = getResultCapture().getCurrentTestName(); +namespace Catch { - std::map<std::string, IGeneratorsForTest*>::const_iterator it = - m_generatorsByTestName.find( testName ); - return it != m_generatorsByTestName.end() - ? it->second - : NULL; - } + class XmlWriter { + public: - IGeneratorsForTest& getGeneratorsForCurrentTest() { - IGeneratorsForTest* generators = findGeneratorsForCurrentTest(); - if( !generators ) { - std::string testName = getResultCapture().getCurrentTestName(); - generators = createGeneratorsForTest(); - m_generatorsByTestName.insert( std::make_pair( testName, generators ) ); + class ScopedElement { + public: + ScopedElement( XmlWriter* writer ) + : m_writer( writer ) + {} + + ScopedElement( const ScopedElement& other ) + : m_writer( other.m_writer ){ + other.m_writer = NULL; } - return *generators; - } - private: - IRunner* m_runner; - IResultCapture* m_resultCapture; - const IConfig* m_config; - std::map<std::string, IGeneratorsForTest*> m_generatorsByTestName; - }; + ~ScopedElement() { + if( m_writer ) + m_writer->endElement(); + } - namespace { - Context* currentContext = NULL; - } - IMutableContext& getCurrentMutableContext() { - if( !currentContext ) - currentContext = new Context(); - return *currentContext; - } - IContext& getCurrentContext() { - return getCurrentMutableContext(); - } + ScopedElement& writeText( const std::string& text ) { + m_writer->writeText( text ); + return *this; + } - std::streambuf* createStreamBuf( const std::string& streamName ) { - if( streamName == "stdout" ) return std::cout.rdbuf(); - if( streamName == "stderr" ) return std::cerr.rdbuf(); - if( streamName == "debug" ) return new StreamBufImpl<OutputDebugWriter>; + template<typename T> + ScopedElement& writeAttribute( const std::string& name, const T& attribute ) { + m_writer->writeAttribute( name, attribute ); + return *this; + } - throw std::domain_error( "Unknown stream: " + streamName ); - } + private: + mutable XmlWriter* m_writer; + }; - void cleanUpContext() { - delete currentContext; - currentContext = NULL; - } -} -// #included from: catch_console_colour_impl.hpp -#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED + XmlWriter() + : m_tagIsOpen( false ), + m_needsNewline( false ), + m_os( &std::cout ) + {} -#ifdef CATCH_PLATFORM_WINDOWS + XmlWriter( std::ostream& os ) + : m_tagIsOpen( false ), + m_needsNewline( false ), + m_os( &os ) + {} -#include <windows.h> + ~XmlWriter() { + while( !m_tags.empty() ) + endElement(); + } -namespace Catch { + XmlWriter& operator = ( const XmlWriter& other ) { + XmlWriter temp( other ); + swap( temp ); + return *this; + } - namespace { + void swap( XmlWriter& other ) { + std::swap( m_tagIsOpen, other.m_tagIsOpen ); + std::swap( m_needsNewline, other.m_needsNewline ); + std::swap( m_tags, other.m_tags ); + std::swap( m_indent, other.m_indent ); + std::swap( m_os, other.m_os ); + } - WORD mapConsoleColour( TextColour::Colours colour ) { - switch( colour ) { - case TextColour::FileName: - return FOREGROUND_INTENSITY; // greyed out - case TextColour::ResultError: - return FOREGROUND_RED | FOREGROUND_INTENSITY; // bright red - case TextColour::ResultSuccess: - return FOREGROUND_GREEN | FOREGROUND_INTENSITY; // bright green - case TextColour::Error: - return FOREGROUND_RED; // dark red - case TextColour::Success: - return FOREGROUND_GREEN; // dark green - case TextColour::OriginalExpression: - return FOREGROUND_BLUE | FOREGROUND_GREEN; // turquoise - case TextColour::ReconstructedExpression: - return FOREGROUND_RED | FOREGROUND_GREEN; // greeny-yellow - default: return 0; - } + XmlWriter& startElement( const std::string& name ) { + ensureTagClosed(); + newlineIfNecessary(); + stream() << m_indent << "<" << name; + m_tags.push_back( name ); + m_indent += " "; + m_tagIsOpen = true; + return *this; } - } - struct ConsoleColourImpl { + ScopedElement scopedElement( const std::string& name ) { + ScopedElement scoped( this ); + startElement( name ); + return scoped; + } - ConsoleColourImpl() - : hStdout( GetStdHandle(STD_OUTPUT_HANDLE) ), - wOldColorAttrs( 0 ) - { - GetConsoleScreenBufferInfo( hStdout, &csbiInfo ); - wOldColorAttrs = csbiInfo.wAttributes; + XmlWriter& endElement() { + newlineIfNecessary(); + m_indent = m_indent.substr( 0, m_indent.size()-2 ); + if( m_tagIsOpen ) { + stream() << "/>\n"; + m_tagIsOpen = false; + } + else { + stream() << m_indent << "</" << m_tags.back() << ">\n"; + } + m_tags.pop_back(); + return *this; } - ~ConsoleColourImpl() { - SetConsoleTextAttribute( hStdout, wOldColorAttrs ); + XmlWriter& writeAttribute( const std::string& name, const std::string& attribute ) { + if( !name.empty() && !attribute.empty() ) { + stream() << " " << name << "=\""; + writeEncodedText( attribute ); + stream() << "\""; + } + return *this; } - void set( TextColour::Colours colour ) { - WORD consoleColour = mapConsoleColour( colour ); - if( consoleColour > 0 ) - SetConsoleTextAttribute( hStdout, consoleColour ); + XmlWriter& writeAttribute( const std::string& name, bool attribute ) { + stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\""; + return *this; } - HANDLE hStdout; - CONSOLE_SCREEN_BUFFER_INFO csbiInfo; - WORD wOldColorAttrs; - }; + template<typename T> + XmlWriter& writeAttribute( const std::string& name, const T& attribute ) { + if( !name.empty() ) + stream() << " " << name << "=\"" << attribute << "\""; + return *this; + } - TextColour::TextColour( Colours colour ) - : m_impl( new ConsoleColourImpl() ) - { - if( colour ) - m_impl->set( colour ); - } + XmlWriter& writeText( const std::string& text ) { + if( !text.empty() ){ + bool tagWasOpen = m_tagIsOpen; + ensureTagClosed(); + if( tagWasOpen ) + stream() << m_indent; + writeEncodedText( text ); + m_needsNewline = true; + } + return *this; + } - TextColour::~TextColour() { - delete m_impl; - } + XmlWriter& writeComment( const std::string& text ) { + ensureTagClosed(); + stream() << m_indent << "<!--" << text << "-->"; + m_needsNewline = true; + return *this; + } - void TextColour::set( Colours colour ) { - m_impl->set( colour ); - } + XmlWriter& writeBlankLine() { + ensureTagClosed(); + stream() << "\n"; + return *this; + } -} // end namespace Catch + private: -#else + std::ostream& stream() { + return *m_os; + } -namespace Catch { - TextColour::TextColour( Colours ){} - TextColour::~TextColour(){} - void TextColour::set( Colours ){} + void ensureTagClosed() { + if( m_tagIsOpen ) { + stream() << ">\n"; + m_tagIsOpen = false; + } + } -} // end namespace Catch + void newlineIfNecessary() { + if( m_needsNewline ) { + stream() << "\n"; + m_needsNewline = false; + } + } -#endif + void writeEncodedText( const std::string& text ) { + static const char* charsToEncode = "<&\""; + std::string mtext = text; + std::string::size_type pos = mtext.find_first_of( charsToEncode ); + while( pos != std::string::npos ) { + stream() << mtext.substr( 0, pos ); -// #included from: catch_generators_impl.hpp -#define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED + switch( mtext[pos] ) { + case '<': + stream() << "<"; + break; + case '&': + stream() << "&"; + break; + case '\"': + stream() << """; + break; + } + mtext = mtext.substr( pos+1 ); + pos = mtext.find_first_of( charsToEncode ); + } + stream() << mtext; + } -#include <vector> -#include <string> -#include <map> + bool m_tagIsOpen; + bool m_needsNewline; + std::vector<std::string> m_tags; + std::string m_indent; + std::ostream* m_os; + }; +} namespace Catch { + class XmlReporter : public SharedImpl<IReporter> { + public: + XmlReporter( const ReporterConfig& config ) : m_config( config ) {} - struct GeneratorInfo : IGeneratorInfo { + static std::string getDescription() { + return "Reports test results as an XML document"; + } + virtual ~XmlReporter(); - GeneratorInfo( std::size_t size ) - : m_size( size ), - m_currentIndex( 0 ) - {} + private: // IReporter - bool moveNext() { - if( ++m_currentIndex == m_size ) { - m_currentIndex = 0; - return false; - } + virtual bool shouldRedirectStdout() const { return true; } - std::size_t getCurrentIndex() const { - return m_currentIndex; + virtual void StartTesting() { + m_xml = XmlWriter( m_config.stream ); + m_xml.startElement( "Catch" ); + if( !m_config.name.empty() ) + m_xml.writeAttribute( "name", m_config.name ); } - std::size_t m_size; - std::size_t m_currentIndex; - }; - - /////////////////////////////////////////////////////////////////////////// - - class GeneratorsForTest : public IGeneratorsForTest { + virtual void EndTesting( const Totals& totals ) { + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", totals.assertions.passed ) + .writeAttribute( "failures", totals.assertions.failed ); + m_xml.endElement(); + } - public: - ~GeneratorsForTest() { - deleteAll( m_generatorsInOrder ); + virtual void StartGroup( const std::string& groupName ) { + m_xml.startElement( "Group" ) + .writeAttribute( "name", groupName ); } - IGeneratorInfo& getGeneratorInfo( const std::string& fileInfo, std::size_t size ) { - std::map<std::string, IGeneratorInfo*>::const_iterator it = m_generatorsByName.find( fileInfo ); - if( it == m_generatorsByName.end() ) { - IGeneratorInfo* info = new GeneratorInfo( size ); - m_generatorsByName.insert( std::make_pair( fileInfo, info ) ); - m_generatorsInOrder.push_back( info ); - return *info; - } - return *it->second; + virtual void EndGroup( const std::string&, const Totals& totals ) { + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", totals.assertions.passed ) + .writeAttribute( "failures", totals.assertions.failed ); + m_xml.endElement(); } - bool moveNext() { - std::vector<IGeneratorInfo*>::const_iterator it = m_generatorsInOrder.begin(); - std::vector<IGeneratorInfo*>::const_iterator itEnd = m_generatorsInOrder.end(); - for(; it != itEnd; ++it ) { - if( (*it)->moveNext() ) - return true; - } - return false; + virtual void StartSection( const std::string& sectionName, const std::string& description ) { + m_xml.startElement( "Section" ) + .writeAttribute( "name", sectionName ) + .writeAttribute( "description", description ); } - private: - std::map<std::string, IGeneratorInfo*> m_generatorsByName; - std::vector<IGeneratorInfo*> m_generatorsInOrder; - }; + virtual void EndSection( const std::string& /*sectionName*/, const Counts& assertions ) { + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", assertions.passed ) + .writeAttribute( "failures", assertions.failed ); + m_xml.endElement(); + } - IGeneratorsForTest* createGeneratorsForTest() - { - return new GeneratorsForTest(); - } + virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) { + m_xml.startElement( "TestCase" ).writeAttribute( "name", testInfo.getName() ); + m_currentTestSuccess = true; + } -} // end namespace Catch + virtual void Result( const Catch::ResultInfo& resultInfo ) { + if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok ) + return; -// #included from: catch_resultinfo.hpp -#define TWOBLUECUBES_CATCH_RESULT_INFO_HPP_INCLUDED + if( resultInfo.hasExpression() ) { + m_xml.startElement( "Expression" ) + .writeAttribute( "success", resultInfo.ok() ) + .writeAttribute( "filename", resultInfo.getFilename() ) + .writeAttribute( "line", resultInfo.getLine() ); -namespace Catch { + m_xml.scopedElement( "Original" ) + .writeText( resultInfo.getExpression() ); + m_xml.scopedElement( "Expanded" ) + .writeText( resultInfo.getExpandedExpression() ); + m_currentTestSuccess &= resultInfo.ok(); + } - ResultInfo::ResultInfo() - : m_macroName(), - m_expr(), - m_lhs(), - m_rhs(), - m_op(), - m_message(), - m_result( ResultWas::Unknown ), - m_isNot( false ) - {} + switch( resultInfo.getResultType() ) { + case ResultWas::ThrewException: + m_xml.scopedElement( "Exception" ) + .writeAttribute( "filename", resultInfo.getFilename() ) + .writeAttribute( "line", resultInfo.getLine() ) + .writeText( resultInfo.getMessage() ); + m_currentTestSuccess = false; + break; + case ResultWas::Info: + m_xml.scopedElement( "Info" ) + .writeText( resultInfo.getMessage() ); + break; + case ResultWas::Warning: + m_xml.scopedElement( "Warning" ) + .writeText( resultInfo.getMessage() ); + break; + case ResultWas::ExplicitFailure: + m_xml.scopedElement( "Failure" ) + .writeText( resultInfo.getMessage() ); + m_currentTestSuccess = false; + break; + case ResultWas::Unknown: + case ResultWas::Ok: + case ResultWas::FailureBit: + case ResultWas::ExpressionFailed: + case ResultWas::Exception: + case ResultWas::DidntThrowException: + break; + } + if( resultInfo.hasExpression() ) + m_xml.endElement(); + } - ResultInfo::ResultInfo(const char* expr, - ResultWas::OfType result, - bool isNot, - const SourceLineInfo& lineInfo, - const char* macroName, - const char* message ) - : m_macroName( macroName ), - m_lineInfo( lineInfo ), - m_expr( expr ), - m_lhs(), - m_rhs(), - m_op( isNotExpression( expr ) ? "!" : "" ), - m_message( message ), - m_result( result ), - m_isNot( isNot ) - { - if( isNot ) - m_expr = "!" + m_expr; + virtual void Aborted() { + // !TBD } - ResultInfo::~ResultInfo() {} + virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string&, const std::string& ) { + m_xml.scopedElement( "OverallResult" ).writeAttribute( "success", m_currentTestSuccess ); + m_xml.endElement(); + } - bool ResultInfo::ok() const { - return ( m_result & ResultWas::FailureBit ) != ResultWas::FailureBit; - } + private: + ReporterConfig m_config; + bool m_currentTestSuccess; + XmlWriter m_xml; + }; - ResultWas::OfType ResultInfo::getResultType() const { - return m_result; - } +} // end namespace Catch - bool ResultInfo::hasExpression() const { - return !m_expr.empty(); - } +// #included from: ../reporters/catch_reporter_junit.hpp +#define TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED - bool ResultInfo::hasMessage() const { - return !m_message.empty(); - } +namespace Catch { - std::string ResultInfo::getExpression() const { - return m_expr; - } + class JunitReporter : public SharedImpl<IReporter> { - bool ResultInfo::hasExpandedExpression() const { - return hasExpression() && getExpandedExpressionInternal() != m_expr; - } + struct TestStats { + std::string m_element; + std::string m_resultType; + std::string m_message; + std::string m_content; + }; - std::string ResultInfo::getExpandedExpression() const { - return hasExpression() ? getExpandedExpressionInternal() : ""; - } + struct TestCaseStats { - std::string ResultInfo::getMessage() const { - return m_message; - } + TestCaseStats( const std::string& name = std::string() ) :m_name( name ){} - std::string ResultInfo::getFilename() const { - return m_lineInfo.file; - } + double m_timeInSeconds; + std::string m_status; + std::string m_className; + std::string m_name; + std::vector<TestStats> m_testStats; + }; - std::size_t ResultInfo::getLine() const { - return m_lineInfo.line; - } + struct Stats { - std::string ResultInfo::getTestMacroName() const { - return m_macroName; - } + Stats( const std::string& name = std::string() ) + : m_testsCount( 0 ), + m_failuresCount( 0 ), + m_disabledCount( 0 ), + m_errorsCount( 0 ), + m_timeInSeconds( 0 ), + m_name( name ) + {} - std::string ResultInfo::getExpandedExpressionInternal() const { - if( m_op == "" || m_isNot ) - return m_lhs.empty() ? m_expr : m_op + m_lhs; - else if( m_op == "matches" ) - return m_lhs + " " + m_rhs; - else if( m_op != "!" ) - { - if( m_lhs.size() + m_rhs.size() < 30 ) - return m_lhs + " " + m_op + " " + m_rhs; - else if( m_lhs.size() < 70 && m_rhs.size() < 70 ) - return "\n\t" + m_lhs + "\n\t" + m_op + "\n\t" + m_rhs; - else - return "\n" + m_lhs + "\n" + m_op + "\n" + m_rhs + "\n\n"; - } - else - return "{can't expand - use " + m_macroName + "_FALSE( " + m_expr.substr(1) + " ) instead of " + m_macroName + "( " + m_expr + " ) for better diagnostics}"; - } + std::size_t m_testsCount; + std::size_t m_failuresCount; + std::size_t m_disabledCount; + std::size_t m_errorsCount; + double m_timeInSeconds; + std::string m_name; - bool ResultInfo::isNotExpression( const char* expr ) { - return expr && expr[0] == '!'; - } + std::vector<TestCaseStats> m_testCaseStats; + }; -} // end namespace Catch + public: + JunitReporter( const ReporterConfig& config ) + : m_config( config ), + m_testSuiteStats( "AllTests" ), + m_currentStats( &m_testSuiteStats ) + {} + virtual ~JunitReporter(); -// #included from: catch_resultinfo_builder.hpp -#define TWOBLUECUBES_CATCH_RESULTINFO_BUILDER_HPP_INCLUDED + static std::string getDescription() { + return "Reports test results in an XML format that looks like Ant's junitreport target"; + } -namespace Catch { + private: // IReporter - ResultInfoBuilder::ResultInfoBuilder() {} + virtual bool shouldRedirectStdout() const { + return true; + } - ResultInfoBuilder::ResultInfoBuilder( const char* expr, - bool isNot, - const SourceLineInfo& lineInfo, - const char* macroName, - const char* message ) - : ResultInfo( expr, ResultWas::Unknown, isNot, lineInfo, macroName, message ) - {} + virtual void StartTesting(){} - void ResultInfoBuilder::setResultType( ResultWas::OfType result ) { - // Flip bool results if isNot is set - if( m_isNot && result == ResultWas::Ok ) - m_result = ResultWas::ExpressionFailed; - else if( m_isNot && result == ResultWas::ExpressionFailed ) - m_result = ResultWas::Ok; - else - m_result = result; - } + virtual void StartGroup( const std::string& groupName ) { + m_statsForSuites.push_back( Stats( groupName ) ); + m_currentStats = &m_statsForSuites.back(); + } - void ResultInfoBuilder::setMessage( const std::string& message ) { - m_message = message; - } + virtual void EndGroup( const std::string&, const Totals& totals ) { + m_currentStats->m_testsCount = totals.assertions.total(); + m_currentStats = &m_testSuiteStats; + } - void ResultInfoBuilder::setLineInfo( const SourceLineInfo& lineInfo ) { - m_lineInfo = lineInfo; - } + virtual void StartSection( const std::string&, const std::string& ){} - void ResultInfoBuilder::setLhs( const std::string& lhs ) { - m_lhs = lhs; - } + virtual void EndSection( const std::string&, const Counts& ){} - void ResultInfoBuilder::setRhs( const std::string& rhs ) { - m_rhs = rhs; - } + virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) { + m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getName() ) ); + } - void ResultInfoBuilder::setOp( const std::string& op ) { - m_op = op; - } + virtual void Result( const Catch::ResultInfo& resultInfo ) { + if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults ) { + TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back(); + TestStats stats; + std::ostringstream oss; + if( !resultInfo.getMessage().empty() ) + oss << resultInfo.getMessage() << " at "; + oss << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() ); + stats.m_content = oss.str(); + stats.m_message = resultInfo.getExpandedExpression(); + stats.m_resultType = resultInfo.getTestMacroName(); - ResultInfoBuilder& ResultInfoBuilder::captureBoolExpression( bool result ) { - m_lhs = Catch::toString( result ); - m_op = m_isNot ? "!" : ""; - setResultType( result ? ResultWas::Ok : ResultWas::ExpressionFailed ); - return *this; - } + switch( resultInfo.getResultType() ) { + case ResultWas::ThrewException: + stats.m_element = "error"; + m_currentStats->m_errorsCount++; + break; + case ResultWas::Info: + stats.m_element = "info"; // !TBD ? + break; + case ResultWas::Warning: + stats.m_element = "warning"; // !TBD ? + break; + case ResultWas::ExplicitFailure: + stats.m_element = "failure"; + m_currentStats->m_failuresCount++; + break; + case ResultWas::ExpressionFailed: + stats.m_element = "failure"; + m_currentStats->m_failuresCount++; + break; + case ResultWas::Ok: + stats.m_element = "success"; + break; + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + case ResultWas::DidntThrowException: + break; + } + testCaseStats.m_testStats.push_back( stats ); + } + } -} // end namespace Catch + virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string& stdOut, const std::string& stdErr ) { + if( !stdOut.empty() ) + m_stdOut << stdOut << "\n"; + if( !stdErr.empty() ) + m_stdErr << stdErr << "\n"; + } -// #included from: catch_test_case_info.hpp -#define TWOBLUECUBES_CATCH_TESTCASEINFO_HPP_INCLUDED + virtual void Aborted() { + // !TBD + } -namespace Catch { + virtual void EndTesting( const Totals& ) { + std::ostream& str = m_config.stream; + { + XmlWriter xml( str ); - TestCaseInfo::TestCaseInfo( ITestCase* testCase, - const char* name, - const char* description, - const SourceLineInfo& lineInfo ) - : m_test( testCase ), - m_name( name ), - m_description( description ), - m_lineInfo( lineInfo ) - {} + if( m_statsForSuites.size() > 0 ) + xml.startElement( "testsuites" ); - TestCaseInfo::TestCaseInfo() - : m_test( NULL ), - m_name(), - m_description() - {} + std::vector<Stats>::const_iterator it = m_statsForSuites.begin(); + std::vector<Stats>::const_iterator itEnd = m_statsForSuites.end(); - TestCaseInfo::TestCaseInfo( const TestCaseInfo& other, const std::string& name ) - : m_test( other.m_test ), - m_name( name ), - m_description( other.m_description ), - m_lineInfo( other.m_lineInfo ) - {} + for(; it != itEnd; ++it ) { + XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" ); + xml.writeAttribute( "name", it->m_name ); + xml.writeAttribute( "errors", it->m_errorsCount ); + xml.writeAttribute( "failures", it->m_failuresCount ); + xml.writeAttribute( "tests", it->m_testsCount ); + xml.writeAttribute( "hostname", "tbd" ); + xml.writeAttribute( "time", "tbd" ); + xml.writeAttribute( "timestamp", "tbd" ); - TestCaseInfo::TestCaseInfo( const TestCaseInfo& other ) - : m_test( other.m_test ), - m_name( other.m_name ), - m_description( other.m_description ), - m_lineInfo( other.m_lineInfo ) - {} + OutputTestCases( xml, *it ); + } - void TestCaseInfo::invoke() const { - m_test->invoke(); - } + xml.scopedElement( "system-out" ).writeText( trim( m_stdOut.str() ) ); + xml.scopedElement( "system-err" ).writeText( trim( m_stdErr.str() ) ); + } + } - const std::string& TestCaseInfo::getName() const { - return m_name; - } + void OutputTestCases( XmlWriter& xml, const Stats& stats ) { + std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin(); + std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end(); + for(; it != itEnd; ++it ) { + xml.writeBlankLine(); + xml.writeComment( "Test case" ); - const std::string& TestCaseInfo::getDescription() const { - return m_description; - } + XmlWriter::ScopedElement e = xml.scopedElement( "testcase" ); + xml.writeAttribute( "classname", it->m_className ); + xml.writeAttribute( "name", it->m_name ); + xml.writeAttribute( "time", "tbd" ); - const SourceLineInfo& TestCaseInfo::getLineInfo() const { - return m_lineInfo; - } + OutputTestResult( xml, *it ); + } + } - bool TestCaseInfo::isHidden() const { - return m_name.size() >= 2 && m_name[0] == '.' && m_name[1] == '/'; - } + void OutputTestResult( XmlWriter& xml, const TestCaseStats& stats ) { + std::vector<TestStats>::const_iterator it = stats.m_testStats.begin(); + std::vector<TestStats>::const_iterator itEnd = stats.m_testStats.end(); + for(; it != itEnd; ++it ) { + if( it->m_element != "success" ) { + XmlWriter::ScopedElement e = xml.scopedElement( it->m_element ); - void TestCaseInfo::swap( TestCaseInfo& other ) { - m_test.swap( other.m_test ); - m_name.swap( other.m_name ); - m_description.swap( other.m_description ); - m_lineInfo.swap( other.m_lineInfo ); - } + xml.writeAttribute( "message", it->m_message ); + xml.writeAttribute( "type", it->m_resultType ); + if( !it->m_content.empty() ) + xml.writeText( it->m_content ); + } + } + } - bool TestCaseInfo::operator == ( const TestCaseInfo& other ) const { - return m_test.get() == other.m_test.get() && m_name == other.m_name; - } + private: + ReporterConfig m_config; + bool m_currentTestSuccess; - bool TestCaseInfo::operator < ( const TestCaseInfo& other ) const { - return m_name < other.m_name; - } - TestCaseInfo& TestCaseInfo::operator = ( const TestCaseInfo& other ) { - TestCaseInfo temp( other ); - swap( temp ); - return *this; - } -} + Stats m_testSuiteStats; + Stats* m_currentStats; + std::vector<Stats> m_statsForSuites; + std::ostringstream m_stdOut; + std::ostringstream m_stdErr; + }; + +} // end namespace Catch namespace Catch { NonCopyable::~NonCopyable() {} @@ -5011,6 +5118,10 @@ namespace Catch { void Config::dummy() {} + INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter ) + INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter ) + INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter ) + } #ifdef __clang__