Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 1 | /* |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 2 | * Created by Phil on 09/11/2010. |
| 3 | * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. |
| 4 | * |
| 5 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Phil Nash | cda2149 | 2012-08-16 18:47:41 +0100 | [diff] [blame] | 9 | #ifdef __clang__ |
Phil Nash | a695eb9 | 2012-08-13 07:46:10 +0100 | [diff] [blame] | 10 | #pragma clang diagnostic ignored "-Wpadded" |
Phil Nash | cda2149 | 2012-08-16 18:47:41 +0100 | [diff] [blame] | 11 | #endif |
Phil Nash | a695eb9 | 2012-08-13 07:46:10 +0100 | [diff] [blame] | 12 | |
Phil Nash | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 13 | #include "catch.hpp" |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 14 | |
| 15 | #include <string> |
Phil Nash | 95627c4 | 2011-01-07 10:22:24 +0000 | [diff] [blame] | 16 | #include <stdexcept> |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 17 | |
Phil Nash | 7b449f7 | 2012-02-15 08:20:06 +0000 | [diff] [blame] | 18 | #include "catch_self_test.hpp" |
| 19 | |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 20 | namespace |
| 21 | { |
Phil Nash | a695eb9 | 2012-08-13 07:46:10 +0100 | [diff] [blame] | 22 | CATCH_ATTRIBUTE_NORETURN |
Phil Nash | d1ee964 | 2011-01-31 20:15:40 +0000 | [diff] [blame] | 23 | int thisThrows(); |
| 24 | |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 25 | int thisThrows() |
| 26 | { |
| 27 | throw std::domain_error( "expected exception" ); |
Phil Nash | d1ee964 | 2011-01-31 20:15:40 +0000 | [diff] [blame] | 28 | /*NOTREACHED*/ |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | int thisDoesntThrow() |
| 32 | { |
| 33 | return 0; |
| 34 | } |
| 35 | } |
| 36 | |
Phil Nash | 684baf1 | 2011-01-14 08:47:43 +0000 | [diff] [blame] | 37 | TEST_CASE( "./succeeding/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 38 | { |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 39 | REQUIRE_THROWS_AS( thisThrows(), std::domain_error ); |
| 40 | REQUIRE_NOTHROW( thisDoesntThrow() ); |
| 41 | REQUIRE_THROWS( thisThrows() ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Phil Nash | a695eb9 | 2012-08-13 07:46:10 +0100 | [diff] [blame] | 44 | CATCH_ATTRIBUTE_NORETURN |
Phil Nash | 684baf1 | 2011-01-14 08:47:43 +0000 | [diff] [blame] | 45 | TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 46 | { |
| 47 | CHECK_THROWS_AS( thisThrows(), std::string ); |
| 48 | CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error ); |
| 49 | CHECK_NOTHROW( thisThrows() ); |
| 50 | } |
| 51 | |
Phil Nash | 4890a1d | 2011-03-14 19:21:35 +0000 | [diff] [blame] | 52 | TEST_CASE_NORETURN( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 53 | { |
| 54 | throw std::domain_error( "unexpected exception" ); |
Phil Nash | d1ee964 | 2011-01-31 20:15:40 +0000 | [diff] [blame] | 55 | /*NOTREACHED*/ |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Phil Nash | d0cc33f | 2012-11-17 17:22:37 +0000 | [diff] [blame] | 58 | TEST_CASE_NORETURN( "./failing/exceptions/implicit/2", "An unchecked exception reports the line of the last assertion" ) |
| 59 | { |
| 60 | CHECK( 1 == 1 ); |
| 61 | throw std::domain_error( "unexpected exception" ); |
| 62 | /*NOTREACHED*/ |
| 63 | } |
Phil Nash | 10ed1e0 | 2013-02-19 19:45:09 +0000 | [diff] [blame^] | 64 | TEST_CASE( "./failing/exceptions/implicit/3", "When unchecked exceptions are thrown they are always failures" ) |
| 65 | { |
| 66 | SECTION( "section name", "" ) |
| 67 | { |
| 68 | throw std::domain_error( "unexpected exception" ); |
| 69 | /*NOTREACHED*/ |
| 70 | } |
| 71 | } |
Phil Nash | d0cc33f | 2012-11-17 17:22:37 +0000 | [diff] [blame] | 72 | |
Phil Nash | 684baf1 | 2011-01-14 08:47:43 +0000 | [diff] [blame] | 73 | TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 74 | { |
| 75 | try |
| 76 | { |
| 77 | throw std::domain_error( "unexpected exception" ); |
| 78 | } |
| 79 | catch(...) |
| 80 | { |
| 81 | } |
| 82 | } |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 83 | |
| 84 | class CustomException |
| 85 | { |
| 86 | public: |
| 87 | CustomException( const std::string& msg ) |
| 88 | : m_msg( msg ) |
| 89 | {} |
| 90 | |
| 91 | std::string getMessage() const |
| 92 | { |
| 93 | return m_msg; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | std::string m_msg; |
| 98 | }; |
| 99 | |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 100 | CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 101 | { |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 102 | return ex.getMessage(); |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 105 | CATCH_TRANSLATE_EXCEPTION( double& ex ) |
| 106 | { |
| 107 | return Catch::toString( ex ); |
| 108 | } |
| 109 | |
| 110 | TEST_CASE_NORETURN( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" ) |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 111 | { |
| 112 | throw CustomException( "custom exception" ); |
| 113 | } |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 114 | |
| 115 | TEST_CASE( "./failing/exceptions/custom/nothrow", "Custom exceptions can be translated when testing for nothrow" ) |
| 116 | { |
| 117 | REQUIRE_NOTHROW( throw CustomException( "unexpected custom exception" ) ); |
| 118 | } |
| 119 | |
| 120 | TEST_CASE( "./failing/exceptions/custom/throw", "Custom exceptions can be translated when testing for throwing as something else" ) |
| 121 | { |
| 122 | REQUIRE_THROWS_AS( throw CustomException( "custom exception - not std" ), std::exception ); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | TEST_CASE_NORETURN( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" ) |
| 127 | { |
| 128 | throw double( 3.14 ); |
| 129 | } |
Phil Nash | 02e597c | 2011-04-21 19:43:55 +0100 | [diff] [blame] | 130 | |
Phil Nash | abf2716 | 2012-07-05 18:37:58 +0100 | [diff] [blame] | 131 | inline int thisFunctionNotImplemented( int ) { |
| 132 | CATCH_NOT_IMPLEMENTED; |
| 133 | } |
| 134 | |
| 135 | TEST_CASE( "./succeeding/exceptions/notimplemented", "" ) |
| 136 | { |
| 137 | REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) ); |
| 138 | } |