From ece5e6a47aef257ddf8488782d34712dbfb3e7f1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 7 Jul 2009 12:19:34 +0000 Subject: [PATCH] added a simple wxLog unit test git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61343 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- tests/Makefile.in | 6 +- tests/log/logtest.cpp | 161 +++++++++++++++++++++++++++++++++++++ tests/makefile.bcc | 4 + tests/makefile.gcc | 4 + tests/makefile.vc | 4 + tests/makefile.wat | 4 + tests/test.bkl | 1 + tests/test_test.dsp | 4 + tests/test_vc7_test.vcproj | 3 + tests/test_vc8_test.vcproj | 4 + tests/test_vc9_test.vcproj | 4 + 11 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 tests/log/logtest.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index 51ab17eb55..b4d15181cd 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -46,7 +46,7 @@ wx_top_builddir = @wx_top_builddir@ DESTDIR = WX_RELEASE = 2.9 -WX_VERSION = $(WX_RELEASE).0 +WX_VERSION = $(WX_RELEASE).1 LIBDIRNAME = $(wx_top_builddir)/lib TEST_CXXFLAGS = $(__test_PCH_INC) -D__WX$(TOOLKIT)__ $(__WXUNIV_DEFINE_p) \ $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) $(__THREAD_DEFINE_p) \ @@ -73,6 +73,7 @@ TEST_OBJECTS = \ test_hashes.o \ test_intltest.o \ test_lists.o \ + test_logtest.o \ test_longlongtest.o \ test_convautotest.o \ test_mbconvtest.o \ @@ -399,6 +400,9 @@ test_intltest.o: $(srcdir)/intl/intltest.cpp $(TEST_ODEP) test_lists.o: $(srcdir)/lists/lists.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/lists/lists.cpp +test_logtest.o: $(srcdir)/log/logtest.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/log/logtest.cpp + test_longlongtest.o: $(srcdir)/longlong/longlongtest.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/longlong/longlongtest.cpp diff --git a/tests/log/logtest.cpp b/tests/log/logtest.cpp new file mode 100644 index 0000000000..c03862816d --- /dev/null +++ b/tests/log/logtest.cpp @@ -0,0 +1,161 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/log/logtest.cpp +// Purpose: wxLog unit test +// Author: Vadim Zeitlin +// Created: 2009-07-07 +// RCS-ID: $Id$ +// Copyright: (c) 2009 Vadim Zeitlin +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/log.h" +#endif // WX_PRECOMP + +// ---------------------------------------------------------------------------- +// test logger +// ---------------------------------------------------------------------------- + +// simple log sink which just stores the messages logged for each level +class TestLog : public wxLog +{ +public: + TestLog() { } + + wxString GetLog(wxLogLevel level) const + { + return m_logs[level]; + } + + void Clear() + { + for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ ) + m_logs[n].clear(); + } + +protected: + virtual void DoLog(wxLogLevel level, const wxString& str, time_t WXUNUSED(t)) + { + m_logs[level] = str; + } + + wxSUPPRESS_DOLOG_HIDE_WARNING() + +private: + wxString m_logs[wxLOG_Trace + 1]; + + wxDECLARE_NO_COPY_CLASS(TestLog); +}; + +// ---------------------------------------------------------------------------- +// test class +// ---------------------------------------------------------------------------- + +class LogTestCase : public CppUnit::TestCase +{ +public: + LogTestCase() { } + + virtual void setUp(); + virtual void tearDown(); + +private: + CPPUNIT_TEST_SUITE( LogTestCase ); + CPPUNIT_TEST( Functions ); + CPPUNIT_TEST( Null ); +#if wxDEBUG_LEVEL + CPPUNIT_TEST( Trace ); +#endif // wxDEBUG_LEVEL + CPPUNIT_TEST_SUITE_END(); + + void Functions(); + void Null(); +#if wxDEBUG_LEVEL + void Trace(); +#endif // wxDEBUG_LEVEL + + TestLog *m_log; + wxLog *m_logOld; + bool m_logWasEnabled; + + wxDECLARE_NO_COPY_CLASS(LogTestCase); +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase, "LogTestCase" ); + +void LogTestCase::setUp() +{ + m_logOld = wxLog::SetActiveTarget(m_log = new TestLog); + m_logWasEnabled = wxLog::EnableLogging(); +} + +void LogTestCase::tearDown() +{ + delete wxLog::SetActiveTarget(m_logOld); + wxLog::EnableLogging(m_logWasEnabled); +} + +void LogTestCase::Functions() +{ + wxLogMessage("Message"); + CPPUNIT_ASSERT_EQUAL( "Message", m_log->GetLog(wxLOG_Message) ); + + wxLogError("Error %d", 17); + CPPUNIT_ASSERT_EQUAL( "Error 17", m_log->GetLog(wxLOG_Error) ); + + wxLogDebug("Debug"); +#if wxDEBUG_LEVEL + CPPUNIT_ASSERT_EQUAL( "Debug", m_log->GetLog(wxLOG_Debug) ); +#else + CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Debug) ); +#endif +} + +void LogTestCase::Null() +{ + { + wxLogNull noLog; + wxLogWarning("%s warning", "Not important"); + + CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Warning) ); + } + + wxLogWarning("%s warning", "Important"); + CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) ); +} + +#if wxDEBUG_LEVEL + +void LogTestCase::Trace() +{ + static const char *TEST_MASK = "test"; + + wxLogTrace(TEST_MASK, "Not shown"); + CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) ); + + wxLog::AddTraceMask(TEST_MASK); + wxLogTrace(TEST_MASK, "Shown"); + CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK), + m_log->GetLog(wxLOG_Trace) ); + + wxLog::RemoveTraceMask(TEST_MASK); + m_log->Clear(); + + wxLogTrace(TEST_MASK, "Not shown again"); + CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) ); +} + +#endif // wxDEBUG_LEVEL diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 52b673772c..bded0d0f18 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -57,6 +57,7 @@ TEST_OBJECTS = \ $(OBJS)\test_hashes.obj \ $(OBJS)\test_intltest.obj \ $(OBJS)\test_lists.obj \ + $(OBJS)\test_logtest.obj \ $(OBJS)\test_longlongtest.obj \ $(OBJS)\test_convautotest.obj \ $(OBJS)\test_mbconvtest.obj \ @@ -442,6 +443,9 @@ $(OBJS)\test_intltest.obj: .\intl\intltest.cpp $(OBJS)\test_lists.obj: .\lists\lists.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\lists\lists.cpp +$(OBJS)\test_logtest.obj: .\log\logtest.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\log\logtest.cpp + $(OBJS)\test_longlongtest.obj: .\longlong\longlongtest.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\longlong\longlongtest.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index bff61a8b8b..0abb687261 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -49,6 +49,7 @@ TEST_OBJECTS = \ $(OBJS)\test_hashes.o \ $(OBJS)\test_intltest.o \ $(OBJS)\test_lists.o \ + $(OBJS)\test_logtest.o \ $(OBJS)\test_longlongtest.o \ $(OBJS)\test_convautotest.o \ $(OBJS)\test_mbconvtest.o \ @@ -429,6 +430,9 @@ $(OBJS)\test_intltest.o: ./intl/intltest.cpp $(OBJS)\test_lists.o: ./lists/lists.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_logtest.o: ./log/logtest.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_longlongtest.o: ./longlong/longlongtest.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index d3353c0e78..dfa04f3367 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -50,6 +50,7 @@ TEST_OBJECTS = \ $(OBJS)\test_hashes.obj \ $(OBJS)\test_intltest.obj \ $(OBJS)\test_lists.obj \ + $(OBJS)\test_logtest.obj \ $(OBJS)\test_longlongtest.obj \ $(OBJS)\test_convautotest.obj \ $(OBJS)\test_mbconvtest.obj \ @@ -522,6 +523,9 @@ $(OBJS)\test_intltest.obj: .\intl\intltest.cpp $(OBJS)\test_lists.obj: .\lists\lists.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\lists\lists.cpp +$(OBJS)\test_logtest.obj: .\log\logtest.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\log\logtest.cpp + $(OBJS)\test_longlongtest.obj: .\longlong\longlongtest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\longlong\longlongtest.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index 7b0d21e5e1..d74e72d028 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -288,6 +288,7 @@ TEST_OBJECTS = & $(OBJS)\test_hashes.obj & $(OBJS)\test_intltest.obj & $(OBJS)\test_lists.obj & + $(OBJS)\test_logtest.obj & $(OBJS)\test_longlongtest.obj & $(OBJS)\test_convautotest.obj & $(OBJS)\test_mbconvtest.obj & @@ -481,6 +482,9 @@ $(OBJS)\test_intltest.obj : .AUTODEPEND .\intl\intltest.cpp $(OBJS)\test_lists.obj : .AUTODEPEND .\lists\lists.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< +$(OBJS)\test_logtest.obj : .AUTODEPEND .\log\logtest.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + $(OBJS)\test_longlongtest.obj : .AUTODEPEND .\longlong\longlongtest.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< diff --git a/tests/test.bkl b/tests/test.bkl index b679cfb471..37a7cdb7f4 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -48,6 +48,7 @@ hashes/hashes.cpp intl/intltest.cpp lists/lists.cpp + log/logtest.cpp longlong/longlongtest.cpp mbconv/convautotest.cpp mbconv/mbconvtest.cpp diff --git a/tests/test_test.dsp b/tests/test_test.dsp index b2d5ec01a3..611166829c 100644 --- a/tests/test_test.dsp +++ b/tests/test_test.dsp @@ -357,6 +357,10 @@ SOURCE=.\lists\lists.cpp # End Source File # Begin Source File +SOURCE=.\log\logtest.cpp +# End Source File +# Begin Source File + SOURCE=.\longlong\longlongtest.cpp # End Source File # Begin Source File diff --git a/tests/test_vc7_test.vcproj b/tests/test_vc7_test.vcproj index b728cb2f74..754193aa5c 100644 --- a/tests/test_vc7_test.vcproj +++ b/tests/test_vc7_test.vcproj @@ -695,6 +695,9 @@ + + diff --git a/tests/test_vc8_test.vcproj b/tests/test_vc8_test.vcproj index 2e9d07f6e2..671f7358b0 100644 --- a/tests/test_vc8_test.vcproj +++ b/tests/test_vc8_test.vcproj @@ -1003,6 +1003,10 @@ RelativePath=".\lists\lists.cpp" > + + diff --git a/tests/test_vc9_test.vcproj b/tests/test_vc9_test.vcproj index 765ae89a64..71d96a4b65 100644 --- a/tests/test_vc9_test.vcproj +++ b/tests/test_vc9_test.vcproj @@ -975,6 +975,10 @@ RelativePath=".\lists\lists.cpp" > + + -- 2.47.2