update .sln too.
[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                 Assert.AreEqual (t1.CultureName, t2.CultureName, "CultureName");
90                 if (compareReadOnly)
91                         Assert.AreEqual (t1.IsReadOnly, t2.IsReadOnly, "IsReadOnly");
92 //FIXME         Assert.AreEqual (t1.IsRightToLeft, t2.IsRightToLeft, "IsRightToLeft");
93                 Assert.AreEqual (t1.LCID, t2.LCID, "LCID");
94         }
95
96         [Test]
97         [Category ("NotWorking")] // OnDeserialization isn't completed
98         public void SerializationRoundtrip ()
99         {
100                 TextInfo enus = new CultureInfo ("en-US").TextInfo;
101                 BinaryFormatter bf = new BinaryFormatter ();
102                 MemoryStream ms = new MemoryStream ();
103                 bf.Serialize (ms, enus);
104
105                 ms.Position = 0;
106                 TextInfo clone = (TextInfo) bf.Deserialize (ms);
107                 CompareProperties (enus, clone, true);
108         }
109
110         [Test]
111         public void Clone ()
112         {
113                 TextInfo enus = new CultureInfo ("en-US").TextInfo;
114                 TextInfo clone = (TextInfo) enus.Clone ();
115                 CompareProperties (enus, clone, true);
116         }
117
118         [Test]
119         public void Clone_ReadOnly ()
120         {
121                 TextInfo enus = TextInfo.ReadOnly (new CultureInfo ("en-US").TextInfo);
122                 Assert.IsTrue (enus.IsReadOnly, "IsReadOnly-1");
123                 TextInfo clone = (TextInfo) enus.Clone ();
124                 Assert.IsFalse (clone.IsReadOnly, "IsReadOnly-2");
125                 CompareProperties (enus, clone, false);
126                 // cloned item is *NOT* read-only
127         }
128
129         [Test]
130         public void ReadOnly ()
131         {
132                 TextInfo enus = new CultureInfo ("en-US").TextInfo;
133                 Assert.IsFalse (enus.IsReadOnly, "IsReadOnly-1");
134                 TextInfo ro = TextInfo.ReadOnly (enus);
135                 Assert.IsTrue (ro.IsReadOnly, "IsReadOnly-2");
136                 CompareProperties (enus, ro, false);
137         }
138
139         [Test]
140         public void IsRightToLeft ()
141         {
142                 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
143                         switch (ci.LCID) {
144                         case 1:         // ar
145                         case 13:        // he
146                         case 32:        // ur
147                         case 41:        // fa
148                         case 0x63:      // ps
149                         case 90:        // syr
150                         case 101:       // div
151                         case 1025:      // ar-SA
152                         case 1037:      // he-IL
153                         case 1056:      // ur-PK
154                         case 1065:      // ra-IR
155                         case 1114:      // syr-SY
156                         case 1125:      // div-MV
157                         case 2049:      // ar-IQ
158                         case 3073:      // ar-EG
159                         case 4097:      // ar-LY
160                         case 5121:      // ar-DZ
161                         case 6145:      // ar-MA
162                         case 7169:      // ar-TN
163                         case 8193:      // ar-OM
164                         case 9217:      // ar-YE
165                         case 10241:     // ar-SY
166                         case 11265:     // ar-JO
167                         case 12289:     // ar-LB
168                         case 13313:     // ar-KW
169                         case 14337:     // ar-AE
170                         case 15361:     // ar-BH
171                         case 16385:     // ar-QA
172                         case 0x463: // ps-AF
173                                 Assert.IsTrue (ci.TextInfo.IsRightToLeft, ci.Name);
174                                 break;
175                         default:
176                                 Assert.IsFalse (ci.TextInfo.IsRightToLeft, ci.Name);
177                                 break;
178                         }
179                 }
180         }
181
182         [Test]
183         public void Deserialization ()
184         {
185                 TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
186                 IDeserializationCallback dc = (ti as IDeserializationCallback);
187                 dc.OnDeserialization (null);
188         }
189 }
190
191 }