2 // System.ComponentModel.ByteConverter 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 ByteConverterTests
22 private ByteConverter converter;
27 converter = new ByteConverter ();
31 public void CanConvertFrom ()
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");
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_MinValue ()
50 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
51 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
52 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
53 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
54 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
58 public void ConvertFrom_MaxValue ()
60 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ff"), "#1");
61 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FF"), "#2");
62 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xff"), "#3");
63 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFF"), "#4");
64 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xff"), "#5");
65 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFF"), "#6");
69 public void ConvertToString ()
71 CultureInfo culture = new MyCultureInfo ();
72 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
74 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
78 [ExpectedException (typeof (NotSupportedException))]
79 public void ConvertFrom_Object ()
81 converter.ConvertFrom (new object ());
85 [ExpectedException (typeof (NotSupportedException))]
86 public void ConvertFrom_Byte ()
88 converter.ConvertFrom (byte.MaxValue);
92 [ExpectedException (typeof (NotSupportedException))]
93 public void ConvertFrom_Int16 ()
95 converter.ConvertFrom ((short) 10);
99 [ExpectedException (typeof (NotSupportedException))]
100 public void ConvertFrom_Int32 ()
102 converter.ConvertFrom (10);
106 public void ConvertTo_MinValue ()
108 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.InvariantCulture),
109 converter.ConvertTo (null, CultureInfo.InvariantCulture, byte.MinValue,
110 typeof (string)), "#1");
111 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
112 converter.ConvertTo (null, CultureInfo.CurrentCulture, byte.MinValue,
113 typeof (string)), "#2");
114 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
115 converter.ConvertTo (byte.MinValue, typeof (string)), "#3");
119 public void ConvertTo_MaxValue ()
121 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.InvariantCulture),
122 converter.ConvertTo (null, CultureInfo.InvariantCulture, byte.MaxValue,
123 typeof (string)), "#1");
124 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
125 converter.ConvertTo (null, CultureInfo.CurrentCulture, byte.MaxValue,
126 typeof (string)), "#2");
127 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
128 converter.ConvertTo (byte.MaxValue, typeof (string)), "#3");
132 public void ConvertToString_MinValue ()
134 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.InvariantCulture),
135 converter.ConvertToString (null, CultureInfo.InvariantCulture,
136 byte.MinValue), "#1");
138 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
139 converter.ConvertToString (null, byte.MinValue), "#2");
140 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
141 converter.ConvertToString (null, CultureInfo.CurrentCulture,
142 byte.MinValue), "#3");
143 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture),
144 converter.ConvertToString (byte.MinValue), "#4");
148 public void ConvertToString_MaxValue ()
150 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.InvariantCulture),
151 converter.ConvertToString (null, CultureInfo.InvariantCulture,
152 byte.MaxValue), "#1");
154 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
155 converter.ConvertToString (null, byte.MaxValue), "#2");
156 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
157 converter.ConvertToString (null, CultureInfo.CurrentCulture,
158 byte.MaxValue), "#3");
159 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture),
160 converter.ConvertToString (byte.MaxValue), "#4");
164 public void ConvertFrom_InvalidValue ()
167 converter.ConvertFrom ("*1");
169 } catch (AssertionException) {
171 } catch (Exception ex) {
172 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
173 Assert.IsNotNull (ex.InnerException, "#3");
174 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
179 public void ConvertFrom_InvalidValue_Invariant ()
182 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
184 } catch (AssertionException) {
186 } catch (Exception ex) {
187 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
188 Assert.IsNotNull (ex.InnerException, "#3");
189 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
194 public void ConvertFrom_Base10_MinOverflow ()
196 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
197 CultureInfo.CurrentCulture);
200 converter.ConvertFrom (minOverflow);
202 } catch (AssertionException) {
204 } catch (Exception ex) {
205 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
206 Assert.IsNotNull (ex.InnerException, "#3");
207 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
212 public void ConvertFrom_Base10_MinOverflow_Invariant ()
214 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
215 CultureInfo.InvariantCulture);
218 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
221 } catch (AssertionException) {
223 } catch (Exception ex) {
224 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
225 Assert.IsNotNull (ex.InnerException, "#3");
226 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
231 public void ConvertFrom_Base10_MaxOverflow ()
233 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
234 CultureInfo.CurrentCulture);
237 converter.ConvertFrom (maxOverflow);
239 } catch (AssertionException) {
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 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
251 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
252 CultureInfo.InvariantCulture);
255 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
258 } catch (AssertionException) {
260 } catch (Exception ex) {
261 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
262 Assert.IsNotNull (ex.InnerException, "#3");
263 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
268 public void ConvertFrom_Base16_MinOverflow ()
270 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
271 CultureInfo.CurrentCulture);
274 converter.ConvertFrom ("#" + minOverflow);
276 } catch (AssertionException) {
278 } catch (Exception ex) {
279 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
280 Assert.IsNotNull (ex.InnerException, "#3");
281 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
286 public void ConvertFrom_Base16_MinOverflow_Invariant ()
288 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
289 CultureInfo.InvariantCulture);
292 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
295 } catch (AssertionException) {
297 } catch (Exception ex) {
298 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
299 Assert.IsNotNull (ex.InnerException, "#3");
300 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
305 public void ConvertFrom_Base16_MaxOverflow ()
307 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
308 CultureInfo.CurrentCulture);
311 converter.ConvertFrom ("#" + maxOverflow);
313 } catch (AssertionException) {
315 } catch (Exception ex) {
316 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
317 Assert.IsNotNull (ex.InnerException, "#3");
318 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
323 public void ConvertFrom_Base16_MaxOverflow_Invariant ()
325 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
326 CultureInfo.InvariantCulture);
329 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
332 } catch (AssertionException) {
334 } catch (Exception ex) {
335 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
336 Assert.IsNotNull (ex.InnerException, "#3");
337 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
342 public void ConvertFromString_InvalidValue ()
345 converter.ConvertFromString ("*1");
347 } catch (AssertionException) {
349 } catch (Exception ex) {
350 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
351 Assert.IsNotNull (ex.InnerException, "#3");
352 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
357 public void ConvertFromString_InvalidValue_Invariant ()
360 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
362 } catch (AssertionException) {
364 } catch (Exception ex) {
365 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
366 Assert.IsNotNull (ex.InnerException, "#3");
367 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
372 public void ConvertFromString_Base10_MinOverflow ()
374 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
375 CultureInfo.CurrentCulture);
378 converter.ConvertFromString (minOverflow);
380 } catch (AssertionException) {
382 } catch (Exception ex) {
383 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
384 Assert.IsNotNull (ex.InnerException, "#3");
385 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
390 public void ConvertFromString_Base10_MinOverflow_Invariant ()
392 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
393 CultureInfo.InvariantCulture);
396 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
399 } catch (AssertionException) {
401 } catch (Exception ex) {
402 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
403 Assert.IsNotNull (ex.InnerException, "#3");
404 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
409 public void ConvertFromString_Base10_MaxOverflow ()
411 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
412 CultureInfo.CurrentCulture);
415 converter.ConvertFromString (maxOverflow);
417 } catch (AssertionException) {
419 } catch (Exception ex) {
420 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
421 Assert.IsNotNull (ex.InnerException, "#3");
422 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
427 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
429 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
430 CultureInfo.InvariantCulture);
433 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
436 } catch (AssertionException) {
438 } catch (Exception ex) {
439 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
440 Assert.IsNotNull (ex.InnerException, "#3");
441 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
446 public void ConvertFromString_Base16_MinOverflow ()
448 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
449 CultureInfo.CurrentCulture);
452 converter.ConvertFromString ("#" + minOverflow);
454 } catch (AssertionException) {
456 } catch (Exception ex) {
457 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
458 Assert.IsNotNull (ex.InnerException, "#3");
459 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
464 public void ConvertFromString_Base16_MinOverflow_Invariant ()
466 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
467 CultureInfo.InvariantCulture);
470 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
473 } catch (AssertionException) {
475 } catch (Exception ex) {
476 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
477 Assert.IsNotNull (ex.InnerException, "#3");
478 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
483 public void ConvertFromString_Base16_MaxOverflow ()
485 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
486 CultureInfo.CurrentCulture);
489 converter.ConvertFromString ("#" + maxOverflow);
491 } catch (AssertionException) {
493 } catch (Exception ex) {
494 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
495 Assert.IsNotNull (ex.InnerException, "#3");
496 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
501 public void ConvertFromString_Base16_MaxOverflow_Invariant ()
503 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
504 CultureInfo.InvariantCulture);
507 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
510 } catch (AssertionException) {
512 } catch (Exception ex) {
513 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
514 Assert.IsNotNull (ex.InnerException, "#3");
515 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
520 private sealed class MyCultureInfo : CultureInfo
522 internal MyCultureInfo ()
527 public override object GetFormat (Type formatType)
529 if (formatType == typeof (NumberFormatInfo)) {
530 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
532 nfi.NegativeSign = "myNegativeSign";
533 return NumberFormatInfo.ReadOnly (nfi);
535 return base.GetFormat (formatType);
540 // adding this override in 1.x shows different result in .NET (it is ignored).
541 // Some compatibility kids might want to fix this issue.
542 public override NumberFormatInfo NumberFormat {
544 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
545 nfi.NegativeSign = "myNegativeSign";
548 set { throw new NotSupportedException (); }