adjusting DATE_TIME_FMT on OS X
[wxWidgets.git] / tests / intl / intltest.cpp
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:
35 IntlTestCase() { m_locale=NULL; }
36
37 virtual void setUp();
38 virtual void tearDown();
39
40 private:
41 CPPUNIT_TEST_SUITE( IntlTestCase );
42 CPPUNIT_TEST( Domain );
43 CPPUNIT_TEST( Headers );
44 CPPUNIT_TEST( DateTimeFmtFrench );
45 CPPUNIT_TEST( DateTimeFmtC );
46 CPPUNIT_TEST_SUITE_END();
47
48 void Domain();
49 void Headers();
50 void DateTimeFmtFrench();
51 void DateTimeFmtC();
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
59 CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase );
60
61 // also include in it's own registry so that these tests can be run alone
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" );
63
64 void IntlTestCase::setUp()
65 {
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;
71
72 wxLocale::AddCatalogLookupPathPrefix("./intl");
73
74 m_locale = new wxLocale;
75 CPPUNIT_ASSERT( m_locale );
76
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
84 void IntlTestCase::tearDown()
85 {
86 if (m_locale)
87 {
88 delete m_locale;
89 m_locale = NULL;
90 }
91 }
92
93 void IntlTestCase::Domain()
94 {
95 if (!m_locale)
96 return;
97
98 // _() searches all domains by default:
99 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
100
101 // search in our domain only:
102 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
103
104 // search in a domain that doesn't have this string:
105 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
106 }
107
108 void IntlTestCase::Headers()
109 {
110 if ( !m_locale )
111 return;
112
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") );
120
121 // check that it fails with a bogus domain:
122 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
123
124 // and that it fails for nonexisting header:
125 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
126 }
127
128 static void
129 CompareFormats(const char *msg, const wxString& expected, wxString actual)
130 {
131 if ( actual.empty() )
132 {
133 // this means that GetInfo() failed which can happen, just ignore
134 return;
135 }
136
137 #ifdef __GLIBC__
138 // glibc uses some extensions in its formats which we need to convert to
139 // standard form
140 actual.Replace("%T", "%H:%M:%S");
141 actual.Replace("%e", "%d");
142 #endif // __GLIBC__
143
144 CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, expected, actual );
145 }
146
147 void IntlTestCase::DateTimeFmtFrench()
148 {
149 if ( !m_locale )
150 return;
151
152 #ifdef __GLIBC__
153 // glibc also uses dots for French locale separator for some reason (the
154 // standard format uses slashes)
155 static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
156 static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y";
157 static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %T %Z";
158 #else
159 static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
160 static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y";
161 #ifdef __WXOSX__
162 static const char *FRENCH_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
163 #else
164 static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
165 #endif
166 #endif
167
168 CompareFormats( "French short date", FRENCH_DATE_FMT,
169 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
170 CompareFormats( "French long date", FRENCH_LONG_DATE_FMT,
171 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
172 CompareFormats( "French date and time", FRENCH_DATE_TIME_FMT,
173 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
174 CompareFormats( "French time", "%H:%M:%S",
175 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
176 }
177
178 void 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";
188 #ifdef __WXOSX__
189 static const char *C_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
190 #else
191 static const char *C_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
192 #endif
193 #endif
194
195 setlocale(LC_ALL, "C");
196
197 CompareFormats( "C short date", C_DATE_FMT,
198 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
199 CompareFormats( "C long date", C_LONG_DATE_FMT,
200 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
201 CompareFormats( "C date and time", C_DATE_TIME_FMT,
202 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
203 CompareFormats( "C time", "%H:%M:%S",
204 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
205 }
206
207 #endif // wxUSE_INTL