]> git.saurik.com Git - wxWidgets.git/blame_incremental - tests/intl/intltest.cpp
fix a problem with setlocale() under Windows: with MSVC7 it sometimes returns garbage...
[wxWidgets.git] / tests / intl / intltest.cpp
... / ...
CommitLineData
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:
35 IntlTestCase() { m_locale=NULL; }
36
37 virtual void setUp();
38 virtual void tearDown();
39
40private:
41 CPPUNIT_TEST_SUITE( IntlTestCase );
42 CPPUNIT_TEST( Domain );
43 CPPUNIT_TEST( Headers );
44 CPPUNIT_TEST( DateTimeFmt );
45 CPPUNIT_TEST_SUITE_END();
46
47 void Domain();
48 void Headers();
49 void DateTimeFmt();
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{
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;
69
70 wxLocale::AddCatalogLookupPathPrefix("./intl");
71
72 m_locale = new wxLocale;
73 CPPUNIT_ASSERT( m_locale );
74
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{
84 if (m_locale)
85 {
86 delete m_locale;
87 m_locale = NULL;
88 }
89}
90
91void IntlTestCase::Domain()
92{
93 if (!m_locale)
94 return;
95
96 // _() searches all domains by default:
97 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
98
99 // search in our domain only:
100 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
101
102 // search in a domain that doesn't have this string:
103 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
104}
105
106void IntlTestCase::Headers()
107{
108 if ( !m_locale )
109 return;
110
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") );
118
119 // check that it fails with a bogus domain:
120 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
121
122 // and that it fails for nonexisting header:
123 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
124}
125
126static void
127CompareFormats(const char *msg, const wxString& expected, wxString actual)
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
142 CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, expected, actual );
143}
144
145void IntlTestCase::DateTimeFmt()
146{
147 if ( !m_locale )
148 return;
149
150#ifdef __GLIBC__
151 // glibc also uses dots for French locale separator for some reason (the
152 // standard format uses slashes)
153 static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
154#else
155 static const char *FRENCH_DATE_FMT = "%d/%m/%y";
156#endif
157
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",
163 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
164 CompareFormats( "French time", "%H:%M:%S",
165 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
166
167 // also test for "C" locale
168 setlocale(LC_ALL, "C");
169
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",
175 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
176 CompareFormats( "C time", "%H:%M:%S",
177 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
178}
179
180#endif // wxUSE_INTL