mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
Removed use of compiler specific techniques for denoting non-returning functions
- use if( Catch::isTrue( true) ) to guard throws instead
This commit is contained in:
parent
2a9d8d9e36
commit
bf37e6879a
@ -80,12 +80,10 @@
|
||||
|
||||
#ifdef CATCH_CONFIG_VARIADIC_MACROS
|
||||
#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
|
||||
#define CATCH_TEST_CASE_NORETURN( ... ) INTERNAL_CATCH_TESTCASE_NORETURN( __VA_ARGS__ )
|
||||
#define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ )
|
||||
#define CATCH_SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ )
|
||||
#else
|
||||
#define CATCH_TEST_CASE( name, description ) INTERNAL_CATCH_TESTCASE( name, description )
|
||||
#define CATCH_TEST_CASE_NORETURN( name, description ) INTERNAL_CATCH_TESTCASE_NORETURN( name, description )
|
||||
#define CATCH_METHOD_AS_TEST_CASE( method, name, description ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, name, description )
|
||||
#define CATCH_SECTION( name, description ) INTERNAL_CATCH_SECTION( name, description )
|
||||
#endif
|
||||
@ -141,12 +139,10 @@
|
||||
|
||||
#ifdef CATCH_CONFIG_VARIADIC_MACROS
|
||||
#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
|
||||
#define TEST_CASE_NORETURN( ... ) INTERNAL_CATCH_TESTCASE_NORETURN( __VA_ARGS__ )
|
||||
#define METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ )
|
||||
#define SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ )
|
||||
#else
|
||||
#define TEST_CASE( name, description ) INTERNAL_CATCH_TESTCASE( name, description )
|
||||
#define TEST_CASE_NORETURN( name, description ) INTERNAL_CATCH_TESTCASE_NORETURN( name, description )
|
||||
#define METHOD_AS_TEST_CASE( method, name, description ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, name, description )
|
||||
#define SECTION( name, description ) INTERNAL_CATCH_SECTION( name, description )
|
||||
#endif
|
||||
|
@ -57,10 +57,6 @@ namespace Catch {
|
||||
|
||||
struct TestFailureException{};
|
||||
|
||||
|
||||
// This is just here to avoid compiler warnings with macro constants and boolean literals
|
||||
inline bool isTrue( bool value ){ return value; }
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -44,7 +44,6 @@ namespace Catch {
|
||||
std::string operator[]( std::size_t i ) const { return m_args[i]; }
|
||||
std::size_t argsCount() const { return m_args.size(); }
|
||||
|
||||
CATCH_ATTRIBUTE_NORETURN
|
||||
void raiseError( std::string const& message ) const {
|
||||
std::ostringstream oss;
|
||||
if( m_name.empty() )
|
||||
@ -56,6 +55,7 @@ namespace Catch {
|
||||
oss << " Arguments were:";
|
||||
for( std::size_t i = 0; i < m_args.size(); ++i )
|
||||
oss << " " << m_args[i];
|
||||
if( isTrue( true ) )
|
||||
throw std::domain_error( oss.str() );
|
||||
}
|
||||
|
||||
|
@ -15,16 +15,12 @@
|
||||
#define INTERNAL_CATCH_STRINGIFY2( expr ) #expr
|
||||
#define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr )
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define CATCH_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
|
||||
#else
|
||||
#define CATCH_ATTRIBUTE_NORETURN
|
||||
#endif
|
||||
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
|
||||
#include "catch_compiler_capabilities.h"
|
||||
|
||||
namespace Catch {
|
||||
|
||||
class NonCopyable {
|
||||
@ -134,10 +130,13 @@ namespace Catch {
|
||||
return os;
|
||||
}
|
||||
|
||||
CATCH_ATTRIBUTE_NORETURN
|
||||
// This is just here to avoid compiler warnings with macro constants and boolean literals
|
||||
inline bool isTrue( bool value ){ return value; }
|
||||
|
||||
inline void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ) {
|
||||
std::ostringstream oss;
|
||||
oss << locationInfo << ": Internal Catch error: '" << message << "'";
|
||||
if( isTrue( true ))
|
||||
throw std::logic_error( oss.str() );
|
||||
}
|
||||
}
|
||||
|
@ -80,12 +80,6 @@ private:
|
||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ ), CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); }\
|
||||
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )()
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define INTERNAL_CATCH_TESTCASE_NORETURN( ... ) \
|
||||
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )() CATCH_ATTRIBUTE_NORETURN; \
|
||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ ), CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); }\
|
||||
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )()
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \
|
||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); }
|
||||
@ -107,12 +101,6 @@ private:
|
||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ ), CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( Name, Desc ) ); }\
|
||||
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )()
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define INTERNAL_CATCH_TESTCASE_NORETURN( Name, Desc ) \
|
||||
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )() CATCH_ATTRIBUTE_NORETURN; \
|
||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ ), CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( Name, Desc ) ); }\
|
||||
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )()
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \
|
||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( Name, Desc ), CATCH_INTERNAL_LINEINFO ); }
|
||||
|
@ -15,13 +15,11 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
CATCH_ATTRIBUTE_NORETURN
|
||||
int thisThrows();
|
||||
|
||||
int thisThrows()
|
||||
inline int thisThrows()
|
||||
{
|
||||
if( Catch::isTrue( true ) )
|
||||
throw std::domain_error( "expected exception" );
|
||||
/*NOTREACHED*/
|
||||
return 1;
|
||||
}
|
||||
|
||||
int thisDoesntThrow()
|
||||
@ -37,7 +35,6 @@ TEST_CASE( "./succeeding/exceptions/explicit", "When checked exceptions are thro
|
||||
REQUIRE_THROWS( thisThrows() );
|
||||
}
|
||||
|
||||
CATCH_ATTRIBUTE_NORETURN
|
||||
TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
|
||||
{
|
||||
CHECK_THROWS_AS( thisThrows(), std::string );
|
||||
@ -45,31 +42,30 @@ TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown
|
||||
CHECK_NOTHROW( thisThrows() );
|
||||
}
|
||||
|
||||
TEST_CASE_NORETURN( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
|
||||
TEST_CASE( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
|
||||
{
|
||||
if( Catch::isTrue( true ) )
|
||||
throw std::domain_error( "unexpected exception" );
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
|
||||
TEST_CASE_NORETURN( "./failing/exceptions/implicit/2", "An unchecked exception reports the line of the last assertion" )
|
||||
TEST_CASE( "./failing/exceptions/implicit/2", "An unchecked exception reports the line of the last assertion" )
|
||||
{
|
||||
CHECK( 1 == 1 );
|
||||
if( Catch::isTrue( true ) )
|
||||
throw std::domain_error( "unexpected exception" );
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
TEST_CASE( "./failing/exceptions/implicit/3", "When unchecked exceptions are thrown they are always failures" )
|
||||
{
|
||||
SECTION( "section name", "" )
|
||||
{
|
||||
if( Catch::isTrue( true ) )
|
||||
throw std::domain_error( "unexpected exception" );
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_NORETURN( "./failing/exceptions/implicit/4", "When unchecked exceptions are thrown they are always failures" )
|
||||
TEST_CASE( "./failing/exceptions/implicit/4", "When unchecked exceptions are thrown they are always failures" )
|
||||
{
|
||||
CHECK( thisThrows() == 0 );
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
|
||||
TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" )
|
||||
@ -109,27 +105,31 @@ CATCH_TRANSLATE_EXCEPTION( double& ex )
|
||||
return Catch::toString( ex );
|
||||
}
|
||||
|
||||
TEST_CASE_NORETURN( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" )
|
||||
TEST_CASE( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" )
|
||||
{
|
||||
if( Catch::isTrue( true ) )
|
||||
throw CustomException( "custom exception" );
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4702) // unreachable code
|
||||
#endif
|
||||
|
||||
inline void throwCustom() {
|
||||
if( Catch::isTrue( true ) )
|
||||
throw CustomException( "custom exception - not std" );
|
||||
}
|
||||
|
||||
TEST_CASE( "./failing/exceptions/custom/nothrow", "Custom exceptions can be translated when testing for nothrow" )
|
||||
{
|
||||
REQUIRE_NOTHROW( throw CustomException( "unexpected custom exception" ) );
|
||||
REQUIRE_NOTHROW( throwCustom() );
|
||||
}
|
||||
|
||||
TEST_CASE( "./failing/exceptions/custom/throw", "Custom exceptions can be translated when testing for throwing as something else" )
|
||||
{
|
||||
REQUIRE_THROWS_AS( throw CustomException( "custom exception - not std" ), std::exception );
|
||||
REQUIRE_THROWS_AS( throwCustom(), std::exception );
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE_NORETURN( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" )
|
||||
TEST_CASE( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" )
|
||||
{
|
||||
if( Catch::isTrue( true ) )
|
||||
throw double( 3.14 );
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user