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");
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 ()
69 Assert.AreEqual (1.5f.ToString (CultureInfo.InvariantCulture),
70 converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f,
71 typeof (string)), "#1");
72 Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
73 converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5f,
74 typeof (string)), "#2");
75 Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
76 converter.ConvertTo (1.5f, typeof (string)), "#3");
80 public void ConvertToString ()
82 CultureInfo culture = new MyCultureInfo ();
83 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
85 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (float) -5), "#1");
86 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
90 public void ConvertFromString ()
92 CultureInfo culture = new MyCultureInfo ();
93 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
95 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
99 public void ConvertFromString_Invalid1 ()
102 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
104 } catch (AssertionException) {
106 } catch (Exception ex) {
107 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
108 Assert.IsNotNull (ex.InnerException, "#3");
109 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
114 public void ConvertFromString_Invalid2 ()
117 converter.ConvertFromString ("*1");
119 } catch (AssertionException) {
121 } catch (Exception ex) {
122 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
123 Assert.IsNotNull (ex.InnerException, "#3");
124 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
129 public void ConvertFrom_InvalidString1 ()
132 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
134 } catch (AssertionException) {
136 } catch (Exception ex) {
137 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
138 Assert.IsNotNull (ex.InnerException, "#3");
139 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
144 public void ConvertFrom_InvalidString2 ()
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 (), "#4");
159 public void ConvertFromString_Hex1 ()
162 converter.ConvertFromString ("#10");
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 (), "#4");
174 public void ConvertFromString_Hex2 ()
177 converter.ConvertFromString ("0x10");
179 } catch (AssertionException) {
181 } catch (Exception ex) {
182 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
183 Assert.IsNotNull (ex.InnerException, "#3");
184 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
189 public void ConvertFrom_HexString1 ()
192 converter.ConvertFrom ("#10");
194 } catch (AssertionException) {
196 } catch (Exception ex) {
197 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
198 Assert.IsNotNull (ex.InnerException, "#3");
199 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
204 public void ConvertFrom_HexString2 ()
207 converter.ConvertFrom ("0x10");
209 } catch (AssertionException) {
211 } catch (Exception ex) {
212 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
213 Assert.IsNotNull (ex.InnerException, "#3");
214 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
219 private sealed class MyCultureInfo : CultureInfo
221 internal MyCultureInfo ()
226 public override object GetFormat (Type formatType)
228 if (formatType == typeof (NumberFormatInfo)) {
229 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
231 nfi.NegativeSign = "myNegativeSign";
232 return NumberFormatInfo.ReadOnly (nfi);
234 return base.GetFormat (formatType);
239 // adding this override in 1.x shows different result in .NET (it is ignored).
240 // Some compatibility kids might want to fix this issue.
241 public override NumberFormatInfo NumberFormat {
243 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
244 nfi.NegativeSign = "myNegativeSign";
247 set { throw new NotSupportedException (); }