2 // System.ComponentModel.UInt32Converter 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 UInt32ConverterTests
22 private UInt32Converter converter;
27 converter = new UInt32Converter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (uint)), "#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 (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
51 Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
52 Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
53 Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
54 Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
58 public void ConvertFrom_MaxValue ()
60 Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffffffff"), "#1");
61 Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFFFFFF"), "#2");
62 Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffffffff"), "#3");
63 Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFFFFFF"), "#4");
64 Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffffffff"), "#5");
65 Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFFFFFF"), "#6");
69 [ExpectedException (typeof (NotSupportedException))]
70 public void ConvertFrom_Object ()
72 converter.ConvertFrom (new object ());
76 public void ConvertTo_MinValue ()
78 Assert.AreEqual (uint.MinValue.ToString (CultureInfo.InvariantCulture),
79 converter.ConvertTo (null, CultureInfo.InvariantCulture, uint.MinValue,
80 typeof (string)), "#1");
81 Assert.AreEqual (uint.MinValue.ToString (CultureInfo.CurrentCulture),
82 converter.ConvertTo (null, CultureInfo.CurrentCulture, uint.MinValue,
83 typeof (string)), "#2");
84 Assert.AreEqual (uint.MinValue.ToString (CultureInfo.CurrentCulture),
85 converter.ConvertTo (uint.MinValue, typeof (string)), "#3");
89 public void ConvertTo_MaxValue ()
91 Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.InvariantCulture),
92 converter.ConvertTo (null, CultureInfo.InvariantCulture, uint.MaxValue,
93 typeof (string)), "#1");
94 Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.CurrentCulture),
95 converter.ConvertTo (null, CultureInfo.CurrentCulture, uint.MaxValue,
96 typeof (string)), "#2");
97 Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.CurrentCulture),
98 converter.ConvertTo (uint.MaxValue, typeof (string)), "#3");
102 public void ConvertToString ()
104 CultureInfo culture = new MyCultureInfo ();
105 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
107 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
111 public void ConvertFromString_Invalid1 ()
114 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
116 } catch (Exception ex) {
117 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
118 Assert.IsNotNull (ex.InnerException, "#3");
119 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
124 public void ConvertFromString_Invalid2 ()
127 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
128 double.MaxValue.ToString(CultureInfo.InvariantCulture));
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_Invalid3 ()
141 converter.ConvertFromString ("*1");
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_Invalid4 ()
154 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
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 ConvertFrom_InvalidString1 ()
167 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
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_InvalidString2 ()
180 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
181 double.MaxValue.ToString (CultureInfo.InvariantCulture));
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_InvalidString3 ()
194 converter.ConvertFrom ("*1");
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_InvalidString4 ()
207 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
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 private sealed class MyCultureInfo : CultureInfo
219 internal MyCultureInfo ()
224 public override object GetFormat (Type formatType)
226 if (formatType == typeof (NumberFormatInfo)) {
227 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
229 nfi.NegativeSign = "myNegativeSign";
230 return NumberFormatInfo.ReadOnly (nfi);
232 return base.GetFormat (formatType);
236 // adding this override in 1.x shows different result in .NET (it is ignored).
237 // Some compatibility kids might want to fix this issue.
238 public override NumberFormatInfo NumberFormat {
240 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
241 nfi.NegativeSign = "myNegativeSign";
244 set { throw new NotSupportedException (); }