intltest.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/intl/intltest.cpp
  3. // Purpose: wxLocale unit test
  4. // Author: Vaclav Slavik
  5. // Created: 2007-03-26
  6. // Copyright: (c) 2007 Vaclav Slavik
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/intl.h"
  19. #if wxUSE_INTL
  20. // ----------------------------------------------------------------------------
  21. // test class
  22. // ----------------------------------------------------------------------------
  23. class IntlTestCase : public CppUnit::TestCase
  24. {
  25. public:
  26. IntlTestCase() { m_locale=NULL; }
  27. virtual void setUp();
  28. virtual void tearDown();
  29. private:
  30. CPPUNIT_TEST_SUITE( IntlTestCase );
  31. CPPUNIT_TEST( RestoreLocale );
  32. CPPUNIT_TEST( Domain );
  33. CPPUNIT_TEST( Headers );
  34. CPPUNIT_TEST( DateTimeFmtFrench );
  35. CPPUNIT_TEST( DateTimeFmtC );
  36. CPPUNIT_TEST( IsAvailable );
  37. CPPUNIT_TEST_SUITE_END();
  38. void RestoreLocale();
  39. void Domain();
  40. void Headers();
  41. void DateTimeFmtFrench();
  42. void DateTimeFmtC();
  43. void IsAvailable();
  44. static wxString GetDecimalPoint()
  45. {
  46. return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
  47. }
  48. wxLocale *m_locale;
  49. DECLARE_NO_COPY_CLASS(IntlTestCase)
  50. };
  51. // register in the unnamed registry so that these tests are run by default
  52. CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase );
  53. // also include in its own registry so that these tests can be run alone
  54. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" );
  55. void IntlTestCase::setUp()
  56. {
  57. // Check that French locale is supported, this test doesn't work without it
  58. // and all the other function need to check whether m_locale is non-NULL
  59. // before continuing
  60. if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
  61. return;
  62. wxLocale::AddCatalogLookupPathPrefix("./intl");
  63. m_locale = new wxLocale;
  64. CPPUNIT_ASSERT( m_locale );
  65. // don't load default catalog, it may be unavailable:
  66. bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT);
  67. CPPUNIT_ASSERT( loaded );
  68. m_locale->AddCatalog("internat");
  69. }
  70. void IntlTestCase::tearDown()
  71. {
  72. if (m_locale)
  73. {
  74. delete m_locale;
  75. m_locale = NULL;
  76. }
  77. }
  78. void IntlTestCase::RestoreLocale()
  79. {
  80. if ( !m_locale )
  81. return;
  82. // We must be using the French locale now, it was changed in setUp().
  83. CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
  84. // Switch to the English locale.
  85. {
  86. wxLocale locEn(wxLANGUAGE_ENGLISH);
  87. CPPUNIT_ASSERT_EQUAL( ".", GetDecimalPoint() );
  88. }
  89. // Verify that after destroying the English locale object, French locale is
  90. // restored.
  91. CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
  92. }
  93. void IntlTestCase::Domain()
  94. {
  95. if (!m_locale)
  96. return;
  97. // _() searches all domains by default:
  98. CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
  99. // search in our domain only:
  100. CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
  101. // search in a domain that doesn't have this string:
  102. CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
  103. }
  104. void IntlTestCase::Headers()
  105. {
  106. if ( !m_locale )
  107. return;
  108. CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale->GetHeaderValue("Project-Id-Version") );
  109. CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale->GetHeaderValue("POT-Creation-Date") );
  110. CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale->GetHeaderValue("PO-Revision-Date") );
  111. CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale->GetHeaderValue("Last-Translator") );
  112. CPPUNIT_ASSERT_EQUAL( "1.0", m_locale->GetHeaderValue("MIME-Version") );
  113. CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale->GetHeaderValue("Content-Type") );
  114. CPPUNIT_ASSERT_EQUAL( "8bit", m_locale->GetHeaderValue("Content-Transfer-Encoding") );
  115. // check that it fails with a bogus domain:
  116. CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
  117. // and that it fails for nonexisting header:
  118. CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
  119. }
  120. static wxString
  121. NormalizeFormat(const wxString& fmtOrig)
  122. {
  123. wxString fmt(fmtOrig);
  124. #ifdef __GLIBC__
  125. // glibc uses some extensions in its formats which we need to convert to
  126. // standard form
  127. fmt.Replace("%T", "%H:%M:%S");
  128. fmt.Replace("%e", "%d");
  129. #endif // __GLIBC__
  130. return fmt;
  131. }
  132. #define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
  133. if ( !actual.empty() ) \
  134. CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
  135. void IntlTestCase::DateTimeFmtFrench()
  136. {
  137. if ( !m_locale )
  138. return;
  139. #ifdef __GLIBC__
  140. // Versions of glibc up to 2.7 wrongly used periods for French locale
  141. // separator.
  142. #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8
  143. static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
  144. #else
  145. static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
  146. #endif
  147. static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y";
  148. static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %H:%M:%S %Z";
  149. #else
  150. static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
  151. static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y";
  152. #ifdef __WXOSX__
  153. static const char *FRENCH_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
  154. #else
  155. static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
  156. #endif
  157. #endif
  158. WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT,
  159. m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
  160. WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT,
  161. m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
  162. WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT,
  163. m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
  164. WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
  165. m_locale->GetInfo(wxLOCALE_TIME_FMT) );
  166. }
  167. void IntlTestCase::DateTimeFmtC()
  168. {
  169. // again, glibc uses different defaults
  170. #ifdef __GLIBC__
  171. static const char *C_DATE_FMT = "%m/%d/%y";
  172. static const char *C_LONG_DATE_FMT = "%a %b %d %Y";
  173. static const char *C_DATE_TIME_FMT = "%a %b %d %H:%M:%S %Y";
  174. #else
  175. static const char *C_DATE_FMT = "%d/%m/%Y";
  176. static const char *C_LONG_DATE_FMT = "%A %d %B %Y";
  177. #ifdef __WXOSX__
  178. static const char *C_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
  179. #else
  180. static const char *C_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
  181. #endif
  182. #endif
  183. setlocale(LC_ALL, "C");
  184. WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT,
  185. m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
  186. WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT,
  187. m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
  188. WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT,
  189. m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
  190. WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
  191. m_locale->GetInfo(wxLOCALE_TIME_FMT) );
  192. }
  193. void IntlTestCase::IsAvailable()
  194. {
  195. const wxString origLocale(setlocale(LC_ALL, NULL));
  196. // Calling IsAvailable() shouldn't change the locale.
  197. wxLocale::IsAvailable(wxLANGUAGE_ENGLISH);
  198. CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) );
  199. }
  200. #endif // wxUSE_INTL