2 // System.ComponentModel.SByteConverter 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 SByteConverterTests
22 private SByteConverter converter;
27 converter = new SByteConverter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (sbyte)), "#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_String ()
49 Assert.AreEqual ((sbyte) 10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
50 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#10"), "#2");
51 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x10"), "#3");
52 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X10"), "#4");
53 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x10"), "#5");
54 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X10"), "#6");
58 [ExpectedException (typeof (NotSupportedException))]
59 public void ConvertFrom_Object ()
61 converter.ConvertFrom (new object ());
65 [ExpectedException (typeof (NotSupportedException))]
66 public void ConvertFrom_Byte ()
68 converter.ConvertFrom (sbyte.MaxValue);
72 [ExpectedException (typeof (NotSupportedException))]
73 public void ConvertFrom_Int16 ()
75 converter.ConvertFrom ((short) 10);
79 [ExpectedException (typeof (NotSupportedException))]
80 public void ConvertFrom_Int32 ()
82 converter.ConvertFrom (10);
86 public void ConvertTo_MinValue ()
88 Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.InvariantCulture),
89 converter.ConvertTo (null, CultureInfo.InvariantCulture, sbyte.MinValue,
90 typeof (string)), "#1");
91 Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.CurrentCulture),
92 converter.ConvertTo (null, CultureInfo.CurrentCulture, sbyte.MinValue,
93 typeof (string)), "#2");
94 Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.CurrentCulture),
95 converter.ConvertTo (sbyte.MinValue, typeof (string)), "#3");
99 public void ConvertTo_MaxValue ()
101 Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.InvariantCulture),
102 converter.ConvertTo (null, CultureInfo.InvariantCulture, sbyte.MaxValue,
103 typeof (string)), "#1");
104 Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.CurrentCulture),
105 converter.ConvertTo (null, CultureInfo.CurrentCulture, sbyte.MaxValue,
106 typeof (string)), "#2");
107 Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.CurrentCulture),
108 converter.ConvertTo (sbyte.MaxValue, typeof (string)), "#3");
112 public void ConvertToString ()
114 CultureInfo culture = new MyCultureInfo ();
115 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
117 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (sbyte) -5), "#1");
118 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (short) -5), "#2");
122 public void ConvertFromString ()
124 CultureInfo culture = new MyCultureInfo ();
125 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
127 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
131 public void ConvertFromString_Invalid1 ()
134 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
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 (Exception ex) {
151 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
152 Assert.IsNotNull (ex.InnerException, "#3");
153 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
158 public void ConvertFromString_Invalid3 ()
161 converter.ConvertFromString ("*1");
163 } catch (Exception ex) {
164 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
165 Assert.IsNotNull (ex.InnerException, "#3");
166 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
171 public void ConvertFromString_Invalid4 ()
174 converter.ConvertFromString ("256");
176 } catch (Exception ex) {
177 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
178 Assert.IsNotNull (ex.InnerException, "#3");
179 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
184 public void ConvertFrom_InvalidString1 ()
187 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
189 } catch (Exception ex) {
190 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
191 Assert.IsNotNull (ex.InnerException, "#3");
192 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
197 public void ConvertFrom_InvalidString2 ()
200 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "256");
202 } catch (Exception ex) {
203 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
204 Assert.IsNotNull (ex.InnerException, "#3");
205 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
210 public void ConvertFrom_InvalidString3 ()
213 converter.ConvertFrom ("*1");
215 } catch (Exception ex) {
216 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
217 Assert.IsNotNull (ex.InnerException, "#3");
218 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
223 public void ConvertFrom_InvalidString4 ()
226 converter.ConvertFrom ("256");
228 } catch (Exception ex) {
229 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
230 Assert.IsNotNull (ex.InnerException, "#3");
231 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
236 public void ConvertFrom_InvalidString5 ()
239 converter.ConvertFrom ("#0b10");
241 } catch (Exception ex) {
242 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
243 Assert.IsNotNull (ex.InnerException, "#3");
244 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
249 private sealed class MyCultureInfo : CultureInfo
251 internal MyCultureInfo ()
256 public override object GetFormat (Type formatType)
258 if (formatType == typeof (NumberFormatInfo)) {
259 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
261 nfi.NegativeSign = "myNegativeSign";
262 return NumberFormatInfo.ReadOnly (nfi);
264 return base.GetFormat (formatType);
269 // adding this override in 1.x shows different result in .NET (it is ignored).
270 // Some compatibility kids might want to fix this issue.
271 public override NumberFormatInfo NumberFormat {
273 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
274 nfi.NegativeSign = "myNegativeSign";
277 set { throw new NotSupportedException (); }