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");
44 Assert.IsTrue (converter.CanConvertTo (typeof (int)), "#3");
48 public void ConvertFrom_String ()
50 Assert.AreEqual ((sbyte) 10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
51 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#10"), "#2");
52 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x10"), "#3");
53 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X10"), "#4");
54 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x10"), "#5");
55 Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X10"), "#6");
59 [ExpectedException (typeof (NotSupportedException))]
60 public void ConvertFrom_Object ()
62 converter.ConvertFrom (new object ());
66 [ExpectedException (typeof (NotSupportedException))]
67 public void ConvertFrom_Byte ()
69 converter.ConvertFrom (sbyte.MaxValue);
73 [ExpectedException (typeof (NotSupportedException))]
74 public void ConvertFrom_Int16 ()
76 converter.ConvertFrom ((short) 10);
80 [ExpectedException (typeof (NotSupportedException))]
81 public void ConvertFrom_Int32 ()
83 converter.ConvertFrom (10);
87 public void ConvertTo_MinValue ()
89 Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.InvariantCulture),
90 converter.ConvertTo (null, CultureInfo.InvariantCulture, sbyte.MinValue,
91 typeof (string)), "#1");
92 Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.CurrentCulture),
93 converter.ConvertTo (null, CultureInfo.CurrentCulture, sbyte.MinValue,
94 typeof (string)), "#2");
95 Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.CurrentCulture),
96 converter.ConvertTo (sbyte.MinValue, typeof (string)), "#3");
100 public void ConvertTo_MaxValue ()
102 Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.InvariantCulture),
103 converter.ConvertTo (null, CultureInfo.InvariantCulture, sbyte.MaxValue,
104 typeof (string)), "#1");
105 Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.CurrentCulture),
106 converter.ConvertTo (null, CultureInfo.CurrentCulture, sbyte.MaxValue,
107 typeof (string)), "#2");
108 Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.CurrentCulture),
109 converter.ConvertTo (sbyte.MaxValue, typeof (string)), "#3");
113 public void ConvertToString ()
115 CultureInfo culture = new MyCultureInfo ();
116 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
118 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (sbyte) -5), "#1");
119 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (short) -5), "#2");
123 public void ConvertFromString ()
125 CultureInfo culture = new MyCultureInfo ();
126 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
128 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
132 public void ConvertFromString_Invalid1 ()
135 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
137 } catch (Exception ex) {
138 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
139 Assert.IsNotNull (ex.InnerException, "#3");
140 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
145 public void ConvertFromString_Invalid2 ()
148 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
149 double.MaxValue.ToString(CultureInfo.InvariantCulture));
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 (Exception ex) {
165 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
166 Assert.IsNotNull (ex.InnerException, "#3");
167 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
172 public void ConvertFromString_Invalid4 ()
175 converter.ConvertFromString ("256");
177 } catch (Exception ex) {
178 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
179 Assert.IsNotNull (ex.InnerException, "#3");
180 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
185 public void ConvertFrom_InvalidString1 ()
188 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
190 } catch (Exception ex) {
191 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
192 Assert.IsNotNull (ex.InnerException, "#3");
193 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
198 public void ConvertFrom_InvalidString2 ()
201 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "256");
203 } catch (Exception ex) {
204 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
205 Assert.IsNotNull (ex.InnerException, "#3");
206 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
211 public void ConvertFrom_InvalidString3 ()
214 converter.ConvertFrom ("*1");
216 } catch (Exception ex) {
217 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
218 Assert.IsNotNull (ex.InnerException, "#3");
219 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
224 public void ConvertFrom_InvalidString4 ()
227 converter.ConvertFrom ("256");
229 } catch (Exception ex) {
230 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
231 Assert.IsNotNull (ex.InnerException, "#3");
232 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
237 public void ConvertFrom_InvalidString5 ()
240 converter.ConvertFrom ("#0b10");
242 } catch (Exception ex) {
243 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
244 Assert.IsNotNull (ex.InnerException, "#3");
245 Assert.AreEqual (typeof (OverflowException), 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);
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 (); }