Exception.tests.cpp 6.3 KB

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