2009-07-07 08:19:34 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/log/logtest.cpp
|
|
|
|
// Purpose: wxLog unit test
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2009-07-07
|
|
|
|
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/log.h"
|
2009-08-18 07:44:31 -04:00
|
|
|
#include "wx/filefn.h"
|
2009-07-07 08:19:34 -04:00
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
2009-07-08 09:47:33 -04:00
|
|
|
#include "wx/scopeguard.h"
|
|
|
|
|
2014-03-12 18:20:50 -04:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include "wx/msw/wrapwin.h"
|
|
|
|
#else
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2009-07-09 08:22:37 -04:00
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
// we override deprecated DoLog() and DoLogString() in this test, suppress
|
|
|
|
// warnings about it
|
2014-05-15 18:32:17 -04:00
|
|
|
#ifdef __VISUALC__
|
2009-07-09 08:22:37 -04:00
|
|
|
#pragma warning(disable: 4996)
|
|
|
|
#endif // VC++ 7+
|
|
|
|
#endif // WXWIN_COMPATIBILITY_2_8
|
|
|
|
|
2009-07-12 10:56:23 -04:00
|
|
|
// all calls to wxLogXXX() functions from this file will use this log component
|
|
|
|
#define wxLOG_COMPONENT "test"
|
|
|
|
|
2009-07-07 08:19:34 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-07-08 09:47:33 -04:00
|
|
|
// test loggers
|
2009-07-07 08:19:34 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-07-08 09:47:33 -04:00
|
|
|
// base class for all test loggers which simply store all logged messages for
|
|
|
|
// future examination in the test code
|
|
|
|
class TestLogBase : public wxLog
|
2009-07-07 08:19:34 -04:00
|
|
|
{
|
|
|
|
public:
|
2009-07-08 09:47:33 -04:00
|
|
|
TestLogBase() { }
|
2009-07-07 08:19:34 -04:00
|
|
|
|
2009-07-12 10:56:23 -04:00
|
|
|
const wxString& GetLog(wxLogLevel level) const
|
2009-07-07 08:19:34 -04:00
|
|
|
{
|
|
|
|
return m_logs[level];
|
|
|
|
}
|
|
|
|
|
2009-07-12 10:56:23 -04:00
|
|
|
const wxLogRecordInfo& GetInfo(wxLogLevel level) const
|
|
|
|
{
|
|
|
|
return m_logsInfo[level];
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:19:34 -04:00
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ )
|
2009-07-12 10:56:23 -04:00
|
|
|
{
|
2009-07-07 08:19:34 -04:00
|
|
|
m_logs[n].clear();
|
2009-07-12 10:56:23 -04:00
|
|
|
m_logsInfo[n] = wxLogRecordInfo();
|
|
|
|
}
|
2009-07-07 08:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2009-07-08 09:47:33 -04:00
|
|
|
wxString m_logs[wxLOG_Trace + 1];
|
2009-07-12 10:56:23 -04:00
|
|
|
wxLogRecordInfo m_logsInfo[wxLOG_Trace + 1];
|
2009-07-08 09:47:33 -04:00
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(TestLogBase);
|
|
|
|
};
|
|
|
|
|
|
|
|
// simple log sink which just stores the messages logged for each level
|
|
|
|
class TestLog : public TestLogBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestLog() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void DoLogRecord(wxLogLevel level,
|
|
|
|
const wxString& msg,
|
2018-09-21 13:46:49 -04:00
|
|
|
const wxLogRecordInfo& info) wxOVERRIDE
|
2009-07-08 09:47:33 -04:00
|
|
|
{
|
|
|
|
m_logs[level] = msg;
|
2009-07-12 10:56:23 -04:00
|
|
|
m_logsInfo[level] = info;
|
2009-07-08 09:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxDECLARE_NO_COPY_CLASS(TestLog);
|
|
|
|
};
|
|
|
|
|
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
|
|
|
|
// log sink overriding the old DoLogXXX() functions should still work too
|
|
|
|
|
|
|
|
// this one overrides DoLog(char*)
|
|
|
|
class CompatTestLog : public TestLogBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CompatTestLog() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void DoLog(wxLogLevel level, const char *str, time_t WXUNUSED(t))
|
2009-07-07 08:19:34 -04:00
|
|
|
{
|
|
|
|
m_logs[level] = str;
|
|
|
|
}
|
|
|
|
|
2009-07-08 09:47:33 -04:00
|
|
|
// get rid of the warning about hiding the other overload
|
|
|
|
virtual void DoLog(wxLogLevel WXUNUSED(level),
|
|
|
|
const wchar_t *WXUNUSED(str),
|
|
|
|
time_t WXUNUSED(t))
|
|
|
|
{
|
|
|
|
}
|
2009-07-07 08:19:34 -04:00
|
|
|
|
|
|
|
private:
|
2009-07-08 09:47:33 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(CompatTestLog);
|
|
|
|
};
|
2009-07-07 08:19:34 -04:00
|
|
|
|
2009-07-08 09:47:33 -04:00
|
|
|
// and this one overload DoLogString(wchar_t*)
|
|
|
|
class CompatTestLog2 : public wxLog
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CompatTestLog2() { }
|
|
|
|
|
|
|
|
const wxString& Get() const { return m_msg; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void DoLogString(const wchar_t *msg, time_t WXUNUSED(t))
|
|
|
|
{
|
|
|
|
m_msg = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get rid of the warning
|
|
|
|
virtual void DoLogString(const char *WXUNUSED(msg), time_t WXUNUSED(t))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxString m_msg;
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(CompatTestLog2);
|
2009-07-07 08:19:34 -04:00
|
|
|
};
|
|
|
|
|
2009-07-08 09:47:33 -04:00
|
|
|
#endif // WXWIN_COMPATIBILITY_2_8
|
|
|
|
|
2009-07-07 08:19:34 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class LogTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LogTestCase() { }
|
|
|
|
|
2018-07-29 05:09:17 -04:00
|
|
|
virtual void setUp() wxOVERRIDE;
|
|
|
|
virtual void tearDown() wxOVERRIDE;
|
2009-07-07 08:19:34 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( LogTestCase );
|
|
|
|
CPPUNIT_TEST( Functions );
|
|
|
|
CPPUNIT_TEST( Null );
|
2009-07-12 10:56:23 -04:00
|
|
|
CPPUNIT_TEST( Component );
|
2009-07-07 08:19:34 -04:00
|
|
|
#if wxDEBUG_LEVEL
|
|
|
|
CPPUNIT_TEST( Trace );
|
|
|
|
#endif // wxDEBUG_LEVEL
|
2009-07-08 09:47:33 -04:00
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
CPPUNIT_TEST( CompatLogger );
|
|
|
|
CPPUNIT_TEST( CompatLogger2 );
|
|
|
|
#endif // WXWIN_COMPATIBILITY_2_8
|
2009-08-17 21:22:48 -04:00
|
|
|
CPPUNIT_TEST( SysError );
|
2013-08-31 13:41:16 -04:00
|
|
|
CPPUNIT_TEST( NoWarnings );
|
2009-07-07 08:19:34 -04:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void Functions();
|
|
|
|
void Null();
|
2009-07-12 10:56:23 -04:00
|
|
|
void Component();
|
2009-07-07 08:19:34 -04:00
|
|
|
#if wxDEBUG_LEVEL
|
|
|
|
void Trace();
|
|
|
|
#endif // wxDEBUG_LEVEL
|
2009-07-08 09:47:33 -04:00
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
void CompatLogger();
|
|
|
|
void CompatLogger2();
|
|
|
|
#endif // WXWIN_COMPATIBILITY_2_8
|
2009-08-17 21:22:48 -04:00
|
|
|
void SysError();
|
2013-08-31 13:41:16 -04:00
|
|
|
void NoWarnings();
|
2009-07-07 08:19:34 -04:00
|
|
|
|
|
|
|
TestLog *m_log;
|
|
|
|
wxLog *m_logOld;
|
|
|
|
bool m_logWasEnabled;
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(LogTestCase);
|
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase );
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2009-07-07 08:19:34 -04:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase, "LogTestCase" );
|
|
|
|
|
|
|
|
void LogTestCase::setUp()
|
|
|
|
{
|
|
|
|
m_logOld = wxLog::SetActiveTarget(m_log = new TestLog);
|
|
|
|
m_logWasEnabled = wxLog::EnableLogging();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogTestCase::tearDown()
|
|
|
|
{
|
|
|
|
delete wxLog::SetActiveTarget(m_logOld);
|
|
|
|
wxLog::EnableLogging(m_logWasEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogTestCase::Functions()
|
|
|
|
{
|
|
|
|
wxLogMessage("Message");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Message", m_log->GetLog(wxLOG_Message) );
|
|
|
|
|
|
|
|
wxLogError("Error %d", 17);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Error 17", m_log->GetLog(wxLOG_Error) );
|
|
|
|
|
|
|
|
wxLogDebug("Debug");
|
|
|
|
#if wxDEBUG_LEVEL
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Debug", m_log->GetLog(wxLOG_Debug) );
|
|
|
|
#else
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Debug) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogTestCase::Null()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
wxLogNull noLog;
|
|
|
|
wxLogWarning("%s warning", "Not important");
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Warning) );
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogWarning("%s warning", "Important");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) );
|
|
|
|
}
|
|
|
|
|
2009-07-12 10:56:23 -04:00
|
|
|
void LogTestCase::Component()
|
|
|
|
{
|
|
|
|
wxLogMessage("Message");
|
Replace CppUnit with Catch for unit tests
Drop the legacy CppUnit testing framework used for the unit tests.
Replacing it with Catch has the advantage of not requiring CppUnit
libraries to be installed on the system in order to be able to run
tests (Catch is header-only and a copy of it is now included in the
main repository itself) and, in the future, of being able to write
the tests in a much more natural way.
For now, however, avoid changing the existing tests code as much as
[reasonably] possible to avoid introducing bugs in them and provide
the CppUnit compatibility macros in the new wx/catch_cppunit.h header
which allow to preserve the 99% of the existing code unchanged. Some
of the required changes are:
- Decompose asserts using "a && b" conditions into multiple asserts
checking "a" and "b" independently. This would have been better
even with CppUnit (to know which part of condition exactly failed)
and is required with Catch.
- Use extra parentheses around such conditions when they can't be
easily decomposed in the arrays test, due to the use of macros.
This is not ideal from the point of view of messages given when
the tests fail but will do for now.
- Rewrite asserts using "a || b" as a combination of condition
checks and assert macros. Again, this is better anyhow, and is
required with Catch. Incidentally, this allowed to fix a bug in
the "exec" unit test which didn't leave enough time for the new
process to be launched before trying to kill it.
- Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros,
our emulation of this macro can be used only once.
- Provide string conversions using Catch-specific StringMaker for
a couple of types.
- Replace custom wxImage comparison with a Catch-specific matcher
class.
- Remove most of test running logic from test.cpp, in particular don't
parse command line ourselves any longer but use Catch built-in
command line parser. This is a source of a minor regression:
previously, both "Foo" and "FooTestCase" could be used as the name of
the test to run, but now only the latter is accepted.
2017-11-01 14:15:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( std::string(wxLOG_COMPONENT),
|
2009-07-12 10:56:23 -04:00
|
|
|
m_log->GetInfo(wxLOG_Message).component );
|
|
|
|
|
|
|
|
// completely disable logging for this component
|
|
|
|
wxLog::SetComponentLevel("test/ignore", wxLOG_FatalError);
|
|
|
|
|
|
|
|
// but enable it for one of its subcomponents
|
|
|
|
wxLog::SetComponentLevel("test/ignore/not", wxLOG_Max);
|
|
|
|
|
|
|
|
#undef wxLOG_COMPONENT
|
|
|
|
#define wxLOG_COMPONENT "test/ignore"
|
|
|
|
|
|
|
|
// this shouldn't be output as this component is ignored
|
|
|
|
wxLogError("Error");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Error) );
|
|
|
|
|
|
|
|
// and so are its subcomponents
|
|
|
|
#undef wxLOG_COMPONENT
|
|
|
|
#define wxLOG_COMPONENT "test/ignore/sub/subsub"
|
|
|
|
wxLogError("Error");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Error) );
|
|
|
|
|
|
|
|
// but one subcomponent is not
|
|
|
|
#undef wxLOG_COMPONENT
|
|
|
|
#define wxLOG_COMPONENT "test/ignore/not"
|
|
|
|
wxLogError("Error");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Error", m_log->GetLog(wxLOG_Error) );
|
|
|
|
|
|
|
|
// restore the original value
|
|
|
|
#undef wxLOG_COMPONENT
|
|
|
|
#define wxLOG_COMPONENT "test"
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:19:34 -04:00
|
|
|
#if wxDEBUG_LEVEL
|
|
|
|
|
2009-07-23 09:40:44 -04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
const char *TEST_MASK = "test";
|
|
|
|
|
|
|
|
// this is a test vararg function (a real one, not a variadic-template-like as
|
|
|
|
// wxVLogTrace(), so care should be taken with its arguments)
|
|
|
|
void TraceTest(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, format);
|
|
|
|
wxVLogTrace(TEST_MASK, format, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2009-07-07 08:19:34 -04:00
|
|
|
void LogTestCase::Trace()
|
|
|
|
{
|
2009-07-23 09:40:44 -04:00
|
|
|
// we use wxLogTrace() or wxVLogTrace() from inside TraceTest()
|
|
|
|
// interchangeably here, it shouldn't make any difference
|
2009-07-07 08:19:34 -04:00
|
|
|
|
|
|
|
wxLogTrace(TEST_MASK, "Not shown");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
|
|
|
|
|
|
|
|
wxLog::AddTraceMask(TEST_MASK);
|
2009-07-23 09:40:44 -04:00
|
|
|
TraceTest("Shown");
|
2009-07-07 08:19:34 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK),
|
|
|
|
m_log->GetLog(wxLOG_Trace) );
|
|
|
|
|
|
|
|
wxLog::RemoveTraceMask(TEST_MASK);
|
|
|
|
m_log->Clear();
|
|
|
|
|
2009-07-23 09:40:44 -04:00
|
|
|
TraceTest("Not shown again");
|
2009-07-07 08:19:34 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // wxDEBUG_LEVEL
|
2009-07-08 09:47:33 -04:00
|
|
|
|
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
|
|
|
|
void LogTestCase::CompatLogger()
|
|
|
|
{
|
|
|
|
CompatTestLog log;
|
|
|
|
wxLog * const logOld = wxLog::SetActiveTarget(&log);
|
|
|
|
wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
|
|
|
|
|
|
|
|
wxLogError("Old error");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Old error", log.GetLog(wxLOG_Error) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogTestCase::CompatLogger2()
|
|
|
|
{
|
|
|
|
CompatTestLog2 log;
|
|
|
|
wxLog * const logOld = wxLog::SetActiveTarget(&log);
|
|
|
|
wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
|
|
|
|
|
|
|
|
wxLogWarning("Old warning");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Old warning", log.Get() );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // WXWIN_COMPATIBILITY_2_8
|
2009-08-17 21:22:48 -04:00
|
|
|
|
|
|
|
void LogTestCase::SysError()
|
|
|
|
{
|
|
|
|
wxString s;
|
|
|
|
|
|
|
|
wxLogSysError(17, "Error");
|
|
|
|
CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Error (", &s) );
|
2010-10-03 13:15:24 -04:00
|
|
|
WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s), s.StartsWith("error 17") );
|
|
|
|
|
2014-03-12 18:20:50 -04:00
|
|
|
// Try to ensure that the system error is 0.
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
::SetLastError(0);
|
|
|
|
#else
|
|
|
|
errno = 0;
|
|
|
|
#endif
|
|
|
|
|
2010-10-03 13:15:24 -04:00
|
|
|
wxLogSysError("Success");
|
|
|
|
CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Success (", &s) );
|
|
|
|
WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s), s.StartsWith("error 0") );
|
2009-08-17 21:22:48 -04:00
|
|
|
|
|
|
|
wxOpen("no-such-file", 0, 0);
|
|
|
|
wxLogSysError("Not found");
|
|
|
|
CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Not found (", &s) );
|
|
|
|
WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s), s.StartsWith("error 2") );
|
|
|
|
}
|
|
|
|
|
2013-08-31 13:41:16 -04:00
|
|
|
void LogTestCase::NoWarnings()
|
|
|
|
{
|
|
|
|
// Check that "else" branch is [not] taken as expected and that this code
|
|
|
|
// compiles without warnings (which used to not be the case).
|
|
|
|
|
|
|
|
bool b = wxFalse;
|
|
|
|
if ( b )
|
|
|
|
wxLogError("Not logged");
|
|
|
|
else
|
|
|
|
b = !b;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( b );
|
|
|
|
|
|
|
|
if ( b )
|
|
|
|
wxLogError("If");
|
|
|
|
else
|
|
|
|
CPPUNIT_FAIL("Should not be taken");
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "If", m_log->GetLog(wxLOG_Error) );
|
|
|
|
}
|