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 [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 (int.MaxValue);
83 public void ConvertTo_MinValue ()
85 Assert.AreEqual (int.MinValue.ToString (CultureInfo.InvariantCulture),
86 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MinValue,
87 typeof (string)), "#1");
88 Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
89 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MinValue,
90 typeof (string)), "#2");
91 Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
92 converter.ConvertTo (int.MinValue, typeof (string)), "#3");
96 public void ConvertTo_MaxValue ()
98 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.InvariantCulture),
99 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MaxValue,
100 typeof (string)), "#1");
101 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
102 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MaxValue,
103 typeof (string)), "#2");
104 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
105 converter.ConvertTo (int.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, -5));
118 public void ConvertFromString ()
120 CultureInfo culture = new MyCultureInfo ();
121 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
123 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
127 public void ConvertFromString_Invalid1 ()
130 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
132 } catch (Exception ex) {
133 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
134 Assert.IsNotNull (ex.InnerException, "#3");
135 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
140 public void ConvertFromString_Invalid2 ()
143 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
144 double.MaxValue.ToString(CultureInfo.InvariantCulture));
146 } catch (Exception ex) {
147 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
148 Assert.IsNotNull (ex.InnerException, "#3");
149 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
154 public void ConvertFromString_Invalid3 ()
157 converter.ConvertFromString ("*1");
159 } catch (Exception ex) {
160 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
161 Assert.IsNotNull (ex.InnerException, "#3");
162 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
167 public void ConvertFromString_Invalid4 ()
170 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
172 } catch (Exception ex) {
173 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
174 Assert.IsNotNull (ex.InnerException, "#3");
175 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
180 public void ConvertFrom_InvalidString1 ()
183 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
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 (Exception ex) {
200 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
201 Assert.IsNotNull (ex.InnerException, "#3");
202 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
207 public void ConvertFrom_InvalidString3 ()
210 converter.ConvertFrom ("*1");
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_InvalidString4 ()
223 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
225 } catch (Exception ex) {
226 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
227 Assert.IsNotNull (ex.InnerException, "#3");
228 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
233 private sealed class MyCultureInfo : CultureInfo
235 internal MyCultureInfo ()
240 public override object GetFormat (Type formatType)
242 if (formatType == typeof (NumberFormatInfo)) {
243 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
245 nfi.NegativeSign = "myNegativeSign";
246 return NumberFormatInfo.ReadOnly (nfi);
248 return base.GetFormat (formatType);