2 // System.ComponentModel.UInt64Converter 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 UInt64ConverterTests
22 private UInt64Converter converter;
27 converter = new UInt64Converter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (ulong)), "#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 (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
50 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
51 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
52 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
53 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
57 public void ConvertFrom_MaxValue ()
59 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffffffffffffffff"), "#1");
60 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFFFFFFFFFFFFFF"), "#2");
61 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffffffffffffffff"), "#3");
62 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFFFFFFFFFFFFFF"), "#4");
63 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffffffffffffffff"), "#5");
64 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFFFFFFFFFFFFFF"), "#6");
68 [ExpectedException (typeof (NotSupportedException))]
69 public void ConvertFrom_Object ()
71 converter.ConvertFrom (new object ());
75 [ExpectedException (typeof (NotSupportedException))]
76 public void ConvertFrom_Int32 ()
78 converter.ConvertFrom (10);
82 public void ConvertTo_MinValue ()
84 Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.InvariantCulture),
85 converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MinValue,
86 typeof (string)), "#1");
87 Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
88 converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MinValue,
89 typeof (string)), "#2");
90 Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
91 converter.ConvertTo (ulong.MinValue, typeof (string)), "#3");
95 public void ConvertTo_MaxValue ()
97 Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.InvariantCulture),
98 converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MaxValue,
99 typeof (string)), "#1");
100 Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
101 converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MaxValue,
102 typeof (string)), "#2");
103 Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
104 converter.ConvertTo (ulong.MaxValue, typeof (string)), "#3");
108 public void ConvertToString ()
110 CultureInfo culture = new MyCultureInfo ();
111 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
113 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
117 public void ConvertFromString_Invalid1 ()
120 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
122 } catch (AssertionException) {
124 } catch (Exception ex) {
125 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
126 Assert.IsNotNull (ex.InnerException, "#3");
127 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
132 public void ConvertFromString_Invalid2 ()
135 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
136 double.MaxValue.ToString(CultureInfo.InvariantCulture));
138 } catch (AssertionException) {
140 } catch (Exception ex) {
141 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
142 Assert.IsNotNull (ex.InnerException, "#3");
143 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
148 public void ConvertFromString_Invalid3 ()
151 converter.ConvertFromString ("*1");
153 } catch (AssertionException) {
155 } catch (Exception ex) {
156 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
157 Assert.IsNotNull (ex.InnerException, "#3");
158 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
163 public void ConvertFromString_Invalid4 ()
166 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
168 } catch (AssertionException) {
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 (AssertionException) {
185 } catch (Exception ex) {
186 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
187 Assert.IsNotNull (ex.InnerException, "#3");
188 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
193 public void ConvertFrom_InvalidString2 ()
196 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
197 double.MaxValue.ToString (CultureInfo.InvariantCulture));
199 } catch (AssertionException) {
201 } catch (Exception ex) {
202 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
203 Assert.IsNotNull (ex.InnerException, "#3");
204 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
209 public void ConvertFrom_InvalidString3 ()
212 converter.ConvertFrom ("*1");
214 } catch (AssertionException) {
216 } catch (Exception ex) {
217 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
218 Assert.IsNotNull (ex.InnerException, "#3");
219 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
224 public void ConvertFrom_InvalidString4 ()
227 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
229 } catch (AssertionException) {
231 } catch (Exception ex) {
232 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
233 Assert.IsNotNull (ex.InnerException, "#3");
234 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
239 private sealed class MyCultureInfo : CultureInfo
241 internal MyCultureInfo ()
246 public override object GetFormat (Type formatType)
248 if (formatType == typeof (NumberFormatInfo)) {
249 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
251 nfi.NegativeSign = "myNegativeSign";
252 return NumberFormatInfo.ReadOnly (nfi);
254 return base.GetFormat (formatType);
259 // adding this override in 1.x shows different result in .NET (it is ignored).
260 // Some compatibility kids might want to fix this issue.
261 public override NumberFormatInfo NumberFormat {
263 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
264 nfi.NegativeSign = "myNegativeSign";
267 set { throw new NotSupportedException (); }