Add new TypeDescriptionProviderTest.cs test file.
[mono.git] / mcs / class / System / Test / System.ComponentModel / Int32ConverterTests.cs
1 //
2 // System.ComponentModel.Int32Converter test cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2005 Novell, Inc. (http://www.ximian.com)
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 Int32ConverterTests
21         {
22                 private Int32Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new Int32Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#2");
35                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
36                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#4");
37                 }
38
39                 [Test]
40                 public void CanConvertTo ()
41                 {
42                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
43                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2");
44                         Assert.IsTrue (converter.CanConvertTo (typeof (int)), "#3");
45                 }
46
47                 [Test]
48                 public void ConvertFrom_MinValue ()
49                 {
50                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#80000000"), "#1");
51                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x80000000"), "#2");
52                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X80000000"), "#3");
53                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x80000000"), "#4");
54                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X80000000"), "#5");
55                 }
56
57                 [Test]
58                 public void ConvertFrom_MaxValue ()
59                 {
60                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffff"), "#1");
61                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFF"), "#2");
62                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffff"), "#3");
63                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFF"), "#4");
64                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffff"), "#5");
65                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFF"), "#6");
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (NotSupportedException))]
70                 public void ConvertFrom_Object ()
71                 {
72                         converter.ConvertFrom (new object ());
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (NotSupportedException))]
77                 public void ConvertFrom_Int32 ()
78                 {
79                         converter.ConvertFrom (int.MaxValue);
80                 }
81
82                 [Test]
83                 public void ConvertTo_MinValue ()
84                 {
85                         Assert.AreEqual (int.MinValue.ToString (CultureInfo.InvariantCulture),
86                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MinValue,
87                                 typeof (string)), "#1");
88                         Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
89                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MinValue,
90                                 typeof (string)), "#2");
91                         Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
92                                 converter.ConvertTo (int.MinValue, typeof (string)), "#3");
93                 }
94
95                 [Test]
96                 public void ConvertTo_MaxValue ()
97                 {
98                         Assert.AreEqual (int.MaxValue.ToString (CultureInfo.InvariantCulture),
99                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MaxValue,
100                                 typeof (string)), "#1");
101                         Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
102                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MaxValue,
103                                 typeof (string)), "#2");
104                         Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
105                                 converter.ConvertTo (int.MaxValue, typeof (string)), "#3");
106                 }
107
108                 [Test]
109                 public void ConvertToString ()
110                 {
111                         CultureInfo culture = new MyCultureInfo ();
112                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
113
114                         Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
115                 }
116
117                 [Test]
118                 public void ConvertFromString ()
119                 {
120                         CultureInfo culture = new MyCultureInfo ();
121                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
122
123                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
124                 }
125
126                 [Test]
127                 public void ConvertFromString_Invalid1 ()
128                 {
129                         try {
130                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
131                                 Assert.Fail ("#1");
132                         } catch (Exception ex) {
133                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
134                                 Assert.IsNotNull (ex.InnerException, "#3");
135                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
136                         }
137                 }
138
139                 [Test]
140                 public void ConvertFromString_Invalid2 ()
141                 {
142                         try {
143                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
144                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
145                                 Assert.Fail ("#1");
146                         } catch (Exception ex) {
147                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
148                                 Assert.IsNotNull (ex.InnerException, "#3");
149                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
150                         }
151                 }
152
153                 [Test]
154                 public void ConvertFromString_Invalid3 ()
155                 {
156                         try {
157                                 converter.ConvertFromString ("*1");
158                                 Assert.Fail ("#1");
159                         } catch (Exception ex) {
160                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
161                                 Assert.IsNotNull (ex.InnerException, "#3");
162                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
163                         }
164                 }
165
166                 [Test]
167                 public void ConvertFromString_Invalid4 ()
168                 {
169                         try {
170                                 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
171                                 Assert.Fail ("#1");
172                         } catch (Exception ex) {
173                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
174                                 Assert.IsNotNull (ex.InnerException, "#3");
175                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
176                         }
177                 }
178
179                 [Test]
180                 public void ConvertFrom_InvalidString1 ()
181                 {
182                         try {
183                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
184                                 Assert.Fail ("#1");
185                         } catch (Exception ex) {
186                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
187                                 Assert.IsNotNull (ex.InnerException, "#3");
188                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
189                         }
190                 }
191
192                 [Test]
193                 public void ConvertFrom_InvalidString2 ()
194                 {
195                         try {
196                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
197                                         double.MaxValue.ToString (CultureInfo.InvariantCulture));
198                                 Assert.Fail ("#1");
199                         } catch (Exception ex) {
200                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
201                                 Assert.IsNotNull (ex.InnerException, "#3");
202                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
203                         }
204                 }
205
206                 [Test]
207                 public void ConvertFrom_InvalidString3 ()
208                 {
209                         try {
210                                 converter.ConvertFrom ("*1");
211                                 Assert.Fail ("#1");
212                         } catch (Exception ex) {
213                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
214                                 Assert.IsNotNull (ex.InnerException, "#3");
215                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
216                         }
217                 }
218
219                 [Test]
220                 public void ConvertFrom_InvalidString4 ()
221                 {
222                         try {
223                                 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
224                                 Assert.Fail ("#1");
225                         } catch (Exception ex) {
226                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
227                                 Assert.IsNotNull (ex.InnerException, "#3");
228                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
229                         }
230                 }
231
232                 [Serializable]
233                 private sealed class MyCultureInfo : CultureInfo
234                 {
235                         internal MyCultureInfo ()
236                                 : base ("en-US")
237                         {
238                         }
239
240                         public override object GetFormat (Type formatType)
241                         {
242                                 if (formatType == typeof (NumberFormatInfo)) {
243                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
244
245                                         nfi.NegativeSign = "myNegativeSign";
246                                         return NumberFormatInfo.ReadOnly (nfi);
247                                 } else {
248                                         return base.GetFormat (formatType);
249                                 }
250                         }
251                 }
252         }
253 }
254