]> 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 37626d780b98ec97f5aef70b853f5a5d06bfa2a3..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
 
     #include "wx/osx/private.h"
 #endif
 
+#if wxUSE_GUI
+    #include "testableframe.h"
+#endif
+
 #include "wx/socket.h"
+#include "wx/evtloop.h"
 
 using namespace std;
 
@@ -80,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,
@@ -99,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
@@ -125,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
@@ -204,7 +246,7 @@ public:
         m_watch.Pause();
         wxPrintf(GetResultStr(m_result));
         if (m_timing)
-            wxPrintf("  %6d ms", m_watch.Time());
+            wxPrintf("  %6ld ms", m_watch.Time());
         wxPrintf("\n");
     }
 
@@ -284,6 +326,9 @@ private:
     wxArrayString m_registries;
     wxLocale *m_locale;
 
+    // event loop for GUI tests
+    wxEventLoop* m_eventloop;
+
     // event handling hooks
     FilterEventFunc m_filterEventFunc;
     ProcessEventFunc m_processEventFunc;
@@ -400,6 +445,7 @@ TestApp::TestApp()
     m_processEventFunc = NULL;
 
     m_locale = NULL;
+    m_eventloop = NULL;
 }
 
 // Init
@@ -418,7 +464,11 @@ bool TestApp::OnInit()
 
 #if wxUSE_GUI
     // create a hidden parent window to be used as parent for the GUI controls
-    new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
+    wxTestableFrame* frame = new wxTestableFrame();
+    frame->Show();
+
+    m_eventloop = new wxEventLoop;
+    wxEventLoop::SetActive(m_eventloop);
 #endif // wxUSE_GUI
 
     return true;
@@ -586,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()
@@ -595,6 +647,8 @@ int TestApp::OnExit()
 
 #if wxUSE_GUI
     delete GetTopWindow();
+    wxEventLoop::SetActive(NULL);
+    delete m_eventloop;
 #endif // wxUSE_GUI
 
     return 0;