| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-15 08:20:06 +00:00
										 |  |  | #include "catch_self_test.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | 		if( Catch::isTrue( true ) ) | 
					
						
							|  |  |  | 		    throw std::domain_error( "expected exception" ); | 
					
						
							|  |  |  | 		return 1; | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int thisDoesntThrow() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     CHECK_THROWS_AS( thisThrows(), std::string ); | 
					
						
							|  |  |  |     CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error ); | 
					
						
							|  |  |  |     CHECK_NOTHROW( thisThrows() ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | 	if( Catch::isTrue( true ) ) | 
					
						
							|  |  |  | 	    throw std::domain_error( "unexpected exception" ); | 
					
						
							| 
									
										
										
										
											2010-11-09 23:24:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											2012-11-17 17:22:37 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     CHECK( 1 == 1 ); | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | 	if( Catch::isTrue( true ) ) | 
					
						
							|  |  |  | 	    throw std::domain_error( "unexpected exception" ); | 
					
						
							| 
									
										
										
										
											2012-11-17 17:22:37 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											2013-02-19 19:45:09 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     SECTION( "section name", "" ) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | 		if( Catch::isTrue( true ) ) | 
					
						
							|  |  |  | 			throw std::domain_error( "unexpected exception" ); | 
					
						
							| 
									
										
										
										
											2013-02-19 19:45:09 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-11-17 17:22:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											2013-04-20 21:04:32 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     CHECK( thisThrows() == 0 ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "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; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | CATCH_TRANSLATE_EXCEPTION( double& ex ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Catch::toString( ex ); | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE("Unexpected custom exceptions can be translated", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | 	if( Catch::isTrue( true ) ) | 
					
						
							|  |  |  | 	    throw CustomException( "custom exception" ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | inline void throwCustom() { | 
					
						
							|  |  |  | 	if( Catch::isTrue( true ) ) | 
					
						
							|  |  |  | 		throw CustomException( "custom exception - not std" ); | 
					
						
							| 
									
										
										
										
											2011-04-20 15:40:40 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing]" ) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "Unexpected exceptions can be translated", "[.][failing]"  ) | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:52:49 +01:00
										 |  |  | 	if( Catch::isTrue( true ) ) | 
					
						
							|  |  |  | 	    throw double( 3.14 ); | 
					
						
							| 
									
										
										
										
											2011-04-20 19:09:41 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-04-21 19:43:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 18:37:58 +01:00
										 |  |  | inline int thisFunctionNotImplemented( int ) { | 
					
						
							|  |  |  |     CATCH_NOT_IMPLEMENTED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 07:21:03 +00:00
										 |  |  | TEST_CASE( "NotImplemented exception", "" ) | 
					
						
							| 
									
										
										
										
											2012-07-05 18:37:58 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) ); | 
					
						
							|  |  |  | } |