From 6e678e3a85b1808490dcacdb1eae0f7ee7b2f9f8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 6 Dec 2017 03:23:26 +0100 Subject: [PATCH] Improve output of test failures for unprintable characters Show non-printable characters when comparing strings more clearly if the comparison fails, this is notably useful for strings containing NULs as they were previously completely lost. --- include/wx/catch_cppunit.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/wx/catch_cppunit.h b/include/wx/catch_cppunit.h index 22ebcf12f9..66e660b158 100644 --- a/include/wx/catch_cppunit.h +++ b/include/wx/catch_cppunit.h @@ -73,6 +73,32 @@ namespace Catch return wxString(ucr).ToStdString(wxConvUTF8); } }; + + // While this conversion already works due to the existence of the stream + // insertion operator for wxString, define a custom one making it more + // obvious when strings containing non-printable characters differ. + template <> + struct StringMaker + { + static std::string convert(const wxString& wxs) + { + std::string s; + s.reserve(wxs.length()); + for ( wxString::const_iterator i = wxs.begin(); + i != wxs.end(); + ++i ) + { +#if wxUSE_UNICODE + if ( !iswprint(*i) ) + s += wxString::Format("\\u%04X", *i).ToStdString(); + else +#endif // wxUSE_UNICODE + s += *i; + } + + return s; + } + }; } // Use a different namespace for our mock ups of the real declarations in