]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/test.cpp
Moved default margins to buffer class Init; reduced chance of accidental selection...
[wxWidgets.git] / tests / test.cpp
index 1e59f598067c3ac4e6e17fc12c89adeadb4e3ad4..e7eeffcfa58a977869858635a749e132866c2023 100644 (file)
@@ -34,6 +34,7 @@
 #include <cppunit/Test.h>
 #include <cppunit/TestResult.h>
 #include <cppunit/TestFailure.h>
+#include <cppunit/TestResultCollector.h>
 
 #ifdef __VISUALC__
     #pragma warning(default:4100)
 #include "wx/afterstd.h"
 
 #include "wx/cmdline.h"
+#include <exception>
 #include <iostream>
 
-#ifdef __WXMSW__
+#ifdef __WINDOWS__
     #include "wx/msw/msvcrt.h"
 #endif
 
@@ -85,6 +87,9 @@ struct CrtAssertFailure
 
 #if wxDEBUG_LEVEL
 
+// Information about the last not yet handled assertion.
+static wxString s_lastAssertMessage;
+
 static wxString FormatAssertMessage(const wxString& file,
                                     int line,
                                     const wxString& func,
@@ -104,16 +109,48 @@ static void TestAssertHandler(const wxString& file,
                               const wxString& cond,
                               const wxString& msg)
 {
-    // can't throw from other threads, die immediately
+    // 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;
+
+    const wxString
+        assertMessage = FormatAssertMessage(file, line, func, cond, msg);
+
     if ( !wxIsMainThread() )
     {
-        wxPrintf("%s in a worker thread -- aborting.",
-                 FormatAssertMessage(file, line, func, cond, msg));
-        fflush(stdout);
-        _exit(-1);
+        // Exceptions thrown from worker threads are not caught currently and
+        // so we'd just die without any useful information -- abort instead.
+        abortReason << assertMessage << "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.
+        if ( s_lastAssertMessage.empty() )
+        {
+            abortReason << assertMessage << "while handling an exception";
+        }
+        else // In this case the exception is due to a previous assert.
+        {
+            abortReason << s_lastAssertMessage << "\n  and another "
+                        << assertMessage << " while handling it.";
+        }
+    }
+    else // Can "safely" throw from here.
+    {
+        // Remember this in case another assert happens while handling this
+        // exception: we want to show the original assert as it's usually more
+        // useful to determine the real root of the problem.
+        s_lastAssertMessage = assertMessage;
 
-    throw TestAssertFailure(file, line, func, cond, msg);
+        throw TestAssertFailure(file, line, func, cond, msg);
+    }
+
+    wxFputs(abortReason, stderr);
+    fflush(stderr);
+    _exit(-1);
 }
 
 #endif // wxDEBUG_LEVEL
@@ -130,8 +167,8 @@ static string GetExceptionMessage()
 #if wxDEBUG_LEVEL
     catch ( TestAssertFailure& e )
     {
-        msg << FormatAssertMessage(e.m_file, e.m_line, e.m_func,
-                                   e.m_cond, e.m_msg);
+        msg = s_lastAssertMessage;
+        s_lastAssertMessage.clear();
     }
 #endif // wxDEBUG_LEVEL
 #ifdef wxUSE_VC_CRTDBG
@@ -599,7 +636,9 @@ int TestApp::OnRun()
     runner.eventManager().pushProtector(new wxUnitTestProtector);
 
     bool printProgress = !(verbose || m_detail || m_timing);
-    return runner.run("", false, true, printProgress) ? EXIT_SUCCESS : EXIT_FAILURE;
+    runner.run("", false, true, printProgress);
+
+    return runner.result().testFailures() == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
 int TestApp::OnExit()