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..
diff --git a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
index 8ab3324..a1f8c23 100644
--- a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
+++ b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
@@ -115,3 +115,46 @@
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");
+}