2 // System.ComponentModel.SingleConverter 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 SingleConverterTests
22 private SingleConverter converter;
27 converter = new SingleConverter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (float)), "#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 ()
70 Assert.AreEqual (1.5f.ToString (CultureInfo.InvariantCulture),
71 converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f,
72 typeof (string)), "#1");
73 Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
74 converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5f,
75 typeof (string)), "#2");
76 Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
77 converter.ConvertTo (1.5f, typeof (string)), "#3");
81 public void ConvertToString ()
83 CultureInfo culture = new MyCultureInfo ();
84 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
86 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (float) -5), "#1");
87 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
91 public void ConvertFromString ()
93 CultureInfo culture = new MyCultureInfo ();
94 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
96 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
100 public void ConvertFromString_Invalid1 ()
103 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
105 } catch (AssertionException) {
107 } catch (Exception ex) {
108 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
109 Assert.IsNotNull (ex.InnerException, "#3");
110 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
115 public void ConvertFromString_Invalid2 ()
118 converter.ConvertFromString ("*1");
120 } catch (AssertionException) {
122 } catch (Exception ex) {
123 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
124 Assert.IsNotNull (ex.InnerException, "#3");
125 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
130 public void ConvertFrom_InvalidString1 ()
133 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
135 } catch (AssertionException) {
137 } catch (Exception ex) {
138 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
139 Assert.IsNotNull (ex.InnerException, "#3");
140 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
145 public void ConvertFrom_InvalidString2 ()
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 (), "#4");
160 public void ConvertFromString_Hex1 ()
163 converter.ConvertFromString ("#10");
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 (), "#4");
175 public void ConvertFromString_Hex2 ()
178 converter.ConvertFromString ("0x10");
180 } catch (AssertionException) {
182 } catch (Exception ex) {
183 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
184 Assert.IsNotNull (ex.InnerException, "#3");
185 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
190 public void ConvertFrom_HexString1 ()
193 converter.ConvertFrom ("#10");
195 } catch (AssertionException) {
197 } catch (Exception ex) {
198 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
199 Assert.IsNotNull (ex.InnerException, "#3");
200 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
205 public void ConvertFrom_HexString2 ()
208 converter.ConvertFrom ("0x10");
210 } catch (AssertionException) {
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 (), "#4");
220 private sealed class MyCultureInfo : CultureInfo
222 internal MyCultureInfo ()
227 public override object GetFormat (Type formatType)
229 if (formatType == typeof (NumberFormatInfo)) {
230 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
232 nfi.NegativeSign = "myNegativeSign";
233 return NumberFormatInfo.ReadOnly (nfi);
235 return base.GetFormat (formatType);
240 // adding this override in 1.x shows different result in .NET (it is ignored).
241 // Some compatibility kids might want to fix this issue.
242 public override NumberFormatInfo NumberFormat {
244 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
245 nfi.NegativeSign = "myNegativeSign";
248 set { throw new NotSupportedException (); }