// Purpose: wxLog unit test
// Author: Vadim Zeitlin
// Created: 2009-07-07
-// RCS-ID: $Id$
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
#ifndef WX_PRECOMP
#include "wx/log.h"
+ #include "wx/filefn.h"
#endif // WX_PRECOMP
#include "wx/scopeguard.h"
CPPUNIT_TEST( CompatLogger );
CPPUNIT_TEST( CompatLogger2 );
#endif // WXWIN_COMPATIBILITY_2_8
+ CPPUNIT_TEST( SysError );
CPPUNIT_TEST_SUITE_END();
void Functions();
void CompatLogger();
void CompatLogger2();
#endif // WXWIN_COMPATIBILITY_2_8
+ void SysError();
TestLog *m_log;
wxLog *m_logOld;
// 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
+// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase, "LogTestCase" );
void LogTestCase::setUp()
#if wxDEBUG_LEVEL
+namespace
+{
+
+const char *TEST_MASK = "test";
+
+// this is a test vararg function (a real one, not a variadic-template-like as
+// wxVLogTrace(), so care should be taken with its arguments)
+void TraceTest(const char *format, ...)
+{
+ va_list argptr;
+ va_start(argptr, format);
+ wxVLogTrace(TEST_MASK, format, argptr);
+ va_end(argptr);
+}
+
+} // anonymous namespace
+
void LogTestCase::Trace()
{
- static const char *TEST_MASK = "test";
+ // we use wxLogTrace() or wxVLogTrace() from inside TraceTest()
+ // interchangeably here, it shouldn't make any difference
wxLogTrace(TEST_MASK, "Not shown");
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
wxLog::AddTraceMask(TEST_MASK);
- wxLogTrace(TEST_MASK, "Shown");
+ TraceTest("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");
+ TraceTest("Not shown again");
CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
}
}
#endif // WXWIN_COMPATIBILITY_2_8
+
+void LogTestCase::SysError()
+{
+ wxString s;
+
+ wxLogSysError(17, "Error");
+ CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Error (", &s) );
+ WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s), s.StartsWith("error 17") );
+
+ // The last error code seems to be set somewhere in MinGW CRT as its value
+ // is just not what we expect (ERROR_INVALID_PARAMETER instead of 0 and 0
+ // instead of ERROR_FILE_NOT_FOUND) so exclude the tests which rely on last
+ // error being preserved for this compiler.
+#ifndef __MINGW32__
+ wxLogSysError("Success");
+ CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Success (", &s) );
+ WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s), s.StartsWith("error 0") );
+
+ wxOpen("no-such-file", 0, 0);
+ wxLogSysError("Not found");
+ CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Not found (", &s) );
+ WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s), s.StartsWith("error 2") );
+#endif // __MINGW32__
+}
+