2 // System.ComponentModel.Int32Converter 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 Int32ConverterTests
22 private Int32Converter converter;
27 converter = new Int32Converter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#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 (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#80000000"), "#1");
51 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x80000000"), "#2");
52 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X80000000"), "#3");
53 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x80000000"), "#4");
54 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X80000000"), "#5");
58 public void ConvertFrom_MaxValue ()
60 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffff"), "#1");
61 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFF"), "#2");
62 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffff"), "#3");
63 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFF"), "#4");
64 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffff"), "#5");
65 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFF"), "#6");
69 public void IsValid ()
71 Assert.IsTrue (converter.IsValid("1"));
72 Assert.IsTrue (converter.IsValid("545"));
73 Assert.IsFalse (converter.IsValid("fred"));
77 [ExpectedException (typeof (NotSupportedException))]
78 public void ConvertFrom_Object ()
80 converter.ConvertFrom (new object ());
84 [ExpectedException (typeof (NotSupportedException))]
85 public void ConvertFrom_Int32 ()
87 converter.ConvertFrom (int.MaxValue);
91 public void ConvertTo_MinValue ()
93 Assert.AreEqual (int.MinValue.ToString (CultureInfo.InvariantCulture),
94 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MinValue,
95 typeof (string)), "#1");
96 Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
97 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MinValue,
98 typeof (string)), "#2");
99 Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
100 converter.ConvertTo (int.MinValue, typeof (string)), "#3");
104 public void ConvertTo_MaxValue ()
106 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.InvariantCulture),
107 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MaxValue,
108 typeof (string)), "#1");
109 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
110 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MaxValue,
111 typeof (string)), "#2");
112 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
113 converter.ConvertTo (int.MaxValue, typeof (string)), "#3");
117 public void ConvertToString ()
119 CultureInfo culture = new MyCultureInfo ();
120 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
122 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
126 public void ConvertFromString ()
128 CultureInfo culture = new MyCultureInfo ();
129 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
131 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
135 public void ConvertFromString_Invalid1 ()
138 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
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_Invalid2 ()
151 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
152 double.MaxValue.ToString(CultureInfo.InvariantCulture));
154 } catch (Exception ex) {
155 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
156 Assert.IsNotNull (ex.InnerException, "#3");
157 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
162 public void ConvertFromString_Invalid3 ()
165 converter.ConvertFromString ("*1");
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 (Exception ex) {
181 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
182 Assert.IsNotNull (ex.InnerException, "#3");
183 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
188 public void ConvertFrom_InvalidString1 ()
191 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
193 } catch (Exception ex) {
194 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
195 Assert.IsNotNull (ex.InnerException, "#3");
196 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
201 public void ConvertFrom_InvalidString2 ()
204 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
205 double.MaxValue.ToString (CultureInfo.InvariantCulture));
207 } catch (Exception ex) {
208 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
209 Assert.IsNotNull (ex.InnerException, "#3");
210 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
215 public void ConvertFrom_InvalidString3 ()
218 converter.ConvertFrom ("*1");
220 } catch (Exception ex) {
221 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
222 Assert.IsNotNull (ex.InnerException, "#3");
223 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
228 public void ConvertFrom_InvalidString4 ()
231 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
233 } catch (Exception ex) {
234 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
235 Assert.IsNotNull (ex.InnerException, "#3");
236 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
241 private sealed class MyCultureInfo : CultureInfo
243 internal MyCultureInfo ()
248 public override object GetFormat (Type formatType)
250 if (formatType == typeof (NumberFormatInfo)) {
251 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
253 nfi.NegativeSign = "myNegativeSign";
254 return NumberFormatInfo.ReadOnly (nfi);
256 return base.GetFormat (formatType);