BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / corlib / Test / System.Globalization / CultureInfoTest.cs
1 //
2 // System.Globalization.CultureInfo Test Cases
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2005 Novell, Inc. (http://www.novell.com)
8 //
9
10 using System;
11 using System.Globalization;
12 using System.IO;
13 using System.Runtime.Serialization.Formatters.Binary;
14 using System.Threading;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Globalization
19 {
20         [TestFixture]
21         public class CultureInfoTest
22         {
23                 CultureInfo old_culture;
24
25                 [SetUp]
26                 public void Setup ()
27                 {
28                         old_culture = Thread.CurrentThread.CurrentCulture;
29                 }
30
31                 [TearDown]
32                 public void TearDown ()
33                 {
34                         Thread.CurrentThread.CurrentCulture = old_culture;
35                 }
36
37                 [Test]
38                 public void Constructor0 ()
39                 {
40                         CultureInfo ci = new CultureInfo (2067);
41                         Assert.IsFalse (ci.IsReadOnly, "#1");
42                         Assert.AreEqual (2067, ci.LCID, "#2");
43                         Assert.AreEqual ("nl-BE", ci.Name, "#3");
44                         Assert.IsTrue (ci.UseUserOverride, "#4");
45                 }
46
47                 [Test]
48                 public void Constructor0_Identifier_Negative ()
49                 {
50                         try {
51                                 new CultureInfo (-1);
52                                 Assert.Fail ("#1");
53                         } catch (ArgumentOutOfRangeException ex) {
54                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
55                                 Assert.IsNull (ex.InnerException, "#3");
56                                 Assert.IsNotNull (ex.Message, "#4");
57                                 Assert.IsNotNull (ex.ParamName, "#5");
58                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
59                         }
60                 }
61
62                 [Test]
63                 public void Constructor1 ()
64                 {
65                         CultureInfo ci = new CultureInfo ("nl-BE");
66                         Assert.IsFalse (ci.IsReadOnly, "#1");
67                         Assert.AreEqual (2067, ci.LCID, "#2");
68                         Assert.AreEqual ("nl-BE", ci.Name, "#3");
69                         Assert.IsTrue (ci.UseUserOverride, "#4");
70                 }
71
72                 [Test]
73                 public void Constructor1_Name_Null ()
74                 {
75                         try {
76                                 new CultureInfo ((string) null);
77                                 Assert.Fail ("#1");
78                         } catch (ArgumentNullException ex) {
79                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
80                                 Assert.IsNull (ex.InnerException, "#3");
81                                 Assert.IsNotNull (ex.Message, "#4");
82                                 Assert.IsNotNull (ex.ParamName, "#5");
83                                 Assert.AreEqual ("name", ex.ParamName, "#6");
84                         }
85                 }
86
87                 [Test]
88                 public void Constructor2 ()
89                 {
90                         CultureInfo ci = new CultureInfo (2067, false);
91                         Assert.IsFalse (ci.IsReadOnly, "#A1");
92                         Assert.AreEqual (2067, ci.LCID, "#A2");
93                         Assert.AreEqual ("nl-BE", ci.Name, "#A3");
94                         Assert.IsFalse (ci.UseUserOverride, "#A4");
95
96                         ci = new CultureInfo (2067, true);
97                         Assert.IsFalse (ci.IsReadOnly, "#B1");
98                         Assert.AreEqual (2067, ci.LCID, "#B2");
99                         Assert.AreEqual ("nl-BE", ci.Name, "#B3");
100                         Assert.IsTrue (ci.UseUserOverride, "#B4");
101                 }
102
103                 [Test]
104                 public void Constructor2_Identifier_Negative ()
105                 {
106                         try {
107                                 new CultureInfo (-1, false);
108                                 Assert.Fail ("#1");
109                         } catch (ArgumentOutOfRangeException ex) {
110                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
111                                 Assert.IsNull (ex.InnerException, "#3");
112                                 Assert.IsNotNull (ex.Message, "#4");
113                                 Assert.IsNotNull (ex.ParamName, "#5");
114                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
115                         }
116                 }
117
118                 [Test]
119                 public void Constructor3 ()
120                 {
121                         CultureInfo ci = new CultureInfo ("nl-BE", false);
122                         Assert.IsFalse (ci.IsReadOnly, "#A1");
123                         Assert.AreEqual (2067, ci.LCID, "#A2");
124                         Assert.AreEqual ("nl-BE", ci.Name, "#A3");
125                         Assert.IsFalse (ci.UseUserOverride, "#A4");
126
127                         ci = new CultureInfo ("nl-BE", true);
128                         Assert.IsFalse (ci.IsReadOnly, "#B1");
129                         Assert.AreEqual (2067, ci.LCID, "#B2");
130                         Assert.AreEqual ("nl-BE", ci.Name, "#B3");
131                         Assert.IsTrue (ci.UseUserOverride, "#B4");
132                 }
133
134                 [Test]
135                 public void Constructor3_Name_Null ()
136                 {
137                         try {
138                                 new CultureInfo ((string) null, false);
139                                 Assert.Fail ("#1");
140                         } catch (ArgumentNullException ex) {
141                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
142                                 Assert.IsNull (ex.InnerException, "#3");
143                                 Assert.IsNotNull (ex.Message, "#4");
144                                 Assert.IsNotNull (ex.ParamName, "#5");
145                                 Assert.AreEqual ("name", ex.ParamName, "#6");
146                         }
147                 }
148
149                 [Test]
150                 public void DateTimeFormat_Neutral_Culture ()
151                 {
152                         CultureInfo ci = new CultureInfo ("nl");
153                         try {
154                                 DateTimeFormatInfo dfi = ci.DateTimeFormat;
155 #if NET_4_0
156                                 Assert.IsNotNull (dfi, "#1");
157 #else
158                                 Assert.Fail ("#1:" + (dfi != null));
159 #endif
160                         } catch (NotSupportedException ex) {
161                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
162                                 Assert.IsNull (ex.InnerException, "#3");
163                                 Assert.IsNotNull (ex.Message, "#4");
164                         }
165                 }
166
167                 [Test] // bug #72081
168                 public void GetAllCulturesInvariant ()
169                 {
170                         CultureInfo invariant = CultureInfo.InvariantCulture;
171                         CultureInfo [] infos = CultureInfo.GetCultures (CultureTypes.AllCultures);
172                         foreach (CultureInfo culture in infos) {
173                                 if (culture.Equals (invariant))
174                                         return;
175                         }
176
177                         Assert.Fail ("InvariantCulture not found in the array from GetCultures()");
178                 }
179
180                 [Test]
181 #if !NET_4_0
182                 [ExpectedException (typeof (NotSupportedException))]
183 #endif
184                 public void TrySetNeutralCultureNotInvariant ()
185                 {
186                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("ar");
187                 }
188
189                 [Test]
190                 [Category ("TargetJvmNotWorking")] //OptionalCalendars not yet supported for TARGET_JVM.
191                 // make sure that all CultureInfo holds non-null calendars.
192                 public void OptionalCalendars ()
193                 {
194                         foreach (CultureInfo ci in CultureInfo.GetCultures (
195                                 CultureTypes.AllCultures))
196                                 Assert.IsNotNull (ci.OptionalCalendars, String.Format ("{0} {1}",
197                                         ci.LCID, ci.Name));
198                 }
199
200                 [Test] // bug #77347
201                 public void CloneNeutral ()
202                 {
203                         CultureInfo culture = new CultureInfo ("en");
204                         CultureInfo cultureClone = culture.Clone () as CultureInfo;
205                         Assert.IsTrue (culture.Equals (cultureClone));
206                 }
207
208                 [Test] // bug #81930
209                 public void IsReadOnly ()
210                 {
211                         CultureInfo ci;
212
213                         ci = new CultureInfo ("en-US");
214                         Assert.IsFalse (ci.IsReadOnly, "#A1");
215                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A2");
216                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A3");
217                         ci.NumberFormat.NumberGroupSeparator = ",";
218                         ci.NumberFormat = new NumberFormatInfo ();
219                         ci.DateTimeFormat.DateSeparator = "/";
220                         ci.DateTimeFormat = new DateTimeFormatInfo ();
221                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A4");
222                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A5");
223
224                         ci = new CultureInfo (CultureInfo.InvariantCulture.LCID);
225                         Assert.IsFalse (ci.IsReadOnly, "#B1");
226                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B2");
227                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B3");
228                         ci.NumberFormat.NumberGroupSeparator = ",";
229                         ci.NumberFormat = new NumberFormatInfo ();
230                         ci.DateTimeFormat.DateSeparator = "/";
231                         ci.DateTimeFormat = new DateTimeFormatInfo ();
232                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B4");
233                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B5");
234
235                         ci = CultureInfo.CurrentCulture;
236                         Assert.IsTrue (ci.IsReadOnly, "#C1:" + ci.DisplayName);
237                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#C2");
238                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#C3");
239                         try {
240                                 ci.NumberFormat.NumberGroupSeparator = ",";
241                                 Assert.Fail ("#C4");
242                         } catch (InvalidOperationException ex) {
243                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C5");
244                                 Assert.IsNull (ex.InnerException, "#C6");
245                                 Assert.IsNotNull (ex.Message, "#C7");
246                         }
247
248                         ci = CultureInfo.CurrentUICulture;
249                         Assert.IsTrue (ci.IsReadOnly, "#D1");
250                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#D2");
251                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#D3");
252                         try {
253                                 ci.NumberFormat.NumberGroupSeparator = ",";
254                                 Assert.Fail ("#D4");
255                         } catch (InvalidOperationException ex) {
256                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D5");
257                                 Assert.IsNull (ex.InnerException, "#D6");
258                                 Assert.IsNotNull (ex.Message, "#D7");
259                         }
260
261                         ci = CultureInfo.InvariantCulture;
262                         Assert.IsTrue (ci.IsReadOnly, "#F1");
263                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#F2");
264                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#F3");
265                         try {
266                                 ci.NumberFormat.NumberGroupSeparator = ",";
267                                 Assert.Fail ("#F4");
268                         } catch (InvalidOperationException ex) {
269                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F5");
270                                 Assert.IsNull (ex.InnerException, "#F6");
271                                 Assert.IsNotNull (ex.Message, "#F7");
272                         }
273
274                         ci = new CultureInfo (string.Empty);
275                         Assert.IsFalse (ci.IsReadOnly, "#G1");
276                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G2");
277                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G3");
278                         ci.NumberFormat.NumberGroupSeparator = ",";
279                         ci.NumberFormat = new NumberFormatInfo ();
280                         ci.DateTimeFormat.DateSeparator = "/";
281                         ci.DateTimeFormat = new DateTimeFormatInfo ();
282                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G4");
283                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G5");
284                 }
285
286                 [Test]
287                 public void IsReadOnly_GetCultures ()
288                 {
289                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
290                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
291                                 Assert.IsFalse (ci.IsReadOnly, "#1:" + cultureMsg);
292                                 if (ci.IsNeutralCulture)
293                                         continue;
294                                 Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
295                                 Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
296                         }
297                 }
298
299                 [Test]
300                 [Category ("NotWorking")]
301                 public void IsReadOnly_InstalledUICulture ()
302                 {
303                         CultureInfo ci = CultureInfo.InstalledUICulture;
304                         Assert.IsTrue (ci.IsReadOnly, "#1");
305                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#2");
306                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#3");
307                         try {
308                                 ci.NumberFormat.NumberGroupSeparator = ",";
309                                 Assert.Fail ("#4");
310                         } catch (InvalidOperationException ex) {
311                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#5");
312                                 Assert.IsNull (ex.InnerException, "#6");
313                                 Assert.IsNotNull (ex.Message, "#7");
314                         }
315                 }
316
317                 [Test] // bug #69652
318                 public void Norwegian ()
319                 {
320                         new CultureInfo ("no");
321                         new CultureInfo ("nb-NO");
322                         new CultureInfo ("nn-NO");
323                 }
324
325                 [Test]
326                 public void NumberFormat_Neutral_Culture ()
327                 {
328                         CultureInfo ci = new CultureInfo ("nl");
329                         try {
330                                 NumberFormatInfo nfi = ci.NumberFormat;
331 #if NET_4_0
332                                 Assert.IsNotNull (nfi, "#1");
333 #else
334                                 Assert.Fail ("#1:" + (nfi != null));
335 #endif
336                         } catch (NotSupportedException ex) {
337                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
338                                 Assert.IsNull (ex.InnerException, "#3");
339                                 Assert.IsNotNull (ex.Message, "#4");
340                         }
341                 }
342
343 #if NET_2_0
344                 [Test]
345                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
346                 public void GetCultureInfo_Identifier ()
347                 {
348                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
349                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
350                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.LCID);
351                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
352                                 if (culture.IsNeutralCulture)
353                                         continue;
354                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
355                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
356                         }
357                 }
358
359                 [Test]
360                 public void GetCultureInfo_Identifier_Negative ()
361                 {
362                         try {
363                                 CultureInfo.GetCultureInfo (-1);
364                                 Assert.Fail ("#1");
365                         } catch (ArgumentOutOfRangeException ex) {
366                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
367                                 Assert.IsNull (ex.InnerException, "#3");
368                                 Assert.IsNotNull (ex.Message, "#4");
369                                 Assert.IsNotNull (ex.ParamName, "#5");
370                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
371                         }
372                 }
373
374                 [Test]
375                 public void GetCultureInfo_Identifier_NotSupported ()
376                 {
377                         try {
378                                 CultureInfo.GetCultureInfo (666);
379                                 Assert.Fail ("#1");
380                         } catch (ArgumentException ex) {
381 #if NET_4_0
382                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
383 #else
384                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
385 #endif
386                                 Assert.IsNull (ex.InnerException, "#3");
387                                 Assert.IsNotNull (ex.Message, "#4");
388                                 Assert.IsNotNull (ex.ParamName, "#5");
389                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
390                         }
391                 }
392
393                 [Test]
394                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
395                 public void GetCultureInfo_Name ()
396                 {
397                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
398                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
399                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.Name);
400                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
401                                 if (culture.IsNeutralCulture)
402                                         continue;
403                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
404                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
405                         }
406                 }
407
408                 [Test]
409                 public void GetCultureInfo_Name_NotSupported ()
410                 {
411                         try {
412                                 CultureInfo.GetCultureInfo ("666");
413                                 Assert.Fail ("#1");
414                         } catch (ArgumentException ex) {
415 #if NET_4_0
416                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
417 #else
418                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
419 #endif
420                                 Assert.IsNull (ex.InnerException, "#3");
421                                 Assert.IsNotNull (ex.Message, "#4");
422                                 Assert.IsNotNull (ex.ParamName, "#5");
423                                 Assert.AreEqual ("name", ex.ParamName, "#6");
424                         }
425                 }
426
427                 [Test]
428                 public void GetCultureInfo_Name_Null ()
429                 {
430                         try {
431                                 CultureInfo.GetCultureInfo ((string) null);
432                                 Assert.Fail ("#1");
433                         } catch (ArgumentNullException ex) {
434                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
435                                 Assert.IsNull (ex.InnerException, "#3");
436                                 Assert.IsNotNull (ex.Message, "#4");
437                                 Assert.IsNotNull (ex.ParamName, "#5");
438                                 Assert.AreEqual ("name", ex.ParamName, "#6");
439                         }
440                 }
441 #endif
442
443                 [Test]
444                 public void UseUserOverride_CurrentCulture ()
445                 {
446                         CultureInfo ci = CultureInfo.CurrentCulture;
447                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
448                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
449
450                         ci = (CultureInfo) ci.Clone ();
451                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
452                 }
453
454                 [Test]
455                 public void UseUserOverride_CurrentUICulture ()
456                 {
457                         CultureInfo ci = CultureInfo.CurrentCulture;
458                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
459                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
460
461                         ci = (CultureInfo) ci.Clone ();
462                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
463                 }
464
465 #if NET_2_0
466                 [Test]
467                 public void UseUserOverride_GetCultureInfo ()
468                 {
469                         CultureInfo culture;
470
471                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
472                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
473                                 culture = CultureInfo.GetCultureInfo (ci.Name);
474                                 Assert.IsFalse (culture.UseUserOverride, "#1: " + cultureMsg);
475                                 culture = CultureInfo.GetCultureInfo (ci.LCID);
476                                 Assert.IsFalse (culture.UseUserOverride, "#2: " + cultureMsg);
477                         }
478                 }
479 #endif
480
481                 [Test]
482                 public void UseUserOverride_GetCultures ()
483                 {
484                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
485                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
486 #if NET_2_0
487                                 if (ci.LCID == CultureInfo.InvariantCulture.LCID)
488                                         Assert.IsFalse (ci.UseUserOverride, cultureMsg);
489                                 else
490                                         Assert.IsTrue (ci.UseUserOverride, cultureMsg);
491 #else
492                                 Assert.IsTrue (ci.UseUserOverride, cultureMsg);
493 #endif
494                         }
495                 }
496
497                 [Test]
498                 public void UseUserOverride_InvariantCulture ()
499                 {
500                         CultureInfo ci = CultureInfo.InvariantCulture;
501                         Assert.IsFalse (ci.UseUserOverride, "#21");
502
503                         ci = (CultureInfo) ci.Clone ();
504                         Assert.IsFalse (ci.UseUserOverride, "#2");
505                 }
506
507                 [Test]
508                 public void Bug402128 ()
509                 {
510                         var culture = new CultureInfo ("en-US");
511                         var ms = new MemoryStream ();
512                         var formatter = new BinaryFormatter ();
513                         formatter.Serialize (ms, culture);
514                         ms.Seek (0, SeekOrigin.Begin);
515                         var deserializedCulture = (CultureInfo) formatter.Deserialize (ms);
516                 }
517
518                 [Test]
519                 public void ZhHant ()
520                 {
521                         Assert.AreEqual (31748, new CultureInfo ("zh-Hant").LCID);
522 #if NET_2_0
523                         Assert.AreEqual (31748, CultureInfo.GetCultureInfo ("zh-Hant").LCID);
524 #endif
525                 }
526         }
527 }