Merge pull request #820 from brendanzagaeski/master
[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"), "#1");
28                 Assert.AreEqual (" The Dude", ti.ToTitleCase (" The Dude"), "#2");
29                 Assert.AreEqual ("La Guerra Yla Paz", ti.ToTitleCase ("la Guerra yLa pAz"), "#3");
30                 Assert.AreEqual ("\tTab\tAnd\tPeace", ti.ToTitleCase ("\ttab\taNd\tpeaCE"), "#4");
31                 Assert.AreEqual ("This_Is\uFE58A\u0095String\u06D4With\uFE33Separators", ti.ToTitleCase ("this_is\uFE58a\u0095string\u06D4with\uFE33separators"), "#5");
32         }
33
34         [Test]
35         public void ListSeparator ()
36         {
37                 TextInfo ti;
38
39                 ti = new CultureInfo ("en-US", false).TextInfo;
40                 Assert.AreEqual (",", ti.ListSeparator, "#1");
41
42                 ti = CultureInfo.InvariantCulture.TextInfo;
43                 Assert.AreEqual (",", ti.ListSeparator, "#2");
44
45                 ti = new CultureInfo ("nl-BE", false).TextInfo;
46                 Assert.AreEqual (";", ti.ListSeparator, "#3");
47         }
48
49         [Test]
50         public void TitleCase2 ()
51         {
52                 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
53                         Check (ci, "AB", "AB");
54                         Check (ci, "ab", "Ab");
55                         Check (ci, "aB", "Ab");
56                         Check (ci, "1Ab", "1Ab");
57                         Check (ci, "abc AB ab aB Ab ABc 1AB 1Ab 1ab 1aB",
58                                 "Abc AB Ab Ab Ab Abc 1AB 1Ab 1Ab 1Ab");
59                         Check (ci, "LJ", "LJ");
60                         Check (ci, "lj", "Lj");
61                         Check (ci, "lJ", "Lj");
62                         Check (ci, "lj abc ljabc", "Lj Abc Ljabc");
63                         Check (ci, "ab", "Ab");
64
65                         // Some greek titlecase characters
66                         Check (ci, "\u01c4", "\u01c5");
67                         Check (ci, "\u01c5", "\u01c5");
68                         Check (ci, "\u01c6", "\u01c5");
69                         Check (ci, "\u01ca", "\u01cb");
70                         Check (ci, "\u01cb", "\u01cb");
71                         Check (ci, "\u01cc", "\u01cb");
72                         // Roman numbers are not converted unlike ToUpper().
73                         Check (ci, "\u2170", "\u2170");
74                         Check (ci, "\u24e9", "\u24e9");
75                 }
76         }
77
78         private void Check (CultureInfo ci, string src, string expected)
79         {
80                 Assert.AreEqual (expected, ci.TextInfo.ToTitleCase (src), src + " at culture " + ci.LCID);
81         }
82
83         private void CompareProperties (TextInfo t1, TextInfo t2, bool compareReadOnly)
84         {
85                 Assert.AreEqual (t1.ANSICodePage, t2.ANSICodePage, "ANSICodePage");
86                 Assert.AreEqual (t1.EBCDICCodePage, t2.EBCDICCodePage, "EBCDICCodePage");
87                 Assert.AreEqual (t1.ListSeparator, t2.ListSeparator, "ListSeparator");
88                 Assert.AreEqual (t1.MacCodePage, t2.MacCodePage, "MacCodePage");
89                 Assert.AreEqual (t1.OEMCodePage, t2.OEMCodePage, "OEMCodePage");
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         }
96
97         [Test]
98         [Category ("NotWorking")] // OnDeserialization isn't completed
99         public void SerializationRoundtrip ()
100         {
101                 TextInfo enus = new CultureInfo ("en-US").TextInfo;
102                 BinaryFormatter bf = new BinaryFormatter ();
103                 MemoryStream ms = new MemoryStream ();
104                 bf.Serialize (ms, enus);
105
106                 ms.Position = 0;
107                 TextInfo clone = (TextInfo) bf.Deserialize (ms);
108                 CompareProperties (enus, clone, true);
109         }
110
111         [Test]
112         public void Clone ()
113         {
114                 TextInfo enus = new CultureInfo ("en-US").TextInfo;
115                 TextInfo clone = (TextInfo) enus.Clone ();
116                 CompareProperties (enus, clone, true);
117         }
118
119         [Test]
120         public void Clone_ReadOnly ()
121         {
122                 TextInfo enus = TextInfo.ReadOnly (new CultureInfo ("en-US").TextInfo);
123                 Assert.IsTrue (enus.IsReadOnly, "IsReadOnly-1");
124                 TextInfo clone = (TextInfo) enus.Clone ();
125                 Assert.IsFalse (clone.IsReadOnly, "IsReadOnly-2");
126                 CompareProperties (enus, clone, false);
127                 // cloned item is *NOT* read-only
128         }
129
130         [Test]
131         public void ReadOnly ()
132         {
133                 TextInfo enus = new CultureInfo ("en-US").TextInfo;
134                 Assert.IsFalse (enus.IsReadOnly, "IsReadOnly-1");
135                 TextInfo ro = TextInfo.ReadOnly (enus);
136                 Assert.IsTrue (ro.IsReadOnly, "IsReadOnly-2");
137                 CompareProperties (enus, ro, false);
138         }
139
140         [Test]
141         public void IsRightToLeft ()
142         {
143                 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
144                         switch (ci.LCID) {
145                         case 1:         // ar
146                         case 13:        // he
147                         case 32:        // ur
148                         case 41:        // fa
149                         case 0x63:      // ps
150                         case 90:        // syr
151                         case 101:       // div
152                         case 1025:      // ar-SA
153                         case 1037:      // he-IL
154                         case 1056:      // ur-PK
155                         case 1065:      // ra-IR
156                         case 1114:      // syr-SY
157                         case 1125:      // div-MV
158                         case 2049:      // ar-IQ
159                         case 3073:      // ar-EG
160                         case 4097:      // ar-LY
161                         case 5121:      // ar-DZ
162                         case 6145:      // ar-MA
163                         case 7169:      // ar-TN
164                         case 8193:      // ar-OM
165                         case 9217:      // ar-YE
166                         case 10241:     // ar-SY
167                         case 11265:     // ar-JO
168                         case 12289:     // ar-LB
169                         case 13313:     // ar-KW
170                         case 14337:     // ar-AE
171                         case 15361:     // ar-BH
172                         case 16385:     // ar-QA
173                         case 0x463: // ps-AF
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
183         [Test]
184         public void Deserialization ()
185         {
186                 TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
187                 IDeserializationCallback dc = (ti as IDeserializationCallback);
188                 dc.OnDeserialization (null);
189         }
190 }
191
192 }