+
+// ----------------------------------------------------------------------------
+// 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)
+{
+ // can't throw from other threads, die immediately
+ if ( !wxIsMainThread() )
+ {
+ wxPrintf("%s in a worker thread -- aborting.",
+ FormatAssertMessage(file, line, func, cond, msg));
+ fflush(stdout);
+ _exit(-1);
+ }
+
+ throw TestAssertFailure(file, line, func, cond, msg);
+}
+
+#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;
+ }
+};
+
+// Displays the test name before starting to execute it: this helps with
+// diagnosing where exactly does a test crash or hang when/if it does.
+class DetailListener : public CppUnit::TestListener
+{
+public:
+ DetailListener(bool doTiming = false):
+ CppUnit::TestListener(),
+ m_timing(doTiming)
+ {
+ }
+
+ virtual void startTest(CppUnit::Test *test)
+ {
+ wxPrintf(" %-60s ", test->getName());
+ m_result = RESULT_OK;
+ m_watch.Start();
+ }
+
+ virtual void addFailure(const CppUnit::TestFailure& failure)
+ {
+ m_result = failure.isError() ? RESULT_ERROR : RESULT_FAIL;
+ }
+
+ virtual void endTest(CppUnit::Test * WXUNUSED(test))
+ {
+ m_watch.Pause();
+ wxPrintf(GetResultStr(m_result));
+ if (m_timing)
+ wxPrintf(" %6d ms", m_watch.Time());
+ wxPrintf("\n");
+ }
+
+protected :
+ enum ResultType
+ {
+ RESULT_OK = 0,
+ RESULT_FAIL,
+ RESULT_ERROR,
+ RESULT_MAX
+ };
+
+ wxString GetResultStr(ResultType type) const
+ {
+ static const char *resultTypeNames[] =
+ {
+ " OK",
+ "FAIL",
+ " ERR"
+ };
+
+ wxCOMPILE_TIME_ASSERT( WXSIZEOF(resultTypeNames) == RESULT_MAX,
+ ResultTypeNamesMismatch );
+
+ return resultTypeNames[type];
+ }
+
+ bool m_timing;
+ wxStopWatch m_watch;
+ ResultType m_result;
+};
+
+#if wxUSE_GUI
+ typedef wxApp TestAppBase;
+#else
+ typedef wxAppConsole TestAppBase;
+#endif