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 | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 9 | #include "catch.hpp" |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 10 | |
| 11 | #include <string> |
Phil Nash | 95627c4 | 2011-01-07 10:22:24 +0000 | [diff] [blame] | 12 | #include <stdexcept> |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 13 | |
| 14 | namespace |
| 15 | { |
Phil Nash | bf37e68 | 2013-04-23 20:52:49 +0100 | [diff] [blame] | 16 | inline int thisThrows() |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 17 | { |
Martin Hořeňovský | c390c4c | 2017-01-26 23:13:12 +0100 | [diff] [blame^] | 18 | if( Catch::alwaysTrue() ) |
| 19 | throw std::domain_error( "expected exception" ); |
| 20 | return 1; |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | int thisDoesntThrow() |
| 24 | { |
| 25 | return 0; |
| 26 | } |
| 27 | } |
| 28 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 29 | TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 30 | { |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 31 | REQUIRE_THROWS_AS( thisThrows(), std::domain_error ); |
| 32 | REQUIRE_NOTHROW( thisDoesntThrow() ); |
| 33 | REQUIRE_THROWS( thisThrows() ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 36 | TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 37 | { |
| 38 | CHECK_THROWS_AS( thisThrows(), std::string ); |
| 39 | CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error ); |
| 40 | CHECK_NOTHROW( thisThrows() ); |
| 41 | } |
| 42 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 43 | TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 44 | { |
Martin Hořeňovský | c390c4c | 2017-01-26 23:13:12 +0100 | [diff] [blame^] | 45 | if( Catch::alwaysTrue() ) |
| 46 | throw std::domain_error( "unexpected exception" ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 49 | TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" ) |
Phil Nash | d0cc33f | 2012-11-17 17:22:37 +0000 | [diff] [blame] | 50 | { |
| 51 | CHECK( 1 == 1 ); |
Martin Hořeňovský | c390c4c | 2017-01-26 23:13:12 +0100 | [diff] [blame^] | 52 | if( Catch::alwaysTrue() ) |
| 53 | throw std::domain_error( "unexpected exception" ); |
Phil Nash | d0cc33f | 2012-11-17 17:22:37 +0000 | [diff] [blame] | 54 | } |
Phil Nash | 5a2df71 | 2014-05-01 19:07:02 +0100 | [diff] [blame] | 55 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 56 | TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" ) |
Phil Nash | 10ed1e0 | 2013-02-19 19:45:09 +0000 | [diff] [blame] | 57 | { |
| 58 | SECTION( "section name", "" ) |
| 59 | { |
Martin Hořeňovský | c390c4c | 2017-01-26 23:13:12 +0100 | [diff] [blame^] | 60 | if( Catch::alwaysTrue() ) |
| 61 | throw std::domain_error( "unexpected exception" ); |
Phil Nash | 10ed1e0 | 2013-02-19 19:45:09 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
Phil Nash | d0cc33f | 2012-11-17 17:22:37 +0000 | [diff] [blame] | 64 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 65 | TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" ) |
Phil Nash | 2666c96 | 2013-04-20 21:04:32 +0100 | [diff] [blame] | 66 | { |
| 67 | CHECK( thisThrows() == 0 ); |
Phil Nash | 2666c96 | 2013-04-20 21:04:32 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 70 | TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" ) |
Phil Nash | a176b93 | 2014-04-12 19:07:24 +0100 | [diff] [blame] | 71 | { |
| 72 | REQUIRE( thisThrows() == 0 ); |
| 73 | FAIL( "This should never happen" ); |
| 74 | } |
| 75 | |
Phil Nash | 6c23a65 | 2017-01-24 09:53:04 +0000 | [diff] [blame] | 76 | TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" ) |
Phil Nash | 1bccc03 | 2014-04-12 19:20:46 +0100 | [diff] [blame] | 77 | { |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 78 | try { |
| 79 | CHECK(thisThrows() == 0); |
| 80 | } |
| 81 | catch(...) { |
| 82 | FAIL( "This should never happen" ); |
| 83 | } |
Phil Nash | 1bccc03 | 2014-04-12 19:20:46 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 86 | TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 87 | { |
| 88 | try |
| 89 | { |
| 90 | throw std::domain_error( "unexpected exception" ); |
| 91 | } |
| 92 | catch(...) |
| 93 | { |
| 94 | } |
| 95 | } |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 96 | |
| 97 | class CustomException |
| 98 | { |
| 99 | public: |
| 100 | CustomException( const std::string& msg ) |
| 101 | : m_msg( msg ) |
| 102 | {} |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 103 | |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 104 | std::string getMessage() const |
| 105 | { |
| 106 | return m_msg; |
| 107 | } |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 108 | |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 109 | private: |
| 110 | std::string m_msg; |
| 111 | }; |
| 112 | |
Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame] | 113 | class CustomStdException : public std::exception |
| 114 | { |
| 115 | public: |
| 116 | CustomStdException( const std::string& msg ) |
| 117 | : m_msg( msg ) |
| 118 | {} |
Phil Nash | d274fc5 | 2015-11-20 17:09:48 +0000 | [diff] [blame] | 119 | ~CustomStdException() CATCH_NOEXCEPT {} |
Phil Nash | 722315a | 2015-12-04 10:20:33 +0000 | [diff] [blame] | 120 | |
Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame] | 121 | std::string getMessage() const |
| 122 | { |
| 123 | return m_msg; |
| 124 | } |
Phil Nash | 722315a | 2015-12-04 10:20:33 +0000 | [diff] [blame] | 125 | |
Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame] | 126 | private: |
| 127 | std::string m_msg; |
| 128 | }; |
| 129 | |
| 130 | |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 131 | CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 132 | { |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 133 | return ex.getMessage(); |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame] | 136 | CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex ) |
| 137 | { |
| 138 | return ex.getMessage(); |
| 139 | } |
| 140 | |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 141 | CATCH_TRANSLATE_EXCEPTION( double& ex ) |
| 142 | { |
| 143 | return Catch::toString( ex ); |
| 144 | } |
| 145 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 146 | TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 147 | { |
Martin Hořeňovský | c390c4c | 2017-01-26 23:13:12 +0100 | [diff] [blame^] | 148 | if( Catch::alwaysTrue() ) |
| 149 | throw CustomException( "custom exception" ); |
Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 150 | } |
Phil Nash | bf37e68 | 2013-04-23 20:52:49 +0100 | [diff] [blame] | 151 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 152 | TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" ) |
Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame] | 153 | { |
| 154 | if( Catch::alwaysTrue() ) |
| 155 | throw CustomException( "custom std exception" ); |
| 156 | } |
| 157 | |
Phil Nash | bf37e68 | 2013-04-23 20:52:49 +0100 | [diff] [blame] | 158 | inline void throwCustom() { |
Martin Hořeňovský | c390c4c | 2017-01-26 23:13:12 +0100 | [diff] [blame^] | 159 | if( Catch::alwaysTrue() ) |
| 160 | throw CustomException( "custom exception - not std" ); |
Phil Nash | bf37e68 | 2013-04-23 20:52:49 +0100 | [diff] [blame] | 161 | } |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 162 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 163 | TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" ) |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 164 | { |
Phil Nash | bf37e68 | 2013-04-23 20:52:49 +0100 | [diff] [blame] | 165 | REQUIRE_NOTHROW( throwCustom() ); |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 168 | TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" ) |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 169 | { |
Phil Nash | bf37e68 | 2013-04-23 20:52:49 +0100 | [diff] [blame] | 170 | REQUIRE_THROWS_AS( throwCustom(), std::exception ); |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 174 | TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]" ) |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 175 | { |
Martin Hořeňovský | c390c4c | 2017-01-26 23:13:12 +0100 | [diff] [blame^] | 176 | if( Catch::alwaysTrue() ) |
| 177 | throw double( 3.14 ); |
Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 178 | } |
Phil Nash | 02e597c | 2011-04-21 19:43:55 +0100 | [diff] [blame] | 179 | |
Phil Nash | abf2716 | 2012-07-05 18:37:58 +0100 | [diff] [blame] | 180 | inline int thisFunctionNotImplemented( int ) { |
| 181 | CATCH_NOT_IMPLEMENTED; |
| 182 | } |
| 183 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 184 | TEST_CASE( "NotImplemented exception", "[!throws]" ) |
Phil Nash | abf2716 | 2012-07-05 18:37:58 +0100 | [diff] [blame] | 185 | { |
| 186 | REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) ); |
| 187 | } |
Phil Nash | 85de743 | 2015-07-13 06:34:41 +0100 | [diff] [blame] | 188 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 189 | TEST_CASE( "Exception messages can be tested for", "[!throws]" ) { |
Phil Nash | 7286892 | 2015-07-15 23:02:25 +0100 | [diff] [blame] | 190 | using namespace Catch::Matchers; |
Phil Nash | 2104ca2 | 2015-07-13 15:03:04 +0100 | [diff] [blame] | 191 | SECTION( "exact match" ) |
| 192 | REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); |
| 193 | SECTION( "different case" ) |
Phil Nash | 7286892 | 2015-07-15 23:02:25 +0100 | [diff] [blame] | 194 | REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) ); |
Phil Nash | 2104ca2 | 2015-07-13 15:03:04 +0100 | [diff] [blame] | 195 | SECTION( "wildcarded" ) { |
Phil Nash | 7286892 | 2015-07-15 23:02:25 +0100 | [diff] [blame] | 196 | REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) ); |
| 197 | REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) ); |
| 198 | REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) ); |
| 199 | REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) ); |
Phil Nash | 2104ca2 | 2015-07-13 15:03:04 +0100 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Phil Nash | 7bcb424 | 2017-01-23 17:56:41 +0000 | [diff] [blame] | 203 | TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) { |
Phil Nash | 2104ca2 | 2015-07-13 15:03:04 +0100 | [diff] [blame] | 204 | REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); |
Phil Nash | 85de743 | 2015-07-13 06:34:41 +0100 | [diff] [blame] | 205 | REQUIRE_THROWS_WITH( thisThrows(), "should fail" ); |
Phil Nash | 7286892 | 2015-07-15 23:02:25 +0100 | [diff] [blame] | 206 | REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); |
Phil Nash | 85de743 | 2015-07-13 06:34:41 +0100 | [diff] [blame] | 207 | } |