2004-03-03 22:56:16 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: test.cpp
|
|
|
|
// Purpose: Test program for wxWidgets
|
|
|
|
// Author: Mike Wetherell
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2004 Mike Wetherell
|
|
|
|
// Licence: wxWidgets licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2004-11-22 05:00:19 +00:00
|
|
|
// For compilers that support precompilation, includes "wx/wx.h"
|
|
|
|
// and "wx/cppunit.h"
|
|
|
|
#include "testprec.h"
|
2004-03-03 22:56:16 +00:00
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2008-11-05 10:08:19 +00:00
|
|
|
#include "wx/beforestd.h"
|
2008-11-18 15:51:09 +00:00
|
|
|
#ifdef __VISUALC__
|
|
|
|
#pragma warning(disable:4100)
|
|
|
|
#endif
|
2008-11-05 10:08:19 +00:00
|
|
|
#include <cppunit/TestListener.h>
|
2008-11-18 15:51:09 +00:00
|
|
|
#ifdef __VISUALC__
|
|
|
|
#pragma warning(default:4100)
|
|
|
|
#endif
|
2008-11-05 10:08:19 +00:00
|
|
|
#include <cppunit/Test.h>
|
|
|
|
#include <cppunit/TestResult.h>
|
|
|
|
#include "wx/afterstd.h"
|
|
|
|
|
2004-03-03 22:56:16 +00:00
|
|
|
#include "wx/cmdline.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
2004-11-10 21:29:08 +00:00
|
|
|
using CppUnit::Test;
|
|
|
|
using CppUnit::TestSuite;
|
|
|
|
using CppUnit::TestFactoryRegistry;
|
|
|
|
|
2008-11-05 10:08:19 +00:00
|
|
|
|
2008-11-18 15:51:09 +00:00
|
|
|
// 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.
|
2008-11-05 10:08:19 +00:00
|
|
|
class DetailListener : public CppUnit::TestListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DetailListener(bool doTiming = false):
|
|
|
|
CppUnit::TestListener(),
|
|
|
|
m_timing(doTiming)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void startTest(CppUnit::Test *test)
|
|
|
|
{
|
2008-11-18 15:51:09 +00:00
|
|
|
std::cout << test->getName () << " ";
|
2008-11-05 10:08:19 +00:00
|
|
|
m_watch.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void endTest(CppUnit::Test * WXUNUSED(test))
|
|
|
|
{
|
|
|
|
m_watch.Pause();
|
|
|
|
if ( m_timing )
|
2008-11-18 15:51:09 +00:00
|
|
|
std::cout << " (in "<< m_watch.Time() << " ms )";
|
|
|
|
std::cout << "\n";
|
2008-11-05 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected :
|
|
|
|
bool m_timing;
|
|
|
|
wxStopWatch m_watch;
|
|
|
|
};
|
|
|
|
|
2008-01-13 01:13:03 +00:00
|
|
|
using namespace std;
|
2004-03-03 22:56:16 +00:00
|
|
|
|
2007-09-26 00:28:31 +00:00
|
|
|
#if wxUSE_GUI
|
|
|
|
typedef wxApp TestAppBase;
|
|
|
|
#else
|
|
|
|
typedef wxAppConsole TestAppBase;
|
|
|
|
#endif
|
|
|
|
|
2004-03-03 22:56:16 +00:00
|
|
|
// The application class
|
|
|
|
//
|
2007-09-26 00:28:31 +00:00
|
|
|
class TestApp : public TestAppBase
|
2004-03-03 22:56:16 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestApp();
|
|
|
|
|
|
|
|
// standard overrides
|
2007-09-26 00:28:31 +00:00
|
|
|
virtual void OnInitCmdLine(wxCmdLineParser& parser);
|
|
|
|
virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
|
|
|
|
virtual bool OnInit();
|
|
|
|
virtual int OnRun();
|
|
|
|
virtual int OnExit();
|
2004-03-03 22:56:16 +00:00
|
|
|
|
2009-01-16 16:21:50 +00:00
|
|
|
// used by events propagation test
|
|
|
|
virtual int FilterEvent(wxEvent& event);
|
|
|
|
virtual bool ProcessEvent(wxEvent& event);
|
|
|
|
|
|
|
|
void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; }
|
|
|
|
void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; }
|
|
|
|
|
2008-12-21 02:28:55 +00:00
|
|
|
#ifdef __WXDEBUG__
|
|
|
|
virtual void OnAssertFailure(const wxChar *,
|
|
|
|
int,
|
|
|
|
const wxChar *,
|
|
|
|
const wxChar *,
|
|
|
|
const wxChar *)
|
|
|
|
{
|
|
|
|
throw TestAssertFailure();
|
|
|
|
}
|
|
|
|
#endif // __WXDEBUG__
|
|
|
|
|
2004-03-03 22:56:16 +00:00
|
|
|
private:
|
2004-04-18 20:04:30 +00:00
|
|
|
void List(Test *test, const string& parent = "") const;
|
2004-03-03 22:56:16 +00:00
|
|
|
|
|
|
|
// command lines options/parameters
|
|
|
|
bool m_list;
|
2004-04-01 11:51:09 +00:00
|
|
|
bool m_longlist;
|
2008-11-05 10:08:19 +00:00
|
|
|
bool m_detail;
|
|
|
|
bool m_timing;
|
2004-04-01 11:51:09 +00:00
|
|
|
vector<string> m_registries;
|
2009-01-16 16:21:50 +00:00
|
|
|
|
|
|
|
// event handling hooks
|
|
|
|
FilterEventFunc m_filterEventFunc;
|
|
|
|
ProcessEventFunc m_processEventFunc;
|
2004-03-03 22:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP_CONSOLE(TestApp)
|
|
|
|
|
|
|
|
TestApp::TestApp()
|
2004-04-18 20:04:30 +00:00
|
|
|
: m_list(false),
|
|
|
|
m_longlist(false)
|
2004-03-03 22:56:16 +00:00
|
|
|
{
|
2009-01-16 16:21:50 +00:00
|
|
|
m_filterEventFunc = NULL;
|
|
|
|
m_processEventFunc = NULL;
|
2004-03-03 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init
|
|
|
|
//
|
|
|
|
bool TestApp::OnInit()
|
|
|
|
{
|
2007-09-26 00:28:31 +00:00
|
|
|
if ( !TestAppBase::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2004-03-03 22:56:16 +00:00
|
|
|
cout << "Test program for wxWidgets\n"
|
2004-11-10 21:29:08 +00:00
|
|
|
<< "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
|
2007-02-11 02:27:29 +00:00
|
|
|
|
2007-09-26 00:28:31 +00:00
|
|
|
#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");
|
|
|
|
#endif // wxUSE_GUI
|
|
|
|
|
|
|
|
return true;
|
2008-07-19 15:36:39 +00:00
|
|
|
}
|
2004-03-03 22:56:16 +00:00
|
|
|
|
|
|
|
// The table of command line options
|
|
|
|
//
|
|
|
|
void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
|
|
|
|
{
|
2007-09-26 00:28:31 +00:00
|
|
|
TestAppBase::OnInitCmdLine(parser);
|
2004-03-03 22:56:16 +00:00
|
|
|
|
|
|
|
static const wxCmdLineEntryDesc cmdLineDesc[] = {
|
2007-09-14 23:26:06 +00:00
|
|
|
{ wxCMD_LINE_SWITCH, "l", "list",
|
|
|
|
"list the test suites, do not run them",
|
2004-04-01 11:51:09 +00:00
|
|
|
wxCMD_LINE_VAL_NONE, 0 },
|
2007-09-14 23:26:06 +00:00
|
|
|
{ wxCMD_LINE_SWITCH, "L", "longlist",
|
|
|
|
"list the test cases, do not run them",
|
2004-03-03 22:56:16 +00:00
|
|
|
wxCMD_LINE_VAL_NONE, 0 },
|
2008-11-05 10:08:19 +00:00
|
|
|
{ wxCMD_LINE_SWITCH, "d", "detail",
|
|
|
|
"print the test case names, run them",
|
|
|
|
wxCMD_LINE_VAL_NONE, 0 },
|
|
|
|
{ wxCMD_LINE_SWITCH, "t", "timing",
|
|
|
|
"print names and mesure running time of individual test, run them",
|
|
|
|
wxCMD_LINE_VAL_NONE, 0 },
|
2007-09-14 23:26:06 +00:00
|
|
|
{ wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
|
2004-04-01 11:51:09 +00:00
|
|
|
wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
|
2007-09-14 23:26:06 +00:00
|
|
|
wxCMD_LINE_DESC_END
|
2004-03-03 22:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
parser.SetDesc(cmdLineDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle command line options
|
|
|
|
//
|
|
|
|
bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
|
|
|
|
{
|
2004-04-01 11:51:09 +00:00
|
|
|
if (parser.GetParamCount())
|
|
|
|
for (size_t i = 0; i < parser.GetParamCount(); i++)
|
|
|
|
m_registries.push_back(string(parser.GetParam(i).mb_str()));
|
|
|
|
else
|
|
|
|
m_registries.push_back("");
|
2004-11-10 21:29:08 +00:00
|
|
|
|
2004-04-01 11:51:09 +00:00
|
|
|
m_longlist = parser.Found(_T("longlist"));
|
|
|
|
m_list = m_longlist || parser.Found(_T("list"));
|
2008-11-05 10:08:19 +00:00
|
|
|
m_timing = parser.Found(_T("timing"));
|
|
|
|
m_detail = !m_timing && parser.Found(_T("detail"));
|
2004-03-03 22:56:16 +00:00
|
|
|
|
2007-09-26 00:28:31 +00:00
|
|
|
return TestAppBase::OnCmdLineParsed(parser);
|
2004-03-03 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
2009-01-16 16:21:50 +00:00
|
|
|
// Event handling
|
|
|
|
int TestApp::FilterEvent(wxEvent& event)
|
|
|
|
{
|
|
|
|
if ( m_filterEventFunc )
|
|
|
|
return (*m_filterEventFunc)(event);
|
|
|
|
|
|
|
|
return TestAppBase::FilterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestApp::ProcessEvent(wxEvent& event)
|
|
|
|
{
|
|
|
|
if ( m_processEventFunc )
|
|
|
|
return (*m_processEventFunc)(event);
|
|
|
|
|
|
|
|
return TestAppBase::ProcessEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void SetFilterEventFunc(FilterEventFunc func)
|
|
|
|
{
|
|
|
|
wxGetApp().SetFilterEventFunc(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void SetProcessEventFunc(ProcessEventFunc func)
|
|
|
|
{
|
|
|
|
wxGetApp().SetProcessEventFunc(func);
|
|
|
|
}
|
|
|
|
|
2004-03-03 22:56:16 +00:00
|
|
|
// Run
|
|
|
|
//
|
|
|
|
int TestApp::OnRun()
|
|
|
|
{
|
2008-08-27 20:37:44 +00:00
|
|
|
CppUnit::TextTestRunner runner;
|
2004-04-01 11:51:09 +00:00
|
|
|
|
2009-03-21 00:06:32 +00:00
|
|
|
for (size_t i = 0; i < m_registries.size(); i++)
|
|
|
|
{
|
|
|
|
std::string reg = m_registries[i];
|
|
|
|
if (!reg.empty() && !wxString(reg).EndsWith("TestCase"))
|
|
|
|
reg += "TestCase";
|
|
|
|
// allow the user to specify the name of the testcase "in short form"
|
|
|
|
// (all wx test cases end with TestCase postfix)
|
|
|
|
|
|
|
|
auto_ptr<Test> test(reg.empty() ?
|
2004-04-01 11:51:09 +00:00
|
|
|
TestFactoryRegistry::getRegistry().makeTest() :
|
2009-03-21 00:06:32 +00:00
|
|
|
TestFactoryRegistry::getRegistry(reg).makeTest());
|
2004-04-01 11:51:09 +00:00
|
|
|
|
|
|
|
TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
|
|
|
|
|
|
|
|
if (suite && suite->countTestCases() == 0)
|
|
|
|
wxLogError(_T("No such test suite: %s"),
|
2009-03-21 00:06:32 +00:00
|
|
|
wxString(reg.c_str(), wxConvUTF8).c_str());
|
2004-04-01 11:51:09 +00:00
|
|
|
else if (m_list)
|
|
|
|
List(test.get());
|
|
|
|
else
|
|
|
|
runner.addTest(test.release());
|
2004-03-03 22:56:16 +00:00
|
|
|
}
|
2004-04-01 11:51:09 +00:00
|
|
|
|
2008-08-27 20:37:44 +00:00
|
|
|
if ( m_list )
|
2009-03-21 00:16:43 +00:00
|
|
|
return 0;
|
2008-08-27 20:37:44 +00:00
|
|
|
|
|
|
|
runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
|
2005-05-01 20:04:47 +00:00
|
|
|
|
2004-07-20 17:18:49 +00:00
|
|
|
#if wxUSE_LOG
|
2004-04-01 11:51:09 +00:00
|
|
|
// Switch off logging unless --verbose
|
2004-11-10 21:29:08 +00:00
|
|
|
bool verbose = wxLog::GetVerbose();
|
|
|
|
wxLog::EnableLogging(verbose);
|
2004-07-20 17:18:49 +00:00
|
|
|
#else
|
2004-11-10 21:29:08 +00:00
|
|
|
bool verbose = false;
|
|
|
|
#endif
|
|
|
|
|
2008-08-27 20:37:44 +00:00
|
|
|
// there is a bug
|
|
|
|
// (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
|
|
|
|
// in some versions of cppunit: they write progress dots to cout (and not
|
|
|
|
// cerr) and don't flush it so all the dots appear at once at the end which
|
|
|
|
// is not very useful so unbuffer cout to work around this
|
|
|
|
cout.setf(ios::unitbuf);
|
|
|
|
|
2008-11-05 10:08:19 +00:00
|
|
|
// add detail listener if needed
|
|
|
|
DetailListener detailListener(m_timing);
|
|
|
|
if ( m_detail || m_timing )
|
|
|
|
runner.eventManager().addListener(&detailListener);
|
|
|
|
|
2009-03-21 00:16:43 +00:00
|
|
|
return runner.run("", false, true, !verbose) ? 0 : 1;
|
2004-03-03 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 00:28:31 +00:00
|
|
|
int TestApp::OnExit()
|
|
|
|
{
|
|
|
|
#if wxUSE_GUI
|
|
|
|
delete GetTopWindow();
|
|
|
|
#endif // wxUSE_GUI
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-03 22:56:16 +00:00
|
|
|
// List the tests
|
|
|
|
//
|
2004-04-18 20:04:30 +00:00
|
|
|
void TestApp::List(Test *test, const string& parent /*=""*/) const
|
2004-03-03 22:56:16 +00:00
|
|
|
{
|
|
|
|
TestSuite *suite = dynamic_cast<TestSuite*>(test);
|
2004-04-18 20:04:30 +00:00
|
|
|
string name;
|
|
|
|
|
2004-04-21 20:17:18 +00:00
|
|
|
if (suite) {
|
2004-04-18 20:04:30 +00:00
|
|
|
// take the last component of the name and append to the parent
|
|
|
|
name = test->getName();
|
|
|
|
string::size_type i = name.find_last_of(".:");
|
2004-11-25 20:37:44 +00:00
|
|
|
if (i != string::npos)
|
|
|
|
name = name.substr(i + 1);
|
|
|
|
name = parent + "." + name;
|
2004-04-18 20:04:30 +00:00
|
|
|
|
|
|
|
// drop the 1st component from the display and indent
|
|
|
|
if (parent != "") {
|
|
|
|
string::size_type j = i = name.find('.', 1);
|
|
|
|
while ((j = name.find('.', j + 1)) != string::npos)
|
|
|
|
cout << " ";
|
|
|
|
cout << " " << name.substr(i + 1) << "\n";
|
|
|
|
}
|
2004-04-01 11:51:09 +00:00
|
|
|
|
2004-04-25 11:06:02 +00:00
|
|
|
typedef vector<Test*> Tests;
|
2004-03-03 22:56:16 +00:00
|
|
|
typedef Tests::const_iterator Iter;
|
|
|
|
|
2004-04-25 11:06:02 +00:00
|
|
|
const Tests& tests = suite->getTests();
|
2004-03-03 22:56:16 +00:00
|
|
|
|
|
|
|
for (Iter it = tests.begin(); it != tests.end(); ++it)
|
2004-04-18 20:04:30 +00:00
|
|
|
List(*it, name);
|
2004-03-03 22:56:16 +00:00
|
|
|
}
|
2004-04-21 20:17:18 +00:00
|
|
|
else if (m_longlist) {
|
|
|
|
string::size_type i = 0;
|
|
|
|
while ((i = parent.find('.', i + 1)) != string::npos)
|
|
|
|
cout << " ";
|
|
|
|
cout << " " << test->getName() << "\n";
|
|
|
|
}
|
2004-03-03 22:56:16 +00:00
|
|
|
}
|