]>
Commit | Line | Data |
---|---|---|
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 | ||
32 | class IntlTestCase : public CppUnit::TestCase | |
33 | { | |
34 | public: | |
7ab4bb30 | 35 | IntlTestCase() { m_locale=NULL; } |
02f935fb VS |
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 ); | |
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 | |
57 | CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase ); | |
58 | ||
59 | // also include in it's own registry so that these tests can be run alone | |
60 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" ); | |
61 | ||
62 | void 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 | ||
82 | void IntlTestCase::tearDown() | |
83 | { | |
7ab4bb30 FM |
84 | if (m_locale) |
85 | { | |
86 | delete m_locale; | |
87 | m_locale = NULL; | |
88 | } | |
02f935fb VS |
89 | } |
90 | ||
91 | void 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 | ||
106 | void 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 | ||
89a7e1ff VZ |
126 | static void CompareFormats(const wxString& expected, wxString actual) |
127 | { | |
128 | if ( actual.empty() ) | |
129 | { | |
130 | // this means that GetInfo() failed which can happen, just ignore | |
131 | return; | |
132 | } | |
133 | ||
134 | #ifdef __GLIBC__ | |
135 | // glibc uses some extensions in its formats which we need to convert to | |
136 | // standard form | |
137 | actual.Replace("%T", "%H:%M:%S"); | |
138 | actual.Replace("%e", "%d"); | |
139 | #endif // __GLIBC__ | |
140 | ||
141 | CPPUNIT_ASSERT_EQUAL( expected, actual ); | |
142 | } | |
143 | ||
144 | void IntlTestCase::DateTimeFmt() | |
145 | { | |
146 | if ( !m_locale ) | |
147 | return; | |
148 | ||
149 | CompareFormats( "%d.%m.%Y", m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) ); | |
150 | CompareFormats( "%a %d %b %Y", m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) ); | |
151 | CompareFormats( "%a %d %b %Y %H:%M:%S %Z", | |
152 | m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) ); | |
153 | CompareFormats( "%H:%M:%S", m_locale->GetInfo(wxLOCALE_TIME_FMT) ); | |
154 | ||
155 | // also test for "C" locale | |
156 | setlocale(LC_ALL, "C"); | |
157 | ||
158 | CompareFormats( "%m/%d/%y", m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) ); | |
159 | CompareFormats( "%a %b %d %Y", m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) ); | |
160 | CompareFormats( "%a %b %d %H:%M:%S %Y", | |
161 | m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) ); | |
162 | CompareFormats( "%H:%M:%S", m_locale->GetInfo(wxLOCALE_TIME_FMT) ); | |
163 | } | |
164 | ||
02f935fb | 165 | #endif // wxUSE_INTL |