X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fac09395009e17d04b8404842d77b49157e60291..92a98ecc5369edfebf13151dc9e9d4acaf326342:/tests/intl/intltest.cpp diff --git a/tests/intl/intltest.cpp b/tests/intl/intltest.cpp index 37c405490b..11f6451afe 100644 --- a/tests/intl/intltest.cpp +++ b/tests/intl/intltest.cpp @@ -3,7 +3,6 @@ // Purpose: wxLocale unit test // Author: Vaclav Slavik // Created: 2007-03-26 -// RCS-ID: $Id$ // Copyright: (c) 2007 Vaclav Slavik /////////////////////////////////////////////////////////////////////////////// @@ -39,16 +38,25 @@ public: private: CPPUNIT_TEST_SUITE( IntlTestCase ); + CPPUNIT_TEST( RestoreLocale ); CPPUNIT_TEST( Domain ); CPPUNIT_TEST( Headers ); CPPUNIT_TEST( DateTimeFmtFrench ); CPPUNIT_TEST( DateTimeFmtC ); + CPPUNIT_TEST( IsAvailable ); CPPUNIT_TEST_SUITE_END(); + void RestoreLocale(); void Domain(); void Headers(); void DateTimeFmtFrench(); void DateTimeFmtC(); + void IsAvailable(); + + static wxString GetDecimalPoint() + { + return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); + } wxLocale *m_locale; @@ -58,7 +66,7 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase ); -// 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( IntlTestCase, "IntlTestCase" ); void IntlTestCase::setUp() @@ -75,7 +83,7 @@ void IntlTestCase::setUp() CPPUNIT_ASSERT( m_locale ); // don't load default catalog, it may be unavailable: - bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_CONV_ENCODING); + bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT); CPPUNIT_ASSERT( loaded ); m_locale->AddCatalog("internat"); @@ -90,6 +98,25 @@ void IntlTestCase::tearDown() } } +void IntlTestCase::RestoreLocale() +{ + if ( !m_locale ) + return; + + // We must be using the French locale now, it was changed in setUp(). + CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() ); + + // Switch to the English locale. + { + wxLocale locEn(wxLANGUAGE_ENGLISH); + CPPUNIT_ASSERT_EQUAL( ".", GetDecimalPoint() ); + } + + // Verify that after destroying the English locale object, French locale is + // restored. + CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() ); +} + void IntlTestCase::Domain() { if (!m_locale) @@ -115,7 +142,7 @@ void IntlTestCase::Headers() CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale->GetHeaderValue("PO-Revision-Date") ); CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin ", m_locale->GetHeaderValue("Last-Translator") ); CPPUNIT_ASSERT_EQUAL( "1.0", m_locale->GetHeaderValue("MIME-Version") ); - CPPUNIT_ASSERT_EQUAL( "text/plain; charset=iso-8859-1", m_locale->GetHeaderValue("Content-Type") ); + CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale->GetHeaderValue("Content-Type") ); CPPUNIT_ASSERT_EQUAL( "8bit", m_locale->GetHeaderValue("Content-Transfer-Encoding") ); // check that it fails with a bogus domain: @@ -125,49 +152,57 @@ void IntlTestCase::Headers() CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") ); } -static void -CompareFormats(const char *msg, const wxString& expected, wxString actual) +static wxString +NormalizeFormat(const wxString& fmtOrig) { - if ( actual.empty() ) - { - // this means that GetInfo() failed which can happen, just ignore - return; - } + wxString fmt(fmtOrig); #ifdef __GLIBC__ // glibc uses some extensions in its formats which we need to convert to // standard form - actual.Replace("%T", "%H:%M:%S"); - actual.Replace("%e", "%d"); + fmt.Replace("%T", "%H:%M:%S"); + fmt.Replace("%e", "%d"); #endif // __GLIBC__ - CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, expected, actual ); + return fmt; } +#define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \ + if ( !actual.empty() ) \ + CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual)) + void IntlTestCase::DateTimeFmtFrench() { if ( !m_locale ) return; #ifdef __GLIBC__ - // glibc also uses dots for French locale separator for some reason (the - // standard format uses slashes) + // Versions of glibc up to 2.7 wrongly used periods for French locale + // separator. +#if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8 + static const char *FRENCH_DATE_FMT = "%d/%m/%Y"; +#else static const char *FRENCH_DATE_FMT = "%d.%m.%Y"; +#endif static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y"; - static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %T %Z"; + static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %H:%M:%S %Z"; #else static const char *FRENCH_DATE_FMT = "%d/%m/%Y"; static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y"; +#ifdef __WXOSX__ + static const char *FRENCH_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S"; +#else static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S"; +#endif #endif - CompareFormats( "French short date", FRENCH_DATE_FMT, + WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT, m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) ); - CompareFormats( "French long date", FRENCH_LONG_DATE_FMT, + WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT, m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) ); - CompareFormats( "French date and time", FRENCH_DATE_TIME_FMT, + WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT, m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) ); - CompareFormats( "French time", "%H:%M:%S", + WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S", m_locale->GetInfo(wxLOCALE_TIME_FMT) ); } @@ -181,19 +216,33 @@ void IntlTestCase::DateTimeFmtC() #else static const char *C_DATE_FMT = "%d/%m/%Y"; static const char *C_LONG_DATE_FMT = "%A %d %B %Y"; +#ifdef __WXOSX__ + static const char *C_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S"; +#else static const char *C_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S"; +#endif #endif setlocale(LC_ALL, "C"); - CompareFormats( "C short date", C_DATE_FMT, + WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT, m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) ); - CompareFormats( "C long date", C_LONG_DATE_FMT, + WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT, m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) ); - CompareFormats( "C date and time", C_DATE_TIME_FMT, + WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT, m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) ); - CompareFormats( "C time", "%H:%M:%S", + WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S", m_locale->GetInfo(wxLOCALE_TIME_FMT) ); } +void IntlTestCase::IsAvailable() +{ + const wxString origLocale(setlocale(LC_ALL, NULL)); + + // Calling IsAvailable() shouldn't change the locale. + wxLocale::IsAvailable(wxLANGUAGE_ENGLISH); + + CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) ); +} + #endif // wxUSE_INTL