Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2018-05-12 17:46:25 +0200
committerMartin Hořeňovský2018-05-12 17:46:25 +0200
commit6c5c4c43a0056b82b2e94b5664c44074348ea8eb (patch)
tree23a494a53c07ca0feb2fba792ca9d9bb09d80c1e /projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
parentc32365848333543c92bc596f5441a63e69adb7fe (diff)
downloadcatch3-6c5c4c43a0056b82b2e94b5664c44074348ea8eb.tar.gz
Add stringification support to std::exception and deriving classes
This support is based on overriden `std::exception::what` method, so if an exception does not do so meaningfully, the message is still pointless. This is only used as a fallback, both `StringMaker` specialization and `operator<<` overload have priority..
Diffstat (limited to 'projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp')
-rw-r--r--projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
index 8ab33249..a1f8c233 100644
--- a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
+++ b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
@@ -115,3 +115,46 @@ TEST_CASE("Static arrays are convertible to string", "[toString]") {
REQUIRE(Catch::Detail::stringify(arr) == R"({ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } })");
}
}
+
+struct WhatException : std::exception {
+ char const* what() const noexcept override {
+ return "This exception has overriden what() method";
+ }
+ ~WhatException() override;
+};
+
+struct OperatorException : std::exception {
+ ~OperatorException() override;
+};
+
+std::ostream& operator<<(std::ostream& out, OperatorException const&) {
+ out << "OperatorException";
+ return out;
+}
+
+struct StringMakerException : std::exception {
+ ~StringMakerException() override;
+};
+
+namespace Catch {
+template <>
+struct StringMaker<StringMakerException> {
+ static std::string convert(StringMakerException const&) {
+ return "StringMakerException";
+ }
+};
+}
+
+// Avoid -Wweak-tables
+WhatException::~WhatException() = default;
+OperatorException::~OperatorException() = default;
+StringMakerException::~StringMakerException() = default;
+
+
+
+
+TEST_CASE("Exception as a value (e.g. in REQUIRE_THROWS_MATCHES) can be stringified", "[toString][exception]") {
+ REQUIRE(::Catch::Detail::stringify(WhatException{}) == "This exception has overriden what() method");
+ REQUIRE(::Catch::Detail::stringify(OperatorException{}) == "OperatorException");
+ REQUIRE(::Catch::Detail::stringify(StringMakerException{}) == "StringMakerException");
+}