2007-03-26 17:12:13 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Name: tests/intl/intltest.cpp
|
|
|
|
|
// Purpose: wxLocale unit test
|
|
|
|
|
// Author: Vaclav Slavik
|
|
|
|
|
// Created: 2007-03-26
|
|
|
|
|
// Copyright: (c) 2007 Vaclav Slavik
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// headers
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
|
#include "wx/wx.h"
|
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
|
|
#include "wx/intl.h"
|
|
|
|
|
|
|
|
|
|
#if wxUSE_INTL
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// test class
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
class IntlTestCase : public CppUnit::TestCase
|
|
|
|
|
{
|
|
|
|
|
public:
|
2009-03-16 11:47:39 -04:00
|
|
|
|
IntlTestCase() { m_locale=NULL; }
|
2007-03-26 17:12:13 -04:00
|
|
|
|
|
2018-07-29 05:09:17 -04:00
|
|
|
|
virtual void setUp() wxOVERRIDE;
|
|
|
|
|
virtual void tearDown() wxOVERRIDE;
|
2007-03-26 17:12:13 -04:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
CPPUNIT_TEST_SUITE( IntlTestCase );
|
2013-07-06 18:48:20 -04:00
|
|
|
|
CPPUNIT_TEST( RestoreLocale );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
CPPUNIT_TEST( Domain );
|
|
|
|
|
CPPUNIT_TEST( Headers );
|
2009-04-03 11:23:26 -04:00
|
|
|
|
CPPUNIT_TEST( DateTimeFmtFrench );
|
2011-04-06 10:37:27 -04:00
|
|
|
|
CPPUNIT_TEST( IsAvailable );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
2013-07-06 18:48:20 -04:00
|
|
|
|
void RestoreLocale();
|
2007-03-26 17:12:13 -04:00
|
|
|
|
void Domain();
|
|
|
|
|
void Headers();
|
2009-04-03 11:23:26 -04:00
|
|
|
|
void DateTimeFmtFrench();
|
2011-04-06 10:37:27 -04:00
|
|
|
|
void IsAvailable();
|
2007-03-26 17:12:13 -04:00
|
|
|
|
|
2013-07-06 18:48:20 -04:00
|
|
|
|
static wxString GetDecimalPoint()
|
|
|
|
|
{
|
|
|
|
|
return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 17:12:13 -04:00
|
|
|
|
wxLocale *m_locale;
|
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
|
wxDECLARE_NO_COPY_CLASS(IntlTestCase);
|
2007-03-26 17:12:13 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase );
|
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
|
// also include in its own registry so that these tests can be run alone
|
2007-03-26 17:12:13 -04:00
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" );
|
|
|
|
|
|
|
|
|
|
void IntlTestCase::setUp()
|
|
|
|
|
{
|
2009-03-29 13:15:43 -04:00
|
|
|
|
// Check that French locale is supported, this test doesn't work without it
|
|
|
|
|
// and all the other function need to check whether m_locale is non-NULL
|
|
|
|
|
// before continuing
|
|
|
|
|
if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
|
|
|
|
|
return;
|
2009-03-16 11:47:39 -04:00
|
|
|
|
|
2007-03-26 17:12:13 -04:00
|
|
|
|
wxLocale::AddCatalogLookupPathPrefix("./intl");
|
|
|
|
|
|
|
|
|
|
m_locale = new wxLocale;
|
2009-03-29 13:15:43 -04:00
|
|
|
|
CPPUNIT_ASSERT( m_locale );
|
|
|
|
|
|
2007-03-26 17:12:13 -04:00
|
|
|
|
// don't load default catalog, it may be unavailable:
|
2010-04-16 07:29:22 -04:00
|
|
|
|
bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT);
|
2007-03-26 17:12:13 -04:00
|
|
|
|
CPPUNIT_ASSERT( loaded );
|
|
|
|
|
|
|
|
|
|
m_locale->AddCatalog("internat");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IntlTestCase::tearDown()
|
|
|
|
|
{
|
2009-03-16 11:47:39 -04:00
|
|
|
|
if (m_locale)
|
|
|
|
|
{
|
|
|
|
|
delete m_locale;
|
|
|
|
|
m_locale = NULL;
|
|
|
|
|
}
|
2007-03-26 17:12:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 18:48:20 -04:00
|
|
|
|
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() );
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 17:12:13 -04:00
|
|
|
|
void IntlTestCase::Domain()
|
|
|
|
|
{
|
2009-03-16 11:47:39 -04:00
|
|
|
|
if (!m_locale)
|
2009-03-29 13:15:43 -04:00
|
|
|
|
return;
|
2009-03-16 11:47:39 -04:00
|
|
|
|
|
2007-03-26 17:12:13 -04:00
|
|
|
|
// _() searches all domains by default:
|
2008-07-18 19:07:23 -04:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
|
|
|
|
|
// search in our domain only:
|
2008-07-18 19:07:23 -04:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
|
|
|
|
|
// search in a domain that doesn't have this string:
|
2008-07-18 19:07:23 -04:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IntlTestCase::Headers()
|
|
|
|
|
{
|
2009-03-29 13:15:43 -04:00
|
|
|
|
if ( !m_locale )
|
|
|
|
|
return;
|
2009-03-16 11:47:39 -04:00
|
|
|
|
|
2008-07-18 19:07:23 -04:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale->GetHeaderValue("Project-Id-Version") );
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale->GetHeaderValue("POT-Creation-Date") );
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale->GetHeaderValue("PO-Revision-Date") );
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale->GetHeaderValue("Last-Translator") );
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "1.0", m_locale->GetHeaderValue("MIME-Version") );
|
2012-01-20 18:18:29 -05:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale->GetHeaderValue("Content-Type") );
|
2008-07-18 19:07:23 -04:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "8bit", m_locale->GetHeaderValue("Content-Transfer-Encoding") );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
|
|
|
|
|
// check that it fails with a bogus domain:
|
2008-07-18 19:07:23 -04:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
|
|
|
|
|
// and that it fails for nonexisting header:
|
2008-07-18 19:07:23 -04:00
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
|
2007-03-26 17:12:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-04 08:09:54 -04:00
|
|
|
|
static wxString
|
|
|
|
|
NormalizeFormat(const wxString& fmtOrig)
|
2009-03-29 13:15:43 -04:00
|
|
|
|
{
|
2009-04-04 08:09:54 -04:00
|
|
|
|
wxString fmt(fmtOrig);
|
2009-03-29 13:15:43 -04:00
|
|
|
|
|
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
|
// glibc uses some extensions in its formats which we need to convert to
|
|
|
|
|
// standard form
|
2009-04-04 08:09:54 -04:00
|
|
|
|
fmt.Replace("%T", "%H:%M:%S");
|
|
|
|
|
fmt.Replace("%e", "%d");
|
2009-03-29 13:15:43 -04:00
|
|
|
|
#endif // __GLIBC__
|
|
|
|
|
|
2009-04-04 08:09:54 -04:00
|
|
|
|
return fmt;
|
2009-03-29 13:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-04 08:09:54 -04:00
|
|
|
|
#define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
|
|
|
|
|
if ( !actual.empty() ) \
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
|
|
|
|
|
|
2009-04-03 11:23:26 -04:00
|
|
|
|
void IntlTestCase::DateTimeFmtFrench()
|
2009-03-29 13:15:43 -04:00
|
|
|
|
{
|
|
|
|
|
if ( !m_locale )
|
|
|
|
|
return;
|
|
|
|
|
|
2009-03-30 17:14:09 -04:00
|
|
|
|
#ifdef __GLIBC__
|
2012-02-20 05:59:37 -05:00
|
|
|
|
// 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
|
2009-03-30 18:23:05 -04:00
|
|
|
|
static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
|
2012-02-20 05:59:37 -05:00
|
|
|
|
#endif
|
2009-04-03 11:23:26 -04:00
|
|
|
|
static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y";
|
2020-10-19 15:14:27 -04:00
|
|
|
|
static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %H:%M:%S";
|
2009-03-30 17:14:09 -04:00
|
|
|
|
#else
|
2009-04-01 17:17:35 -04:00
|
|
|
|
static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
|
2009-04-03 11:23:26 -04:00
|
|
|
|
static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y";
|
|
|
|
|
static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
|
2009-03-30 17:14:09 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
2009-04-04 08:09:54 -04:00
|
|
|
|
WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT,
|
2017-12-09 21:28:18 -05:00
|
|
|
|
wxLocale::GetInfo(wxLOCALE_SHORT_DATE_FMT) );
|
2009-04-04 08:09:54 -04:00
|
|
|
|
WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT,
|
2017-12-09 21:28:18 -05:00
|
|
|
|
wxLocale::GetInfo(wxLOCALE_LONG_DATE_FMT) );
|
2017-12-09 21:02:16 -05:00
|
|
|
|
|
2020-10-19 15:14:27 -04:00
|
|
|
|
wxString fmtDT = wxLocale::GetInfo(wxLOCALE_DATE_TIME_FMT);
|
|
|
|
|
INFO("French date and time format is \"" << fmtDT << "\"");
|
|
|
|
|
|
2017-12-09 21:02:16 -05:00
|
|
|
|
#ifdef __WXOSX__
|
|
|
|
|
// Things are difficult to test under macOS as the format keeps changing,
|
|
|
|
|
// e.g. at some time between 10.10 and 10.12 a new " <20> " string appeared in
|
|
|
|
|
// its middle, so test it piece-wise and hope it doesn't change too much.
|
|
|
|
|
CHECK( fmtDT.StartsWith("%A %d %B %Y") );
|
|
|
|
|
CHECK( fmtDT.EndsWith("%H:%M:%S") );
|
|
|
|
|
#else
|
2020-10-19 15:14:27 -04:00
|
|
|
|
// Some glic versions have " %Z" at the end of the locale and some don't.
|
|
|
|
|
// The test is still useful if we just ignore this difference.
|
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
|
wxString fmtDTWithoutTZ;
|
|
|
|
|
if ( fmtDT.EndsWith(" %Z", &fmtDTWithoutTZ) )
|
|
|
|
|
fmtDT.swap(fmtDTWithoutTZ);
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-12-09 21:02:16 -05:00
|
|
|
|
WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT, fmtDT );
|
|
|
|
|
#endif
|
2009-04-04 08:09:54 -04:00
|
|
|
|
WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
|
2017-12-09 21:28:18 -05:00
|
|
|
|
wxLocale::GetInfo(wxLOCALE_TIME_FMT) );
|
2009-04-03 11:23:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-06 10:37:27 -04:00
|
|
|
|
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) );
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 17:12:13 -04:00
|
|
|
|
#endif // wxUSE_INTL
|