Catch/projects/SelfTest/ExceptionTests.cpp

131 lines
3.1 KiB
C++
Raw Normal View History

2010-11-09 18:24:00 -05: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)
*/
2012-08-16 13:47:41 -04:00
#ifdef __clang__
#pragma clang diagnostic ignored "-Wpadded"
2012-08-16 13:47:41 -04:00
#endif
2011-04-26 03:32:40 -04:00
#include "catch.hpp"
2010-11-09 18:24:00 -05:00
#include <string>
2011-01-07 05:22:24 -05:00
#include <stdexcept>
2010-11-09 18:24:00 -05:00
#include "catch_self_test.hpp"
2010-11-09 18:24:00 -05:00
namespace
{
CATCH_ATTRIBUTE_NORETURN
2011-01-31 15:15:40 -05:00
int thisThrows();
2010-11-09 18:24:00 -05:00
int thisThrows()
{
throw std::domain_error( "expected exception" );
2011-01-31 15:15:40 -05:00
/*NOTREACHED*/
2010-11-09 18:24:00 -05:00
}
int thisDoesntThrow()
{
return 0;
}
}
TEST_CASE( "./succeeding/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
2010-11-09 18:24:00 -05:00
{
REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
REQUIRE_NOTHROW( thisDoesntThrow() );
REQUIRE_THROWS( thisThrows() );
2010-11-09 18:24:00 -05:00
}
CATCH_ATTRIBUTE_NORETURN
TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
2010-11-09 18:24:00 -05:00
{
CHECK_THROWS_AS( thisThrows(), std::string );
CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
CHECK_NOTHROW( thisThrows() );
}
2011-03-14 15:21:35 -04:00
TEST_CASE_NORETURN( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
2010-11-09 18:24:00 -05:00
{
throw std::domain_error( "unexpected exception" );
2011-01-31 15:15:40 -05:00
/*NOTREACHED*/
2010-11-09 18:24:00 -05:00
}
TEST_CASE_NORETURN( "./failing/exceptions/implicit/2", "An unchecked exception reports the line of the last assertion" )
{
CHECK( 1 == 1 );
throw std::domain_error( "unexpected exception" );
/*NOTREACHED*/
}
TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" )
2010-11-09 18:24:00 -05:00
{
try
{
throw std::domain_error( "unexpected exception" );
}
catch(...)
{
}
}
2011-04-20 10:40:40 -04:00
class CustomException
{
public:
CustomException( const std::string& msg )
: m_msg( msg )
{}
std::string getMessage() const
{
return m_msg;
}
private:
std::string m_msg;
};
2011-04-20 14:09:41 -04:00
CATCH_TRANSLATE_EXCEPTION( CustomException& ex )
2011-04-20 10:40:40 -04:00
{
2011-04-20 14:09:41 -04:00
return ex.getMessage();
}
2011-04-20 10:40:40 -04:00
2011-04-20 14:09:41 -04:00
CATCH_TRANSLATE_EXCEPTION( double& ex )
{
return Catch::toString( ex );
2011-04-20 10:40:40 -04:00
}
2011-04-20 14:09:41 -04:00
TEST_CASE_NORETURN( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" )
2011-04-20 10:40:40 -04:00
{
throw CustomException( "custom exception" );
}
2011-04-20 14:09:41 -04:00
TEST_CASE( "./failing/exceptions/custom/nothrow", "Custom exceptions can be translated when testing for nothrow" )
{
REQUIRE_NOTHROW( throw CustomException( "unexpected custom exception" ) );
}
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 );
}
TEST_CASE_NORETURN( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" )
{
throw double( 3.14 );
}
2012-07-05 13:37:58 -04:00
inline int thisFunctionNotImplemented( int ) {
CATCH_NOT_IMPLEMENTED;
}
TEST_CASE( "./succeeding/exceptions/notimplemented", "" )
{
REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) );
}