| 
									
										
										
										
											2010-11-09 23:24:00 +00: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 08:32:40 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 10:52:21 +01:00
										 |  |  | #ifdef _MSC_VER
 | 
					
						
							|  |  |  | #pragma warning(disable:4702) // Unreachable code -- MSVC 19 (VS 2015) sees right through the indirection
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-09-07 16:51:33 +02:00
										 |  |  | #ifdef __clang__
 | 
					
						
							|  |  |  | #pragma clang diagnostic push
 | 
					
						
							|  |  |  | #pragma clang diagnostic ignored "-Wweak-vtables"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-03-06 10:52:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  | namespace | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  |     inline int thisThrows() | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |         if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |             throw std::domain_error( "expected exception" ); | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int thisDoesntThrow() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" ) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     CHECK_THROWS_AS( thisThrows(), std::string ); | 
					
						
							|  |  |  |     CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error ); | 
					
						
							|  |  |  |     CHECK_NOTHROW( thisThrows() ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |     if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |         throw std::domain_error( "unexpected exception" ); | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2012-11-17 17:22:37 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     CHECK( 1 == 1 ); | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |     if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |         throw std::domain_error( "unexpected exception" ); | 
					
						
							| 
									
										
										
										
											2012-11-17 17:22:37 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-01 19:07:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2013-02-19 19:45:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-07-13 09:20:37 +01:00
										 |  |  |     SECTION( "section name" ) | 
					
						
							| 
									
										
										
										
											2013-02-19 19:45:09 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |         if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |             throw std::domain_error( "unexpected exception" ); | 
					
						
							| 
									
										
										
										
											2013-02-19 19:45:09 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-11-17 17:22:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2013-04-20 21:04:32 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     CHECK( thisThrows() == 0 ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2014-04-12 19:07:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     REQUIRE( thisThrows() == 0 ); | 
					
						
							|  |  |  |     FAIL( "This should never happen" ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-24 09:53:04 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2014-04-12 19:20:46 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  |     try { | 
					
						
							|  |  |  |         CHECK(thisThrows() == 0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch(...) { | 
					
						
							|  |  |  |         FAIL( "This should never happen" ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-04-12 19:20:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" ) | 
					
						
							| 
									
										
										
										
											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 ) | 
					
						
							|  |  |  |     {} | 
					
						
							| 
									
										
										
										
											2015-11-04 18:01:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  |     std::string getMessage() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_msg; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-11-04 18:01:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | private: | 
					
						
							|  |  |  |     std::string m_msg; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 08:39:21 +00:00
										 |  |  | class CustomStdException : public std::exception | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     CustomStdException( const std::string& msg ) | 
					
						
							|  |  |  |     : m_msg( msg ) | 
					
						
							|  |  |  |     {} | 
					
						
							| 
									
										
										
										
											2017-04-25 12:41:30 +02:00
										 |  |  |     ~CustomStdException() noexcept {} | 
					
						
							| 
									
										
										
										
											2015-12-04 10:20:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 08:39:21 +00:00
										 |  |  |     std::string getMessage() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_msg; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-04 10:20:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 08:39:21 +00:00
										 |  |  | private: | 
					
						
							|  |  |  |     std::string m_msg; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  |     return ex.getMessage(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 08:39:21 +00:00
										 |  |  | CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ex.getMessage(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | CATCH_TRANSLATE_EXCEPTION( double& ex ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-02 23:51:03 +02:00
										 |  |  |     return Catch::Detail::stringify( ex ); | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |     if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |         throw CustomException( "custom exception" ); | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2015-11-18 08:39:21 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |         throw CustomException( "custom std exception" ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | inline void throwCustom() { | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |     if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |         throw CustomException( "custom exception - not std" ); | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  |     REQUIRE_NOTHROW( throwCustom() ); | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" ) | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  |     REQUIRE_THROWS_AS( throwCustom(), std::exception ); | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]"  ) | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |     if( Catch::alwaysTrue() ) | 
					
						
							|  |  |  |         throw double( 3.14 ); | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-04-21 19:43:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 12:10:14 +01:00
										 |  |  | #ifndef CATCH_CONFIG_DISABLE_MATCHERS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "Exception messages can be tested for", "[!throws]" ) { | 
					
						
							| 
									
										
										
										
											2015-07-15 23:02:25 +01:00
										 |  |  |     using namespace Catch::Matchers; | 
					
						
							| 
									
										
										
										
											2015-07-13 15:03:04 +01:00
										 |  |  |     SECTION( "exact match" ) | 
					
						
							|  |  |  |         REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); | 
					
						
							|  |  |  |     SECTION( "different case" ) | 
					
						
							| 
									
										
										
										
											2015-07-15 23:02:25 +01:00
										 |  |  |     REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) ); | 
					
						
							| 
									
										
										
										
											2015-07-13 15:03:04 +01:00
										 |  |  |     SECTION( "wildcarded" ) { | 
					
						
							| 
									
										
										
										
											2015-07-15 23:02:25 +01:00
										 |  |  |         REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) ); | 
					
						
							|  |  |  |         REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) ); | 
					
						
							|  |  |  |         REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) ); | 
					
						
							|  |  |  |         REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) ); | 
					
						
							| 
									
										
										
										
											2015-07-13 15:03:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 12:10:14 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:56:41 +00:00
										 |  |  | TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) { | 
					
						
							| 
									
										
										
										
											2015-07-13 06:34:41 +01:00
										 |  |  |     REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); | 
					
						
							|  |  |  |     REQUIRE_THROWS_WITH( thisThrows(), "should fail" ); | 
					
						
							| 
									
										
										
										
											2015-07-15 23:02:25 +01:00
										 |  |  |     REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:34:41 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-31 01:06:35 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 10:38:29 +01:00
										 |  |  | TEST_CASE( "#748 - captures with unexpected exceptions", "[.][failing][!throws][!shouldfail]" ) { | 
					
						
							| 
									
										
										
										
											2017-03-31 01:06:35 +03:00
										 |  |  |     int answer = 42; | 
					
						
							| 
									
										
										
										
											2017-04-04 11:31:13 +02:00
										 |  |  |     CAPTURE( answer ); | 
					
						
							| 
									
										
										
										
											2017-03-31 01:06:35 +03:00
										 |  |  |     // the message should be printed on the first two sections but not on the third
 | 
					
						
							|  |  |  |     SECTION( "outside assertions" ) { | 
					
						
							|  |  |  |         thisThrows(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     SECTION( "inside REQUIRE_NOTHROW" ) { | 
					
						
							| 
									
										
										
										
											2017-04-04 11:31:13 +02:00
										 |  |  |         REQUIRE_NOTHROW( thisThrows() ); | 
					
						
							| 
									
										
										
										
											2017-03-31 01:06:35 +03:00
										 |  |  |     } | 
					
						
							|  |  |  |     SECTION( "inside REQUIRE_THROWS" ) { | 
					
						
							| 
									
										
										
										
											2017-04-04 11:31:13 +02:00
										 |  |  |         REQUIRE_THROWS( thisThrows() ); | 
					
						
							| 
									
										
										
										
											2017-03-31 01:06:35 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-04 11:31:13 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-07 16:51:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef __clang__
 | 
					
						
							|  |  |  | #pragma clang diagnostic pop
 | 
					
						
							|  |  |  | #endif
 |