Added CHECKED_IF and CHECKED_ELSE

This commit is contained in:
Phil Nash 2012-02-10 08:30:13 +00:00
parent 6abf702895
commit a162e22fa3
9 changed files with 102 additions and 14 deletions

2
.gitignore vendored
View File

@ -7,5 +7,5 @@ Debug
Release Release
*.user *.user
*.xcuserstate *.xcuserstate
/Test/Test.xcodeproj/xcuserdata/
.DS_Store .DS_Store
xcuserdata

View File

@ -53,6 +53,8 @@
#define CHECK( expr ) INTERNAL_CATCH_TEST( expr, false, false, "CHECK" ) #define CHECK( expr ) INTERNAL_CATCH_TEST( expr, false, false, "CHECK" )
#define CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, false, "CHECK_FALSE" ) #define CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, false, "CHECK_FALSE" )
#define CHECKED_IF( expr ) INTERNAL_CATCH_IF( expr, false, false, "CHECKED_IF" )
#define CHECKED_ELSE( expr ) INTERNAL_CATCH_ELSE( expr, false, false, "CHECKED_ELSE" )
#define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., false, "CHECK_THROWS" ) #define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., false, "CHECK_THROWS" )
#define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" ) #define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" )

View File

@ -687,6 +687,16 @@ inline bool isTrue
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); }
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \
INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ); \
if( Catch::Hub::getResultCapture().getLastResult()->ok() )
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_ELSE( expr, isNot, stopOnFailure, macroName ) \
INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ); \
if( !Catch::Hub::getResultCapture().getLastResult()->ok() )
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_NO_THROW( expr, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_NO_THROW( expr, stopOnFailure, macroName ) \
try \ try \

View File

@ -20,6 +20,7 @@ namespace Catch
class TestCaseInfo; class TestCaseInfo;
class ScopedInfo; class ScopedInfo;
class MutableResultInfo; class MutableResultInfo;
class ResultInfo;
struct IResultCapture struct IResultCapture
{ {
@ -67,6 +68,8 @@ namespace Catch
virtual std::string getCurrentTestName virtual std::string getCurrentTestName
() const = 0; () const = 0;
virtual const ResultInfo* getLastResult
() const = 0;
}; };
} }

View File

@ -63,10 +63,11 @@ namespace Catch
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual ~ResultInfo virtual ~ResultInfo
() ()
{ {
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
bool ok bool ok
() ()

View File

@ -549,6 +549,14 @@ namespace Catch
: ""; : "";
} }
///////////////////////////////////////////////////////////////////////////
virtual const ResultInfo* getLastResult
()
const
{
return &m_lastResult;
}
private: private:
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -556,10 +564,10 @@ namespace Catch
() ()
{ {
testEnded( m_currentResult ); testEnded( m_currentResult );
m_lastResult = m_currentResult;
bool ok = m_currentResult.ok();
m_currentResult = MutableResultInfo(); m_currentResult = MutableResultInfo();
if( ok ) if( m_lastResult.ok() )
return ResultAction::None; return ResultAction::None;
else if( shouldDebugBreak() ) else if( shouldDebugBreak() )
return ResultAction::DebugFailed; return ResultAction::DebugFailed;
@ -612,6 +620,7 @@ namespace Catch
private: private:
RunningTest* m_runningTest; RunningTest* m_runningTest;
MutableResultInfo m_currentResult; MutableResultInfo m_currentResult;
ResultInfo m_lastResult;
const Config& m_config; const Config& m_config;
std::size_t m_successes; std::size_t m_successes;

View File

@ -121,3 +121,39 @@ TEST_CASE( "./failing/info", "sends information to INFO" )
CAPTURE( i ); CAPTURE( i );
REQUIRE( false ); REQUIRE( false );
} }
inline bool testCheckedIf( bool flag )
{
CHECKED_IF( flag )
return true;
else
return false;
}
TEST_CASE( "./succeeding/checkedif", "" )
{
REQUIRE( testCheckedIf( true ) );
}
TEST_CASE( "./failing/checkedif", "" )
{
REQUIRE( testCheckedIf( false ) );
}
inline bool testCheckedElse( bool flag )
{
CHECKED_ELSE( flag )
return false;
return true;
}
TEST_CASE( "./succeeding/checkedelse", "" )
{
REQUIRE( testCheckedElse( true ) );
}
TEST_CASE( "./failing/checkedelse", "" )
{
REQUIRE( testCheckedElse( false ) );
}

View File

@ -47,7 +47,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results"
"Number of 'succeeding' tests is fixed" ) "Number of 'succeeding' tests is fixed" )
{ {
runner.runMatching( "./succeeding/*" ); runner.runMatching( "./succeeding/*" );
CHECK( runner.getSuccessCount() == 258 ); CHECK( runner.getSuccessCount() == 262 );
CHECK( runner.getFailureCount() == 0 ); CHECK( runner.getFailureCount() == 0 );
} }
@ -56,7 +56,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results"
{ {
runner.runMatching( "./failing/*" ); runner.runMatching( "./failing/*" );
CHECK( runner.getSuccessCount() == 0 ); CHECK( runner.getSuccessCount() == 0 );
CHECK( runner.getFailureCount() == 64 ); CHECK( runner.getFailureCount() == 68 );
} }
} }
} }

View File

@ -701,10 +701,11 @@ namespace Catch
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual ~ResultInfo virtual ~ResultInfo
() ()
{ {
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
bool ok bool ok
() ()
@ -844,6 +845,7 @@ namespace Catch
class TestCaseInfo; class TestCaseInfo;
class ScopedInfo; class ScopedInfo;
class MutableResultInfo; class MutableResultInfo;
class ResultInfo;
struct IResultCapture struct IResultCapture
{ {
@ -891,6 +893,8 @@ namespace Catch
virtual std::string getCurrentTestName virtual std::string getCurrentTestName
() const = 0; () const = 0;
virtual const ResultInfo* getLastResult
() const = 0;
}; };
} }
@ -1869,6 +1873,16 @@ inline bool isTrue
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); }
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \
INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ); \
if( Catch::Hub::getResultCapture().getLastResult()->ok() )
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_ELSE( expr, isNot, stopOnFailure, macroName ) \
INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ); \
if( !Catch::Hub::getResultCapture().getLastResult()->ok() )
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_NO_THROW( expr, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_NO_THROW( expr, stopOnFailure, macroName ) \
try \ try \
@ -2786,7 +2800,9 @@ namespace Catch
#define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED #define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED
#import <Foundation/Foundation.h>
#import <objc/runtime.h> #import <objc/runtime.h>
#include <string> #include <string>
// NB. Any general catch headers included here must be included // NB. Any general catch headers included here must be included
@ -4143,6 +4159,14 @@ namespace Catch
: ""; : "";
} }
///////////////////////////////////////////////////////////////////////////
virtual const ResultInfo* getLastResult
()
const
{
return &m_lastResult;
}
private: private:
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -4150,10 +4174,10 @@ namespace Catch
() ()
{ {
testEnded( m_currentResult ); testEnded( m_currentResult );
m_lastResult = m_currentResult;
bool ok = m_currentResult.ok();
m_currentResult = MutableResultInfo(); m_currentResult = MutableResultInfo();
if( ok ) if( m_lastResult.ok() )
return ResultAction::None; return ResultAction::None;
else if( shouldDebugBreak() ) else if( shouldDebugBreak() )
return ResultAction::DebugFailed; return ResultAction::DebugFailed;
@ -4206,6 +4230,7 @@ namespace Catch
private: private:
RunningTest* m_runningTest; RunningTest* m_runningTest;
MutableResultInfo m_currentResult; MutableResultInfo m_currentResult;
ResultInfo m_lastResult;
const Config& m_config; const Config& m_config;
std::size_t m_successes; std::size_t m_successes;
@ -6208,6 +6233,8 @@ int main (int argc, char * const argv[])
#define CHECK( expr ) INTERNAL_CATCH_TEST( expr, false, false, "CHECK" ) #define CHECK( expr ) INTERNAL_CATCH_TEST( expr, false, false, "CHECK" )
#define CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, false, "CHECK_FALSE" ) #define CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, false, "CHECK_FALSE" )
#define CHECKED_IF( expr ) INTERNAL_CATCH_IF( expr, false, false, "CHECKED_IF" )
#define CHECKED_ELSE( expr ) INTERNAL_CATCH_ELSE( expr, false, false, "CHECKED_ELSE" )
#define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., false, "CHECK_THROWS" ) #define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., false, "CHECK_THROWS" )
#define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" ) #define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" )