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");
47 public void ConvertFrom_MinValue ()
49 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
50 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
51 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
52 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
53 Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
57 public void ConvertFrom_MaxValue ()
59 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffff"), "#1");
60 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFF"), "#2");
61 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffff"), "#3");
62 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFF"), "#4");
63 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffff"), "#5");
64 Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFF"), "#6");
68 [ExpectedException (typeof (NotSupportedException))]
69 public void ConvertFrom_Object ()
71 converter.ConvertFrom (new object ());
75 [ExpectedException (typeof (NotSupportedException))]
76 public void ConvertFrom_Int16 ()
78 converter.ConvertFrom ((short) 10);
82 [ExpectedException (typeof (NotSupportedException))]
83 public void ConvertFrom_Int32 ()
85 converter.ConvertFrom (10);
89 public void ConvertTo_MinValue ()
91 Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.InvariantCulture),
92 converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MinValue,
93 typeof (string)), "#1");
94 Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
95 converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MinValue,
96 typeof (string)), "#2");
97 Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
98 converter.ConvertTo (ushort.MinValue, typeof (string)), "#3");
102 public void ConvertTo_MaxValue ()
104 Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.InvariantCulture),
105 converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MaxValue,
106 typeof (string)), "#1");
107 Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
108 converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MaxValue,
109 typeof (string)), "#2");
110 Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
111 converter.ConvertTo (ushort.MaxValue, typeof (string)), "#3");
115 public void ConvertToString ()
117 CultureInfo culture = new MyCultureInfo ();
118 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
120 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
124 public void ConvertFromString_Invalid1 ()
127 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
129 } catch (Exception ex) {
130 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
131 Assert.IsNotNull (ex.InnerException, "#3");
132 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
137 public void ConvertFromString_Invalid2 ()
140 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
141 double.MaxValue.ToString(CultureInfo.InvariantCulture));
143 } catch (Exception ex) {
144 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
145 Assert.IsNotNull (ex.InnerException, "#3");
146 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
151 public void ConvertFromString_Invalid3 ()
154 converter.ConvertFromString ("*1");
156 } catch (Exception ex) {
157 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
158 Assert.IsNotNull (ex.InnerException, "#3");
159 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
164 public void ConvertFromString_Invalid4 ()
167 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
169 } catch (Exception ex) {
170 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
171 Assert.IsNotNull (ex.InnerException, "#3");
172 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
177 public void ConvertFrom_InvalidString1 ()
180 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
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 (), "#3");
190 public void ConvertFrom_InvalidString2 ()
193 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
194 double.MaxValue.ToString (CultureInfo.InvariantCulture));
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 (), "#3");
204 public void ConvertFrom_InvalidString3 ()
207 converter.ConvertFrom ("*1");
209 } catch (Exception ex) {
210 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
211 Assert.IsNotNull (ex.InnerException, "#3");
212 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
217 public void ConvertFrom_InvalidString4 ()
220 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
222 } catch (Exception ex) {
223 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
224 Assert.IsNotNull (ex.InnerException, "#3");
225 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
230 private sealed class MyCultureInfo : CultureInfo
232 internal MyCultureInfo ()
237 public override object GetFormat (Type formatType)
239 if (formatType == typeof (NumberFormatInfo)) {
240 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
242 nfi.NegativeSign = "myNegativeSign";
243 return NumberFormatInfo.ReadOnly (nfi);
245 return base.GetFormat (formatType);
249 // adding this override in 1.x shows different result in .NET (it is ignored).
250 // Some compatibility kids might want to fix this issue.
251 public override NumberFormatInfo NumberFormat {
253 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
254 nfi.NegativeSign = "myNegativeSign";
257 set { throw new NotSupportedException (); }