]> git.saurik.com Git - wxWidgets.git/blame - tests/intl/intltest.cpp
Only show the default close button in wxInfoBar if there are no others.
[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 );
02f935fb
VS
46 CPPUNIT_TEST_SUITE_END();
47
48 void Domain();
49 void Headers();
fac09395
VZ
50 void DateTimeFmtFrench();
51 void DateTimeFmtC();
02f935fb
VS
52
53 wxLocale *m_locale;
54
55 DECLARE_NO_COPY_CLASS(IntlTestCase)
56};
57
58// register in the unnamed registry so that these tests are run by default
59CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase );
60
61// also include in it's own registry so that these tests can be run alone
62CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" );
63
64void IntlTestCase::setUp()
65{
89a7e1ff
VZ
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
68 // before continuing
69 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
70 return;
7ab4bb30 71
02f935fb
VS
72 wxLocale::AddCatalogLookupPathPrefix("./intl");
73
74 m_locale = new wxLocale;
89a7e1ff
VZ
75 CPPUNIT_ASSERT( m_locale );
76
02f935fb
VS
77 // don't load default catalog, it may be unavailable:
78 bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_CONV_ENCODING);
79 CPPUNIT_ASSERT( loaded );
80
81 m_locale->AddCatalog("internat");
82}
83
84void IntlTestCase::tearDown()
85{
7ab4bb30
FM
86 if (m_locale)
87 {
88 delete m_locale;
89 m_locale = NULL;
90 }
02f935fb
VS
91}
92
93void IntlTestCase::Domain()
94{
7ab4bb30 95 if (!m_locale)
89a7e1ff 96 return;
7ab4bb30 97
02f935fb 98 // _() searches all domains by default:
1de532f5 99 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
02f935fb
VS
100
101 // search in our domain only:
1de532f5 102 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
02f935fb
VS
103
104 // search in a domain that doesn't have this string:
1de532f5 105 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
02f935fb
VS
106}
107
108void IntlTestCase::Headers()
109{
89a7e1ff
VZ
110 if ( !m_locale )
111 return;
7ab4bb30 112
1de532f5
VZ
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") );
02f935fb
VS
120
121 // check that it fails with a bogus domain:
1de532f5 122 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
02f935fb
VS
123
124 // and that it fails for nonexisting header:
1de532f5 125 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
02f935fb
VS
126}
127
df6de630
VZ
128static wxString
129NormalizeFormat(const wxString& fmtOrig)
89a7e1ff 130{
df6de630 131 wxString fmt(fmtOrig);
89a7e1ff
VZ
132
133#ifdef __GLIBC__
134 // glibc uses some extensions in its formats which we need to convert to
135 // standard form
df6de630
VZ
136 fmt.Replace("%T", "%H:%M:%S");
137 fmt.Replace("%e", "%d");
89a7e1ff
VZ
138#endif // __GLIBC__
139
df6de630 140 return fmt;
89a7e1ff
VZ
141}
142
df6de630
VZ
143#define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
144 if ( !actual.empty() ) \
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
146
fac09395 147void IntlTestCase::DateTimeFmtFrench()
89a7e1ff
VZ
148{
149 if ( !m_locale )
150 return;
151
b9d22034
VZ
152#ifdef __GLIBC__
153 // glibc also uses dots for French locale separator for some reason (the
154 // standard format uses slashes)
d8a696fb 155 static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
fac09395 156 static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y";
df6de630 157 static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %H:%M:%S %Z";
b9d22034 158#else
27aec6b6 159 static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
fac09395 160 static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y";
db227bf8
SC
161#ifdef __WXOSX__
162 static const char *FRENCH_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
163#else
fac09395 164 static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
db227bf8 165#endif
b9d22034
VZ
166#endif
167
df6de630 168 WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT,
d8a696fb 169 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
df6de630 170 WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT,
d8a696fb 171 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
df6de630 172 WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT,
89a7e1ff 173 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
df6de630 174 WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
d8a696fb 175 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
fac09395
VZ
176}
177
178void IntlTestCase::DateTimeFmtC()
179{
180 // again, glibc uses different defaults
181#ifdef __GLIBC__
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";
185#else
186 static const char *C_DATE_FMT = "%d/%m/%Y";
187 static const char *C_LONG_DATE_FMT = "%A %d %B %Y";
db227bf8
SC
188#ifdef __WXOSX__
189 static const char *C_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
190#else
fac09395 191 static const char *C_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
db227bf8 192#endif
fac09395 193#endif
89a7e1ff 194
89a7e1ff
VZ
195 setlocale(LC_ALL, "C");
196
df6de630 197 WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT,
d8a696fb 198 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
df6de630 199 WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT,
d8a696fb 200 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
df6de630 201 WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT,
89a7e1ff 202 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
df6de630 203 WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
d8a696fb 204 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
89a7e1ff
VZ
205}
206
02f935fb 207#endif // wxUSE_INTL