New test.
[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                 }
45
46                 [Test]
47                 public void ConvertFrom_String ()
48                 {
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");
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (NotSupportedException))]
59                 public void ConvertFrom_Object ()
60                 {
61                         converter.ConvertFrom (new object ());
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (NotSupportedException))]
66                 public void ConvertFrom_Byte ()
67                 {
68                         converter.ConvertFrom (sbyte.MaxValue);
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (NotSupportedException))]
73                 public void ConvertFrom_Int16 ()
74                 {
75                         converter.ConvertFrom ((short) 10);
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (NotSupportedException))]
80                 public void ConvertFrom_Int32 ()
81                 {
82                         converter.ConvertFrom (10);
83                 }
84
85                 [Test]
86                 public void ConvertTo_MinValue ()
87                 {
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");
96                 }
97
98                 [Test]
99                 public void ConvertTo_MaxValue ()
100                 {
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");
109                 }
110
111                 [Test]
112                 public void ConvertToString ()
113                 {
114                         CultureInfo culture = new MyCultureInfo ();
115                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
116
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");
119                 }
120
121                 [Test]
122                 public void ConvertFromString ()
123                 {
124                         CultureInfo culture = new MyCultureInfo ();
125                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
126
127                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
128                 }
129
130                 [Test]
131                 public void ConvertFromString_Invalid1 ()
132                 {
133                         try {
134                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
135                                 Assert.Fail ("#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");
140                         }
141                 }
142
143                 [Test]
144                 public void ConvertFromString_Invalid2 ()
145                 {
146                         try {
147                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
148                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
149                                 Assert.Fail ("#1");
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");
154                         }
155                 }
156
157                 [Test]
158                 public void ConvertFromString_Invalid3 ()
159                 {
160                         try {
161                                 converter.ConvertFromString ("*1");
162                                 Assert.Fail ("#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");
167                         }
168                 }
169
170                 [Test]
171                 public void ConvertFromString_Invalid4 ()
172                 {
173                         try {
174                                 converter.ConvertFromString ("256");
175                                 Assert.Fail ("#1");
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");
180                         }
181                 }
182
183                 [Test]
184                 public void ConvertFrom_InvalidString1 ()
185                 {
186                         try {
187                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
188                                 Assert.Fail ("#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");
193                         }
194                 }
195
196                 [Test]
197                 public void ConvertFrom_InvalidString2 ()
198                 {
199                         try {
200                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "256");
201                                 Assert.Fail ("#1");
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");
206                         }
207                 }
208
209                 [Test]
210                 public void ConvertFrom_InvalidString3 ()
211                 {
212                         try {
213                                 converter.ConvertFrom ("*1");
214                                 Assert.Fail ("#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");
219                         }
220                 }
221
222                 [Test]
223                 public void ConvertFrom_InvalidString4 ()
224                 {
225                         try {
226                                 converter.ConvertFrom ("256");
227                                 Assert.Fail ("#1");
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");
232                         }
233                 }
234
235                 [Test]
236                 public void ConvertFrom_InvalidString5 ()
237                 {
238                         try {
239                                 converter.ConvertFrom ("#0b10");
240                                 Assert.Fail ("#1");
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");
245                         }
246                 }
247
248                 [Serializable]
249                 private sealed class MyCultureInfo : CultureInfo
250                 {
251                         internal MyCultureInfo ()
252                                 : base ("en-US")
253                         {
254                         }
255
256                         public override object GetFormat (Type formatType)
257                         {
258                                 if (formatType == typeof (NumberFormatInfo)) {
259                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
260
261                                         nfi.NegativeSign = "myNegativeSign";
262                                         return NumberFormatInfo.ReadOnly (nfi);
263                                 } else {
264                                         return base.GetFormat (formatType);
265                                 }
266                         }
267
268 #if NET_2_0
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 {
272                                 get {
273                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
274                                         nfi.NegativeSign = "myNegativeSign";
275                                         return nfi;
276                                 }
277                                 set { throw new NotSupportedException (); }
278                         }
279 #endif
280                 }
281         }
282 }
283