]>
git.saurik.com Git - wxWidgets.git/blob - tests/log/logtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/log/logtest.cpp
3 // Purpose: wxLog unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
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 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // base class for all test loggers which simply store all logged messages for
39 // future examination in the test code
40 class TestLogBase
: public wxLog
45 wxString
GetLog(wxLogLevel level
) const
52 for ( unsigned n
= 0; n
< WXSIZEOF(m_logs
); n
++ )
57 wxString m_logs
[wxLOG_Trace
+ 1];
59 wxDECLARE_NO_COPY_CLASS(TestLogBase
);
62 // simple log sink which just stores the messages logged for each level
63 class TestLog
: public TestLogBase
69 virtual void DoLogRecord(wxLogLevel level
,
71 const wxLogRecordInfo
& WXUNUSED(info
))
77 wxDECLARE_NO_COPY_CLASS(TestLog
);
80 #if WXWIN_COMPATIBILITY_2_8
82 // log sink overriding the old DoLogXXX() functions should still work too
84 // this one overrides DoLog(char*)
85 class CompatTestLog
: public TestLogBase
91 virtual void DoLog(wxLogLevel level
, const char *str
, time_t WXUNUSED(t
))
96 // get rid of the warning about hiding the other overload
97 virtual void DoLog(wxLogLevel
WXUNUSED(level
),
98 const wchar_t *WXUNUSED(str
),
104 wxDECLARE_NO_COPY_CLASS(CompatTestLog
);
107 // and this one overload DoLogString(wchar_t*)
108 class CompatTestLog2
: public wxLog
113 const wxString
& Get() const { return m_msg
; }
116 virtual void DoLogString(const wchar_t *msg
, time_t WXUNUSED(t
))
121 // get rid of the warning
122 virtual void DoLogString(const char *WXUNUSED(msg
), time_t WXUNUSED(t
))
129 wxDECLARE_NO_COPY_CLASS(CompatTestLog2
);
132 #endif // WXWIN_COMPATIBILITY_2_8
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 class LogTestCase
: public CppUnit::TestCase
143 virtual void setUp();
144 virtual void tearDown();
147 CPPUNIT_TEST_SUITE( LogTestCase
);
148 CPPUNIT_TEST( Functions
);
149 CPPUNIT_TEST( Null
);
151 CPPUNIT_TEST( Trace
);
152 #endif // wxDEBUG_LEVEL
153 #if WXWIN_COMPATIBILITY_2_8
154 CPPUNIT_TEST( CompatLogger
);
155 CPPUNIT_TEST( CompatLogger2
);
156 #endif // WXWIN_COMPATIBILITY_2_8
157 CPPUNIT_TEST_SUITE_END();
163 #endif // wxDEBUG_LEVEL
164 #if WXWIN_COMPATIBILITY_2_8
166 void CompatLogger2();
167 #endif // WXWIN_COMPATIBILITY_2_8
171 bool m_logWasEnabled
;
173 wxDECLARE_NO_COPY_CLASS(LogTestCase
);
176 // register in the unnamed registry so that these tests are run by default
177 CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase
);
179 // also include in it's own registry so that these tests can be run alone
180 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase
, "LogTestCase" );
182 void LogTestCase::setUp()
184 m_logOld
= wxLog::SetActiveTarget(m_log
= new TestLog
);
185 m_logWasEnabled
= wxLog::EnableLogging();
188 void LogTestCase::tearDown()
190 delete wxLog::SetActiveTarget(m_logOld
);
191 wxLog::EnableLogging(m_logWasEnabled
);
194 void LogTestCase::Functions()
196 wxLogMessage("Message");
197 CPPUNIT_ASSERT_EQUAL( "Message", m_log
->GetLog(wxLOG_Message
) );
199 wxLogError("Error %d", 17);
200 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log
->GetLog(wxLOG_Error
) );
204 CPPUNIT_ASSERT_EQUAL( "Debug", m_log
->GetLog(wxLOG_Debug
) );
206 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Debug
) );
210 void LogTestCase::Null()
214 wxLogWarning("%s warning", "Not important");
216 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Warning
) );
219 wxLogWarning("%s warning", "Important");
220 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log
->GetLog(wxLOG_Warning
) );
225 void LogTestCase::Trace()
227 static const char *TEST_MASK
= "test";
229 wxLogTrace(TEST_MASK
, "Not shown");
230 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
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
) );
237 wxLog::RemoveTraceMask(TEST_MASK
);
240 wxLogTrace(TEST_MASK
, "Not shown again");
241 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
244 #endif // wxDEBUG_LEVEL
246 #if WXWIN_COMPATIBILITY_2_8
248 void LogTestCase::CompatLogger()
251 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
252 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
254 wxLogError("Old error");
255 CPPUNIT_ASSERT_EQUAL( "Old error", log
.GetLog(wxLOG_Error
) );
258 void LogTestCase::CompatLogger2()
261 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
262 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
264 wxLogWarning("Old warning");
265 CPPUNIT_ASSERT_EQUAL( "Old warning", log
.Get() );
268 #endif // WXWIN_COMPATIBILITY_2_8