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