diff options
| author | Martin Hořeňovský | 2019-10-13 20:37:07 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2019-10-13 20:37:07 +0200 |
| commit | 2cc0c718562d41a9900daca3ba53b6753515ee49 (patch) | |
| tree | 399afaed97c2611b11371c8ac2d20b1942c654b5 /projects/SelfTest/UsageTests | |
| parent | 28663fb9594ae60109255625d8e6020a36331105 (diff) | |
| download | catch3-2cc0c718562d41a9900daca3ba53b6753515ee49.tar.gz | |
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/
#
Diffstat (limited to 'projects/SelfTest/UsageTests')
| -rw-r--r-- | projects/SelfTest/UsageTests/Matchers.tests.cpp | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/projects/SelfTest/UsageTests/Matchers.tests.cpp b/projects/SelfTest/UsageTests/Matchers.tests.cpp index 9917bd78..6f954ca0 100644 --- a/projects/SelfTest/UsageTests/Matchers.tests.cpp +++ b/projects/SelfTest/UsageTests/Matchers.tests.cpp @@ -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 |