2 // System.ComponentModel.UInt16Converter 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 UInt16ConverterTests
22 private UInt16Converter converter;
27 converter = new UInt16Converter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (short)), "#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_MinValue ()
50 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
51 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
52 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
53 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
54 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
58 public void ConvertFrom_MaxValue ()
60 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffff"), "#1");
61 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFF"), "#2");
62 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffff"), "#3");
63 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFF"), "#4");
64 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffff"), "#5");
65 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFF"), "#6");
69 [ExpectedException (typeof (NotSupportedException))]
70 public void ConvertFrom_Object ()
72 converter.ConvertFrom (new object ());
76 [ExpectedException (typeof (NotSupportedException))]
77 public void ConvertFrom_Int16 ()
79 converter.ConvertFrom ((short) 10);
83 [ExpectedException (typeof (NotSupportedException))]
84 public void ConvertFrom_Int32 ()
86 converter.ConvertFrom (10);
90 public void ConvertTo_MinValue ()
92 Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.InvariantCulture),
93 converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MinValue,
94 typeof (string)), "#1");
95 Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
96 converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MinValue,
97 typeof (string)), "#2");
98 Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
99 converter.ConvertTo (ushort.MinValue, typeof (string)), "#3");
103 public void ConvertTo_MaxValue ()
105 Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.InvariantCulture),
106 converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MaxValue,
107 typeof (string)), "#1");
108 Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
109 converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MaxValue,
110 typeof (string)), "#2");
111 Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
112 converter.ConvertTo (ushort.MaxValue, typeof (string)), "#3");
116 public void ConvertToString ()
118 CultureInfo culture = new MyCultureInfo ();
119 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
121 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
125 public void ConvertFromString_Invalid1 ()
128 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
130 } catch (Exception ex) {
131 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
132 Assert.IsNotNull (ex.InnerException, "#3");
133 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
138 public void ConvertFromString_Invalid2 ()
141 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
142 double.MaxValue.ToString(CultureInfo.InvariantCulture));
144 } catch (Exception ex) {
145 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
146 Assert.IsNotNull (ex.InnerException, "#3");
147 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
152 public void ConvertFromString_Invalid3 ()
155 converter.ConvertFromString ("*1");
157 } catch (Exception ex) {
158 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
159 Assert.IsNotNull (ex.InnerException, "#3");
160 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
165 public void ConvertFromString_Invalid4 ()
168 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
170 } catch (Exception ex) {
171 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
172 Assert.IsNotNull (ex.InnerException, "#3");
173 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
178 public void ConvertFrom_InvalidString1 ()
181 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
183 } catch (Exception ex) {
184 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
185 Assert.IsNotNull (ex.InnerException, "#3");
186 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
191 public void ConvertFrom_InvalidString2 ()
194 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
195 double.MaxValue.ToString (CultureInfo.InvariantCulture));
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 (), "#3");
205 public void ConvertFrom_InvalidString3 ()
208 converter.ConvertFrom ("*1");
210 } catch (Exception ex) {
211 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
212 Assert.IsNotNull (ex.InnerException, "#3");
213 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
218 public void ConvertFrom_InvalidString4 ()
221 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
223 } catch (Exception ex) {
224 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
225 Assert.IsNotNull (ex.InnerException, "#3");
226 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
231 private sealed class MyCultureInfo : CultureInfo
233 internal MyCultureInfo ()
238 public override object GetFormat (Type formatType)
240 if (formatType == typeof (NumberFormatInfo)) {
241 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
243 nfi.NegativeSign = "myNegativeSign";
244 return NumberFormatInfo.ReadOnly (nfi);
246 return base.GetFormat (formatType);
250 // adding this override in 1.x shows different result in .NET (it is ignored).
251 // Some compatibility kids might want to fix this issue.
252 public override NumberFormatInfo NumberFormat {
254 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
255 nfi.NegativeSign = "myNegativeSign";
258 set { throw new NotSupportedException (); }