| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <catch2/catch_translate_exception.hpp> |
| 11 | #include <catch2/matchers/catch_matchers_string.hpp> |
| 12 | |
| 13 | #include <string> |
| 14 | #include <stdexcept> |
| 15 | |
| 16 | #ifdef _MSC_VER |
| 17 | #pragma warning(disable:4702) // Unreachable code -- unconditional throws and so on |
| 18 | #endif |
| 19 | #ifdef __clang__ |
| 20 | #pragma clang diagnostic push |
| 21 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 22 | #pragma clang diagnostic ignored "-Wmissing-noreturn" |
| 23 | #pragma clang diagnostic ignored "-Wunreachable-code-return" |
| 24 | #endif |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | int thisThrows() { |
| 29 | throw std::domain_error("expected exception"); |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | int thisDoesntThrow() { |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | class CustomException { |
| 38 | public: |
| 39 | explicit CustomException(const std::string& msg) |
| 40 | : m_msg(msg) {} |
| 41 | |
| 42 | std::string const& getMessage() const { |
| 43 | return m_msg; |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | std::string m_msg; |
| 48 | }; |
| 49 | |
| 50 | class CustomStdException : public std::exception { |
| 51 | public: |
| 52 | explicit CustomStdException(const std::string& msg) |
| 53 | : m_msg(msg) {} |
| 54 | ~CustomStdException() noexcept override = default; |
| 55 | |
| 56 | CustomStdException( CustomStdException const& ) = default; |
| 57 | CustomStdException& operator=( CustomStdException const& ) = default; |
| 58 | |
| 59 | std::string const& getMessage() const { |
| 60 | return m_msg; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | std::string m_msg; |
| 65 | }; |
| 66 | |
| 67 | [[noreturn]] void throwCustom() { |
| 68 | throw CustomException("custom exception - not std"); |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" ) { |
| 74 | REQUIRE_THROWS_AS( thisThrows(), std::domain_error ); |
| 75 | REQUIRE_NOTHROW( thisDoesntThrow() ); |
| 76 | REQUIRE_THROWS( thisThrows() ); |
| 77 | } |
| 78 | |
| 79 | TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" ) { |
| 80 | CHECK_THROWS_AS( thisThrows(), std::string ); |
| 81 | CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error ); |
| 82 | CHECK_NOTHROW( thisThrows() ); |
| 83 | } |
| 84 | |
| 85 | TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" ) { |
| 86 | throw std::domain_error( "unexpected exception" ); |
| 87 | } |
| 88 | |
| 89 | TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" ) { |
| 90 | CHECK( 1 == 1 ); |
| 91 | throw std::domain_error( "unexpected exception" ); |
| 92 | } |
| 93 | |
| 94 | TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" ) { |
| 95 | SECTION( "section name" ) { |
| 96 | throw std::domain_error("unexpected exception"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" ) { |
| 101 | CHECK( thisThrows() == 0 ); |
| 102 | } |
| 103 | |
| 104 | TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" ) { |
| 105 | REQUIRE( thisThrows() == 0 ); |
| 106 | FAIL( "This should never happen" ); |
| 107 | } |
| 108 | |
| 109 | TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" ) { |
| 110 | try { |
| 111 | CHECK(thisThrows() == 0); |
| 112 | } |
| 113 | catch(...) { |
| 114 | FAIL( "This should never happen" ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" ) { |
| 119 | try { |
| 120 | throw std::domain_error( "unexpected exception" ); |
| 121 | } |
| 122 | catch(...) {} |
| 123 | } |
| 124 | |
| 125 | |
| 126 | CATCH_TRANSLATE_EXCEPTION( CustomException const& ex ) { |
| 127 | return ex.getMessage(); |
| 128 | } |
| 129 | |
| 130 | CATCH_TRANSLATE_EXCEPTION( CustomStdException const& ex ) { |
| 131 | return ex.getMessage(); |
| 132 | } |
| 133 | |
| 134 | CATCH_TRANSLATE_EXCEPTION( double const& ex ) { |
| 135 | return Catch::Detail::stringify( ex ); |
| 136 | } |
| 137 | |
| 138 | TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) { |
| 139 | throw CustomException( "custom exception" ); |
| 140 | } |
| 141 | |
| 142 | TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" ) { |
| 143 | throw CustomStdException( "custom std exception" ); |
| 144 | } |
| 145 | |
| 146 | TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" ) { |
| 147 | REQUIRE_NOTHROW( throwCustom() ); |
| 148 | } |
| 149 | |
| 150 | TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" ) { |
| 151 | REQUIRE_THROWS_AS( throwCustom(), std::exception ); |
| 152 | } |
| 153 | |
| 154 | TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]" ) { |
| 155 | throw double( 3.14 ); |
| 156 | } |
| 157 | |
| 158 | TEST_CASE("Thrown string literals are translated", "[.][failing][!throws]") { |
| 159 | throw "For some reason someone is throwing a string literal!"; |
| 160 | } |
| 161 | |
| 162 | TEST_CASE("thrown std::strings are translated", "[.][failing][!throws]") { |
| 163 | throw std::string{ "Why would you throw a std::string?" }; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | TEST_CASE( "Exception messages can be tested for", "[!throws]" ) { |
| 168 | using namespace Catch::Matchers; |
| 169 | SECTION( "exact match" ) |
| 170 | REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); |
| 171 | SECTION( "different case" ) |
| 172 | REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) ); |
| 173 | SECTION( "wildcarded" ) { |
| 174 | REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) ); |
| 175 | REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) ); |
| 176 | REQUIRE_THROWS_WITH( thisThrows(), ContainsSubstring( "except" ) ); |
| 177 | REQUIRE_THROWS_WITH( thisThrows(), ContainsSubstring( "exCept", Catch::CaseSensitive::No ) ); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) { |
| 182 | REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); |
| 183 | REQUIRE_THROWS_WITH( thisThrows(), "should fail" ); |
| 184 | REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ); |
| 185 | } |
| 186 | |
| 187 | TEST_CASE( "#748 - captures with unexpected exceptions", "[.][failing][!throws][!shouldfail]" ) { |
| 188 | int answer = 42; |
| 189 | CAPTURE( answer ); |
| 190 | // the message should be printed on the first two sections but not on the third |
| 191 | SECTION( "outside assertions" ) { |
| 192 | thisThrows(); |
| 193 | } |
| 194 | SECTION( "inside REQUIRE_NOTHROW" ) { |
| 195 | REQUIRE_NOTHROW( thisThrows() ); |
| 196 | } |
| 197 | SECTION( "inside REQUIRE_THROWS" ) { |
| 198 | REQUIRE_THROWS( thisThrows() ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #ifdef __clang__ |
| 203 | #pragma clang diagnostic pop |
| 204 | #endif |