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");
44 Assert.IsTrue (converter.CanConvertTo (typeof (int)), "#3");
48 public void ConvertFrom_MinValue ()
50 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
51 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
52 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
53 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
54 Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
58 public void ConvertFrom_MaxValue ()
60 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffffffffffffffff"), "#1");
61 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFFFFFFFFFFFFFF"), "#2");
62 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffffffffffffffff"), "#3");
63 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFFFFFFFFFFFFFF"), "#4");
64 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffffffffffffffff"), "#5");
65 Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFFFFFFFFFFFFFF"), "#6");
69 [ExpectedException (typeof (NotSupportedException))]
70 public void ConvertFrom_Object ()
72 converter.ConvertFrom (new object ());
76 [ExpectedException (typeof (NotSupportedException))]
77 public void ConvertFrom_Int32 ()
79 converter.ConvertFrom (10);
83 public void ConvertTo_MinValue ()
85 Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.InvariantCulture),
86 converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MinValue,
87 typeof (string)), "#1");
88 Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
89 converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MinValue,
90 typeof (string)), "#2");
91 Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
92 converter.ConvertTo (ulong.MinValue, typeof (string)), "#3");
96 public void ConvertTo_MaxValue ()
98 Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.InvariantCulture),
99 converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MaxValue,
100 typeof (string)), "#1");
101 Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
102 converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MaxValue,
103 typeof (string)), "#2");
104 Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
105 converter.ConvertTo (ulong.MaxValue, typeof (string)), "#3");
109 public void ConvertToString ()
111 CultureInfo culture = new MyCultureInfo ();
112 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
114 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
118 public void ConvertFromString_Invalid1 ()
121 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
123 } catch (AssertionException) {
125 } catch (Exception ex) {
126 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
127 Assert.IsNotNull (ex.InnerException, "#3");
128 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
133 public void ConvertFromString_Invalid2 ()
136 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
137 double.MaxValue.ToString(CultureInfo.InvariantCulture));
139 } catch (AssertionException) {
141 } catch (Exception ex) {
142 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
143 Assert.IsNotNull (ex.InnerException, "#3");
144 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
149 public void ConvertFromString_Invalid3 ()
152 converter.ConvertFromString ("*1");
154 } catch (AssertionException) {
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 (AssertionException) {
171 } catch (Exception ex) {
172 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
173 Assert.IsNotNull (ex.InnerException, "#3");
174 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
179 public void ConvertFrom_InvalidString1 ()
182 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
184 } catch (AssertionException) {
186 } catch (Exception ex) {
187 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
188 Assert.IsNotNull (ex.InnerException, "#3");
189 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
194 public void ConvertFrom_InvalidString2 ()
197 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
198 double.MaxValue.ToString (CultureInfo.InvariantCulture));
200 } catch (AssertionException) {
202 } catch (Exception ex) {
203 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
204 Assert.IsNotNull (ex.InnerException, "#3");
205 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
210 public void ConvertFrom_InvalidString3 ()
213 converter.ConvertFrom ("*1");
215 } catch (AssertionException) {
217 } catch (Exception ex) {
218 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
219 Assert.IsNotNull (ex.InnerException, "#3");
220 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
225 public void ConvertFrom_InvalidString4 ()
228 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
230 } catch (AssertionException) {
232 } catch (Exception ex) {
233 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
234 Assert.IsNotNull (ex.InnerException, "#3");
235 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
240 private sealed class MyCultureInfo : CultureInfo
242 internal MyCultureInfo ()
247 public override object GetFormat (Type formatType)
249 if (formatType == typeof (NumberFormatInfo)) {
250 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
252 nfi.NegativeSign = "myNegativeSign";
253 return NumberFormatInfo.ReadOnly (nfi);
255 return base.GetFormat (formatType);
260 // adding this override in 1.x shows different result in .NET (it is ignored).
261 // Some compatibility kids might want to fix this issue.
262 public override NumberFormatInfo NumberFormat {
264 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
265 nfi.NegativeSign = "myNegativeSign";
268 set { throw new NotSupportedException (); }