catch2/projects/SelfTest/ExceptionTests.cpp

167 lines
4.3 KiB
C++
Raw Normal View History

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)
*/
2012-08-16 19:47:41 +02:00
#ifdef __clang__
#pragma clang diagnostic ignored "-Wpadded"
2012-08-16 19:47:41 +02:00
#endif
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
#include "catch_self_test.hpp"
2010-11-10 00:24:00 +01:00
namespace
{
CATCH_ATTRIBUTE_NORETURN
2011-01-31 21:15:40 +01:00
int thisThrows();
2010-11-10 00:24:00 +01:00
int thisThrows()
{
throw std::domain_error( "expected exception" );
2011-01-31 21:15:40 +01:00
/*NOTREACHED*/
2010-11-10 00:24:00 +01:00
}
int thisDoesntThrow()
{
return 0;
}
}
TEST_CASE( "./succeeding/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
2010-11-10 00:24:00 +01:00
{
REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
REQUIRE_NOTHROW( thisDoesntThrow() );
REQUIRE_THROWS( thisThrows() );
2010-11-10 00:24:00 +01:00
}
CATCH_ATTRIBUTE_NORETURN
TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
2010-11-10 00:24:00 +01:00
{
CHECK_THROWS_AS( thisThrows(), std::string );
CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
CHECK_NOTHROW( thisThrows() );
}
2011-03-14 20:21:35 +01:00
TEST_CASE_NORETURN( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
2010-11-10 00:24:00 +01:00
{
throw std::domain_error( "unexpected exception" );
2011-01-31 21:15:40 +01:00
/*NOTREACHED*/
2010-11-10 00:24:00 +01:00
}
TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" )
2010-11-10 00:24:00 +01:00
{
try
{
throw std::domain_error( "unexpected exception" );
}
catch(...)
{
}
}
2011-04-20 16:40:40 +02: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 20:09:41 +02:00
CATCH_TRANSLATE_EXCEPTION( CustomException& ex )
2011-04-20 16:40:40 +02:00
{
2011-04-20 20:09:41 +02:00
return ex.getMessage();
}
2011-04-20 16:40:40 +02:00
2011-04-20 20:09:41 +02:00
CATCH_TRANSLATE_EXCEPTION( double& ex )
{
return Catch::toString( ex );
2011-04-20 16:40:40 +02:00
}
2011-04-20 20:09:41 +02:00
TEST_CASE_NORETURN( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" )
2011-04-20 16:40:40 +02:00
{
throw CustomException( "custom exception" );
}
2011-04-20 20:09:41 +02: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-08-16 21:35:52 +02:00
#ifdef __GNUC__
2012-06-05 11:38:18 +02:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
2012-08-16 21:35:52 +02:00
#endif
2012-06-05 11:38:18 +02:00
TEST_CASE( "./failing/exceptions/in-section", "Exceptions thrown from sections report file/ line or section" )
{
SECTION( "the section", "" )
{
CATCH_REGISTER_LINE_INFO( "the section2" ) SECTION( "the section2", "" )
{
throw std::domain_error( "Exception from section" );
}
}
}
2012-08-16 21:35:52 +02:00
#ifdef __GNUC__
2012-06-05 11:38:18 +02:00
#pragma GCC diagnostic pop
2012-08-16 21:35:52 +02:00
#endif
2012-06-05 11:38:18 +02:00
TEST_CASE( "./succeeding/exceptions/error messages", "The error messages produced by exceptions caught by Catch matched the expected form" )
{
Catch::EmbeddedRunner runner;
using namespace Catch::Matchers;
SECTION( "custom, unexpected", "" )
{
runner.runMatching( "./failing/exceptions/custom" );
// CHECK_THAT( runner.getLog(), Contains( "Unexpected exception" ) ); // Mock reporter doesn't say this
CHECK_THAT( runner.getLog(), Contains( "custom exception" ) );
}
SECTION( "in section", "" )
{
runner.runMatching( "./failing/exceptions/in-section" );
INFO( runner.getLog() );
// CHECK( runner.getLog().find( "Unexpected exception" ) != std::string::npos ); // Mock reporter doesn't say this
CHECK_THAT( runner.getLog(), Contains( "Exception from section" ) );
Squashed commit of the following: commit 70c5ef9eede41e7061ff1af45b5babe0d7cde4ba Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 21:46:01 2012 +0000 Regen single include commit 4ea535e5059a664cc393404b454f29e6dad0886e Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 09:56:30 2012 +0000 Tidied up result enums commit 7717c29072013fd45e36d1bc48527927fa94cbed Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 09:45:29 2012 +0000 Implemented CHECK_NOFAIL Previous commit missed some files commit d539da903032e5a6f2bf40d6d7d8e7874c860088 Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 09:44:52 2012 +0000 Implemented CHECK_NO_FAIL commit af1a321860a8b63fb57fcade75ba7f33cad7400c Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 18:46:39 2012 +0000 Regen single include commit f54ac5625e4702e1c032f7b5a7f4d9346c64f502 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 18:44:12 2012 +0000 New (combined) baselines commit defca58566653fd088f615c2979d7e7a681bf107 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 18:43:23 2012 +0000 negate() -> endExpression(), takes ResultDisposition commit b2ef99882590ccd3824ebca44cb9b4cfc42f4728 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 10:35:09 2012 +0000 Changed shouldNegate boolean to use part of ResultDisposition enum commit 1af13dba97474c2a0b33f1ca1919c80ea4f95ca3 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 10:20:08 2012 +0000 Changed StopOnFailure boolean to an enum commit a1dc7e312cffb0a2d241273adcae4a126d49ff15 Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 19:34:35 2012 +0000 Regen single include commit 20e59ce9d133b469798c4ce2a8d681c0d9b18975 Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 19:34:10 2012 +0000 Added tags docs commit b5b1b1e430c8c61dbd3162da0255a368ba9ad75f Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 19:13:25 2012 +0000 Some expression/ evaluation clean-up commit ec5956f47114ff8b597be85fd7654d29da93aea0 Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 07:52:28 2012 +0000 Fix for #134 The INTERNAL_CATCH_MSG macro, which is used by INFO, WARN and FAIL places its lines in a do…while block so it can be used after an if statement with no block commit 88b70828f24bbbeae636d663b14fd0117165108a Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:39:38 2012 +0000 Regen single include commit b323fc7e6c57caa535d65cae31e6018822ed9e3c Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:39:16 2012 +0000 Fixed line/no regression for SECTIONs Unexpected exceptions within a section now get the SECTION's line no. again (instead of TEST_CASE line/no) commit 78fba28c4b9f48ec25c9d234a29c36e4427ca6e0 Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:11:59 2012 +0000 Added className to TestCaseInfo className is passed through from class based test methods and held in the TestCaseInfo. For free-function based test cases it is set to "global". The JUnit reporter uses the className value to populate he class attribute. commit 81cb69ef1831ed29bf770254c9112b8f0465e912 Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:09:22 2012 +0000 AssertionInfo captures more info (for test cases and sections) commit a4e088c999439e9ec0fe3a4205b7520fbf258a5f Author: Phil Nash <github@philnash.me> Date: Fri Nov 2 08:29:03 2012 +0000 Removed __FUNCTION__ from SourceLineInfo commit 2a1e8bfc6ed3bcc1e3746d717726a9e525de6369 Author: Phil Nash <github@philnash.me> Date: Thu Nov 1 08:16:15 2012 +0000 Updated colour comments commit f0f407fc3ebdcbdad50fc1f9a45f15ab3cc54c63 Author: Phil Nash <github@philnash.me> Date: Wed Oct 31 18:28:21 2012 +0000 Manually applied merge #133 from Master commit 355b95fda1de28ba830541768aad32c7b1790290 Author: Phil Nash <github@philnash.me> Date: Wed Oct 31 18:04:22 2012 +0000 Cleaned up ANSI colour code impl a bit commit 778f9c4fc7fa66566679d9f1d8a721079d8589bf Author: Phil Nash <github@philnash.me> Date: Tue Oct 30 09:09:30 2012 +0000 Removed "no-" from Wno-global-constructors when disabling commit 5efa4bcb8a620efa13e8eb5e68d0899681919ca1 Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 20:49:22 2012 +0000 Regenerated single_include commit 108f1937d8cbe04038ab8d2aaf9f6e958fe8168c Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 20:46:45 2012 +0000 Added terminal colour codes for POSIX With thanks to Adam Strzelecki commit 8f4cc541d55cf55b2a67b5d6b77b5c4ceb4d99ee Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 19:55:34 2012 +0000 Added regression test baselines commit 2e203a183474672e386f4cf840847cf29ccf90b9 Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 19:55:13 2012 +0000 Fixed remaining reporting regressions commit 134e45b3ade922e5cbdb2df6b81830fbd93fe448 Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 20:57:21 2012 +0000 Fixed #132 commit 2f92db9898978ca1ee27c964d8a51fa507391935 Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 12:15:34 2012 +0000 Updated the readme specifically for the Integration branch commit 82acc2ca05459ce353de71469f141ba93e9b63cf Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 12:07:17 2012 +0000 Regenerated single include commit fe1d7c1d08f06c0a83b727e9b2e00cc49e74175d Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 10:27:44 2012 +0000 Small fixes and tweaks commit 355b5e546d2f64dd0abbc0134dd3579ce6c28cea Author: Phil Nash <github@philnash.me> Date: Fri Oct 26 09:05:36 2012 +0100 Some tidy-up commit f847186ebbf0e7eeabd99925574c8988add600fb Author: Phil Nash <github@philnash.me> Date: Fri Oct 26 08:45:23 2012 +0100 AssertionResultBuilder -> ExpressionResultBuilder commit 8cca2f13699aa6aa5cd4a60e293f49a1bae54629 Author: Phil Nash <github@philnash.me> Date: Wed Oct 24 22:09:01 2012 +0100 ExpressionBuilder ->ExpressionDecomposer Expression -> ExpressionLhs commit e04e74f896c714de1f03a9cc006bcd071318ea11 Author: Phil Nash <github@philnash.me> Date: Wed Oct 24 21:59:47 2012 +0100 More AssertionResult refactoring commit 1dd56d4d2b29520cdd62f5f449a28e67f954f5db Author: Phil Nash <github@philnash.me> Date: Fri Oct 19 08:01:34 2012 +0100 AssertionResultBuilder can be constructed from result type commit f2d5f1b3e444055ff02ed0536d5d0610b08ecb9f Author: Phil Nash <github@philnash.me> Date: Fri Oct 19 08:01:05 2012 +0100 Expression has its own result builder - not passed in from expression builder commit e3b111a39a4edd3bfe4d88290c99c723b8b74417 Author: Phil Nash <github@philnash.me> Date: Thu Oct 18 22:59:16 2012 +0100 streamlined acceptResult commit 3ad13256e1e50a6ee4a2697e7b6d0f669ae82ab5 Author: Phil Nash <github@philnash.me> Date: Thu Oct 18 08:39:44 2012 +0100 Refactored assertion builder stuff out of expression builder commit c96f9330a095f527673de4d8db4f434d38a7e813 Author: Phil Nash <github@philnash.me> Date: Wed Oct 17 08:14:22 2012 +0100 Collect assertion info up front commit a5fa78284d6d95770cd647a2ec10df09d378ba46 Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:33:13 2012 +0100 ResultData -> AssertionResultData commit c597a893fa95462b8d2d5029547d4054eda1839f Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:31:05 2012 +0100 ResultInfo -> AssertionResult filenames and variables commit d16955f63a4a128221da41602215b5592df39eb9 Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:27:21 2012 +0100 Renamed ResultInfo -> AssertionResult commit 175da3ef6454b6fa0465d22c96d21d58dd9d2f90 Author: Phil Nash <github@philnash.me> Date: Fri Oct 12 18:39:22 2012 +0100 regen test 3
2012-11-13 22:59:50 +01:00
CHECK_THAT( runner.getLog(), Contains( CATCH_GET_LINE_INFO( "the section2" ) ) );
}
}
2012-07-05 19:37:58 +02:00
inline int thisFunctionNotImplemented( int ) {
CATCH_NOT_IMPLEMENTED;
}
TEST_CASE( "./succeeding/exceptions/notimplemented", "" )
{
REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) );
}