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_SUITE_END();
50 void DateTimeFmtFrench();
55 DECLARE_NO_COPY_CLASS(IntlTestCase
)
58 // register in the unnamed registry so that these tests are run by default
59 CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase
);
61 // also include in it's own registry so that these tests can be run alone
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase
, "IntlTestCase" );
64 void IntlTestCase::setUp()
66 // Check that French locale is supported, this test doesn't work without it
67 // and all the other function need to check whether m_locale is non-NULL
69 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH
) )
72 wxLocale::AddCatalogLookupPathPrefix("./intl");
74 m_locale
= new wxLocale
;
75 CPPUNIT_ASSERT( m_locale
);
77 // don't load default catalog, it may be unavailable:
78 bool loaded
= m_locale
->Init(wxLANGUAGE_FRENCH
, wxLOCALE_DONT_LOAD_DEFAULT
);
79 CPPUNIT_ASSERT( loaded
);
81 m_locale
->AddCatalog("internat");
84 void IntlTestCase::tearDown()
93 void IntlTestCase::Domain()
98 // _() searches all domains by default:
99 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
101 // search in our domain only:
102 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
104 // search in a domain that doesn't have this string:
105 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
108 void IntlTestCase::Headers()
113 CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale
->GetHeaderValue("Project-Id-Version") );
114 CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale
->GetHeaderValue("POT-Creation-Date") );
115 CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale
->GetHeaderValue("PO-Revision-Date") );
116 CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale
->GetHeaderValue("Last-Translator") );
117 CPPUNIT_ASSERT_EQUAL( "1.0", m_locale
->GetHeaderValue("MIME-Version") );
118 CPPUNIT_ASSERT_EQUAL( "text/plain; charset=iso-8859-1", m_locale
->GetHeaderValue("Content-Type") );
119 CPPUNIT_ASSERT_EQUAL( "8bit", m_locale
->GetHeaderValue("Content-Transfer-Encoding") );
121 // check that it fails with a bogus domain:
122 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("POT-Creation-Date", "Bogus") );
124 // and that it fails for nonexisting header:
125 CPPUNIT_ASSERT_EQUAL( "", m_locale
->GetHeaderValue("X-Not-Here") );
129 NormalizeFormat(const wxString
& fmtOrig
)
131 wxString
fmt(fmtOrig
);
134 // glibc uses some extensions in its formats which we need to convert to
136 fmt
.Replace("%T", "%H:%M:%S");
137 fmt
.Replace("%e", "%d");
143 #define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
144 if ( !actual.empty() ) \
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
147 void IntlTestCase::DateTimeFmtFrench()
153 // glibc also uses dots for French locale separator for some reason (the
154 // standard format uses slashes)
155 static const char *FRENCH_DATE_FMT
= "%d.%m.%Y";
156 static const char *FRENCH_LONG_DATE_FMT
= "%a %d %b %Y";
157 static const char *FRENCH_DATE_TIME_FMT
= "%a %d %b %Y %H:%M:%S %Z";
159 static const char *FRENCH_DATE_FMT
= "%d/%m/%Y";
160 static const char *FRENCH_LONG_DATE_FMT
= "%A %d %B %Y";
162 static const char *FRENCH_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
164 static const char *FRENCH_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
168 WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT
,
169 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
170 WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT
,
171 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
172 WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT
,
173 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
174 WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
175 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );
178 void IntlTestCase::DateTimeFmtC()
180 // again, glibc uses different defaults
182 static const char *C_DATE_FMT
= "%m/%d/%y";
183 static const char *C_LONG_DATE_FMT
= "%a %b %d %Y";
184 static const char *C_DATE_TIME_FMT
= "%a %b %d %H:%M:%S %Y";
186 static const char *C_DATE_FMT
= "%d/%m/%Y";
187 static const char *C_LONG_DATE_FMT
= "%A %d %B %Y";
189 static const char *C_DATE_TIME_FMT
= "%A %d %B %Y %H:%M:%S";
191 static const char *C_DATE_TIME_FMT
= "%d/%m/%Y %H:%M:%S";
195 setlocale(LC_ALL
, "C");
197 WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT
,
198 m_locale
->GetInfo(wxLOCALE_SHORT_DATE_FMT
) );
199 WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT
,
200 m_locale
->GetInfo(wxLOCALE_LONG_DATE_FMT
) );
201 WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT
,
202 m_locale
->GetInfo(wxLOCALE_DATE_TIME_FMT
) );
203 WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
204 m_locale
->GetInfo(wxLOCALE_TIME_FMT
) );