correct asserts in the test
[mono.git] / mcs / class / System / Test / System.ComponentModel / ByteConverterTests.cs
1 //
2 // System.ComponentModel.ByteConverter 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 ByteConverterTests
21         {
22                 private ByteConverter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new ByteConverter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (byte)), "#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_MinValue ()
48                 {
49                         Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
50                         Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
51                         Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
52                         Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
53                         Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
54                 }
55
56                 [Test]
57                 public void ConvertFrom_MaxValue ()
58                 {
59                         Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ff"), "#1");
60                         Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FF"), "#2");
61                         Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xff"), "#3");
62                         Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFF"), "#4");
63                         Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xff"), "#5");
64                         Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFF"), "#6");
65                 }
66
67                 [Test]
68                 public void ConvertToString ()
69                 {
70                         CultureInfo culture = new MyCultureInfo ();
71                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
72
73                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (NotSupportedException))]
78                 public void ConvertFrom_Object ()
79                 {
80                         converter.ConvertFrom (new object ());
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (NotSupportedException))]
85                 public void ConvertFrom_Byte ()
86                 {
87                         converter.ConvertFrom (byte.MaxValue);
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (NotSupportedException))]
92                 public void ConvertFrom_Int16 ()
93                 {
94                         converter.ConvertFrom ((short) 10);
95                 }
96
97                 [Test]
98                 [ExpectedException (typeof (NotSupportedException))]
99                 public void ConvertFrom_Int32 ()
100                 {
101                         converter.ConvertFrom (10);
102                 }
103
104                 [Test]
105                 public void ConvertTo_MinValue ()
106                 {
107                         Assert.AreEqual (byte.MinValue.ToString (CultureInfo.InvariantCulture),
108                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, byte.MinValue,
109                                 typeof (string)), "#1");
110                         Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
111                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, byte.MinValue,
112                                 typeof (string)), "#2");
113                         Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
114                                 converter.ConvertTo (byte.MinValue, typeof (string)), "#3");
115                 }
116
117                 [Test]
118                 public void ConvertTo_MaxValue ()
119                 {
120                         Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.InvariantCulture),
121                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, byte.MaxValue,
122                                 typeof (string)), "#1");
123                         Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
124                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, byte.MaxValue,
125                                 typeof (string)), "#2");
126                         Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
127                                 converter.ConvertTo (byte.MaxValue, typeof (string)), "#3");
128                 }
129
130                 [Test]
131                 public void ConvertToString_MinValue ()
132                 {
133                         Assert.AreEqual (byte.MinValue.ToString (CultureInfo.InvariantCulture),
134                                 converter.ConvertToString (null, CultureInfo.InvariantCulture,
135                                 byte.MinValue), "#1");
136
137                         Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
138                                 converter.ConvertToString (null, byte.MinValue), "#2");
139                         Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
140                                 converter.ConvertToString (null, CultureInfo.CurrentCulture,
141                                 byte.MinValue), "#3");
142                         Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
143                                 converter.ConvertToString (byte.MinValue), "#4");
144                 }
145
146                 [Test]
147                 public void ConvertToString_MaxValue ()
148                 {
149                         Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.InvariantCulture),
150                                 converter.ConvertToString (null, CultureInfo.InvariantCulture,
151                                 byte.MaxValue), "#1");
152
153                         Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
154                                 converter.ConvertToString (null, byte.MaxValue), "#2");
155                         Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
156                                 converter.ConvertToString (null, CultureInfo.CurrentCulture,
157                                 byte.MaxValue), "#3");
158                         Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
159                                 converter.ConvertToString (byte.MaxValue), "#4");
160                 }
161
162                 [Test]
163                 public void ConvertFrom_InvalidValue ()
164                 {
165                         try {
166                                 converter.ConvertFrom ("*1");
167                                 Assert.Fail ("#1");
168                         } catch (AssertionException) {
169                                 throw;
170                         } catch (Exception ex) {
171                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
172                                 Assert.IsNotNull (ex.InnerException, "#3");
173                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
174                         }
175                 }
176
177                 [Test]
178                 public void ConvertFrom_InvalidValue_Invariant ()
179                 {
180                         try {
181                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
182                                 Assert.Fail ("#1");
183                         } catch (AssertionException) {
184                                 throw;
185                         } catch (Exception ex) {
186                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
187                                 Assert.IsNotNull (ex.InnerException, "#3");
188                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
189                         }
190                 }
191
192                 [Test]
193                 public void ConvertFrom_Base10_MinOverflow ()
194                 {
195                         string minOverflow = ((int) (byte.MinValue - 1)).ToString (
196                                 CultureInfo.CurrentCulture);
197
198                         try {
199                                 converter.ConvertFrom (minOverflow);
200                                 Assert.Fail ("#1");
201                         } catch (AssertionException) {
202                                 throw;
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_Base10_MinOverflow_Invariant ()
212                 {
213                         string minOverflow = ((int) (byte.MinValue - 1)).ToString (
214                                 CultureInfo.InvariantCulture);
215
216                         try {
217                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 
218                                         minOverflow);
219                                 Assert.Fail ("#1");
220                         } catch (AssertionException) {
221                                 throw;
222                         } catch (Exception ex) {
223                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
224                                 Assert.IsNotNull (ex.InnerException, "#3");
225                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
226                         }
227                 }
228
229                 [Test]
230                 public void ConvertFrom_Base10_MaxOverflow ()
231                 {
232                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
233                                 CultureInfo.CurrentCulture);
234
235                         try {
236                                 converter.ConvertFrom (maxOverflow);
237                                 Assert.Fail ("#1");
238                         } catch (AssertionException) {
239                                 throw;
240                         } catch (Exception ex) {
241                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
242                                 Assert.IsNotNull (ex.InnerException, "#3");
243                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
244                         }
245                 }
246
247                 [Test]
248                 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
249                 {
250                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
251                                 CultureInfo.InvariantCulture);
252
253                         try {
254                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 
255                                         maxOverflow);
256                                 Assert.Fail ("#1");
257                         } catch (AssertionException) {
258                                 throw;
259                         } catch (Exception ex) {
260                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
261                                 Assert.IsNotNull (ex.InnerException, "#3");
262                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
263                         }
264                 }
265
266                 [Test]
267                 public void ConvertFrom_Base16_MinOverflow ()
268                 {
269                         string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x", 
270                                 CultureInfo.CurrentCulture);
271
272                         try {
273                                 converter.ConvertFrom ("#" + minOverflow);
274                                 Assert.Fail ("#1");
275                         } catch (AssertionException) {
276                                 throw;
277                         } catch (Exception ex) {
278                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
279                                 Assert.IsNotNull (ex.InnerException, "#3");
280                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
281                         }
282                 }
283
284                 [Test]
285                 public void ConvertFrom_Base16_MinOverflow_Invariant ()
286                 {
287                         string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
288                                 CultureInfo.InvariantCulture);
289
290                         try {
291                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 
292                                         "#" + minOverflow);
293                                 Assert.Fail ("#1");
294                         } catch (AssertionException) {
295                                 throw;
296                         } catch (Exception ex) {
297                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
298                                 Assert.IsNotNull (ex.InnerException, "#3");
299                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
300                         }
301                 }
302
303                 [Test]
304                 public void ConvertFrom_Base16_MaxOverflow ()
305                 {
306                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
307                                 CultureInfo.CurrentCulture);
308
309                         try {
310                                 converter.ConvertFrom ("#" + maxOverflow);
311                                 Assert.Fail ("#1");
312                         } catch (AssertionException) {
313                                 throw;
314                         } catch (Exception ex) {
315                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
316                                 Assert.IsNotNull (ex.InnerException, "#3");
317                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
318                         }
319                 }
320
321                 [Test]
322                 public void ConvertFrom_Base16_MaxOverflow_Invariant ()
323                 {
324                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
325                                 CultureInfo.InvariantCulture);
326
327                         try {
328                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 
329                                         "#" + maxOverflow);
330                                 Assert.Fail ("#1");
331                         } catch (AssertionException) {
332                                 throw;
333                         } catch (Exception ex) {
334                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
335                                 Assert.IsNotNull (ex.InnerException, "#3");
336                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
337                         }
338                 }
339
340                 [Test]
341                 public void ConvertFromString_InvalidValue ()
342                 {
343                         try {
344                                 converter.ConvertFromString ("*1");
345                                 Assert.Fail ("#1");
346                         } catch (AssertionException) {
347                                 throw;
348                         } catch (Exception ex) {
349                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
350                                 Assert.IsNotNull (ex.InnerException, "#3");
351                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
352                         }
353                 }
354
355                 [Test]
356                 public void ConvertFromString_InvalidValue_Invariant ()
357                 {
358                         try {
359                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
360                                 Assert.Fail ("#1");
361                         } catch (AssertionException) {
362                                 throw;
363                         } catch (Exception ex) {
364                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
365                                 Assert.IsNotNull (ex.InnerException, "#3");
366                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
367                         }
368                 }
369
370                 [Test]
371                 public void ConvertFromString_Base10_MinOverflow ()
372                 {
373                         string minOverflow = ((int) (byte.MinValue - 1)).ToString (
374                                 CultureInfo.CurrentCulture);
375
376                         try {
377                                 converter.ConvertFromString (minOverflow);
378                                 Assert.Fail ("#1");
379                         } catch (AssertionException) {
380                                 throw;
381                         } catch (Exception ex) {
382                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
383                                 Assert.IsNotNull (ex.InnerException, "#3");
384                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
385                         }
386                 }
387
388                 [Test]
389                 public void ConvertFromString_Base10_MinOverflow_Invariant ()
390                 {
391                         string minOverflow = ((int) (byte.MinValue - 1)).ToString (
392                                 CultureInfo.InvariantCulture);
393
394                         try {
395                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
396                                         minOverflow);
397                                 Assert.Fail ("#1");
398                         } catch (AssertionException) {
399                                 throw;
400                         } catch (Exception ex) {
401                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
402                                 Assert.IsNotNull (ex.InnerException, "#3");
403                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
404                         }
405                 }
406
407                 [Test]
408                 public void ConvertFromString_Base10_MaxOverflow ()
409                 {
410                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
411                                 CultureInfo.CurrentCulture);
412
413                         try {
414                                 converter.ConvertFromString (maxOverflow);
415                                 Assert.Fail ("#1");
416                         } catch (AssertionException) {
417                                 throw;
418                         } catch (Exception ex) {
419                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
420                                 Assert.IsNotNull (ex.InnerException, "#3");
421                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
422                         }
423                 }
424
425                 [Test]
426                 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
427                 {
428                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
429                                 CultureInfo.InvariantCulture);
430
431                         try {
432                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
433                                         maxOverflow);
434                                 Assert.Fail ("#1");
435                         } catch (AssertionException) {
436                                 throw;
437                         } catch (Exception ex) {
438                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
439                                 Assert.IsNotNull (ex.InnerException, "#3");
440                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
441                         }
442                 }
443
444                 [Test]
445                 public void ConvertFromString_Base16_MinOverflow ()
446                 {
447                         string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
448                                 CultureInfo.CurrentCulture);
449
450                         try {
451                                 converter.ConvertFromString ("#" + minOverflow);
452                                 Assert.Fail ("#1");
453                         } catch (AssertionException) {
454                                 throw;
455                         } catch (Exception ex) {
456                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
457                                 Assert.IsNotNull (ex.InnerException, "#3");
458                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
459                         }
460                 }
461
462                 [Test]
463                 public void ConvertFromString_Base16_MinOverflow_Invariant ()
464                 {
465                         string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
466                                 CultureInfo.InvariantCulture);
467
468                         try {
469                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
470                                         "#" + minOverflow);
471                                 Assert.Fail ("#1");
472                         } catch (AssertionException) {
473                                 throw;
474                         } catch (Exception ex) {
475                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
476                                 Assert.IsNotNull (ex.InnerException, "#3");
477                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
478                         }
479                 }
480
481                 [Test]
482                 public void ConvertFromString_Base16_MaxOverflow ()
483                 {
484                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
485                                 CultureInfo.CurrentCulture);
486
487                         try {
488                                 converter.ConvertFromString ("#" + maxOverflow);
489                                 Assert.Fail ("#1");
490                         } catch (AssertionException) {
491                                 throw;
492                         } catch (Exception ex) {
493                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
494                                 Assert.IsNotNull (ex.InnerException, "#3");
495                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
496                         }
497                 }
498
499                 [Test]
500                 public void ConvertFromString_Base16_MaxOverflow_Invariant ()
501                 {
502                         string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
503                                 CultureInfo.InvariantCulture);
504
505                         try {
506                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
507                                         "#" + maxOverflow);
508                                 Assert.Fail ("#1");
509                         } catch (AssertionException) {
510                                 throw;
511                         } catch (Exception ex) {
512                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
513                                 Assert.IsNotNull (ex.InnerException, "#3");
514                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
515                         }
516                 }
517
518                 [Serializable]
519                 private sealed class MyCultureInfo : CultureInfo
520                 {
521                         internal MyCultureInfo ()
522                                 : base ("en-US")
523                         {
524                         }
525
526                         public override object GetFormat (Type formatType)
527                         {
528                                 if (formatType == typeof (NumberFormatInfo)) {
529                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
530
531                                         nfi.NegativeSign = "myNegativeSign";
532                                         return NumberFormatInfo.ReadOnly (nfi);
533                                 } else {
534                                         return base.GetFormat (formatType);
535                                 }
536                         }
537
538 #if NET_2_0
539 // adding this override in 1.x shows different result in .NET (it is ignored).
540 // Some compatibility kids might want to fix this issue.
541                         public override NumberFormatInfo NumberFormat {
542                                 get {
543                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
544                                         nfi.NegativeSign = "myNegativeSign";
545                                         return nfi;
546                                 }
547                                 set { throw new NotSupportedException (); }
548                         }
549 #endif
550                 }
551         }
552 }