2 // System.ComponentModel.Int64Converter 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 Int64ConverterTests
22 private Int64Converter converter;
27 converter = new Int64Converter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (long)), "#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 (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#8000000000000000"), "#1");
51 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x8000000000000000"), "#2");
52 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X8000000000000000"), "#3");
53 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x8000000000000000"), "#4");
54 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X8000000000000000"), "#5");
58 public void ConvertFrom_MaxValue ()
60 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffffffffffff"), "#1");
61 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFFFFFFFFFF"), "#2");
62 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffffffffffff"), "#3");
63 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFFFFFFFFFF"), "#4");
64 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffffffffffff"), "#5");
65 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFFFFFFFFFF"), "#6");
70 [ExpectedException (typeof (NotSupportedException))]
71 public void ConvertFrom_Object ()
73 converter.ConvertFrom (new object ());
77 [ExpectedException (typeof (NotSupportedException))]
78 public void ConvertFrom_Int32 ()
80 converter.ConvertFrom (10);
84 public void ConvertTo_MinValue ()
86 Assert.AreEqual (long.MinValue.ToString (CultureInfo.InvariantCulture),
87 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MinValue,
88 typeof (string)), "#1");
89 Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
90 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MinValue,
91 typeof (string)), "#2");
92 Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
93 converter.ConvertTo (long.MinValue, typeof (string)), "#3");
97 public void ConvertTo_MaxValue ()
99 Assert.AreEqual (long.MaxValue.ToString (CultureInfo.InvariantCulture),
100 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MaxValue,
101 typeof (string)), "#1");
102 Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
103 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MaxValue,
104 typeof (string)), "#2");
105 Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
106 converter.ConvertTo (long.MaxValue, typeof (string)), "#3");
110 public void ConvertToString ()
112 CultureInfo culture = new MyCultureInfo ();
113 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
115 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (long) -5), "#1");
116 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
120 public void ConvertFromString ()
122 CultureInfo culture = new MyCultureInfo ();
123 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
125 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
129 public void ConvertFromString_Invalid1 ()
132 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
134 } catch (AssertionException) {
136 } catch (Exception ex) {
137 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
138 Assert.IsNotNull (ex.InnerException, "#3");
139 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
144 public void ConvertFromString_Invalid2 ()
147 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
148 double.MaxValue.ToString(CultureInfo.InvariantCulture));
150 } catch (AssertionException) {
152 } catch (Exception ex) {
153 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
154 Assert.IsNotNull (ex.InnerException, "#3");
155 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
160 public void ConvertFromString_Invalid3 ()
163 converter.ConvertFromString ("*1");
165 } catch (AssertionException) {
167 } catch (Exception ex) {
168 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
169 Assert.IsNotNull (ex.InnerException, "#3");
170 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
175 public void ConvertFromString_Invalid4 ()
178 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
180 } catch (AssertionException) {
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_InvalidString1 ()
193 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
195 } catch (AssertionException) {
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_InvalidString2 ()
208 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
209 double.MaxValue.ToString (CultureInfo.InvariantCulture));
211 } catch (AssertionException) {
213 } catch (Exception ex) {
214 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
215 Assert.IsNotNull (ex.InnerException, "#3");
216 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
221 public void ConvertFrom_InvalidString3 ()
224 converter.ConvertFrom ("*1");
226 } catch (AssertionException) {
228 } catch (Exception ex) {
229 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
230 Assert.IsNotNull (ex.InnerException, "#3");
231 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
236 public void ConvertFrom_InvalidString4 ()
239 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
241 } catch (AssertionException) {
243 } catch (Exception ex) {
244 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
245 Assert.IsNotNull (ex.InnerException, "#3");
246 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
251 private sealed class MyCultureInfo : CultureInfo
253 internal MyCultureInfo ()
258 public override object GetFormat (Type formatType)
260 if (formatType == typeof (NumberFormatInfo)) {
261 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
263 nfi.NegativeSign = "myNegativeSign";
264 return NumberFormatInfo.ReadOnly (nfi);
266 return base.GetFormat (formatType);
271 // adding this override in 1.x shows different result in .NET (it is ignored).
272 // Some compatibility kids might want to fix this issue.
273 public override NumberFormatInfo NumberFormat {
275 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
276 nfi.NegativeSign = "myNegativeSign";
279 set { throw new NotSupportedException (); }