]> git.saurik.com Git - wxWidgets.git/blame - tests/intl/intltest.cpp
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[wxWidgets.git] / tests / intl / intltest.cpp
CommitLineData
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
32class IntlTestCase : public CppUnit::TestCase
33{
34public:
7ab4bb30 35 IntlTestCase() { m_locale=NULL; }
02f935fb
VS
36
37 virtual void setUp();
38 virtual void tearDown();
39
40private:
41 CPPUNIT_TEST_SUITE( IntlTestCase );
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
49 void Domain();
50 void Headers();
fac09395
VZ
51 void DateTimeFmtFrench();
52 void DateTimeFmtC();
fd1c361c 53 void IsAvailable();
02f935fb
VS
54
55 wxLocale *m_locale;
56
57 DECLARE_NO_COPY_CLASS(IntlTestCase)
58};
59
60// register in the unnamed registry so that these tests are run by default
61CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase );
62
e3778b4d 63// also include in its own registry so that these tests can be run alone
02f935fb
VS
64CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" );
65
66void IntlTestCase::setUp()
67{
89a7e1ff
VZ
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
70 // before continuing
71 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
72 return;
7ab4bb30 73
02f935fb
VS
74 wxLocale::AddCatalogLookupPathPrefix("./intl");
75
76 m_locale = new wxLocale;
89a7e1ff
VZ
77 CPPUNIT_ASSERT( m_locale );
78
02f935fb 79 // don't load default catalog, it may be unavailable:
3acf8a8d 80 bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT);
02f935fb
VS
81 CPPUNIT_ASSERT( loaded );
82
83 m_locale->AddCatalog("internat");
84}
85
86void IntlTestCase::tearDown()
87{
7ab4bb30
FM
88 if (m_locale)
89 {
90 delete m_locale;
91 m_locale = NULL;
92 }
02f935fb
VS
93}
94
95void IntlTestCase::Domain()
96{
7ab4bb30 97 if (!m_locale)
89a7e1ff 98 return;
7ab4bb30 99
02f935fb 100 // _() searches all domains by default:
1de532f5 101 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
02f935fb
VS
102
103 // search in our domain only:
1de532f5 104 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
02f935fb
VS
105
106 // search in a domain that doesn't have this string:
1de532f5 107 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
02f935fb
VS
108}
109
110void IntlTestCase::Headers()
111{
89a7e1ff
VZ
112 if ( !m_locale )
113 return;
7ab4bb30 114
1de532f5
VZ
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") );
71f44b1b 120 CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale->GetHeaderValue("Content-Type") );
1de532f5 121 CPPUNIT_ASSERT_EQUAL( "8bit", m_locale->GetHeaderValue("Content-Transfer-Encoding") );
02f935fb
VS
122
123 // check that it fails with a bogus domain:
1de532f5 124 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
02f935fb
VS
125
126 // and that it fails for nonexisting header:
1de532f5 127 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
02f935fb
VS
128}
129
df6de630
VZ
130static wxString
131NormalizeFormat(const wxString& fmtOrig)
89a7e1ff 132{
df6de630 133 wxString fmt(fmtOrig);
89a7e1ff
VZ
134
135#ifdef __GLIBC__
136 // glibc uses some extensions in its formats which we need to convert to
137 // standard form
df6de630
VZ
138 fmt.Replace("%T", "%H:%M:%S");
139 fmt.Replace("%e", "%d");
89a7e1ff
VZ
140#endif // __GLIBC__
141
df6de630 142 return fmt;
89a7e1ff
VZ
143}
144
df6de630
VZ
145#define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
146 if ( !actual.empty() ) \
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
148
fac09395 149void IntlTestCase::DateTimeFmtFrench()
89a7e1ff
VZ
150{
151 if ( !m_locale )
152 return;
153
b9d22034 154#ifdef __GLIBC__
9362d823
VZ
155 // Versions of glibc up to 2.7 wrongly used periods for French locale
156 // separator.
157#if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8
158 static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
159#else
d8a696fb 160 static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
9362d823 161#endif
fac09395 162 static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y";
df6de630 163 static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %H:%M:%S %Z";
b9d22034 164#else
27aec6b6 165 static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
fac09395 166 static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y";
db227bf8
SC
167#ifdef __WXOSX__
168 static const char *FRENCH_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
169#else
fac09395 170 static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
db227bf8 171#endif
b9d22034
VZ
172#endif
173
df6de630 174 WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT,
d8a696fb 175 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
df6de630 176 WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT,
d8a696fb 177 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
df6de630 178 WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT,
89a7e1ff 179 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
df6de630 180 WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
d8a696fb 181 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
fac09395
VZ
182}
183
184void IntlTestCase::DateTimeFmtC()
185{
186 // again, glibc uses different defaults
187#ifdef __GLIBC__
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";
191#else
192 static const char *C_DATE_FMT = "%d/%m/%Y";
193 static const char *C_LONG_DATE_FMT = "%A %d %B %Y";
db227bf8
SC
194#ifdef __WXOSX__
195 static const char *C_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
196#else
fac09395 197 static const char *C_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
db227bf8 198#endif
fac09395 199#endif
89a7e1ff 200
89a7e1ff
VZ
201 setlocale(LC_ALL, "C");
202
df6de630 203 WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT,
d8a696fb 204 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
df6de630 205 WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT,
d8a696fb 206 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
df6de630 207 WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT,
89a7e1ff 208 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
df6de630 209 WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
d8a696fb 210 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
89a7e1ff
VZ
211}
212
fd1c361c
VZ
213void IntlTestCase::IsAvailable()
214{
215 const wxString origLocale(setlocale(LC_ALL, NULL));
216
217 // Calling IsAvailable() shouldn't change the locale.
218 wxLocale::IsAvailable(wxLANGUAGE_ENGLISH);
219
220 CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) );
221}
222
02f935fb 223#endif // wxUSE_INTL