1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/intl/intltest.cpp
3 // Purpose: wxLocale unit test
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2007 Vaclav Slavik
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class IntlTestCase
: public CppUnit::TestCase
34 IntlTestCase() { m_locale
=NULL
; }
37 virtual void tearDown();
40 CPPUNIT_TEST_SUITE( IntlTestCase
);
41 CPPUNIT_TEST( RestoreLocale
);
42 CPPUNIT_TEST( Domain
);
43 CPPUNIT_TEST( Headers
);
44 CPPUNIT_TEST( DateTimeFmtFrench
);
45 CPPUNIT_TEST( DateTimeFmtC
);
46 CPPUNIT_TEST( IsAvailable
);
47 CPPUNIT_TEST_SUITE_END();
52 void DateTimeFmtFrench();
56 static wxString
GetDecimalPoint()
58 return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
63 DECLARE_NO_COPY_CLASS(IntlTestCase
)
66 // register in the unnamed registry so that these tests are run by default
67 CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase
);
69 // also include in its own registry so that these tests can be run alone
70 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase
, "IntlTestCase" );
72 void IntlTestCase::setUp()
74 // Check that French locale is supported, this test doesn't work without it
75 // and all the other function need to check whether m_locale is non-NULL
77 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH
) )
80 wxLocale::AddCatalogLookupPathPrefix("./intl");
82 m_locale
= new wxLocale
;
83 CPPUNIT_ASSERT( m_locale
);
85 // don't load default catalog, it may be unavailable:
86 bool loaded
= m_locale
->Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
);
87 CPPUNIT_ASSERT( loaded
);
89 m_locale
->AddCatalog("internat");
92 void IntlTestCase::tearDown()
101 void IntlTestCase::RestoreLocale()
106 // We must be using the French locale now, it was changed in setUp().
107 CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
109 // Switch to the English locale.
111 wxLocale
locEn(wxLANGUAGE_ENGLISH
);
112 CPPUNIT_ASSERT_EQUAL( ".", GetDecimalPoint() );
115 // Verify that after destroying the English locale object, French locale is
117 CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
120 void IntlTestCase::Domain()
125 // _() searches all domains by default:
126 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
128 // search in our domain only:
129 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
131 // search in a domain that doesn't have this string:
132 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
135 void IntlTestCase::Headers()
140 CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale
->GetHeaderValue("Project-Id-Version") );
141 CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale
->GetHeaderValue("POT-Creation-Date") );
142 CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale
->GetHeaderValue("PO-Revision-Date") );
143 CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale
->GetHeaderValue("Last-Translator") );
144 CPPUNIT_ASSERT_EQUAL( "1.0", m_locale
->GetHeaderValue("MIME-Version") );
145 CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale
->GetHeaderValue("Content-Type") );
146 CPPUNIT_ASSERT_EQUAL( "8bit", m_locale
->GetHeaderValue("Content-Transfer-Encoding") );
148 // check that it fails with a bogus domain:
149 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("POT-Creation-Date", "Bogus") );
151 // and that it fails for nonexisting header:
152 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("X-Not-Here") );
156 NormalizeFormat(const wxString
& fmtOrig
)
158 wxString
fmt(fmtOrig
);
161 // glibc uses some extensions in its formats which we need to convert to
163 fmt
.Replace("%T", "%H:%M:%S");
164 fmt
.Replace("%e", "%d");
170 #define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
171 if ( !actual.empty() ) \
172 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
174 void IntlTestCase::DateTimeFmtFrench()
180 // Versions of glibc up to 2.7 wrongly used periods for French locale
182 #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8
183 static const char *FRENCH_DATE_FMT
= "%d/%m/%Y";
185 static const char *FRENCH_DATE_FMT
= "%d.%m.%Y";
187 static const char *FRENCH_LONG_DATE_FMT
= "%a %d %b %Y";
188 static const char *FRENCH_DATE_TIME_FMT
= "%a %d %b %Y %H:%M:%S %Z";
190 static const char *FRENCH_DATE_FMT
= "%d/%m/%Y";
191 static const char *FRENCH_LONG_DATE_FMT
= "%A %d %B %Y";
193 static const char *FRENCH_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
195 static const char *FRENCH_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
199 WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT
,
200 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
201 WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT
,
202 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
203 WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT
,
204 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
205 WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
206 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );
209 void IntlTestCase::DateTimeFmtC()
211 // again, glibc uses different defaults
213 static const char *C_DATE_FMT
= "%m/%d/%y";
214 static const char *C_LONG_DATE_FMT
= "%a %b %d %Y";
215 static const char *C_DATE_TIME_FMT
= "%a %b %d %H:%M:%S %Y";
217 static const char *C_DATE_FMT
= "%d/%m/%Y";
218 static const char *C_LONG_DATE_FMT
= "%A %d %B %Y";
220 static const char *C_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
222 static const char *C_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
226 setlocale(LC_ALL
, "C");
228 WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT
,
229 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
230 WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT
,
231 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
232 WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT
,
233 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
234 WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
235 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );
238 void IntlTestCase::IsAvailable()
240 const wxString
origLocale(setlocale(LC_ALL
, NULL
));
242 // Calling IsAvailable() shouldn't change the locale.
243 wxLocale::IsAvailable(wxLANGUAGE_ENGLISH
);
245 CPPUNIT_ASSERT_EQUAL( origLocale
, setlocale(LC_ALL
, NULL
) );