Some code clean-up

This commit is contained in:
Phil Nash 2011-01-11 19:48:48 +00:00
parent 1172d26705
commit b81e0b9e9c
6 changed files with 182 additions and 47 deletions

View File

@ -82,23 +82,23 @@ std::string toString( const T& value )
{ {
return Detail::StringMaker<T, Detail::IsStreamable<T>::value>::apply( value ); return Detail::StringMaker<T, Detail::IsStreamable<T>::value>::apply( value );
} }
template<> // Shortcut overloads
inline std::string toString<std::string>( const std::string& value ) inline std::string toString( const std::string& value )
{ {
return value; return value;
} }
inline std::string toString( const char* value )
template<> {
inline std::string toString<int>( const int& value ) return value;
}
inline std::string toString( int value )
{ {
std::ostringstream oss; std::ostringstream oss;
oss << value; oss << value;
return oss.str(); return oss.str();
} }
inline std::string toString( const double value )
template<>
inline std::string toString<double>( const double& value )
{ {
std::ostringstream oss; std::ostringstream oss;
oss << value; oss << value;

View File

@ -53,6 +53,7 @@ namespace Catch
}; }; }; };
///////////////////////////////////////////////////////////////////////////
Config() Config()
: m_reporter( NULL ), : m_reporter( NULL ),
m_listSpec( List::None ), m_listSpec( List::None ),
@ -62,6 +63,7 @@ namespace Catch
m_includeWhat( Include::FailedOnly ) m_includeWhat( Include::FailedOnly )
{} {}
///////////////////////////////////////////////////////////////////////////
void setReporter( const std::string& reporterName ) void setReporter( const std::string& reporterName )
{ {
if( m_reporter.get() ) if( m_reporter.get() )
@ -69,73 +71,93 @@ namespace Catch
setReporter( Hub::getReporterRegistry().create( reporterName, *this ) ); setReporter( Hub::getReporterRegistry().create( reporterName, *this ) );
} }
///////////////////////////////////////////////////////////////////////////
void addTestSpec( const std::string& testSpec ) void addTestSpec( const std::string& testSpec )
{ {
m_testSpecs.push_back( testSpec ); m_testSpecs.push_back( testSpec );
} }
///////////////////////////////////////////////////////////////////////////
void setListSpec( List::What listSpec ) void setListSpec( List::What listSpec )
{ {
m_listSpec = listSpec; m_listSpec = listSpec;
} }
///////////////////////////////////////////////////////////////////////////
void setFilename( const std::string& filename ) void setFilename( const std::string& filename )
{ {
m_filename = filename; m_filename = filename;
} }
///////////////////////////////////////////////////////////////////////////
std::string getFilename() std::string getFilename()
{ {
return m_filename; return m_filename;
} }
///////////////////////////////////////////////////////////////////////////
void setError( const std::string& errorMessage ) void setError( const std::string& errorMessage )
{ {
m_message = errorMessage + "\n\n" + "Usage: ..."; m_message = errorMessage + "\n\n" + "Usage: ...";
} }
///////////////////////////////////////////////////////////////////////////
void setReporter( IReporter* reporter ) void setReporter( IReporter* reporter )
{ {
m_reporter = std::auto_ptr<IReporter>( reporter ); m_reporter = std::auto_ptr<IReporter>( reporter );
} }
///////////////////////////////////////////////////////////////////////////
IReporter* getReporter() IReporter* getReporter()
{ {
if( !m_reporter.get() ) if( !m_reporter.get() )
setReporter( Hub::getReporterRegistry().create( "basic", *this ) ); setReporter( Hub::getReporterRegistry().create( "basic", *this ) );
return m_reporter.get(); return m_reporter.get();
} }
///////////////////////////////////////////////////////////////////////////
IReporter* getReporter() const IReporter* getReporter() const
{ {
return const_cast<Config*>( this )->getReporter(); return const_cast<Config*>( this )->getReporter();
} }
///////////////////////////////////////////////////////////////////////////
List::What listWhat() const List::What listWhat() const
{ {
return (List::What)( m_listSpec & List::WhatMask ); return (List::What)( m_listSpec & List::WhatMask );
} }
///////////////////////////////////////////////////////////////////////////
List::What listAs() const List::What listAs() const
{ {
return (List::What)( m_listSpec & List::AsMask ); return (List::What)( m_listSpec & List::AsMask );
} }
///////////////////////////////////////////////////////////////////////////
void setIncludeWhat( Include::What includeWhat ) void setIncludeWhat( Include::What includeWhat )
{ {
m_includeWhat = includeWhat; m_includeWhat = includeWhat;
} }
///////////////////////////////////////////////////////////////////////////
void setShouldDebugBreak( bool shouldDebugBreak ) void setShouldDebugBreak( bool shouldDebugBreak )
{ {
m_shouldDebugBreak = shouldDebugBreak; m_shouldDebugBreak = shouldDebugBreak;
} }
///////////////////////////////////////////////////////////////////////////
bool shouldDebugBreak() const bool shouldDebugBreak() const
{ {
return m_shouldDebugBreak; return m_shouldDebugBreak;
} }
///////////////////////////////////////////////////////////////////////////
void setShowHelp( bool showHelp ) void setShowHelp( bool showHelp )
{ {
m_showHelp = showHelp; m_showHelp = showHelp;
} }
///////////////////////////////////////////////////////////////////////////
bool showHelp() const bool showHelp() const
{ {
return m_showHelp; return m_showHelp;
@ -159,8 +181,7 @@ namespace Catch
return m_includeWhat == Include::SuccessfulResults; return m_includeWhat == Include::SuccessfulResults;
} }
std::auto_ptr<IReporter> m_reporter; std::auto_ptr<IReporter> m_reporter;
std::string m_filename; std::string m_filename;
std::string m_message; std::string m_message;

View File

@ -19,7 +19,7 @@
namespace Catch namespace Catch
{ {
struct TestCaseInfo; class TestCaseInfo;
struct IResultCapture; struct IResultCapture;
struct ITestCaseRegistry; struct ITestCaseRegistry;
struct IRunner; struct IRunner;
@ -27,17 +27,30 @@ namespace Catch
class Hub class Hub
{ {
Hub(); Hub();
static Hub& me(); static Hub& me();
public: public:
static void setRunner( IRunner* runner ); static void setRunner
static void setResultCapture( IResultCapture* resultCapture ); ( IRunner* runner
);
static IResultCapture& getResultCapture();
static IReporterRegistry& getReporterRegistry(); static void setResultCapture
static ITestCaseRegistry& getTestCaseRegistry(); ( IResultCapture* resultCapture
static IRunner& getRunner(); );
static IResultCapture& getResultCapture
();
static IReporterRegistry& getReporterRegistry
();
static ITestCaseRegistry& getTestCaseRegistry
();
static IRunner& getRunner
();
private: private:
std::auto_ptr<IReporterRegistry> m_reporterRegistry; std::auto_ptr<IReporterRegistry> m_reporterRegistry;

View File

@ -18,10 +18,18 @@ namespace Catch
{ {
struct IRunner struct IRunner
{ {
virtual void runAll() = 0; virtual void runAll
virtual std::size_t runMatching( const std::string& rawTestSpec ) = 0; () = 0;
virtual std::size_t getSuccessCount() const = 0;
virtual std:: size_t getFailureCount() const = 0; virtual std::size_t runMatching
( const std::string& rawTestSpec
) = 0;
virtual std::size_t getSuccessCount
() const = 0;
virtual std:: size_t getFailureCount
() const = 0;
}; };
} }

View File

@ -21,6 +21,7 @@ namespace Catch
{ {
public: public:
///////////////////////////////////////////////////////////////////////////
ResultInfo() ResultInfo()
: m_line( 0 ), : m_line( 0 ),
m_result( ResultWas::Unknown ), m_result( ResultWas::Unknown ),
@ -28,7 +29,16 @@ namespace Catch
m_expressionIncomplete( false ) m_expressionIncomplete( false )
{} {}
ResultInfo( const char* expr, ResultWas::OfType result, bool isNot, const char* filename, std::size_t line, const char* macroName ) ///////////////////////////////////////////////////////////////////////////
ResultInfo
(
const char* expr,
ResultWas::OfType result,
bool isNot,
const char* filename,
std::size_t line,
const char* macroName
)
: m_macroName( macroName ), : m_macroName( macroName ),
m_filename( filename ), m_filename( filename ),
m_line( line ), m_line( line ),
@ -42,28 +52,37 @@ namespace Catch
m_expr = "!" + m_expr; m_expr = "!" + m_expr;
} }
///////////////////////////////////////////////////////////////////////////
bool ok() const bool ok() const
{ {
return ( m_result & ResultWas::FailureBit ) != ResultWas::FailureBit; return ( m_result & ResultWas::FailureBit ) != ResultWas::FailureBit;
} }
///////////////////////////////////////////////////////////////////////////
ResultWas::OfType getResultType() const ResultWas::OfType getResultType() const
{ {
return m_result; return m_result;
} }
///////////////////////////////////////////////////////////////////////////
bool hasExpression() const bool hasExpression() const
{ {
return !m_expr.empty(); return !m_expr.empty();
} }
///////////////////////////////////////////////////////////////////////////
bool hasMessage() const bool hasMessage() const
{ {
return !m_message.empty(); return !m_message.empty();
} }
///////////////////////////////////////////////////////////////////////////
std::string getExpression() const std::string getExpression() const
{ {
return m_expr; return m_expr;
} }
///////////////////////////////////////////////////////////////////////////
std::string getExpandedExpression() const std::string getExpandedExpression() const
{ {
if( !hasExpression() ) if( !hasExpression() )
@ -74,27 +93,33 @@ namespace Catch
: getExpandedExpressionInternal(); : getExpandedExpressionInternal();
} }
///////////////////////////////////////////////////////////////////////////
std::string getMessage() const std::string getMessage() const
{ {
return m_message; return m_message;
} }
///////////////////////////////////////////////////////////////////////////
std::string getFilename() const std::string getFilename() const
{ {
return m_filename; return m_filename;
} }
///////////////////////////////////////////////////////////////////////////
std::size_t getLine() const std::size_t getLine() const
{ {
return m_line; return m_line;
} }
///////////////////////////////////////////////////////////////////////////
std::string getTestMacroName() const std::string getTestMacroName() const
{ {
return m_macroName; return m_macroName;
} }
protected: protected:
///////////////////////////////////////////////////////////////////////////
std::string getExpandedExpressionInternal() const std::string getExpandedExpressionInternal() const
{ {
if( m_op == "" || m_isNot ) if( m_op == "" || m_isNot )

View File

@ -71,13 +71,19 @@ namespace Catch
std::string& m_targetString; std::string& m_targetString;
}; };
class Runner : public IResultCapture, public IRunner class Runner : public IResultCapture, public IRunner
{ {
Runner( const Runner& ); Runner( const Runner& );
void operator =( const Runner& ); void operator =( const Runner& );
public: public:
explicit Runner( const Config& config )
///////////////////////////////////////////////////////////////////////////
explicit Runner
(
const Config& config
)
: m_config( config ), : m_config( config ),
m_successes( 0 ), m_successes( 0 ),
m_failures( 0 ), m_failures( 0 ),
@ -88,14 +94,18 @@ namespace Catch
m_reporter->StartTesting(); m_reporter->StartTesting();
} }
~Runner() ///////////////////////////////////////////////////////////////////////////
~Runner
()
{ {
m_reporter->EndTesting( m_successes, m_failures ); m_reporter->EndTesting( m_successes, m_failures );
Hub::setRunner( NULL ); Hub::setRunner( NULL );
Hub::setResultCapture( NULL ); Hub::setResultCapture( NULL );
} }
virtual void runAll() ///////////////////////////////////////////////////////////////////////////
virtual void runAll
()
{ {
std::vector<TestCaseInfo> allTests = Hub::getTestCaseRegistry().getAllTests(); std::vector<TestCaseInfo> allTests = Hub::getTestCaseRegistry().getAllTests();
for( std::size_t i=0; i < allTests.size(); ++i ) for( std::size_t i=0; i < allTests.size(); ++i )
@ -104,7 +114,11 @@ namespace Catch
} }
} }
virtual std::size_t runMatching( const std::string& rawTestSpec ) ///////////////////////////////////////////////////////////////////////////
virtual std::size_t runMatching
(
const std::string& rawTestSpec
)
{ {
TestSpec testSpec( rawTestSpec ); TestSpec testSpec( rawTestSpec );
@ -121,7 +135,11 @@ namespace Catch
return testsRun; return testsRun;
} }
void runTest( const TestCaseInfo& testInfo ) ///////////////////////////////////////////////////////////////////////////
void runTest
(
const TestCaseInfo& testInfo
)
{ {
m_reporter->StartTestCase( testInfo ); m_reporter->StartTestCase( testInfo );
@ -152,24 +170,38 @@ namespace Catch
m_reporter->EndTestCase( testInfo, redirectedCout, redirectedCerr ); m_reporter->EndTestCase( testInfo, redirectedCout, redirectedCerr );
} }
virtual std::size_t getSuccessCount() const ///////////////////////////////////////////////////////////////////////////
virtual std::size_t getSuccessCount
()
const
{ {
return m_successes; return m_successes;
} }
virtual std:: size_t getFailureCount() const ///////////////////////////////////////////////////////////////////////////
virtual std:: size_t getFailureCount
()
const
{ {
return m_failures; return m_failures;
} }
private: // IResultCapture private: // IResultCapture
virtual ResultAction::Value acceptResult( bool result ) ///////////////////////////////////////////////////////////////////////////
virtual ResultAction::Value acceptResult
(
bool result
)
{ {
return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed ); return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed );
} }
virtual ResultAction::Value acceptResult( ResultWas::OfType result ) ///////////////////////////////////////////////////////////////////////////
virtual ResultAction::Value acceptResult
(
ResultWas::OfType result
)
{ {
m_currentResult.setResultType( result ); m_currentResult.setResultType( result );
testEnded( m_currentResult ); testEnded( m_currentResult );
@ -185,19 +217,29 @@ namespace Catch
} }
virtual void acceptExpression( const MutableResultInfo& resultInfo ) ///////////////////////////////////////////////////////////////////////////
virtual void acceptExpression
(
const MutableResultInfo& resultInfo
)
{ {
m_currentResult = resultInfo; m_currentResult = resultInfo;
} }
virtual void acceptMessage( const std::string& msg ) ///////////////////////////////////////////////////////////////////////////
virtual void acceptMessage
(
const std::string& msg
)
{ {
m_currentResult.setMessage( msg ); m_currentResult.setMessage( msg );
} }
/// ///////////////////////////////////////////////////////////////////////////
virtual void testEnded
virtual void testEnded( const ResultInfo& result ) (
const ResultInfo& result
)
{ {
if( result.getResultType() == ResultWas::Ok ) if( result.getResultType() == ResultWas::Ok )
{ {
@ -220,7 +262,14 @@ namespace Catch
m_reporter->Result( result ); m_reporter->Result( result );
} }
virtual bool sectionStarted( const std::string& name, const std::string& description, std::size_t& successes, std::size_t& failures ) ///////////////////////////////////////////////////////////////////////////
virtual bool sectionStarted
(
const std::string& name,
const std::string& description,
std::size_t& successes, \
std::size_t& failures
)
{ {
m_reporter->StartSection( name, description ); m_reporter->StartSection( name, description );
successes = m_successes; successes = m_successes;
@ -230,21 +279,40 @@ namespace Catch
return true; return true;
} }
virtual void sectionEnded( const std::string& name, std::size_t prevSuccesses, std::size_t prevFailures ) ///////////////////////////////////////////////////////////////////////////
virtual void sectionEnded
(
const std::string& name,
std::size_t prevSuccesses,
std::size_t prevFailures
)
{ {
m_reporter->EndSection( name, m_successes - prevSuccesses, m_failures - prevFailures ); m_reporter->EndSection( name, m_successes - prevSuccesses, m_failures - prevFailures );
} }
virtual void pushScopedInfo( ScopedInfo* scopedInfo ) ///////////////////////////////////////////////////////////////////////////
virtual void pushScopedInfo
(
ScopedInfo* scopedInfo
)
{ {
m_scopedInfos.push_back( scopedInfo ); m_scopedInfos.push_back( scopedInfo );
} }
virtual void popScopedInfo( ScopedInfo* scopedInfo )
///////////////////////////////////////////////////////////////////////////
virtual void popScopedInfo
(
ScopedInfo* scopedInfo
)
{ {
if( m_scopedInfos.back() == scopedInfo ) if( m_scopedInfos.back() == scopedInfo )
m_scopedInfos.pop_back(); m_scopedInfos.pop_back();
} }
virtual bool shouldDebugBreak() const
///////////////////////////////////////////////////////////////////////////
virtual bool shouldDebugBreak
()
const
{ {
return m_config.shouldDebugBreak(); return m_config.shouldDebugBreak();
} }