]>
git.saurik.com Git - wxWidgets.git/blob - tests/log/logtest.cpp
089ef82c51182d3429c05f15c5ca5b3fddff3e04
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 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 // base class for all test loggers which simply store all logged messages for
31 // future examination in the test code
32 class TestLogBase
: public wxLog
37 wxString
GetLog(wxLogLevel level
) const
44 for ( unsigned n
= 0; n
< WXSIZEOF(m_logs
); n
++ )
49 wxString m_logs
[wxLOG_Trace
+ 1];
51 wxDECLARE_NO_COPY_CLASS(TestLogBase
);
54 // simple log sink which just stores the messages logged for each level
55 class TestLog
: public TestLogBase
61 virtual void DoLogRecord(wxLogLevel level
,
63 const wxLogRecordInfo
& WXUNUSED(info
))
69 wxDECLARE_NO_COPY_CLASS(TestLog
);
72 #if WXWIN_COMPATIBILITY_2_8
74 // log sink overriding the old DoLogXXX() functions should still work too
76 // this one overrides DoLog(char*)
77 class CompatTestLog
: public TestLogBase
83 virtual void DoLog(wxLogLevel level
, const char *str
, time_t WXUNUSED(t
))
88 // get rid of the warning about hiding the other overload
89 virtual void DoLog(wxLogLevel
WXUNUSED(level
),
90 const wchar_t *WXUNUSED(str
),
96 wxDECLARE_NO_COPY_CLASS(CompatTestLog
);
99 // and this one overload DoLogString(wchar_t*)
100 class CompatTestLog2
: public wxLog
105 const wxString
& Get() const { return m_msg
; }
108 virtual void DoLogString(const wchar_t *msg
, time_t WXUNUSED(t
))
113 // get rid of the warning
114 virtual void DoLogString(const char *WXUNUSED(msg
), time_t WXUNUSED(t
))
121 wxDECLARE_NO_COPY_CLASS(CompatTestLog2
);
124 #endif // WXWIN_COMPATIBILITY_2_8
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 class LogTestCase
: public CppUnit::TestCase
135 virtual void setUp();
136 virtual void tearDown();
139 CPPUNIT_TEST_SUITE( LogTestCase
);
140 CPPUNIT_TEST( Functions
);
141 CPPUNIT_TEST( Null
);
143 CPPUNIT_TEST( Trace
);
144 #endif // wxDEBUG_LEVEL
145 #if WXWIN_COMPATIBILITY_2_8
146 CPPUNIT_TEST( CompatLogger
);
147 CPPUNIT_TEST( CompatLogger2
);
148 #endif // WXWIN_COMPATIBILITY_2_8
149 CPPUNIT_TEST_SUITE_END();
155 #endif // wxDEBUG_LEVEL
156 #if WXWIN_COMPATIBILITY_2_8
158 void CompatLogger2();
159 #endif // WXWIN_COMPATIBILITY_2_8
163 bool m_logWasEnabled
;
165 wxDECLARE_NO_COPY_CLASS(LogTestCase
);
168 // register in the unnamed registry so that these tests are run by default
169 CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase
);
171 // also include in it's own registry so that these tests can be run alone
172 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase
, "LogTestCase" );
174 void LogTestCase::setUp()
176 m_logOld
= wxLog::SetActiveTarget(m_log
= new TestLog
);
177 m_logWasEnabled
= wxLog::EnableLogging();
180 void LogTestCase::tearDown()
182 delete wxLog::SetActiveTarget(m_logOld
);
183 wxLog::EnableLogging(m_logWasEnabled
);
186 void LogTestCase::Functions()
188 wxLogMessage("Message");
189 CPPUNIT_ASSERT_EQUAL( "Message", m_log
->GetLog(wxLOG_Message
) );
191 wxLogError("Error %d", 17);
192 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log
->GetLog(wxLOG_Error
) );
196 CPPUNIT_ASSERT_EQUAL( "Debug", m_log
->GetLog(wxLOG_Debug
) );
198 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Debug
) );
202 void LogTestCase::Null()
206 wxLogWarning("%s warning", "Not important");
208 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Warning
) );
211 wxLogWarning("%s warning", "Important");
212 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log
->GetLog(wxLOG_Warning
) );
217 void LogTestCase::Trace()
219 static const char *TEST_MASK
= "test";
221 wxLogTrace(TEST_MASK
, "Not shown");
222 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
224 wxLog::AddTraceMask(TEST_MASK
);
225 wxLogTrace(TEST_MASK
, "Shown");
226 CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK
),
227 m_log
->GetLog(wxLOG_Trace
) );
229 wxLog::RemoveTraceMask(TEST_MASK
);
232 wxLogTrace(TEST_MASK
, "Not shown again");
233 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
236 #endif // wxDEBUG_LEVEL
238 #if WXWIN_COMPATIBILITY_2_8
240 void LogTestCase::CompatLogger()
243 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
244 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
246 wxLogError("Old error");
247 CPPUNIT_ASSERT_EQUAL( "Old error", log
.GetLog(wxLOG_Error
) );
250 void LogTestCase::CompatLogger2()
253 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
254 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
256 wxLogWarning("Old warning");
257 CPPUNIT_ASSERT_EQUAL( "Old warning", log
.Get() );
260 #endif // WXWIN_COMPATIBILITY_2_8