Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System / Test / System.ComponentModel / DoubleConverterTests.cs
1 //
2 // System.ComponentModel.DoubleConverter 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 DoubleConverterTests
21         {
22                 private DoubleConverter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new DoubleConverter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (double)), "#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_String ()
49                 {
50                         Assert.AreEqual (10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (NotSupportedException))]
55                 public void ConvertFrom_Object ()
56                 {
57                         converter.ConvertFrom (new object ());
58                 }
59
60                 [Test]
61                 [ExpectedException (typeof (NotSupportedException))]
62                 public void ConvertFrom_Int32 ()
63                 {
64                         converter.ConvertFrom (int.MaxValue);
65                 }
66
67                 [Test]
68                 public void ConvertTo_Negative ()
69                 {
70                         Assert.AreEqual ((-1.5D).ToString (CultureInfo.InvariantCulture),
71                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, -1.5D,
72                                 typeof (string)), "#1");
73                         Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
74                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, -1.5D,
75                                 typeof (string)), "#2");
76                         Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
77                                 converter.ConvertTo (-1.5D, typeof (string)), "#3");
78                 }
79
80                 [Test]
81                 public void ConvertTo_Positive ()
82                 {
83                         Assert.AreEqual (1.5D.ToString (CultureInfo.InvariantCulture),
84                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5D, 
85                                 typeof (string)), "#1");
86                         Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
87                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5D, 
88                                 typeof (string)), "#2");
89                         Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
90                                 converter.ConvertTo (1.5D, typeof (string)), "#3");
91                 }
92
93                 [Test]
94                 public void ConvertToString_Negative ()
95                 {
96                         Assert.AreEqual ((-1.5D).ToString (CultureInfo.InvariantCulture),
97                                 converter.ConvertToString (null, CultureInfo.InvariantCulture,
98                                 -1.5D), "#1");
99
100                         Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
101                                 converter.ConvertToString (null, -1.5D), "#2");
102                         Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
103                                 converter.ConvertToString (null, CultureInfo.CurrentCulture,
104                                 -1.5D), "#3");
105                         Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
106                                 converter.ConvertToString (-1.5D), "#4");
107                 }
108
109                 [Test]
110                 public void ConvertToString_Positive ()
111                 {
112                         Assert.AreEqual (1.5D.ToString (CultureInfo.InvariantCulture),
113                                 converter.ConvertToString (null, CultureInfo.InvariantCulture,
114                                 1.5D), "#1");
115
116                         Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
117                                 converter.ConvertToString (null, 1.5D), "#2");
118                         Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
119                                 converter.ConvertToString (null, CultureInfo.CurrentCulture,
120                                 1.5D), "#3");
121                         Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
122                                 converter.ConvertToString (1.5D), "#4");
123                 }
124
125                 [Test]
126                 public void ConvertToString ()
127                 {
128                         CultureInfo culture = new MyCultureInfo ();
129                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
130
131                         Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (double) -5), "#1");
132                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
133                 }
134
135                 [Test]
136                 public void ConvertFromString ()
137                 {
138                         CultureInfo culture = new MyCultureInfo ();
139                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
140
141                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
142                 }
143
144                 [Test]
145                 public void ConvertFrom_InvalidValue ()
146                 {
147                         try {
148                                 converter.ConvertFrom ("*1");
149                                 Assert.Fail ("#1");
150                         } catch (AssertionException) {
151                                 throw;
152                         } catch (Exception ex) {
153                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
154                                 Assert.IsNotNull (ex.InnerException, "#3");
155                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
156                         }
157                 }
158
159                 [Test]
160                 public void ConvertFrom_InvalidValue_Invariant ()
161                 {
162                         try {
163                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
164                                 Assert.Fail ("#1");
165                         } catch (AssertionException) {
166                                 throw;
167                         } catch (Exception ex) {
168                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
169                                 Assert.IsNotNull (ex.InnerException, "#3");
170                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
171                         }
172                 }
173
174                 [Test]
175                 public void ConvertFrom_Base10_MinOverflow ()
176                 {
177                         string minOverflow = double.MinValue.ToString (
178                                 CultureInfo.CurrentCulture);
179
180                         try {
181                                 converter.ConvertFrom (minOverflow);
182                                 Assert.Fail ("#1");
183                         } catch (AssertionException) {
184                                 throw;
185                         } catch (Exception ex) {
186                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
187                                 Assert.IsNotNull (ex.InnerException, "#3");
188                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
189                         }
190                 }
191
192                 [Test]
193                 public void ConvertFrom_Base10_MinOverflow_Invariant ()
194                 {
195                         string minOverflow = double.MinValue.ToString (
196                                 CultureInfo.InvariantCulture);
197
198                         try {
199                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
200                                         minOverflow);
201                                 Assert.Fail ("#1");
202                         } catch (AssertionException) {
203                                 throw;
204                         } catch (Exception ex) {
205                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
206                                 Assert.IsNotNull (ex.InnerException, "#3");
207                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
208                         }
209                 }
210
211                 [Test]
212                 public void ConvertFrom_Base10_MaxOverflow ()
213                 {
214                         string maxOverflow = double.MaxValue.ToString (
215                                 CultureInfo.CurrentCulture);
216
217                         try {
218                                 converter.ConvertFrom (maxOverflow);
219                                 Assert.Fail ("#1");
220                         } catch (AssertionException) {
221                                 throw;
222                         } catch (Exception ex) {
223                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
224                                 Assert.IsNotNull (ex.InnerException, "#3");
225                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
226                         }
227                 }
228
229                 [Test]
230                 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
231                 {
232                         string maxOverflow = double.MaxValue.ToString (
233                                 CultureInfo.InvariantCulture);
234
235                         try {
236                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
237                                         maxOverflow);
238                                 Assert.Fail ("#1");
239                         } catch (AssertionException) {
240                                 throw;
241                         } catch (Exception ex) {
242                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
243                                 Assert.IsNotNull (ex.InnerException, "#3");
244                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
245                         }
246                 }
247
248                 [Test]
249                 public void ConvertFromString_InvalidValue ()
250                 {
251                         try {
252                                 converter.ConvertFromString ("*1");
253                                 Assert.Fail ("#1");
254                         } catch (AssertionException) {
255                                 throw;
256                         } catch (Exception ex) {
257                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
258                                 Assert.IsNotNull (ex.InnerException, "#3");
259                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
260                         }
261                 }
262
263                 [Test]
264                 public void ConvertFromString_InvalidValue_Invariant ()
265                 {
266                         try {
267                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
268                                 Assert.Fail ("#1");
269                         } catch (AssertionException) {
270                                 throw;
271                         } catch (Exception ex) {
272                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
273                                 Assert.IsNotNull (ex.InnerException, "#3");
274                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
275                         }
276                 }
277
278                 [Test]
279                 public void ConvertFromString_Base10_MinOverflow ()
280                 {
281                         string minOverflow = double.MinValue.ToString (
282                                 CultureInfo.CurrentCulture);
283
284                         try {
285                                 converter.ConvertFromString (minOverflow);
286                                 Assert.Fail ("#1");
287                         } catch (AssertionException) {
288                                 throw;
289                         } catch (Exception ex) {
290                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
291                                 Assert.IsNotNull (ex.InnerException, "#3");
292                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
293                         }
294                 }
295
296                 [Test]
297                 public void ConvertFromString_Base10_MinOverflow_Invariant ()
298                 {
299                         string minOverflow = double.MinValue.ToString (
300                                 CultureInfo.InvariantCulture);
301
302                         try {
303                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
304                                         minOverflow);
305                                 Assert.Fail ("#1");
306                         } catch (AssertionException) {
307                                 throw;
308                         } catch (Exception ex) {
309                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
310                                 Assert.IsNotNull (ex.InnerException, "#3");
311                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
312                         }
313                 }
314
315                 [Test]
316                 public void ConvertFromString_Base10_MaxOverflow ()
317                 {
318                         string maxOverflow = double.MaxValue.ToString (
319                                 CultureInfo.CurrentCulture);
320
321                         try {
322                                 converter.ConvertFromString (maxOverflow);
323                                 Assert.Fail ("#1");
324                         } catch (AssertionException) {
325                                 throw;
326                         } catch (Exception ex) {
327                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
328                                 Assert.IsNotNull (ex.InnerException, "#3");
329                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
330                         }
331                 }
332
333                 [Test]
334                 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
335                 {
336                         string maxOverflow = double.MaxValue.ToString (
337                                 CultureInfo.InvariantCulture);
338
339                         try {
340                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
341                                         maxOverflow);
342                                 Assert.Fail ("#1");
343                         } catch (AssertionException) {
344                                 throw;
345                         } catch (Exception ex) {
346                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
347                                 Assert.IsNotNull (ex.InnerException, "#3");
348                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
349                         }
350                 }
351
352                 [Serializable]
353                 private sealed class MyCultureInfo : CultureInfo
354                 {
355                         internal MyCultureInfo ()
356                                 : base ("en-US")
357                         {
358                         }
359
360                         public override object GetFormat (Type formatType)
361                         {
362                                 if (formatType == typeof (NumberFormatInfo)) {
363                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
364
365                                         nfi.NegativeSign = "myNegativeSign";
366                                         return NumberFormatInfo.ReadOnly (nfi);
367                                 } else {
368                                         return base.GetFormat (formatType);
369                                 }
370                         }
371
372 #if NET_2_0
373 // adding this override in 1.x shows different result in .NET (it is ignored).
374 // Some compatibility kids might want to fix this issue.
375                         public override NumberFormatInfo NumberFormat {
376                                 get {
377                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
378                                         nfi.NegativeSign = "myNegativeSign";
379                                         return nfi;
380                                 }
381                                 set { throw new NotSupportedException (); }
382                         }
383 #endif
384                 }
385         }
386 }