mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 21:05:39 +02:00
Add a matcher that checks exception's message
Only works for exceptions that publicly derive from `std::exception` and the matching is done exactly, including case and whitespace. Closes #1649 Closes #1728 # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Your branch is up-to-date with 'origin/master'. # # Changes to be committed: # modified: ../docs/matchers.md # modified: ../include/internal/catch_capture_matchers.h # modified: ../projects/CMakeLists.txt # modified: ../projects/SelfTest/Baselines/compact.sw.approved.txt # modified: ../projects/SelfTest/Baselines/console.std.approved.txt # modified: ../projects/SelfTest/Baselines/console.sw.approved.txt # modified: ../projects/SelfTest/Baselines/junit.sw.approved.txt # modified: ../projects/SelfTest/Baselines/xml.sw.approved.txt # modified: ../projects/SelfTest/UsageTests/Matchers.tests.cpp # # Untracked files: # ./ # ../clang-full/ # ../clang-test/ # ../clang10-build/ # ../coverage-build/ # ../gcc-build/ # ../gcc-full/ # ../include/internal/catch_matchers_exception.cpp # ../include/internal/catch_matchers_exception.hpp # ../misc-build/ # ../msvc-sln/ # ../notes.txt # ../test-install/ #
This commit is contained in:
@@ -52,10 +52,16 @@ namespace { namespace MatchersTests {
|
||||
int i;
|
||||
};
|
||||
|
||||
struct DerivedException : std::exception {
|
||||
char const* what() const noexcept override {
|
||||
return "DerivedException::what";
|
||||
}
|
||||
};
|
||||
|
||||
void doesNotThrow() {}
|
||||
|
||||
[[noreturn]]
|
||||
void throws(int i) {
|
||||
void throwsSpecialException(int i) {
|
||||
throw SpecialException{i};
|
||||
}
|
||||
|
||||
@@ -64,6 +70,11 @@ namespace { namespace MatchersTests {
|
||||
throw i;
|
||||
}
|
||||
|
||||
[[noreturn]]
|
||||
void throwsDerivedException() {
|
||||
throw DerivedException{};
|
||||
}
|
||||
|
||||
class ExceptionMatcher : public Catch::MatcherBase<SpecialException> {
|
||||
int m_expected;
|
||||
public:
|
||||
@@ -319,8 +330,8 @@ namespace { namespace MatchersTests {
|
||||
}
|
||||
|
||||
TEST_CASE("Exception matchers that succeed", "[matchers][exceptions][!throws]") {
|
||||
CHECK_THROWS_MATCHES(throws(1), SpecialException, ExceptionMatcher{1});
|
||||
REQUIRE_THROWS_MATCHES(throws(2), SpecialException, ExceptionMatcher{2});
|
||||
CHECK_THROWS_MATCHES(throwsSpecialException(1), SpecialException, ExceptionMatcher{1});
|
||||
REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, ExceptionMatcher{2});
|
||||
}
|
||||
|
||||
TEST_CASE("Exception matchers that fail", "[matchers][exceptions][!throws][.failing]") {
|
||||
@@ -333,8 +344,8 @@ namespace { namespace MatchersTests {
|
||||
REQUIRE_THROWS_MATCHES(throwsAsInt(1), SpecialException, ExceptionMatcher{1});
|
||||
}
|
||||
SECTION("Contents are wrong") {
|
||||
CHECK_THROWS_MATCHES(throws(3), SpecialException, ExceptionMatcher{1});
|
||||
REQUIRE_THROWS_MATCHES(throws(4), SpecialException, ExceptionMatcher{1});
|
||||
CHECK_THROWS_MATCHES(throwsSpecialException(3), SpecialException, ExceptionMatcher{1});
|
||||
REQUIRE_THROWS_MATCHES(throwsSpecialException(4), SpecialException, ExceptionMatcher{1});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,6 +544,13 @@ namespace { namespace MatchersTests {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Exceptions matchers", "[matchers][exceptions][!throws]") {
|
||||
REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, Message("DerivedException::what"));
|
||||
REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, !Message("derivedexception::what"));
|
||||
REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, !Message("DerivedException::what"));
|
||||
REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, Message("SpecialException::what"));
|
||||
}
|
||||
|
||||
} } // namespace MatchersTests
|
||||
|
||||
#endif // CATCH_CONFIG_DISABLE_MATCHERS
|
||||
|
Reference in New Issue
Block a user