2010-11-10 00:24:00 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 09/11/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
|
2011-04-26 09:32:40 +02:00
|
|
|
#include "catch.hpp"
|
2010-11-10 00:24:00 +01:00
|
|
|
|
|
|
|
#include <string>
|
2011-01-07 11:22:24 +01:00
|
|
|
#include <stdexcept>
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-02-15 09:20:06 +01:00
|
|
|
#include "catch_self_test.hpp"
|
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
namespace
|
|
|
|
{
|
2013-04-23 21:52:49 +02:00
|
|
|
inline int thisThrows()
|
2010-11-10 00:24:00 +01:00
|
|
|
{
|
2013-04-23 21:52:49 +02:00
|
|
|
if( Catch::isTrue( true ) )
|
|
|
|
throw std::domain_error( "expected exception" );
|
|
|
|
return 1;
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int thisDoesntThrow()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
namespace ExceptionTests
|
2010-11-10 00:24:00 +01:00
|
|
|
{
|
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./succeeding/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
|
|
|
|
{
|
|
|
|
REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
|
|
|
|
REQUIRE_NOTHROW( thisDoesntThrow() );
|
|
|
|
REQUIRE_THROWS( thisThrows() );
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
|
2013-02-19 20:45:09 +01:00
|
|
|
{
|
2013-11-17 15:18:02 +01:00
|
|
|
CHECK_THROWS_AS( thisThrows(), std::string );
|
|
|
|
CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
|
|
|
|
CHECK_NOTHROW( thisThrows() );
|
2013-02-19 20:45:09 +01:00
|
|
|
}
|
2012-11-17 18:22:37 +01:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
|
|
|
|
{
|
|
|
|
if( Catch::isTrue( true ) )
|
|
|
|
throw std::domain_error( "unexpected exception" );
|
|
|
|
}
|
2013-04-20 22:04:32 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/implicit/2", "An unchecked exception reports the line of the last assertion" )
|
2010-11-10 00:24:00 +01:00
|
|
|
{
|
2013-11-17 15:18:02 +01:00
|
|
|
CHECK( 1 == 1 );
|
|
|
|
if( Catch::isTrue( true ) )
|
|
|
|
throw std::domain_error( "unexpected exception" );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/implicit/3", "When unchecked exceptions are thrown they are always failures" )
|
2010-11-10 00:24:00 +01:00
|
|
|
{
|
2013-11-17 15:18:02 +01:00
|
|
|
SECTION( "section name", "" )
|
|
|
|
{
|
|
|
|
if( Catch::isTrue( true ) )
|
|
|
|
throw std::domain_error( "unexpected exception" );
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/implicit/4", "When unchecked exceptions are thrown they are always failures" )
|
|
|
|
{
|
|
|
|
CHECK( thisThrows() == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" )
|
2011-04-20 16:40:40 +02:00
|
|
|
{
|
2013-11-17 15:18:02 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
throw std::domain_error( "unexpected exception" );
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
}
|
2013-11-17 15:18:02 +01:00
|
|
|
|
|
|
|
class CustomException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CustomException( const std::string& msg )
|
|
|
|
: m_msg( msg )
|
|
|
|
{}
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
std::string getMessage() const
|
|
|
|
{
|
|
|
|
return m_msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_msg;
|
|
|
|
};
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
CATCH_TRANSLATE_EXCEPTION( CustomException& ex )
|
|
|
|
{
|
|
|
|
return ex.getMessage();
|
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
CATCH_TRANSLATE_EXCEPTION( double& ex )
|
|
|
|
{
|
|
|
|
return Catch::toString( ex );
|
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" )
|
|
|
|
{
|
|
|
|
if( Catch::isTrue( true ) )
|
|
|
|
throw CustomException( "custom exception" );
|
|
|
|
}
|
2013-04-23 21:52:49 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
inline void throwCustom() {
|
|
|
|
if( Catch::isTrue( true ) )
|
|
|
|
throw CustomException( "custom exception - not std" );
|
|
|
|
}
|
2011-04-20 20:09:41 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/custom/nothrow", "Custom exceptions can be translated when testing for nothrow" )
|
|
|
|
{
|
|
|
|
REQUIRE_NOTHROW( throwCustom() );
|
|
|
|
}
|
2011-04-20 20:09:41 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/custom/throw", "Custom exceptions can be translated when testing for throwing as something else" )
|
|
|
|
{
|
|
|
|
REQUIRE_THROWS_AS( throwCustom(), std::exception );
|
|
|
|
}
|
2011-04-20 20:09:41 +02:00
|
|
|
|
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" )
|
|
|
|
{
|
|
|
|
if( Catch::isTrue( true ) )
|
|
|
|
throw double( 3.14 );
|
|
|
|
}
|
2011-04-21 20:43:55 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
inline int thisFunctionNotImplemented( int ) {
|
|
|
|
CATCH_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-07-05 19:37:58 +02:00
|
|
|
|
2013-11-17 15:18:02 +01:00
|
|
|
TEST_CASE( "./succeeding/exceptions/notimplemented", "" )
|
|
|
|
{
|
|
|
|
REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) );
|
|
|
|
}
|
2012-07-05 19:37:58 +02:00
|
|
|
}
|