Exception.tests.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  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)
  7. */
  8. #include "catch.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 { namespace ExceptionTests {
  21. #ifndef EXCEPTION_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
  22. #define EXCEPTION_TEST_HELPERS_INCLUDED
  23. int thisThrows() {
  24. throw std::domain_error( "expected exception" );
  25. return 1;
  26. }
  27. int thisDoesntThrow() {
  28. return 0;
  29. }
  30. class CustomException {
  31. public:
  32. explicit CustomException( const std::string& msg )
  33. : m_msg( msg )
  34. {}
  35. std::string getMessage() const {
  36. return m_msg;
  37. }
  38. private:
  39. std::string m_msg;
  40. };
  41. class CustomStdException : public std::exception {
  42. public:
  43. explicit CustomStdException( const std::string& msg )
  44. : m_msg( msg )
  45. {}
  46. ~CustomStdException() noexcept override {}
  47. std::string getMessage() const {
  48. return m_msg;
  49. }
  50. private:
  51. std::string m_msg;
  52. };
  53. [[noreturn]] void throwCustom() {
  54. throw CustomException( "custom exception - not std" );
  55. }
  56. #endif
  57. TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" ) {
  58. REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
  59. REQUIRE_NOTHROW( thisDoesntThrow() );
  60. REQUIRE_THROWS( thisThrows() );
  61. }
  62. TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" ) {
  63. CHECK_THROWS_AS( thisThrows(), std::string );
  64. CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
  65. CHECK_NOTHROW( thisThrows() );
  66. }
  67. TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" ) {
  68. throw std::domain_error( "unexpected exception" );
  69. }
  70. TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" ) {
  71. CHECK( 1 == 1 );
  72. throw std::domain_error( "unexpected exception" );
  73. }
  74. TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" ) {
  75. SECTION( "section name" ) {
  76. throw std::domain_error("unexpected exception");
  77. }
  78. }
  79. TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" ) {
  80. CHECK( thisThrows() == 0 );
  81. }
  82. TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" ) {
  83. REQUIRE( thisThrows() == 0 );
  84. FAIL( "This should never happen" );
  85. }
  86. TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" ) {
  87. try {
  88. CHECK(thisThrows() == 0);
  89. }
  90. catch(...) {
  91. FAIL( "This should never happen" );
  92. }
  93. }
  94. TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" ) {
  95. try {
  96. throw std::domain_error( "unexpected exception" );
  97. }
  98. catch(...) {}
  99. }
  100. CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) {
  101. return ex.getMessage();
  102. }
  103. CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex ) {
  104. return ex.getMessage();
  105. }
  106. CATCH_TRANSLATE_EXCEPTION( double& ex ) {
  107. return Catch::Detail::stringify( ex );
  108. }
  109. TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) {
  110. throw CustomException( "custom exception" );
  111. }
  112. TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" ) {
  113. throw CustomException( "custom std exception" );
  114. }
  115. TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" ) {
  116. REQUIRE_NOTHROW( throwCustom() );
  117. }
  118. TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" ) {
  119. REQUIRE_THROWS_AS( throwCustom(), std::exception );
  120. }
  121. TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]" ) {
  122. throw double( 3.14 );
  123. }
  124. TEST_CASE("Thrown string literals are translated", "[.][failing][!throws]") {
  125. throw "For some reason someone is throwing a string literal!";
  126. }
  127. TEST_CASE("thrown std::strings are translated", "[.][failing][!throws]") {
  128. throw std::string{ "Why would you throw a std::string?" };
  129. }
  130. #ifndef CATCH_CONFIG_DISABLE_MATCHERS
  131. TEST_CASE( "Exception messages can be tested for", "[!throws]" ) {
  132. using namespace Catch::Matchers;
  133. SECTION( "exact match" )
  134. REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
  135. SECTION( "different case" )
  136. REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) );
  137. SECTION( "wildcarded" ) {
  138. REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) );
  139. REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) );
  140. REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) );
  141. REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) );
  142. }
  143. }
  144. #endif
  145. TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) {
  146. REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
  147. REQUIRE_THROWS_WITH( thisThrows(), "should fail" );
  148. REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
  149. }
  150. TEST_CASE( "#748 - captures with unexpected exceptions", "[.][failing][!throws][!shouldfail]" ) {
  151. int answer = 42;
  152. CAPTURE( answer );
  153. // the message should be printed on the first two sections but not on the third
  154. SECTION( "outside assertions" ) {
  155. thisThrows();
  156. }
  157. SECTION( "inside REQUIRE_NOTHROW" ) {
  158. REQUIRE_NOTHROW( thisThrows() );
  159. }
  160. SECTION( "inside REQUIRE_THROWS" ) {
  161. REQUIRE_THROWS( thisThrows() );
  162. }
  163. }
  164. }} // namespace ExceptionTests
  165. #ifdef __clang__
  166. #pragma clang diagnostic pop
  167. #endif