diff options
| author | Phil Nash | 2012-02-10 08:30:13 +0000 |
|---|---|---|
| committer | Phil Nash | 2012-02-10 08:30:13 +0000 |
| commit | a162e22fa3080faa7c3051677ef24a14b9f8daf4 (patch) | |
| tree | c63e72a22cd308f29604a27dd707568410b60a3a | |
| parent | 6abf702895c5433f0790845b76eb6554d6cc7445 (diff) | |
| download | catch3-a162e22fa3080faa7c3051677ef24a14b9f8daf4.tar.gz | |
Added CHECKED_IF and CHECKED_ELSE
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | include/catch.hpp | 2 | ||||
| -rw-r--r-- | include/internal/catch_capture.hpp | 10 | ||||
| -rw-r--r-- | include/internal/catch_interfaces_capture.h | 3 | ||||
| -rw-r--r-- | include/internal/catch_resultinfo.hpp | 7 | ||||
| -rw-r--r-- | include/internal/catch_runner_impl.hpp | 13 | ||||
| -rw-r--r-- | projects/SelfTest/MiscTests.cpp | 36 | ||||
| -rw-r--r-- | projects/SelfTest/TestMain.cpp | 4 | ||||
| -rw-r--r-- | single_include/catch.hpp | 37 |
9 files changed, 102 insertions, 14 deletions
@@ -7,5 +7,5 @@ Debug Release *.user *.xcuserstate -/Test/Test.xcodeproj/xcuserdata/ -.DS_Store
\ No newline at end of file +.DS_Store +xcuserdata
\ No newline at end of file diff --git a/include/catch.hpp b/include/catch.hpp index d314d3c2..7697c231 100644 --- a/include/catch.hpp +++ b/include/catch.hpp @@ -53,6 +53,8 @@ #define CHECK( expr ) INTERNAL_CATCH_TEST( expr, false, false, "CHECK" ) #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_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" ) diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index 738b8214..2449f60b 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -688,6 +688,16 @@ inline bool isTrue 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 ) \ try \ { \ diff --git a/include/internal/catch_interfaces_capture.h b/include/internal/catch_interfaces_capture.h index f3a530d6..e74563ef 100644 --- a/include/internal/catch_interfaces_capture.h +++ b/include/internal/catch_interfaces_capture.h @@ -20,6 +20,7 @@ namespace Catch class TestCaseInfo; class ScopedInfo; class MutableResultInfo; + class ResultInfo; struct IResultCapture { @@ -66,6 +67,8 @@ namespace Catch ) = 0; virtual std::string getCurrentTestName + () const = 0; + virtual const ResultInfo* getLastResult () const = 0; }; diff --git a/include/internal/catch_resultinfo.hpp b/include/internal/catch_resultinfo.hpp index 258cc47c..f9da25a8 100644 --- a/include/internal/catch_resultinfo.hpp +++ b/include/internal/catch_resultinfo.hpp @@ -63,10 +63,11 @@ namespace Catch } /////////////////////////////////////////////////////////////////////////// - virtual ~ResultInfo + virtual ~ResultInfo () - { - } + { + } + /////////////////////////////////////////////////////////////////////////// bool ok () diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 0bb28203..17ef47fe 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -548,6 +548,14 @@ namespace Catch ? m_runningTest->getTestCaseInfo().getName() : ""; } + + /////////////////////////////////////////////////////////////////////////// + virtual const ResultInfo* getLastResult + () + const + { + return &m_lastResult; + } private: @@ -556,10 +564,10 @@ namespace Catch () { testEnded( m_currentResult ); + m_lastResult = m_currentResult; - bool ok = m_currentResult.ok(); m_currentResult = MutableResultInfo(); - if( ok ) + if( m_lastResult.ok() ) return ResultAction::None; else if( shouldDebugBreak() ) return ResultAction::DebugFailed; @@ -612,6 +620,7 @@ namespace Catch private: RunningTest* m_runningTest; MutableResultInfo m_currentResult; + ResultInfo m_lastResult; const Config& m_config; std::size_t m_successes; diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp index 0a4d1bb9..60a57350 100644 --- a/projects/SelfTest/MiscTests.cpp +++ b/projects/SelfTest/MiscTests.cpp @@ -121,3 +121,39 @@ TEST_CASE( "./failing/info", "sends information to INFO" ) CAPTURE( i ); 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 ) ); +} diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp index 0ca760e5..868f0f60 100644 --- a/projects/SelfTest/TestMain.cpp +++ b/projects/SelfTest/TestMain.cpp @@ -47,7 +47,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" "Number of 'succeeding' tests is fixed" ) { runner.runMatching( "./succeeding/*" ); - CHECK( runner.getSuccessCount() == 258 ); + CHECK( runner.getSuccessCount() == 262 ); CHECK( runner.getFailureCount() == 0 ); } @@ -56,7 +56,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" { runner.runMatching( "./failing/*" ); CHECK( runner.getSuccessCount() == 0 ); - CHECK( runner.getFailureCount() == 64 ); + CHECK( runner.getFailureCount() == 68 ); } } } diff --git a/single_include/catch.hpp b/single_include/catch.hpp index c808f605..3b2f8d8f 100644 --- a/single_include/catch.hpp +++ b/single_include/catch.hpp @@ -701,10 +701,11 @@ namespace Catch } /////////////////////////////////////////////////////////////////////////// - virtual ~ResultInfo + virtual ~ResultInfo () - { - } + { + } + /////////////////////////////////////////////////////////////////////////// bool ok () @@ -844,6 +845,7 @@ namespace Catch class TestCaseInfo; class ScopedInfo; class MutableResultInfo; + class ResultInfo; struct IResultCapture { @@ -891,6 +893,8 @@ namespace Catch virtual std::string getCurrentTestName () const = 0; + virtual const ResultInfo* getLastResult + () const = 0; }; } @@ -1870,6 +1874,16 @@ inline bool isTrue 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 ) \ try \ { \ @@ -2786,7 +2800,9 @@ namespace Catch #define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED +#import <Foundation/Foundation.h> #import <objc/runtime.h> + #include <string> // 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: /////////////////////////////////////////////////////////////////////////// @@ -4150,10 +4174,10 @@ namespace Catch () { testEnded( m_currentResult ); + m_lastResult = m_currentResult; - bool ok = m_currentResult.ok(); m_currentResult = MutableResultInfo(); - if( ok ) + if( m_lastResult.ok() ) return ResultAction::None; else if( shouldDebugBreak() ) return ResultAction::DebugFailed; @@ -4206,6 +4230,7 @@ namespace Catch private: RunningTest* m_runningTest; MutableResultInfo m_currentResult; + ResultInfo m_lastResult; const Config& m_config; 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_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_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" ) |