Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System / Test / System.ComponentModel / SByteConverterTests.cs
1 //
2 // System.ComponentModel.SByteConverter test cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2005 Novell, Inc. (http://www.ximian.com)
8 //
9
10 using System;
11 using System.ComponentModel;
12 using System.ComponentModel.Design.Serialization;
13 using System.Globalization;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.ComponentModel
18 {
19         [TestFixture]
20         public class SByteConverterTests
21         {
22                 private SByteConverter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new SByteConverter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
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");
37                 }
38
39                 [Test]
40                 public void CanConvertTo ()
41                 {
42                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
43                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2");
44                         Assert.IsTrue (converter.CanConvertTo (typeof (int)), "#3");
45                 }
46
47                 [Test]
48                 public void ConvertFrom_String ()
49                 {
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");
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (NotSupportedException))]
60                 public void ConvertFrom_Object ()
61                 {
62                         converter.ConvertFrom (new object ());
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (NotSupportedException))]
67                 public void ConvertFrom_Byte ()
68                 {
69                         converter.ConvertFrom (sbyte.MaxValue);
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (NotSupportedException))]
74                 public void ConvertFrom_Int16 ()
75                 {
76                         converter.ConvertFrom ((short) 10);
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (NotSupportedException))]
81                 public void ConvertFrom_Int32 ()
82                 {
83                         converter.ConvertFrom (10);
84                 }
85
86                 [Test]
87                 public void ConvertTo_MinValue ()
88                 {
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");
97                 }
98
99                 [Test]
100                 public void ConvertTo_MaxValue ()
101                 {
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");
110                 }
111
112                 [Test]
113                 public void ConvertToString ()
114                 {
115                         CultureInfo culture = new MyCultureInfo ();
116                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
117
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");
120                 }
121
122                 [Test]
123                 public void ConvertFromString ()
124                 {
125                         CultureInfo culture = new MyCultureInfo ();
126                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
127
128                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
129                 }
130
131                 [Test]
132                 public void ConvertFromString_Invalid1 ()
133                 {
134                         try {
135                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
136                                 Assert.Fail ("#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");
141                         }
142                 }
143
144                 [Test]
145                 public void ConvertFromString_Invalid2 ()
146                 {
147                         try {
148                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
149                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
150                                 Assert.Fail ("#1");
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");
155                         }
156                 }
157
158                 [Test]
159                 public void ConvertFromString_Invalid3 ()
160                 {
161                         try {
162                                 converter.ConvertFromString ("*1");
163                                 Assert.Fail ("#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");
168                         }
169                 }
170
171                 [Test]
172                 public void ConvertFromString_Invalid4 ()
173                 {
174                         try {
175                                 converter.ConvertFromString ("256");
176                                 Assert.Fail ("#1");
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");
181                         }
182                 }
183
184                 [Test]
185                 public void ConvertFrom_InvalidString1 ()
186                 {
187                         try {
188                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
189                                 Assert.Fail ("#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");
194                         }
195                 }
196
197                 [Test]
198                 public void ConvertFrom_InvalidString2 ()
199                 {
200                         try {
201                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "256");
202                                 Assert.Fail ("#1");
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");
207                         }
208                 }
209
210                 [Test]
211                 public void ConvertFrom_InvalidString3 ()
212                 {
213                         try {
214                                 converter.ConvertFrom ("*1");
215                                 Assert.Fail ("#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");
220                         }
221                 }
222
223                 [Test]
224                 public void ConvertFrom_InvalidString4 ()
225                 {
226                         try {
227                                 converter.ConvertFrom ("256");
228                                 Assert.Fail ("#1");
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");
233                         }
234                 }
235
236                 [Test]
237                 public void ConvertFrom_InvalidString5 ()
238                 {
239                         try {
240                                 converter.ConvertFrom ("#0b10");
241                                 Assert.Fail ("#1");
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");
246                         }
247                 }
248
249                 [Serializable]
250                 private sealed class MyCultureInfo : CultureInfo
251                 {
252                         internal MyCultureInfo ()
253                                 : base ("en-US")
254                         {
255                         }
256
257                         public override object GetFormat (Type formatType)
258                         {
259                                 if (formatType == typeof (NumberFormatInfo)) {
260                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
261
262                                         nfi.NegativeSign = "myNegativeSign";
263                                         return NumberFormatInfo.ReadOnly (nfi);
264                                 } else {
265                                         return base.GetFormat (formatType);
266                                 }
267                         }
268
269 #if NET_2_0
270 // adding this override in 1.x shows different result in .NET (it is ignored).
271 // Some compatibility kids might want to fix this issue.
272                         public override NumberFormatInfo NumberFormat {
273                                 get {
274                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
275                                         nfi.NegativeSign = "myNegativeSign";
276                                         return nfi;
277                                 }
278                                 set { throw new NotSupportedException (); }
279                         }
280 #endif
281                 }
282         }
283 }
284