]> git.saurik.com Git - wxWidgets.git/blame - tests/intl/intltest.cpp
fix a problem with setlocale() under Windows: with MSVC7 it sometimes returns garbage...
[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 );
89a7e1ff 44 CPPUNIT_TEST( DateTimeFmt );
02f935fb
VS
45 CPPUNIT_TEST_SUITE_END();
46
47 void Domain();
48 void Headers();
89a7e1ff 49 void DateTimeFmt();
02f935fb
VS
50
51 wxLocale *m_locale;
52
53 DECLARE_NO_COPY_CLASS(IntlTestCase)
54};
55
56// register in the unnamed registry so that these tests are run by default
57CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase );
58
59// also include in it's own registry so that these tests can be run alone
60CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" );
61
62void IntlTestCase::setUp()
63{
89a7e1ff
VZ
64 // Check that French locale is supported, this test doesn't work without it
65 // and all the other function need to check whether m_locale is non-NULL
66 // before continuing
67 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
68 return;
7ab4bb30 69
02f935fb
VS
70 wxLocale::AddCatalogLookupPathPrefix("./intl");
71
72 m_locale = new wxLocale;
89a7e1ff
VZ
73 CPPUNIT_ASSERT( m_locale );
74
02f935fb
VS
75 // don't load default catalog, it may be unavailable:
76 bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_CONV_ENCODING);
77 CPPUNIT_ASSERT( loaded );
78
79 m_locale->AddCatalog("internat");
80}
81
82void IntlTestCase::tearDown()
83{
7ab4bb30
FM
84 if (m_locale)
85 {
86 delete m_locale;
87 m_locale = NULL;
88 }
02f935fb
VS
89}
90
91void IntlTestCase::Domain()
92{
7ab4bb30 93 if (!m_locale)
89a7e1ff 94 return;
7ab4bb30 95
02f935fb 96 // _() searches all domains by default:
1de532f5 97 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
02f935fb
VS
98
99 // search in our domain only:
1de532f5 100 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
02f935fb
VS
101
102 // search in a domain that doesn't have this string:
1de532f5 103 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
02f935fb
VS
104}
105
106void IntlTestCase::Headers()
107{
89a7e1ff
VZ
108 if ( !m_locale )
109 return;
7ab4bb30 110
1de532f5
VZ
111 CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale->GetHeaderValue("Project-Id-Version") );
112 CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale->GetHeaderValue("POT-Creation-Date") );
113 CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale->GetHeaderValue("PO-Revision-Date") );
114 CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale->GetHeaderValue("Last-Translator") );
115 CPPUNIT_ASSERT_EQUAL( "1.0", m_locale->GetHeaderValue("MIME-Version") );
116 CPPUNIT_ASSERT_EQUAL( "text/plain; charset=iso-8859-1", m_locale->GetHeaderValue("Content-Type") );
117 CPPUNIT_ASSERT_EQUAL( "8bit", m_locale->GetHeaderValue("Content-Transfer-Encoding") );
02f935fb
VS
118
119 // check that it fails with a bogus domain:
1de532f5 120 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
02f935fb
VS
121
122 // and that it fails for nonexisting header:
1de532f5 123 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
02f935fb
VS
124}
125
d8a696fb
VZ
126static void
127CompareFormats(const char *msg, const wxString& expected, wxString actual)
89a7e1ff
VZ
128{
129 if ( actual.empty() )
130 {
131 // this means that GetInfo() failed which can happen, just ignore
132 return;
133 }
134
135#ifdef __GLIBC__
136 // glibc uses some extensions in its formats which we need to convert to
137 // standard form
138 actual.Replace("%T", "%H:%M:%S");
139 actual.Replace("%e", "%d");
140#endif // __GLIBC__
141
d8a696fb 142 CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, expected, actual );
89a7e1ff
VZ
143}
144
145void IntlTestCase::DateTimeFmt()
146{
147 if ( !m_locale )
148 return;
149
b9d22034
VZ
150#ifdef __GLIBC__
151 // glibc also uses dots for French locale separator for some reason (the
152 // standard format uses slashes)
d8a696fb 153 static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
b9d22034
VZ
154#else
155 static const char *FRENCH_DATE_FMT = "%d/%m/%y";
156#endif
157
d8a696fb
VZ
158 CompareFormats( "French short date", FRENCH_DATE_FMT,
159 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
160 CompareFormats( "French long date", "%a %d %b %Y",
161 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
162 CompareFormats( "French date and time", "%a %d %b %Y %H:%M:%S %Z",
89a7e1ff 163 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
d8a696fb
VZ
164 CompareFormats( "French time", "%H:%M:%S",
165 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
89a7e1ff
VZ
166
167 // also test for "C" locale
168 setlocale(LC_ALL, "C");
169
d8a696fb
VZ
170 CompareFormats( "C short date", "%m/%d/%y",
171 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
172 CompareFormats( "C long date", "%a %b %d %Y",
173 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
174 CompareFormats( "C date and time", "%a %b %d %H:%M:%S %Y",
89a7e1ff 175 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
d8a696fb
VZ
176 CompareFormats( "C time", "%H:%M:%S",
177 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
89a7e1ff
VZ
178}
179
02f935fb 180#endif // wxUSE_INTL