TextInfoTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // TextInfoTest.cs - NUnit Test Cases for the
  2. // System.Globalization.TextInfo class
  3. //
  4. // Miguel de Icaza <[email protected]>
  5. //
  6. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Runtime.Serialization;
  13. using System.Runtime.Serialization.Formatters.Binary;
  14. namespace MonoTests.System.Globalization
  15. {
  16. [TestFixture]
  17. public class TextInfoTest {
  18. [Test]
  19. public void TitleCase ()
  20. {
  21. TextInfo ti = new CultureInfo ("en-US", false).TextInfo;
  22. Assert.AreEqual (" The Dog", ti.ToTitleCase (" the dog"));
  23. Assert.AreEqual (" The Dude", ti.ToTitleCase (" The Dude"));
  24. Assert.AreEqual ("La Guerra Yla Paz", ti.ToTitleCase ("la Guerra yLa pAz"));
  25. Assert.AreEqual ("\tTab\tAnd\tPeace", ti.ToTitleCase ("\ttab\taNd\tpeaCE"));
  26. }
  27. [Test]
  28. public void ListSeparator ()
  29. {
  30. TextInfo ti;
  31. ti = new CultureInfo ("en-US", false).TextInfo;
  32. Assert.AreEqual (",", ti.ListSeparator, "#1");
  33. ti = CultureInfo.InvariantCulture.TextInfo;
  34. Assert.AreEqual (",", ti.ListSeparator, "#2");
  35. ti = new CultureInfo ("nl-BE", false).TextInfo;
  36. Assert.AreEqual (";", ti.ListSeparator, "#3");
  37. }
  38. [Test]
  39. public void TitleCase2 ()
  40. {
  41. foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
  42. Check (ci, "AB", "AB");
  43. Check (ci, "ab", "Ab");
  44. Check (ci, "aB", "Ab");
  45. Check (ci, "1Ab", "1Ab");
  46. Check (ci, "abc AB ab aB Ab ABc 1AB 1Ab 1ab 1aB",
  47. "Abc AB Ab Ab Ab Abc 1AB 1Ab 1Ab 1Ab");
  48. Check (ci, "LJ", "LJ");
  49. Check (ci, "lj", "Lj");
  50. Check (ci, "lJ", "Lj");
  51. Check (ci, "lj abc ljabc", "Lj Abc Ljabc");
  52. Check (ci, "ab", "Ab");
  53. // Some greek titlecase characters
  54. Check (ci, "\u01c4", "\u01c5");
  55. Check (ci, "\u01c5", "\u01c5");
  56. Check (ci, "\u01c6", "\u01c5");
  57. Check (ci, "\u01ca", "\u01cb");
  58. Check (ci, "\u01cb", "\u01cb");
  59. Check (ci, "\u01cc", "\u01cb");
  60. // Roman numbers are not converted unlike ToUpper().
  61. Check (ci, "\u2170", "\u2170");
  62. Check (ci, "\u24e9", "\u24e9");
  63. }
  64. }
  65. private void Check (CultureInfo ci, string src, string expected)
  66. {
  67. Assert.AreEqual (expected, ci.TextInfo.ToTitleCase (src), src + " at culture " + ci.LCID);
  68. }
  69. private void CompareProperties (TextInfo t1, TextInfo t2, bool compareReadOnly)
  70. {
  71. Assert.AreEqual (t1.ANSICodePage, t2.ANSICodePage, "ANSICodePage");
  72. Assert.AreEqual (t1.EBCDICCodePage, t2.EBCDICCodePage, "EBCDICCodePage");
  73. Assert.AreEqual (t1.ListSeparator, t2.ListSeparator, "ListSeparator");
  74. Assert.AreEqual (t1.MacCodePage, t2.MacCodePage, "MacCodePage");
  75. Assert.AreEqual (t1.OEMCodePage, t2.OEMCodePage, "OEMCodePage");
  76. #if NET_2_0
  77. Assert.AreEqual (t1.CultureName, t2.CultureName, "CultureName");
  78. if (compareReadOnly)
  79. Assert.AreEqual (t1.IsReadOnly, t2.IsReadOnly, "IsReadOnly");
  80. //FIXME Assert.AreEqual (t1.IsRightToLeft, t2.IsRightToLeft, "IsRightToLeft");
  81. Assert.AreEqual (t1.LCID, t2.LCID, "LCID");
  82. #endif
  83. }
  84. [Test]
  85. [Category ("NotWorking")] // OnDeserialization isn't completed
  86. public void SerializationRoundtrip ()
  87. {
  88. TextInfo enus = new CultureInfo ("en-US").TextInfo;
  89. BinaryFormatter bf = new BinaryFormatter ();
  90. MemoryStream ms = new MemoryStream ();
  91. bf.Serialize (ms, enus);
  92. ms.Position = 0;
  93. TextInfo clone = (TextInfo) bf.Deserialize (ms);
  94. CompareProperties (enus, clone, true);
  95. }
  96. #if NET_2_0
  97. [Test]
  98. public void Clone ()
  99. {
  100. TextInfo enus = new CultureInfo ("en-US").TextInfo;
  101. TextInfo clone = (TextInfo) enus.Clone ();
  102. CompareProperties (enus, clone, true);
  103. }
  104. [Test]
  105. public void Clone_ReadOnly ()
  106. {
  107. TextInfo enus = TextInfo.ReadOnly (new CultureInfo ("en-US").TextInfo);
  108. Assert.IsTrue (enus.IsReadOnly, "IsReadOnly-1");
  109. TextInfo clone = (TextInfo) enus.Clone ();
  110. Assert.IsFalse (clone.IsReadOnly, "IsReadOnly-2");
  111. CompareProperties (enus, clone, false);
  112. // cloned item is *NOT* read-only
  113. }
  114. [Test]
  115. public void ReadOnly ()
  116. {
  117. TextInfo enus = new CultureInfo ("en-US").TextInfo;
  118. Assert.IsFalse (enus.IsReadOnly, "IsReadOnly-1");
  119. TextInfo ro = TextInfo.ReadOnly (enus);
  120. Assert.IsTrue (ro.IsReadOnly, "IsReadOnly-2");
  121. CompareProperties (enus, ro, false);
  122. }
  123. [Test]
  124. public void IsRightToLeft ()
  125. {
  126. foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
  127. switch (ci.LCID) {
  128. case 1: // ar
  129. case 13: // he
  130. case 32: // ur
  131. case 41: // fa
  132. case 90: // syr
  133. case 101: // div
  134. case 1025: // ar-SA
  135. case 1037: // he-IL
  136. case 1056: // ur-PK
  137. case 1065: // ra-IR
  138. case 1114: // syr-SY
  139. case 1125: // div-MV
  140. case 2049: // ar-IQ
  141. case 3073: // ar-EG
  142. case 4097: // ar-LY
  143. case 5121: // ar-DZ
  144. case 6145: // ar-MA
  145. case 7169: // ar-TN
  146. case 8193: // ar-OM
  147. case 9217: // ar-YE
  148. case 10241: // ar-SY
  149. case 11265: // ar-JO
  150. case 12289: // ar-LB
  151. case 13313: // ar-KW
  152. case 14337: // ar-AE
  153. case 15361: // ar-BH
  154. case 16385: // ar-QA
  155. Assert.IsTrue (ci.TextInfo.IsRightToLeft, ci.Name);
  156. break;
  157. default:
  158. Assert.IsFalse (ci.TextInfo.IsRightToLeft, ci.Name);
  159. break;
  160. }
  161. }
  162. }
  163. #endif
  164. [Test]
  165. public void Deserialization ()
  166. {
  167. TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
  168. IDeserializationCallback dc = (ti as IDeserializationCallback);
  169. dc.OnDeserialization (null);
  170. }
  171. }
  172. }