+// ----------------------------------------------------------------------------
+// helper classes
+// ----------------------------------------------------------------------------
+
+// exception class for MSVC debug CRT assertion failures
+#ifdef wxUSE_VC_CRTDBG
+
+struct CrtAssertFailure
+{
+ CrtAssertFailure(const char *message) : m_msg(message) { }
+
+ const wxString m_msg;
+
+ wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure);
+};
+
+#endif // wxUSE_VC_CRTDBG
+
+#if wxDEBUG_LEVEL
+
+static wxString FormatAssertMessage(const wxString& file,
+ int line,
+ const wxString& func,
+ const wxString& cond,
+ const wxString& msg)
+{
+ wxString str;
+ str << "wxWidgets assert: " << cond << " failed "
+ "at " << file << ":" << line << " in " << func
+ << " with message '" << msg << "'";
+ return str;
+}
+
+static void TestAssertHandler(const wxString& file,
+ int line,
+ const wxString& func,
+ const wxString& cond,
+ const wxString& msg)
+{
+ // Determine whether we can safely throw an exception to just make the test
+ // fail or whether we need to abort (in this case "msg" will contain the
+ // explanation why did we decide to do it).
+ wxString abortReason;
+ if ( !wxIsMainThread() )
+ {
+ // Exceptions thrown from worker threads are not caught currently and
+ // so we'd just die without any useful information -- abort instead.
+ abortReason = "in a worker thread";
+ }
+ else if ( uncaught_exception() )
+ {
+ // Throwing while already handling an exception would result in
+ // terminate() being called and we wouldn't get any useful information
+ // about why the test failed then.
+ abortReason = "while handling an exception";
+ }
+ else // Can "safely" throw from here.
+ {
+ throw TestAssertFailure(file, line, func, cond, msg);
+ }
+
+ wxFprintf(stderr, "%s %s -- aborting.",
+ FormatAssertMessage(file, line, func, cond, msg),
+ abortReason);
+ fflush(stderr);
+ _exit(-1);
+}
+
+#endif // wxDEBUG_LEVEL
+
+// this function should only be called from a catch clause
+static string GetExceptionMessage()
+{
+ wxString msg;
+
+ try
+ {
+ throw;
+ }
+#if wxDEBUG_LEVEL
+ catch ( TestAssertFailure& e )
+ {
+ msg << FormatAssertMessage(e.m_file, e.m_line, e.m_func,
+ e.m_cond, e.m_msg);
+ }
+#endif // wxDEBUG_LEVEL
+#ifdef wxUSE_VC_CRTDBG
+ catch ( CrtAssertFailure& e )
+ {
+ msg << "CRT assert failure: " << e.m_msg;
+ }
+#endif // wxUSE_VC_CRTDBG
+ catch ( std::exception& e )
+ {
+ msg << "std::exception: " << e.what();
+ }
+ catch ( ... )
+ {
+ msg = "Unknown exception caught.";
+ }
+
+ return string(msg.mb_str());
+}
+
+// Protector adding handling of wx-specific (this includes MSVC debug CRT in
+// this context) exceptions
+class wxUnitTestProtector : public CppUnit::Protector
+{
+public:
+ virtual bool protect(const CppUnit::Functor &functor,
+ const CppUnit::ProtectorContext& context)
+ {
+ try
+ {
+ return functor();
+ }
+ catch ( std::exception& )
+ {
+ // cppunit deals with the standard exceptions itself, let it do as
+ // it output more details (especially for std::exception-derived
+ // CppUnit::Exception) than we do
+ throw;
+ }
+ catch ( ... )
+ {
+ reportError(context, CppUnit::Message("Uncaught exception",
+ GetExceptionMessage()));
+ }
+
+ return false;
+ }
+};
+