1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/intl/intltest.cpp
3 // Purpose: wxLocale unit test
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2007 Vaclav Slavik
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 class IntlTestCase
: public CppUnit::TestCase
35 IntlTestCase() { m_locale
=NULL
; }
38 virtual void tearDown();
41 CPPUNIT_TEST_SUITE( IntlTestCase
);
42 CPPUNIT_TEST( RestoreLocale
);
43 CPPUNIT_TEST( Domain
);
44 CPPUNIT_TEST( Headers
);
45 CPPUNIT_TEST( DateTimeFmtFrench
);
46 CPPUNIT_TEST( DateTimeFmtC
);
47 CPPUNIT_TEST( IsAvailable
);
48 CPPUNIT_TEST_SUITE_END();
53 void DateTimeFmtFrench();
57 static wxString
GetDecimalPoint()
59 return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
64 DECLARE_NO_COPY_CLASS(IntlTestCase
)
67 // register in the unnamed registry so that these tests are run by default
68 CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase
);
70 // also include in its own registry so that these tests can be run alone
71 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase
, "IntlTestCase" );
73 void IntlTestCase::setUp()
75 // Check that French locale is supported, this test doesn't work without it
76 // and all the other function need to check whether m_locale is non-NULL
78 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH
) )
81 wxLocale::AddCatalogLookupPathPrefix("./intl");
83 m_locale
= new wxLocale
;
84 CPPUNIT_ASSERT( m_locale
);
86 // don't load default catalog, it may be unavailable:
87 bool loaded
= m_locale
->Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
);
88 CPPUNIT_ASSERT( loaded
);
90 m_locale
->AddCatalog("internat");
93 void IntlTestCase::tearDown()
102 void IntlTestCase::RestoreLocale()
107 // We must be using the French locale now, it was changed in setUp().
108 CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
110 // Switch to the English locale.
112 wxLocale
locEn(wxLANGUAGE_ENGLISH
);
113 CPPUNIT_ASSERT_EQUAL( ".", GetDecimalPoint() );
116 // Verify that after destroying the English locale object, French locale is
118 CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
121 void IntlTestCase::Domain()
126 // _() searches all domains by default:
127 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
129 // search in our domain only:
130 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
132 // search in a domain that doesn't have this string:
133 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
136 void IntlTestCase::Headers()
141 CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale
->GetHeaderValue("Project-Id-Version") );
142 CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale
->GetHeaderValue("POT-Creation-Date") );
143 CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale
->GetHeaderValue("PO-Revision-Date") );
144 CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale
->GetHeaderValue("Last-Translator") );
145 CPPUNIT_ASSERT_EQUAL( "1.0", m_locale
->GetHeaderValue("MIME-Version") );
146 CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale
->GetHeaderValue("Content-Type") );
147 CPPUNIT_ASSERT_EQUAL( "8bit", m_locale
->GetHeaderValue("Content-Transfer-Encoding") );
149 // check that it fails with a bogus domain:
150 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("POT-Creation-Date", "Bogus") );
152 // and that it fails for nonexisting header:
153 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("X-Not-Here") );
157 NormalizeFormat(const wxString
& fmtOrig
)
159 wxString
fmt(fmtOrig
);
162 // glibc uses some extensions in its formats which we need to convert to
164 fmt
.Replace("%T", "%H:%M:%S");
165 fmt
.Replace("%e", "%d");
171 #define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
172 if ( !actual.empty() ) \
173 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
175 void IntlTestCase::DateTimeFmtFrench()
181 // Versions of glibc up to 2.7 wrongly used periods for French locale
183 #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8
184 static const char *FRENCH_DATE_FMT
= "%d/%m/%Y";
186 static const char *FRENCH_DATE_FMT
= "%d.%m.%Y";
188 static const char *FRENCH_LONG_DATE_FMT
= "%a %d %b %Y";
189 static const char *FRENCH_DATE_TIME_FMT
= "%a %d %b %Y %H:%M:%S %Z";
191 static const char *FRENCH_DATE_FMT
= "%d/%m/%Y";
192 static const char *FRENCH_LONG_DATE_FMT
= "%A %d %B %Y";
194 static const char *FRENCH_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
196 static const char *FRENCH_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
200 WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT
,
201 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
202 WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT
,
203 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
204 WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT
,
205 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
206 WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
207 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );
210 void IntlTestCase::DateTimeFmtC()
212 // again, glibc uses different defaults
214 static const char *C_DATE_FMT
= "%m/%d/%y";
215 static const char *C_LONG_DATE_FMT
= "%a %b %d %Y";
216 static const char *C_DATE_TIME_FMT
= "%a %b %d %H:%M:%S %Y";
218 static const char *C_DATE_FMT
= "%d/%m/%Y";
219 static const char *C_LONG_DATE_FMT
= "%A %d %B %Y";
221 static const char *C_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
223 static const char *C_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
227 setlocale(LC_ALL
, "C");
229 WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT
,
230 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
231 WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT
,
232 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
233 WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT
,
234 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
235 WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
236 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );
239 void IntlTestCase::IsAvailable()
241 const wxString
origLocale(setlocale(LC_ALL
, NULL
));
243 // Calling IsAvailable() shouldn't change the locale.
244 wxLocale::IsAvailable(wxLANGUAGE_ENGLISH
);
246 CPPUNIT_ASSERT_EQUAL( origLocale
, setlocale(LC_ALL
, NULL
) );