]> git.saurik.com Git - wxWidgets.git/blob - tests/log/logtest.cpp
added a simple wxLog unit test
[wxWidgets.git] / tests / log / logtest.cpp
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
24 // ----------------------------------------------------------------------------
25 // test logger
26 // ----------------------------------------------------------------------------
27
28 // simple log sink which just stores the messages logged for each level
29 class TestLog : public wxLog
30 {
31 public:
32 TestLog() { }
33
34 wxString GetLog(wxLogLevel level) const
35 {
36 return m_logs[level];
37 }
38
39 void Clear()
40 {
41 for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ )
42 m_logs[n].clear();
43 }
44
45 protected:
46 virtual void DoLog(wxLogLevel level, const wxString& str, time_t WXUNUSED(t))
47 {
48 m_logs[level] = str;
49 }
50
51 wxSUPPRESS_DOLOG_HIDE_WARNING()
52
53 private:
54 wxString m_logs[wxLOG_Trace + 1];
55
56 wxDECLARE_NO_COPY_CLASS(TestLog);
57 };
58
59 // ----------------------------------------------------------------------------
60 // test class
61 // ----------------------------------------------------------------------------
62
63 class LogTestCase : public CppUnit::TestCase
64 {
65 public:
66 LogTestCase() { }
67
68 virtual void setUp();
69 virtual void tearDown();
70
71 private:
72 CPPUNIT_TEST_SUITE( LogTestCase );
73 CPPUNIT_TEST( Functions );
74 CPPUNIT_TEST( Null );
75 #if wxDEBUG_LEVEL
76 CPPUNIT_TEST( Trace );
77 #endif // wxDEBUG_LEVEL
78 CPPUNIT_TEST_SUITE_END();
79
80 void Functions();
81 void Null();
82 #if wxDEBUG_LEVEL
83 void Trace();
84 #endif // wxDEBUG_LEVEL
85
86 TestLog *m_log;
87 wxLog *m_logOld;
88 bool m_logWasEnabled;
89
90 wxDECLARE_NO_COPY_CLASS(LogTestCase);
91 };
92
93 // register in the unnamed registry so that these tests are run by default
94 CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase );
95
96 // also include in it's own registry so that these tests can be run alone
97 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase, "LogTestCase" );
98
99 void LogTestCase::setUp()
100 {
101 m_logOld = wxLog::SetActiveTarget(m_log = new TestLog);
102 m_logWasEnabled = wxLog::EnableLogging();
103 }
104
105 void LogTestCase::tearDown()
106 {
107 delete wxLog::SetActiveTarget(m_logOld);
108 wxLog::EnableLogging(m_logWasEnabled);
109 }
110
111 void LogTestCase::Functions()
112 {
113 wxLogMessage("Message");
114 CPPUNIT_ASSERT_EQUAL( "Message", m_log->GetLog(wxLOG_Message) );
115
116 wxLogError("Error %d", 17);
117 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log->GetLog(wxLOG_Error) );
118
119 wxLogDebug("Debug");
120 #if wxDEBUG_LEVEL
121 CPPUNIT_ASSERT_EQUAL( "Debug", m_log->GetLog(wxLOG_Debug) );
122 #else
123 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Debug) );
124 #endif
125 }
126
127 void LogTestCase::Null()
128 {
129 {
130 wxLogNull noLog;
131 wxLogWarning("%s warning", "Not important");
132
133 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Warning) );
134 }
135
136 wxLogWarning("%s warning", "Important");
137 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) );
138 }
139
140 #if wxDEBUG_LEVEL
141
142 void LogTestCase::Trace()
143 {
144 static const char *TEST_MASK = "test";
145
146 wxLogTrace(TEST_MASK, "Not shown");
147 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
148
149 wxLog::AddTraceMask(TEST_MASK);
150 wxLogTrace(TEST_MASK, "Shown");
151 CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK),
152 m_log->GetLog(wxLOG_Trace) );
153
154 wxLog::RemoveTraceMask(TEST_MASK);
155 m_log->Clear();
156
157 wxLogTrace(TEST_MASK, "Not shown again");
158 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
159 }
160
161 #endif // wxDEBUG_LEVEL