2 // System.ComponentModel.DoubleConverter test cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (c) 2005 Novell, Inc. (http://www.ximian.com)
11 using System.ComponentModel;
12 using System.ComponentModel.Design.Serialization;
13 using System.Globalization;
15 using NUnit.Framework;
17 namespace MonoTests.System.ComponentModel
20 public class DoubleConverterTests
22 private DoubleConverter converter;
27 converter = new DoubleConverter ();
31 public void CanConvertFrom ()
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");
40 public void CanConvertTo ()
42 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
43 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2");
44 Assert.IsTrue (converter.CanConvertTo (typeof (int)), "#3");
48 public void ConvertFrom_String ()
50 Assert.AreEqual (10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
54 [ExpectedException (typeof (NotSupportedException))]
55 public void ConvertFrom_Object ()
57 converter.ConvertFrom (new object ());
61 [ExpectedException (typeof (NotSupportedException))]
62 public void ConvertFrom_Int32 ()
64 converter.ConvertFrom (int.MaxValue);
68 public void ConvertTo_Negative ()
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");
81 public void ConvertTo_Positive ()
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");
94 public void ConvertToString_Negative ()
96 Assert.AreEqual ((-1.5D).ToString (CultureInfo.InvariantCulture),
97 converter.ConvertToString (null, CultureInfo.InvariantCulture,
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,
105 Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
106 converter.ConvertToString (-1.5D), "#4");
110 public void ConvertToString_Positive ()
112 Assert.AreEqual (1.5D.ToString (CultureInfo.InvariantCulture),
113 converter.ConvertToString (null, CultureInfo.InvariantCulture,
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,
121 Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
122 converter.ConvertToString (1.5D), "#4");
126 public void ConvertToString ()
128 CultureInfo culture = new MyCultureInfo ();
129 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
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");
136 public void ConvertFromString ()
138 CultureInfo culture = new MyCultureInfo ();
139 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
141 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
145 public void ConvertFrom_InvalidValue ()
148 converter.ConvertFrom ("*1");
150 } catch (AssertionException) {
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");
160 public void ConvertFrom_InvalidValue_Invariant ()
163 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
165 } catch (AssertionException) {
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");
175 public void ConvertFrom_Base10_MinOverflow ()
177 string minOverflow = double.MinValue.ToString (
178 CultureInfo.CurrentCulture);
181 converter.ConvertFrom (minOverflow);
183 } catch (AssertionException) {
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");
193 public void ConvertFrom_Base10_MinOverflow_Invariant ()
195 string minOverflow = double.MinValue.ToString (
196 CultureInfo.InvariantCulture);
199 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
202 } catch (AssertionException) {
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");
212 public void ConvertFrom_Base10_MaxOverflow ()
214 string maxOverflow = double.MaxValue.ToString (
215 CultureInfo.CurrentCulture);
218 converter.ConvertFrom (maxOverflow);
220 } catch (AssertionException) {
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");
230 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
232 string maxOverflow = double.MaxValue.ToString (
233 CultureInfo.InvariantCulture);
236 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
239 } catch (AssertionException) {
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");
249 public void ConvertFromString_InvalidValue ()
252 converter.ConvertFromString ("*1");
254 } catch (AssertionException) {
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");
264 public void ConvertFromString_InvalidValue_Invariant ()
267 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
269 } catch (AssertionException) {
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");
279 public void ConvertFromString_Base10_MinOverflow ()
281 string minOverflow = double.MinValue.ToString (
282 CultureInfo.CurrentCulture);
285 converter.ConvertFromString (minOverflow);
287 } catch (AssertionException) {
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");
297 public void ConvertFromString_Base10_MinOverflow_Invariant ()
299 string minOverflow = double.MinValue.ToString (
300 CultureInfo.InvariantCulture);
303 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
306 } catch (AssertionException) {
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");
316 public void ConvertFromString_Base10_MaxOverflow ()
318 string maxOverflow = double.MaxValue.ToString (
319 CultureInfo.CurrentCulture);
322 converter.ConvertFromString (maxOverflow);
324 } catch (AssertionException) {
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");
334 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
336 string maxOverflow = double.MaxValue.ToString (
337 CultureInfo.InvariantCulture);
340 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
343 } catch (AssertionException) {
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");
353 private sealed class MyCultureInfo : CultureInfo
355 internal MyCultureInfo ()
360 public override object GetFormat (Type formatType)
362 if (formatType == typeof (NumberFormatInfo)) {
363 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
365 nfi.NegativeSign = "myNegativeSign";
366 return NumberFormatInfo.ReadOnly (nfi);
368 return base.GetFormat (formatType);
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 {
377 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
378 nfi.NegativeSign = "myNegativeSign";
381 set { throw new NotSupportedException (); }