Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ExceptionTests.cpp |
| 3 | * Catch - Test |
| 4 | * |
| 5 | * Created by Phil on 09/11/2010. |
| 6 | * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. |
| 7 | * |
| 8 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 9 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 10 | * |
| 11 | */ |
| 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 | d1ee964 | 2011-01-31 20:15:40 +0000 | [diff] [blame] | 22 | ATTRIBUTE_NORETURN |
| 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 | 684baf1 | 2011-01-14 08:47:43 +0000 | [diff] [blame] | 44 | 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] | 45 | { |
| 46 | CHECK_THROWS_AS( thisThrows(), std::string ); |
| 47 | CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error ); |
| 48 | CHECK_NOTHROW( thisThrows() ); |
| 49 | } |
| 50 | |
Phil Nash | 4890a1d | 2011-03-14 19:21:35 +0000 | [diff] [blame] | 51 | 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] | 52 | { |
| 53 | throw std::domain_error( "unexpected exception" ); |
Phil Nash | d1ee964 | 2011-01-31 20:15:40 +0000 | [diff] [blame] | 54 | /*NOTREACHED*/ |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Phil Nash | 684baf1 | 2011-01-14 08:47:43 +0000 | [diff] [blame] | 57 | 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] | 58 | { |
| 59 | try |
| 60 | { |
| 61 | throw std::domain_error( "unexpected exception" ); |
| 62 | } |
| 63 | catch(...) |
| 64 | { |
| 65 | } |
| 66 | } |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 67 | |
| 68 | class CustomException |
| 69 | { |
| 70 | public: |
| 71 | CustomException( const std::string& msg ) |
| 72 | : m_msg( msg ) |
| 73 | {} |
| 74 | |
| 75 | std::string getMessage() const |
| 76 | { |
| 77 | return m_msg; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | std::string m_msg; |
| 82 | }; |
| 83 | |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 84 | CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 85 | { |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 86 | return ex.getMessage(); |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 89 | CATCH_TRANSLATE_EXCEPTION( double& ex ) |
| 90 | { |
| 91 | return Catch::toString( ex ); |
| 92 | } |
| 93 | |
| 94 | TEST_CASE_NORETURN( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" ) |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 95 | { |
| 96 | throw CustomException( "custom exception" ); |
| 97 | } |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 98 | |
| 99 | TEST_CASE( "./failing/exceptions/custom/nothrow", "Custom exceptions can be translated when testing for nothrow" ) |
| 100 | { |
| 101 | REQUIRE_NOTHROW( throw CustomException( "unexpected custom exception" ) ); |
| 102 | } |
| 103 | |
| 104 | TEST_CASE( "./failing/exceptions/custom/throw", "Custom exceptions can be translated when testing for throwing as something else" ) |
| 105 | { |
| 106 | REQUIRE_THROWS_AS( throw CustomException( "custom exception - not std" ), std::exception ); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | TEST_CASE_NORETURN( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" ) |
| 111 | { |
| 112 | throw double( 3.14 ); |
| 113 | } |
Phil Nash | 02e597c | 2011-04-21 19:43:55 +0100 | [diff] [blame] | 114 | |
| 115 | TEST_CASE( "./failing/exceptions/in-section", "Exceptions thrown from sections report file/ line or section" ) |
| 116 | { |
| 117 | SECTION( "the section", "" ) |
| 118 | { |
Phil Nash | 7b449f7 | 2012-02-15 08:20:06 +0000 | [diff] [blame^] | 119 | CATCH_REGISTER_LINE_INFO( "the section2" ) SECTION( "the section2", "" ) |
Phil Nash | 02e597c | 2011-04-21 19:43:55 +0100 | [diff] [blame] | 120 | { |
| 121 | throw std::domain_error( "Exception from section" ); |
| 122 | } |
| 123 | } |
| 124 | } |
Phil Nash | 7b449f7 | 2012-02-15 08:20:06 +0000 | [diff] [blame^] | 125 | |
| 126 | TEST_CASE( "./succeeding/exceptions/error messages", "The error messages produced by exceptions caught by Catch matched the expected form" ) |
| 127 | { |
| 128 | Catch::EmbeddedRunner runner; |
| 129 | |
| 130 | SECTION( "custom, unexpected", "" ) |
| 131 | { |
| 132 | runner.runMatching( "./failing/exceptions/custom" ); |
| 133 | INFO( runner.getOutput() ); |
| 134 | CHECK( runner.getOutput().find( "Unexpected exception" ) != std::string::npos ); |
| 135 | CHECK( runner.getOutput().find( "custom exception" ) != std::string::npos ); |
| 136 | } |
| 137 | |
| 138 | SECTION( "in section", "" ) |
| 139 | { |
| 140 | runner.runMatching( "./failing/exceptions/in-section" ); |
| 141 | INFO( runner.getOutput() ); |
| 142 | CHECK( runner.getOutput().find( "Unexpected exception" ) != std::string::npos ); |
| 143 | CHECK( runner.getOutput().find( "Exception from section" ) != std::string::npos ); |
| 144 | CHECK( runner.getOutput().find( CATCH_GET_LINE_INFO( "the section2" ) ) != std::string::npos ); |
| 145 | } |
| 146 | |
| 147 | } |