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");
47 public void ConvertFrom_MinValue ()
49 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#80000000"), "#1");
50 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x80000000"), "#2");
51 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X80000000"), "#3");
52 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x80000000"), "#4");
53 Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X80000000"), "#5");
57 public void ConvertFrom_MaxValue ()
59 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffff"), "#1");
60 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFF"), "#2");
61 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffff"), "#3");
62 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFF"), "#4");
63 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffff"), "#5");
64 Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFF"), "#6");
68 [ExpectedException (typeof (NotSupportedException))]
69 public void ConvertFrom_Object ()
71 converter.ConvertFrom (new object ());
75 [ExpectedException (typeof (NotSupportedException))]
76 public void ConvertFrom_Int32 ()
78 converter.ConvertFrom (int.MaxValue);
82 public void ConvertTo_MinValue ()
84 Assert.AreEqual (int.MinValue.ToString (CultureInfo.InvariantCulture),
85 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MinValue,
86 typeof (string)), "#1");
87 Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
88 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MinValue,
89 typeof (string)), "#2");
90 Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
91 converter.ConvertTo (int.MinValue, typeof (string)), "#3");
95 public void ConvertTo_MaxValue ()
97 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.InvariantCulture),
98 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MaxValue,
99 typeof (string)), "#1");
100 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
101 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MaxValue,
102 typeof (string)), "#2");
103 Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
104 converter.ConvertTo (int.MaxValue, typeof (string)), "#3");
108 public void ConvertToString ()
110 CultureInfo culture = new MyCultureInfo ();
111 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
113 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
117 public void ConvertFromString ()
119 CultureInfo culture = new MyCultureInfo ();
120 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
122 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
126 public void ConvertFromString_Invalid1 ()
129 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
131 } catch (Exception ex) {
132 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
133 Assert.IsNotNull (ex.InnerException, "#3");
134 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
139 public void ConvertFromString_Invalid2 ()
142 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
143 double.MaxValue.ToString(CultureInfo.InvariantCulture));
145 } catch (Exception ex) {
146 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
147 Assert.IsNotNull (ex.InnerException, "#3");
148 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
153 public void ConvertFromString_Invalid3 ()
156 converter.ConvertFromString ("*1");
158 } catch (Exception ex) {
159 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
160 Assert.IsNotNull (ex.InnerException, "#3");
161 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
166 public void ConvertFromString_Invalid4 ()
169 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
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 (Exception ex) {
185 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
186 Assert.IsNotNull (ex.InnerException, "#3");
187 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
192 public void ConvertFrom_InvalidString2 ()
195 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
196 double.MaxValue.ToString (CultureInfo.InvariantCulture));
198 } catch (Exception ex) {
199 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
200 Assert.IsNotNull (ex.InnerException, "#3");
201 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
206 public void ConvertFrom_InvalidString3 ()
209 converter.ConvertFrom ("*1");
211 } catch (Exception ex) {
212 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
213 Assert.IsNotNull (ex.InnerException, "#3");
214 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
219 public void ConvertFrom_InvalidString4 ()
222 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
224 } catch (Exception ex) {
225 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
226 Assert.IsNotNull (ex.InnerException, "#3");
227 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
232 private sealed class MyCultureInfo : CultureInfo
234 internal MyCultureInfo ()
239 public override object GetFormat (Type formatType)
241 if (formatType == typeof (NumberFormatInfo)) {
242 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
244 nfi.NegativeSign = "myNegativeSign";
245 return NumberFormatInfo.ReadOnly (nfi);
247 return base.GetFormat (formatType);