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