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