]>
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 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 // simple log sink which just stores the messages logged for each level
29 class TestLog
: public wxLog
34 wxString
GetLog(wxLogLevel level
) const
41 for ( unsigned n
= 0; n
< WXSIZEOF(m_logs
); n
++ )
46 virtual void DoLog(wxLogLevel level
, const wxString
& str
, time_t WXUNUSED(t
))
51 wxSUPPRESS_DOLOG_HIDE_WARNING()
54 wxString m_logs
[wxLOG_Trace
+ 1];
56 wxDECLARE_NO_COPY_CLASS(TestLog
);
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 class LogTestCase
: public CppUnit::TestCase
69 virtual void tearDown();
72 CPPUNIT_TEST_SUITE( LogTestCase
);
73 CPPUNIT_TEST( Functions
);
76 CPPUNIT_TEST( Trace
);
77 #endif // wxDEBUG_LEVEL
78 CPPUNIT_TEST_SUITE_END();
84 #endif // wxDEBUG_LEVEL
90 wxDECLARE_NO_COPY_CLASS(LogTestCase
);
93 // register in the unnamed registry so that these tests are run by default
94 CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase
);
96 // also include in it's own registry so that these tests can be run alone
97 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase
, "LogTestCase" );
99 void LogTestCase::setUp()
101 m_logOld
= wxLog::SetActiveTarget(m_log
= new TestLog
);
102 m_logWasEnabled
= wxLog::EnableLogging();
105 void LogTestCase::tearDown()
107 delete wxLog::SetActiveTarget(m_logOld
);
108 wxLog::EnableLogging(m_logWasEnabled
);
111 void LogTestCase::Functions()
113 wxLogMessage("Message");
114 CPPUNIT_ASSERT_EQUAL( "Message", m_log
->GetLog(wxLOG_Message
) );
116 wxLogError("Error %d", 17);
117 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log
->GetLog(wxLOG_Error
) );
121 CPPUNIT_ASSERT_EQUAL( "Debug", m_log
->GetLog(wxLOG_Debug
) );
123 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Debug
) );
127 void LogTestCase::Null()
131 wxLogWarning("%s warning", "Not important");
133 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Warning
) );
136 wxLogWarning("%s warning", "Important");
137 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log
->GetLog(wxLOG_Warning
) );
142 void LogTestCase::Trace()
144 static const char *TEST_MASK
= "test";
146 wxLogTrace(TEST_MASK
, "Not shown");
147 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
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
) );
154 wxLog::RemoveTraceMask(TEST_MASK
);
157 wxLogTrace(TEST_MASK
, "Not shown again");
158 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
161 #endif // wxDEBUG_LEVEL