Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System / Test / System.ComponentModel / CultureInfoConverterTest.cs
1 //
2 // System.ComponentModel.CultureInfoConverter test cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2008 Gert Driesen
8 //
9
10 using System;
11 using System.ComponentModel;
12 using System.ComponentModel.Design.Serialization;
13 using System.Globalization;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.ComponentModel
18 {
19         [TestFixture]
20         public class CultureInfoConverterTest
21         {
22                 private CultureInfoConverter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new CultureInfoConverter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (CultureInfo)), "#2");
35                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
36                         Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#4");
37                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#5");
38                 }
39
40                 [Test]
41                 public void CanConvertTo ()
42                 {
43                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
44                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2");
45                         Assert.IsFalse (converter.CanConvertTo (typeof (CultureInfo)), "#3");
46                         Assert.IsFalse (converter.CanConvertTo (typeof (int)), "#4");
47                         Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#5");
48                 }
49
50                 [Test]
51                 public void ConvertFrom_String ()
52                 {
53                         CultureInfo c;
54
55                         c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
56                                 String.Empty);
57                         Assert.AreEqual (CultureInfo.InvariantCulture, c, "#1");
58
59                         c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
60                                 "nl-BE");
61                         Assert.AreEqual (new CultureInfo ("nl-BE"), c, "#2");
62
63                         c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
64                                 "Dut");
65                         Assert.AreEqual (new CultureInfo ("nl"), c, "#3");
66
67                         c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
68                                 "Dutch (Bel");
69                         Assert.AreEqual (new CultureInfo ("nl-BE"), c, "#4");
70
71                         c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
72                                 "duTcH (Bel");
73                         Assert.AreEqual (new CultureInfo ("nl-BE"), c, "#5");
74
75                         c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
76                                 "(Default)");
77                         Assert.AreEqual (CultureInfo.InvariantCulture, c, "#6");
78
79 #if ONLY_1_1
80                         c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
81                                 "(defAuLt)");
82                         Assert.AreEqual (CultureInfo.InvariantCulture, c, "#6");
83 #endif
84                 }
85
86                 [Test]
87                 public void ConvertFrom_String_IncompleteName ()
88                 {
89                         try {
90                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
91                                         "nl-B");
92                                 Assert.Fail ("#1");
93                         } catch (ArgumentException ex) {
94                                 // The nl-B culture cannot be converted to a
95                                 // CultureInfo object on this computer
96                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
97                                 Assert.IsNull (ex.InnerException, "#3");
98                                 Assert.IsNotNull (ex.Message, "#4");
99                                 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#5");
100                                 Assert.IsTrue (ex.Message.IndexOf ("nl-B") != -1, "#6");
101                                 Assert.IsNull (ex.ParamName, "#7");
102                         }
103                 }
104
105                 [Test]
106                 public void ConvertFrom_String_InvalidCulture ()
107                 {
108 #if NET_2_0
109                         try {
110                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
111                                         "(default)");
112                                 Assert.Fail ("#A1");
113                         } catch (ArgumentException ex) {
114                                 // The (default) culture cannot be converted to
115                                 // a CultureInfo object on this computer
116                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
117                                 Assert.IsNull (ex.InnerException, "#A3");
118                                 Assert.IsNotNull (ex.Message, "#A4");
119                                 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#A5");
120                                 Assert.IsTrue (ex.Message.IndexOf ("(default)") != -1, "#A6");
121                                 Assert.IsNull (ex.ParamName, "#A7");
122                         }
123 #endif
124
125                         try {
126                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
127                                         " ");
128                                 Assert.Fail ("#B1");
129                         } catch (ArgumentException ex) {
130                                 // The   culture cannot be converted to
131                                 // a CultureInfo object on this computer
132                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
133                                 Assert.IsNull (ex.InnerException, "#B3");
134                                 Assert.IsNotNull (ex.Message, "#B4");
135                                 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#B5");
136                                 Assert.IsTrue (ex.Message.IndexOf ("   ") != -1, "#B6");
137                                 Assert.IsNull (ex.ParamName, "#B7");
138                         }
139
140                         try {
141                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
142                                         "\r\n");
143                                 Assert.Fail ("#C1");
144                         } catch (ArgumentException ex) {
145                                 // The \r\n culture cannot be converted to
146                                 // a CultureInfo object on this computer
147                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
148                                 Assert.IsNull (ex.InnerException, "#C3");
149                                 Assert.IsNotNull (ex.Message, "#C4");
150                                 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#C5");
151                                 Assert.IsTrue (ex.Message.IndexOf ("\r\n") != -1, "#C6");
152                                 Assert.IsNull (ex.ParamName, "#C7");
153                         }
154                 }
155
156                 [Test]
157                 public void ConvertFrom_Value_Null ()
158                 {
159                         try {
160                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
161                                         (string) null);
162                                 Assert.Fail ("#1");
163                         } catch (NotSupportedException ex) {
164                                 // CultureInfoConverter cannot convert from (null)
165                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
166                                 Assert.IsNull (ex.InnerException, "#3");
167                                 Assert.IsNotNull (ex.Message, "#4");
168                                 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfoConverter).Name) != -1, "#5");
169                                 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#6");
170                         }
171                 }
172
173                 [Test]
174                 public void ConvertToString ()
175                 {
176                         string result;
177
178                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
179                                 new MyCultureInfo ());
180                         Assert.AreEqual ("display", result, "#1");
181
182                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
183                                 null);
184                         Assert.AreEqual ("(Default)", result, "#2");
185
186                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
187                                 CultureInfo.InvariantCulture);
188                         Assert.AreEqual ("(Default)", result, "#3");
189
190                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
191                                 new CultureInfo ("nl-BE"));
192                         Assert.AreEqual ("Dutch (Belgium)", result, "#4");
193                 }
194
195                 [Serializable]
196                 private sealed class MyCultureInfo : CultureInfo
197                 {
198                         internal MyCultureInfo () : base ("nl-BE")
199                         {
200                         }
201
202                         public override string DisplayName {
203                                 get { return "display"; }
204                         }
205
206                         public override string EnglishName {
207                                 get { return "english"; }
208                         }
209                 }
210
211 #if NET_4_0
212                 [Test]
213                 public void GetCultureName ()
214                 {
215                         CustomCultureInfoConverter custom_converter = new CustomCultureInfoConverter ();
216
217                         CultureInfo fr_culture = CultureInfo.GetCultureInfo ("fr-FR");
218                         Assert.AreEqual (fr_culture.Name, custom_converter.GetCultureName (fr_culture), "#A1");
219
220                         CultureInfo es_culture = CultureInfo.GetCultureInfo ("es-MX");
221                         Assert.AreEqual (es_culture.Name, custom_converter.GetCultureName (es_culture), "#A2");
222                 }
223
224                 class CustomCultureInfoConverter : CultureInfoConverter
225                 {
226                         public new string GetCultureName (CultureInfo culture)
227                         {
228                                 return base.GetCultureName (culture);
229                         }
230                 }
231 #endif
232         }
233 }