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");
47 public void ConvertFrom_String ()
49 Assert.AreEqual (10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
53 [ExpectedException (typeof (NotSupportedException))]
54 public void ConvertFrom_Object ()
56 converter.ConvertFrom (new object ());
60 [ExpectedException (typeof (NotSupportedException))]
61 public void ConvertFrom_Int32 ()
63 converter.ConvertFrom (int.MaxValue);
67 public void ConvertTo_Negative ()
69 Assert.AreEqual ((-1.5D).ToString (CultureInfo.InvariantCulture),
70 converter.ConvertTo (null, CultureInfo.InvariantCulture, -1.5D,
71 typeof (string)), "#1");
72 Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
73 converter.ConvertTo (null, CultureInfo.CurrentCulture, -1.5D,
74 typeof (string)), "#2");
75 Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
76 converter.ConvertTo (-1.5D, typeof (string)), "#3");
80 public void ConvertTo_Positive ()
82 Assert.AreEqual (1.5D.ToString (CultureInfo.InvariantCulture),
83 converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5D,
84 typeof (string)), "#1");
85 Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
86 converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5D,
87 typeof (string)), "#2");
88 Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
89 converter.ConvertTo (1.5D, typeof (string)), "#3");
93 public void ConvertToString_Negative ()
95 Assert.AreEqual ((-1.5D).ToString (CultureInfo.InvariantCulture),
96 converter.ConvertToString (null, CultureInfo.InvariantCulture,
99 Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
100 converter.ConvertToString (null, -1.5D), "#2");
101 Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
102 converter.ConvertToString (null, CultureInfo.CurrentCulture,
104 Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
105 converter.ConvertToString (-1.5D), "#4");
109 public void ConvertToString_Positive ()
111 Assert.AreEqual (1.5D.ToString (CultureInfo.InvariantCulture),
112 converter.ConvertToString (null, CultureInfo.InvariantCulture,
115 Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
116 converter.ConvertToString (null, 1.5D), "#2");
117 Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
118 converter.ConvertToString (null, CultureInfo.CurrentCulture,
120 Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
121 converter.ConvertToString (1.5D), "#4");
125 public void ConvertToString ()
127 CultureInfo culture = new MyCultureInfo ();
128 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
130 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (double) -5), "#1");
131 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
135 public void ConvertFromString ()
137 CultureInfo culture = new MyCultureInfo ();
138 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
140 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
144 public void ConvertFrom_InvalidValue ()
147 converter.ConvertFrom ("*1");
149 } catch (AssertionException) {
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");
159 public void ConvertFrom_InvalidValue_Invariant ()
162 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
164 } catch (AssertionException) {
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");
174 public void ConvertFrom_Base10_MinOverflow ()
176 string minOverflow = double.MinValue.ToString (
177 CultureInfo.CurrentCulture);
180 converter.ConvertFrom (minOverflow);
182 } catch (AssertionException) {
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");
192 public void ConvertFrom_Base10_MinOverflow_Invariant ()
194 string minOverflow = double.MinValue.ToString (
195 CultureInfo.InvariantCulture);
198 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
201 } catch (AssertionException) {
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");
211 public void ConvertFrom_Base10_MaxOverflow ()
213 string maxOverflow = double.MaxValue.ToString (
214 CultureInfo.CurrentCulture);
217 converter.ConvertFrom (maxOverflow);
219 } catch (AssertionException) {
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");
229 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
231 string maxOverflow = double.MaxValue.ToString (
232 CultureInfo.InvariantCulture);
235 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
238 } catch (AssertionException) {
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");
248 public void ConvertFromString_InvalidValue ()
251 converter.ConvertFromString ("*1");
253 } catch (AssertionException) {
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");
263 public void ConvertFromString_InvalidValue_Invariant ()
266 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
268 } catch (AssertionException) {
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");
278 public void ConvertFromString_Base10_MinOverflow ()
280 string minOverflow = double.MinValue.ToString (
281 CultureInfo.CurrentCulture);
284 converter.ConvertFromString (minOverflow);
286 } catch (AssertionException) {
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");
296 public void ConvertFromString_Base10_MinOverflow_Invariant ()
298 string minOverflow = double.MinValue.ToString (
299 CultureInfo.InvariantCulture);
302 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
305 } catch (AssertionException) {
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");
315 public void ConvertFromString_Base10_MaxOverflow ()
317 string maxOverflow = double.MaxValue.ToString (
318 CultureInfo.CurrentCulture);
321 converter.ConvertFromString (maxOverflow);
323 } catch (AssertionException) {
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");
333 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
335 string maxOverflow = double.MaxValue.ToString (
336 CultureInfo.InvariantCulture);
339 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
342 } catch (AssertionException) {
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");
352 private sealed class MyCultureInfo : CultureInfo
354 internal MyCultureInfo ()
359 public override object GetFormat (Type formatType)
361 if (formatType == typeof (NumberFormatInfo)) {
362 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
364 nfi.NegativeSign = "myNegativeSign";
365 return NumberFormatInfo.ReadOnly (nfi);
367 return base.GetFormat (formatType);
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 {
376 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
377 nfi.NegativeSign = "myNegativeSign";
380 set { throw new NotSupportedException (); }