]> git.saurik.com Git - wxWidgets.git/blame - tests/log/logtest.cpp
Yet another fix for the colour property 'Custom' entry (get drop-down list index...
[wxWidgets.git] / tests / log / logtest.cpp
CommitLineData
ece5e6a4
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/log/logtest.cpp
3// Purpose: wxLog unit test
4// Author: Vadim Zeitlin
5// Created: 2009-07-07
6// RCS-ID: $Id$
7// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14#include "testprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/log.h"
22#endif // WX_PRECOMP
23
bc73d5ae
VZ
24#include "wx/scopeguard.h"
25
48557701
VZ
26#if WXWIN_COMPATIBILITY_2_8
27 // we override deprecated DoLog() and DoLogString() in this test, suppress
28 // warnings about it
29 #if wxCHECK_VISUALC_VERSION(7)
30 #pragma warning(disable: 4996)
31 #endif // VC++ 7+
32#endif // WXWIN_COMPATIBILITY_2_8
33
ece5e6a4 34// ----------------------------------------------------------------------------
bc73d5ae 35// test loggers
ece5e6a4
VZ
36// ----------------------------------------------------------------------------
37
bc73d5ae
VZ
38// base class for all test loggers which simply store all logged messages for
39// future examination in the test code
40class TestLogBase : public wxLog
ece5e6a4
VZ
41{
42public:
bc73d5ae 43 TestLogBase() { }
ece5e6a4
VZ
44
45 wxString GetLog(wxLogLevel level) const
46 {
47 return m_logs[level];
48 }
49
50 void Clear()
51 {
52 for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ )
53 m_logs[n].clear();
54 }
55
56protected:
bc73d5ae
VZ
57 wxString m_logs[wxLOG_Trace + 1];
58
59 wxDECLARE_NO_COPY_CLASS(TestLogBase);
60};
61
62// simple log sink which just stores the messages logged for each level
63class TestLog : public TestLogBase
64{
65public:
66 TestLog() { }
67
68protected:
69 virtual void DoLogRecord(wxLogLevel level,
70 const wxString& msg,
71 const wxLogRecordInfo& WXUNUSED(info))
72 {
73 m_logs[level] = msg;
74 }
75
76private:
77 wxDECLARE_NO_COPY_CLASS(TestLog);
78};
79
80#if WXWIN_COMPATIBILITY_2_8
81
82// log sink overriding the old DoLogXXX() functions should still work too
83
84// this one overrides DoLog(char*)
85class CompatTestLog : public TestLogBase
86{
87public:
88 CompatTestLog() { }
89
90protected:
91 virtual void DoLog(wxLogLevel level, const char *str, time_t WXUNUSED(t))
ece5e6a4
VZ
92 {
93 m_logs[level] = str;
94 }
95
bc73d5ae
VZ
96 // get rid of the warning about hiding the other overload
97 virtual void DoLog(wxLogLevel WXUNUSED(level),
98 const wchar_t *WXUNUSED(str),
99 time_t WXUNUSED(t))
100 {
101 }
ece5e6a4
VZ
102
103private:
bc73d5ae
VZ
104 wxDECLARE_NO_COPY_CLASS(CompatTestLog);
105};
ece5e6a4 106
bc73d5ae
VZ
107// and this one overload DoLogString(wchar_t*)
108class CompatTestLog2 : public wxLog
109{
110public:
111 CompatTestLog2() { }
112
113 const wxString& Get() const { return m_msg; }
114
115protected:
116 virtual void DoLogString(const wchar_t *msg, time_t WXUNUSED(t))
117 {
118 m_msg = msg;
119 }
120
121 // get rid of the warning
122 virtual void DoLogString(const char *WXUNUSED(msg), time_t WXUNUSED(t))
123 {
124 }
125
126private:
127 wxString m_msg;
128
129 wxDECLARE_NO_COPY_CLASS(CompatTestLog2);
ece5e6a4
VZ
130};
131
bc73d5ae
VZ
132#endif // WXWIN_COMPATIBILITY_2_8
133
ece5e6a4
VZ
134// ----------------------------------------------------------------------------
135// test class
136// ----------------------------------------------------------------------------
137
138class LogTestCase : public CppUnit::TestCase
139{
140public:
141 LogTestCase() { }
142
143 virtual void setUp();
144 virtual void tearDown();
145
146private:
147 CPPUNIT_TEST_SUITE( LogTestCase );
148 CPPUNIT_TEST( Functions );
149 CPPUNIT_TEST( Null );
150#if wxDEBUG_LEVEL
151 CPPUNIT_TEST( Trace );
152#endif // wxDEBUG_LEVEL
bc73d5ae
VZ
153#if WXWIN_COMPATIBILITY_2_8
154 CPPUNIT_TEST( CompatLogger );
155 CPPUNIT_TEST( CompatLogger2 );
156#endif // WXWIN_COMPATIBILITY_2_8
ece5e6a4
VZ
157 CPPUNIT_TEST_SUITE_END();
158
159 void Functions();
160 void Null();
161#if wxDEBUG_LEVEL
162 void Trace();
163#endif // wxDEBUG_LEVEL
bc73d5ae
VZ
164#if WXWIN_COMPATIBILITY_2_8
165 void CompatLogger();
166 void CompatLogger2();
167#endif // WXWIN_COMPATIBILITY_2_8
ece5e6a4
VZ
168
169 TestLog *m_log;
170 wxLog *m_logOld;
171 bool m_logWasEnabled;
172
173 wxDECLARE_NO_COPY_CLASS(LogTestCase);
174};
175
176// register in the unnamed registry so that these tests are run by default
177CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase );
178
179// also include in it's own registry so that these tests can be run alone
180CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase, "LogTestCase" );
181
182void LogTestCase::setUp()
183{
184 m_logOld = wxLog::SetActiveTarget(m_log = new TestLog);
185 m_logWasEnabled = wxLog::EnableLogging();
186}
187
188void LogTestCase::tearDown()
189{
190 delete wxLog::SetActiveTarget(m_logOld);
191 wxLog::EnableLogging(m_logWasEnabled);
192}
193
194void LogTestCase::Functions()
195{
196 wxLogMessage("Message");
197 CPPUNIT_ASSERT_EQUAL( "Message", m_log->GetLog(wxLOG_Message) );
198
199 wxLogError("Error %d", 17);
200 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log->GetLog(wxLOG_Error) );
201
202 wxLogDebug("Debug");
203#if wxDEBUG_LEVEL
204 CPPUNIT_ASSERT_EQUAL( "Debug", m_log->GetLog(wxLOG_Debug) );
205#else
206 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Debug) );
207#endif
208}
209
210void LogTestCase::Null()
211{
212 {
213 wxLogNull noLog;
214 wxLogWarning("%s warning", "Not important");
215
216 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Warning) );
217 }
218
219 wxLogWarning("%s warning", "Important");
220 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) );
221}
222
223#if wxDEBUG_LEVEL
224
225void LogTestCase::Trace()
226{
227 static const char *TEST_MASK = "test";
228
229 wxLogTrace(TEST_MASK, "Not shown");
230 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
231
232 wxLog::AddTraceMask(TEST_MASK);
233 wxLogTrace(TEST_MASK, "Shown");
234 CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK),
235 m_log->GetLog(wxLOG_Trace) );
236
237 wxLog::RemoveTraceMask(TEST_MASK);
238 m_log->Clear();
239
240 wxLogTrace(TEST_MASK, "Not shown again");
241 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
242}
243
244#endif // wxDEBUG_LEVEL
bc73d5ae
VZ
245
246#if WXWIN_COMPATIBILITY_2_8
247
248void LogTestCase::CompatLogger()
249{
250 CompatTestLog log;
251 wxLog * const logOld = wxLog::SetActiveTarget(&log);
252 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
253
254 wxLogError("Old error");
255 CPPUNIT_ASSERT_EQUAL( "Old error", log.GetLog(wxLOG_Error) );
256}
257
258void LogTestCase::CompatLogger2()
259{
260 CompatTestLog2 log;
261 wxLog * const logOld = wxLog::SetActiveTarget(&log);
262 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
263
264 wxLogWarning("Old warning");
265 CPPUNIT_ASSERT_EQUAL( "Old warning", log.Get() );
266}
267
268#endif // WXWIN_COMPATIBILITY_2_8