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");
47 public void ConvertFrom_MinValue ()
49 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#8000000000000000"), "#1");
50 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x8000000000000000"), "#2");
51 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X8000000000000000"), "#3");
52 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x8000000000000000"), "#4");
53 Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X8000000000000000"), "#5");
57 public void ConvertFrom_MaxValue ()
59 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffffffffffff"), "#1");
60 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFFFFFFFFFF"), "#2");
61 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffffffffffff"), "#3");
62 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFFFFFFFFFF"), "#4");
63 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffffffffffff"), "#5");
64 Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFFFFFFFFFF"), "#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 (long.MinValue.ToString (CultureInfo.InvariantCulture),
86 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MinValue,
87 typeof (string)), "#1");
88 Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
89 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MinValue,
90 typeof (string)), "#2");
91 Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
92 converter.ConvertTo (long.MinValue, typeof (string)), "#3");
96 public void ConvertTo_MaxValue ()
98 Assert.AreEqual (long.MaxValue.ToString (CultureInfo.InvariantCulture),
99 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MaxValue,
100 typeof (string)), "#1");
101 Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
102 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MaxValue,
103 typeof (string)), "#2");
104 Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
105 converter.ConvertTo (long.MaxValue, typeof (string)), "#3");
109 public void ConvertToString ()
111 CultureInfo culture = new MyCultureInfo ();
112 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
114 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (long) -5), "#1");
115 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
119 public void ConvertFromString ()
121 CultureInfo culture = new MyCultureInfo ();
122 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
124 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
128 public void ConvertFromString_Invalid1 ()
131 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
133 } catch (AssertionException) {
135 } catch (Exception ex) {
136 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
137 Assert.IsNotNull (ex.InnerException, "#3");
138 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
143 public void ConvertFromString_Invalid2 ()
146 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
147 double.MaxValue.ToString(CultureInfo.InvariantCulture));
149 } catch (AssertionException) {
151 } catch (Exception ex) {
152 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
153 Assert.IsNotNull (ex.InnerException, "#3");
154 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
159 public void ConvertFromString_Invalid3 ()
162 converter.ConvertFromString ("*1");
164 } catch (AssertionException) {
166 } catch (Exception ex) {
167 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
168 Assert.IsNotNull (ex.InnerException, "#3");
169 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
174 public void ConvertFromString_Invalid4 ()
177 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
179 } catch (AssertionException) {
181 } catch (Exception ex) {
182 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
183 Assert.IsNotNull (ex.InnerException, "#3");
184 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
189 public void ConvertFrom_InvalidString1 ()
192 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
194 } catch (AssertionException) {
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_InvalidString2 ()
207 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
208 double.MaxValue.ToString (CultureInfo.InvariantCulture));
210 } catch (AssertionException) {
212 } catch (Exception ex) {
213 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
214 Assert.IsNotNull (ex.InnerException, "#3");
215 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
220 public void ConvertFrom_InvalidString3 ()
223 converter.ConvertFrom ("*1");
225 } catch (AssertionException) {
227 } catch (Exception ex) {
228 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
229 Assert.IsNotNull (ex.InnerException, "#3");
230 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
235 public void ConvertFrom_InvalidString4 ()
238 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
240 } catch (AssertionException) {
242 } catch (Exception ex) {
243 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
244 Assert.IsNotNull (ex.InnerException, "#3");
245 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
250 private sealed class MyCultureInfo : CultureInfo
252 internal MyCultureInfo ()
257 public override object GetFormat (Type formatType)
259 if (formatType == typeof (NumberFormatInfo)) {
260 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
262 nfi.NegativeSign = "myNegativeSign";
263 return NumberFormatInfo.ReadOnly (nfi);
265 return base.GetFormat (formatType);
270 // adding this override in 1.x shows different result in .NET (it is ignored).
271 // Some compatibility kids might want to fix this issue.
272 public override NumberFormatInfo NumberFormat {
274 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
275 nfi.NegativeSign = "myNegativeSign";
278 set { throw new NotSupportedException (); }