]>
Commit | Line | Data |
---|---|---|
02f935fb VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/intl/intltest.cpp | |
3 | // Purpose: wxLocale unit test | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2007-03-26 | |
02f935fb VS |
6 | // Copyright: (c) 2007 Vaclav Slavik |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | // ---------------------------------------------------------------------------- | |
10 | // headers | |
11 | // ---------------------------------------------------------------------------- | |
12 | ||
13 | #include "testprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif // WX_PRECOMP | |
22 | ||
23 | #include "wx/intl.h" | |
24 | ||
25 | #if wxUSE_INTL | |
26 | ||
27 | // ---------------------------------------------------------------------------- | |
28 | // test class | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
31 | class IntlTestCase : public CppUnit::TestCase | |
32 | { | |
33 | public: | |
7ab4bb30 | 34 | IntlTestCase() { m_locale=NULL; } |
02f935fb VS |
35 | |
36 | virtual void setUp(); | |
37 | virtual void tearDown(); | |
38 | ||
39 | private: | |
40 | CPPUNIT_TEST_SUITE( IntlTestCase ); | |
700256bb | 41 | CPPUNIT_TEST( RestoreLocale ); |
02f935fb VS |
42 | CPPUNIT_TEST( Domain ); |
43 | CPPUNIT_TEST( Headers ); | |
fac09395 VZ |
44 | CPPUNIT_TEST( DateTimeFmtFrench ); |
45 | CPPUNIT_TEST( DateTimeFmtC ); | |
fd1c361c | 46 | CPPUNIT_TEST( IsAvailable ); |
02f935fb VS |
47 | CPPUNIT_TEST_SUITE_END(); |
48 | ||
700256bb | 49 | void RestoreLocale(); |
02f935fb VS |
50 | void Domain(); |
51 | void Headers(); | |
fac09395 VZ |
52 | void DateTimeFmtFrench(); |
53 | void DateTimeFmtC(); | |
fd1c361c | 54 | void IsAvailable(); |
02f935fb | 55 | |
700256bb VZ |
56 | static wxString GetDecimalPoint() |
57 | { | |
58 | return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); | |
59 | } | |
60 | ||
02f935fb VS |
61 | wxLocale *m_locale; |
62 | ||
63 | DECLARE_NO_COPY_CLASS(IntlTestCase) | |
64 | }; | |
65 | ||
66 | // register in the unnamed registry so that these tests are run by default | |
67 | CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase ); | |
68 | ||
e3778b4d | 69 | // also include in its own registry so that these tests can be run alone |
02f935fb VS |
70 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" ); |
71 | ||
72 | void IntlTestCase::setUp() | |
73 | { | |
89a7e1ff VZ |
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 | |
76 | // before continuing | |
77 | if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) ) | |
78 | return; | |
7ab4bb30 | 79 | |
02f935fb VS |
80 | wxLocale::AddCatalogLookupPathPrefix("./intl"); |
81 | ||
82 | m_locale = new wxLocale; | |
89a7e1ff VZ |
83 | CPPUNIT_ASSERT( m_locale ); |
84 | ||
02f935fb | 85 | // don't load default catalog, it may be unavailable: |
3acf8a8d | 86 | bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT); |
02f935fb VS |
87 | CPPUNIT_ASSERT( loaded ); |
88 | ||
89 | m_locale->AddCatalog("internat"); | |
90 | } | |
91 | ||
92 | void IntlTestCase::tearDown() | |
93 | { | |
7ab4bb30 FM |
94 | if (m_locale) |
95 | { | |
96 | delete m_locale; | |
97 | m_locale = NULL; | |
98 | } | |
02f935fb VS |
99 | } |
100 | ||
700256bb VZ |
101 | void IntlTestCase::RestoreLocale() |
102 | { | |
103 | if ( !m_locale ) | |
104 | return; | |
105 | ||
106 | // We must be using the French locale now, it was changed in setUp(). | |
107 | CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() ); | |
108 | ||
109 | // Switch to the English locale. | |
110 | { | |
111 | wxLocale locEn(wxLANGUAGE_ENGLISH); | |
112 | CPPUNIT_ASSERT_EQUAL( ".", GetDecimalPoint() ); | |
113 | } | |
114 | ||
115 | // Verify that after destroying the English locale object, French locale is | |
116 | // restored. | |
117 | CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() ); | |
118 | } | |
119 | ||
02f935fb VS |
120 | void IntlTestCase::Domain() |
121 | { | |
7ab4bb30 | 122 | if (!m_locale) |
89a7e1ff | 123 | return; |
7ab4bb30 | 124 | |
02f935fb | 125 | // _() searches all domains by default: |
1de532f5 | 126 | CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") ); |
02f935fb VS |
127 | |
128 | // search in our domain only: | |
1de532f5 | 129 | CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") ); |
02f935fb VS |
130 | |
131 | // search in a domain that doesn't have this string: | |
1de532f5 | 132 | CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") ); |
02f935fb VS |
133 | } |
134 | ||
135 | void IntlTestCase::Headers() | |
136 | { | |
89a7e1ff VZ |
137 | if ( !m_locale ) |
138 | return; | |
7ab4bb30 | 139 | |
1de532f5 VZ |
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") ); | |
71f44b1b | 145 | CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale->GetHeaderValue("Content-Type") ); |
1de532f5 | 146 | CPPUNIT_ASSERT_EQUAL( "8bit", m_locale->GetHeaderValue("Content-Transfer-Encoding") ); |
02f935fb VS |
147 | |
148 | // check that it fails with a bogus domain: | |
1de532f5 | 149 | CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") ); |
02f935fb VS |
150 | |
151 | // and that it fails for nonexisting header: | |
1de532f5 | 152 | CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") ); |
02f935fb VS |
153 | } |
154 | ||
df6de630 VZ |
155 | static wxString |
156 | NormalizeFormat(const wxString& fmtOrig) | |
89a7e1ff | 157 | { |
df6de630 | 158 | wxString fmt(fmtOrig); |
89a7e1ff VZ |
159 | |
160 | #ifdef __GLIBC__ | |
161 | // glibc uses some extensions in its formats which we need to convert to | |
162 | // standard form | |
df6de630 VZ |
163 | fmt.Replace("%T", "%H:%M:%S"); |
164 | fmt.Replace("%e", "%d"); | |
89a7e1ff VZ |
165 | #endif // __GLIBC__ |
166 | ||
df6de630 | 167 | return fmt; |
89a7e1ff VZ |
168 | } |
169 | ||
df6de630 VZ |
170 | #define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \ |
171 | if ( !actual.empty() ) \ | |
172 | CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual)) | |
173 | ||
fac09395 | 174 | void IntlTestCase::DateTimeFmtFrench() |
89a7e1ff VZ |
175 | { |
176 | if ( !m_locale ) | |
177 | return; | |
178 | ||
b9d22034 | 179 | #ifdef __GLIBC__ |
9362d823 VZ |
180 | // Versions of glibc up to 2.7 wrongly used periods for French locale |
181 | // separator. | |
182 | #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8 | |
183 | static const char *FRENCH_DATE_FMT = "%d/%m/%Y"; | |
184 | #else | |
d8a696fb | 185 | static const char *FRENCH_DATE_FMT = "%d.%m.%Y"; |
9362d823 | 186 | #endif |
fac09395 | 187 | static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y"; |
df6de630 | 188 | static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %H:%M:%S %Z"; |
b9d22034 | 189 | #else |
27aec6b6 | 190 | static const char *FRENCH_DATE_FMT = "%d/%m/%Y"; |
fac09395 | 191 | static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y"; |
db227bf8 SC |
192 | #ifdef __WXOSX__ |
193 | static const char *FRENCH_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S"; | |
194 | #else | |
fac09395 | 195 | static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S"; |
db227bf8 | 196 | #endif |
b9d22034 VZ |
197 | #endif |
198 | ||
df6de630 | 199 | WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT, |
d8a696fb | 200 | m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) ); |
df6de630 | 201 | WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT, |
d8a696fb | 202 | m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) ); |
df6de630 | 203 | WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT, |
89a7e1ff | 204 | m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) ); |
df6de630 | 205 | WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S", |
d8a696fb | 206 | m_locale->GetInfo(wxLOCALE_TIME_FMT) ); |
fac09395 VZ |
207 | } |
208 | ||
209 | void IntlTestCase::DateTimeFmtC() | |
210 | { | |
211 | // again, glibc uses different defaults | |
212 | #ifdef __GLIBC__ | |
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"; | |
216 | #else | |
217 | static const char *C_DATE_FMT = "%d/%m/%Y"; | |
218 | static const char *C_LONG_DATE_FMT = "%A %d %B %Y"; | |
db227bf8 SC |
219 | #ifdef __WXOSX__ |
220 | static const char *C_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S"; | |
221 | #else | |
fac09395 | 222 | static const char *C_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S"; |
db227bf8 | 223 | #endif |
fac09395 | 224 | #endif |
89a7e1ff | 225 | |
89a7e1ff VZ |
226 | setlocale(LC_ALL, "C"); |
227 | ||
df6de630 | 228 | WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT, |
d8a696fb | 229 | m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) ); |
df6de630 | 230 | WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT, |
d8a696fb | 231 | m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) ); |
df6de630 | 232 | WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT, |
89a7e1ff | 233 | m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) ); |
df6de630 | 234 | WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S", |
d8a696fb | 235 | m_locale->GetInfo(wxLOCALE_TIME_FMT) ); |
89a7e1ff VZ |
236 | } |
237 | ||
fd1c361c VZ |
238 | void IntlTestCase::IsAvailable() |
239 | { | |
240 | const wxString origLocale(setlocale(LC_ALL, NULL)); | |
241 | ||
242 | // Calling IsAvailable() shouldn't change the locale. | |
243 | wxLocale::IsAvailable(wxLANGUAGE_ENGLISH); | |
244 | ||
245 | CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) ); | |
246 | } | |
247 | ||
02f935fb | 248 | #endif // wxUSE_INTL |