2010-11-09 23:24:00 +00:00
|
|
|
/*
|
|
|
|
* ExceptionTests.cpp
|
|
|
|
* Catch - Test
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-12 08:12:01 +00:00
|
|
|
#include "../catch.hpp"
|
2010-11-09 23:24:00 +00:00
|
|
|
|
|
|
|
#include <string>
|
2011-01-07 10:22:24 +00:00
|
|
|
#include <stdexcept>
|
2010-11-09 23:24:00 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2011-01-31 20:15:40 +00:00
|
|
|
ATTRIBUTE_NORETURN
|
|
|
|
int thisThrows();
|
|
|
|
|
2010-11-09 23:24:00 +00:00
|
|
|
int thisThrows()
|
|
|
|
{
|
|
|
|
throw std::domain_error( "expected exception" );
|
2011-01-31 20:15:40 +00:00
|
|
|
/*NOTREACHED*/
|
2010-11-09 23:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int thisDoesntThrow()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-14 08:47:43 +00:00
|
|
|
TEST_CASE( "./succeeding/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
|
2010-11-09 23:24:00 +00:00
|
|
|
{
|
2010-12-14 09:00:09 +00:00
|
|
|
REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
|
|
|
|
REQUIRE_NOTHROW( thisDoesntThrow() );
|
|
|
|
REQUIRE_THROWS( thisThrows() );
|
2010-11-09 23:24:00 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 08:47:43 +00:00
|
|
|
TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
|
2010-11-09 23:24:00 +00:00
|
|
|
{
|
|
|
|
CHECK_THROWS_AS( thisThrows(), std::string );
|
|
|
|
CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
|
|
|
|
CHECK_NOTHROW( thisThrows() );
|
|
|
|
}
|
|
|
|
|
2011-03-14 19:21:35 +00:00
|
|
|
TEST_CASE_NORETURN( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
|
2010-11-09 23:24:00 +00:00
|
|
|
{
|
|
|
|
throw std::domain_error( "unexpected exception" );
|
2011-01-31 20:15:40 +00:00
|
|
|
/*NOTREACHED*/
|
2010-11-09 23:24:00 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 08:47:43 +00:00
|
|
|
TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" )
|
2010-11-09 23:24:00 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
throw std::domain_error( "unexpected exception" );
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 15:40:40 +01:00
|
|
|
|
|
|
|
class CustomException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CustomException( const std::string& msg )
|
|
|
|
: m_msg( msg )
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::string getMessage() const
|
|
|
|
{
|
|
|
|
return m_msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace Catch
|
|
|
|
{
|
|
|
|
ExceptionTranslator<CustomException> translator;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::string ExceptionTranslator<CustomException>::translate( CustomException& ex ) const
|
|
|
|
{
|
|
|
|
return ex.getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_NORETURN( "./succeeding/exceptions/custom", "" )
|
|
|
|
{
|
|
|
|
throw CustomException( "custom exception" );
|
|
|
|
}
|