1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/log/logtest.cpp
3 // Purpose: wxLog unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
21 #include "wx/filefn.h"
24 #include "wx/scopeguard.h"
26 #if WXWIN_COMPATIBILITY_2_8
27 // we override deprecated DoLog() and DoLogString() in this test, suppress
29 #if wxCHECK_VISUALC_VERSION(7)
30 #pragma warning(disable: 4996)
32 #endif // WXWIN_COMPATIBILITY_2_8
34 // all calls to wxLogXXX() functions from this file will use this log component
35 #define wxLOG_COMPONENT "test"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // base class for all test loggers which simply store all logged messages for
42 // future examination in the test code
43 class TestLogBase
: public wxLog
48 const wxString
& GetLog(wxLogLevel level
) const
53 const wxLogRecordInfo
& GetInfo(wxLogLevel level
) const
55 return m_logsInfo
[level
];
60 for ( unsigned n
= 0; n
< WXSIZEOF(m_logs
); n
++ )
63 m_logsInfo
[n
] = wxLogRecordInfo();
68 wxString m_logs
[wxLOG_Trace
+ 1];
69 wxLogRecordInfo m_logsInfo
[wxLOG_Trace
+ 1];
71 wxDECLARE_NO_COPY_CLASS(TestLogBase
);
74 // simple log sink which just stores the messages logged for each level
75 class TestLog
: public TestLogBase
81 virtual void DoLogRecord(wxLogLevel level
,
83 const wxLogRecordInfo
& info
)
86 m_logsInfo
[level
] = info
;
90 wxDECLARE_NO_COPY_CLASS(TestLog
);
93 #if WXWIN_COMPATIBILITY_2_8
95 // log sink overriding the old DoLogXXX() functions should still work too
97 // this one overrides DoLog(char*)
98 class CompatTestLog
: public TestLogBase
104 virtual void DoLog(wxLogLevel level
, const char *str
, time_t WXUNUSED(t
))
109 // get rid of the warning about hiding the other overload
110 virtual void DoLog(wxLogLevel
WXUNUSED(level
),
111 const wchar_t *WXUNUSED(str
),
117 wxDECLARE_NO_COPY_CLASS(CompatTestLog
);
120 // and this one overload DoLogString(wchar_t*)
121 class CompatTestLog2
: public wxLog
126 const wxString
& Get() const { return m_msg
; }
129 virtual void DoLogString(const wchar_t *msg
, time_t WXUNUSED(t
))
134 // get rid of the warning
135 virtual void DoLogString(const char *WXUNUSED(msg
), time_t WXUNUSED(t
))
142 wxDECLARE_NO_COPY_CLASS(CompatTestLog2
);
145 #endif // WXWIN_COMPATIBILITY_2_8
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 class LogTestCase
: public CppUnit::TestCase
156 virtual void setUp();
157 virtual void tearDown();
160 CPPUNIT_TEST_SUITE( LogTestCase
);
161 CPPUNIT_TEST( Functions
);
162 CPPUNIT_TEST( Null
);
163 CPPUNIT_TEST( Component
);
165 CPPUNIT_TEST( Trace
);
166 #endif // wxDEBUG_LEVEL
167 #if WXWIN_COMPATIBILITY_2_8
168 CPPUNIT_TEST( CompatLogger
);
169 CPPUNIT_TEST( CompatLogger2
);
170 #endif // WXWIN_COMPATIBILITY_2_8
171 CPPUNIT_TEST( SysError
);
172 CPPUNIT_TEST_SUITE_END();
179 #endif // wxDEBUG_LEVEL
180 #if WXWIN_COMPATIBILITY_2_8
182 void CompatLogger2();
183 #endif // WXWIN_COMPATIBILITY_2_8
188 bool m_logWasEnabled
;
190 wxDECLARE_NO_COPY_CLASS(LogTestCase
);
193 // register in the unnamed registry so that these tests are run by default
194 CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase
);
196 // also include in its own registry so that these tests can be run alone
197 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase
, "LogTestCase" );
199 void LogTestCase::setUp()
201 m_logOld
= wxLog::SetActiveTarget(m_log
= new TestLog
);
202 m_logWasEnabled
= wxLog::EnableLogging();
205 void LogTestCase::tearDown()
207 delete wxLog::SetActiveTarget(m_logOld
);
208 wxLog::EnableLogging(m_logWasEnabled
);
211 void LogTestCase::Functions()
213 wxLogMessage("Message");
214 CPPUNIT_ASSERT_EQUAL( "Message", m_log
->GetLog(wxLOG_Message
) );
216 wxLogError("Error %d", 17);
217 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log
->GetLog(wxLOG_Error
) );
221 CPPUNIT_ASSERT_EQUAL( "Debug", m_log
->GetLog(wxLOG_Debug
) );
223 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Debug
) );
227 void LogTestCase::Null()
231 wxLogWarning("%s warning", "Not important");
233 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Warning
) );
236 wxLogWarning("%s warning", "Important");
237 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log
->GetLog(wxLOG_Warning
) );
240 void LogTestCase::Component()
242 wxLogMessage("Message");
243 CPPUNIT_ASSERT_EQUAL( wxLOG_COMPONENT
,
244 m_log
->GetInfo(wxLOG_Message
).component
);
246 // completely disable logging for this component
247 wxLog::SetComponentLevel("test/ignore", wxLOG_FatalError
);
249 // but enable it for one of its subcomponents
250 wxLog::SetComponentLevel("test/ignore/not", wxLOG_Max
);
252 #undef wxLOG_COMPONENT
253 #define wxLOG_COMPONENT "test/ignore"
255 // this shouldn't be output as this component is ignored
257 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Error
) );
259 // and so are its subcomponents
260 #undef wxLOG_COMPONENT
261 #define wxLOG_COMPONENT "test/ignore/sub/subsub"
263 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Error
) );
265 // but one subcomponent is not
266 #undef wxLOG_COMPONENT
267 #define wxLOG_COMPONENT "test/ignore/not"
269 CPPUNIT_ASSERT_EQUAL( "Error", m_log
->GetLog(wxLOG_Error
) );
271 // restore the original value
272 #undef wxLOG_COMPONENT
273 #define wxLOG_COMPONENT "test"
281 const char *TEST_MASK
= "test";
283 // this is a test vararg function (a real one, not a variadic-template-like as
284 // wxVLogTrace(), so care should be taken with its arguments)
285 void TraceTest(const char *format
, ...)
288 va_start(argptr
, format
);
289 wxVLogTrace(TEST_MASK
, format
, argptr
);
293 } // anonymous namespace
295 void LogTestCase::Trace()
297 // we use wxLogTrace() or wxVLogTrace() from inside TraceTest()
298 // interchangeably here, it shouldn't make any difference
300 wxLogTrace(TEST_MASK
, "Not shown");
301 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
303 wxLog::AddTraceMask(TEST_MASK
);
305 CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK
),
306 m_log
->GetLog(wxLOG_Trace
) );
308 wxLog::RemoveTraceMask(TEST_MASK
);
311 TraceTest("Not shown again");
312 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
315 #endif // wxDEBUG_LEVEL
317 #if WXWIN_COMPATIBILITY_2_8
319 void LogTestCase::CompatLogger()
322 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
323 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
325 wxLogError("Old error");
326 CPPUNIT_ASSERT_EQUAL( "Old error", log
.GetLog(wxLOG_Error
) );
329 void LogTestCase::CompatLogger2()
332 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
333 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
335 wxLogWarning("Old warning");
336 CPPUNIT_ASSERT_EQUAL( "Old warning", log
.Get() );
339 #endif // WXWIN_COMPATIBILITY_2_8
341 void LogTestCase::SysError()
345 wxLogSysError(17, "Error");
346 CPPUNIT_ASSERT( m_log
->GetLog(wxLOG_Error
).StartsWith("Error (", &s
) );
347 WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s
), s
.StartsWith("error 17") );
349 // The last error code seems to be set somewhere in MinGW CRT as its value
350 // is just not what we expect (ERROR_INVALID_PARAMETER instead of 0 and 0
351 // instead of ERROR_FILE_NOT_FOUND) so exclude the tests which rely on last
352 // error being preserved for this compiler.
354 wxLogSysError("Success");
355 CPPUNIT_ASSERT( m_log
->GetLog(wxLOG_Error
).StartsWith("Success (", &s
) );
356 WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s
), s
.StartsWith("error 0") );
358 wxOpen("no-such-file", 0, 0);
359 wxLogSysError("Not found");
360 CPPUNIT_ASSERT( m_log
->GetLog(wxLOG_Error
).StartsWith("Not found (", &s
) );
361 WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s
), s
.StartsWith("error 2") );
362 #endif // __MINGW32__