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( Domain
);
43 CPPUNIT_TEST( Headers
);
44 CPPUNIT_TEST( DateTimeFmtFrench
);
45 CPPUNIT_TEST( DateTimeFmtC
);
46 CPPUNIT_TEST( IsAvailable
);
47 CPPUNIT_TEST_SUITE_END();
51 void DateTimeFmtFrench();
57 DECLARE_NO_COPY_CLASS(IntlTestCase
)
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase
);
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase
, "IntlTestCase" );
66 void IntlTestCase::setUp()
68 // Check that French locale is supported, this test doesn't work without it
69 // and all the other function need to check whether m_locale is non-NULL
71 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH
) )
74 wxLocale::AddCatalogLookupPathPrefix("./intl");
76 m_locale
= new wxLocale
;
77 CPPUNIT_ASSERT( m_locale
);
79 // don't load default catalog, it may be unavailable:
80 bool loaded
= m_locale
->Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
);
81 CPPUNIT_ASSERT( loaded
);
83 m_locale
->AddCatalog("internat");
86 void IntlTestCase::tearDown()
95 void IntlTestCase::Domain()
100 // _() searches all domains by default:
101 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
103 // search in our domain only:
104 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
106 // search in a domain that doesn't have this string:
107 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
110 void IntlTestCase::Headers()
115 CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale
->GetHeaderValue("Project-Id-Version") );
116 CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale
->GetHeaderValue("POT-Creation-Date") );
117 CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale
->GetHeaderValue("PO-Revision-Date") );
118 CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale
->GetHeaderValue("Last-Translator") );
119 CPPUNIT_ASSERT_EQUAL( "1.0", m_locale
->GetHeaderValue("MIME-Version") );
120 CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale
->GetHeaderValue("Content-Type") );
121 CPPUNIT_ASSERT_EQUAL( "8bit", m_locale
->GetHeaderValue("Content-Transfer-Encoding") );
123 // check that it fails with a bogus domain:
124 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("POT-Creation-Date", "Bogus") );
126 // and that it fails for nonexisting header:
127 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("X-Not-Here") );
131 NormalizeFormat(const wxString
& fmtOrig
)
133 wxString
fmt(fmtOrig
);
136 // glibc uses some extensions in its formats which we need to convert to
138 fmt
.Replace("%T", "%H:%M:%S");
139 fmt
.Replace("%e", "%d");
145 #define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
146 if ( !actual.empty() ) \
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
149 void IntlTestCase::DateTimeFmtFrench()
155 // Versions of glibc up to 2.7 wrongly used periods for French locale
157 #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8
158 static const char *FRENCH_DATE_FMT
= "%d/%m/%Y";
160 static const char *FRENCH_DATE_FMT
= "%d.%m.%Y";
162 static const char *FRENCH_LONG_DATE_FMT
= "%a %d %b %Y";
163 static const char *FRENCH_DATE_TIME_FMT
= "%a %d %b %Y %H:%M:%S %Z";
165 static const char *FRENCH_DATE_FMT
= "%d/%m/%Y";
166 static const char *FRENCH_LONG_DATE_FMT
= "%A %d %B %Y";
168 static const char *FRENCH_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
170 static const char *FRENCH_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
174 WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT
,
175 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
176 WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT
,
177 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
178 WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT
,
179 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
180 WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
181 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );
184 void IntlTestCase::DateTimeFmtC()
186 // again, glibc uses different defaults
188 static const char *C_DATE_FMT
= "%m/%d/%y";
189 static const char *C_LONG_DATE_FMT
= "%a %b %d %Y";
190 static const char *C_DATE_TIME_FMT
= "%a %b %d %H:%M:%S %Y";
192 static const char *C_DATE_FMT
= "%d/%m/%Y";
193 static const char *C_LONG_DATE_FMT
= "%A %d %B %Y";
195 static const char *C_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
197 static const char *C_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
201 setlocale(LC_ALL
, "C");
203 WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT
,
204 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
205 WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT
,
206 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
207 WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT
,
208 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
209 WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
210 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );
213 void IntlTestCase::IsAvailable()
215 const wxString
origLocale(setlocale(LC_ALL
, NULL
));
217 // Calling IsAvailable() shouldn't change the locale.
218 wxLocale::IsAvailable(wxLANGUAGE_ENGLISH
);
220 CPPUNIT_ASSERT_EQUAL( origLocale
, setlocale(LC_ALL
, NULL
) );