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");
47 public void ConvertFrom_MinValue ()
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");
57 public void ConvertFrom_MaxValue ()
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");
68 public void ConvertToString ()
70 CultureInfo culture = new MyCultureInfo ();
71 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
73 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
77 [ExpectedException (typeof (NotSupportedException))]
78 public void ConvertFrom_Object ()
80 converter.ConvertFrom (new object ());
84 [ExpectedException (typeof (NotSupportedException))]
85 public void ConvertFrom_Byte ()
87 converter.ConvertFrom (byte.MaxValue);
91 [ExpectedException (typeof (NotSupportedException))]
92 public void ConvertFrom_Int16 ()
94 converter.ConvertFrom ((short) 10);
98 [ExpectedException (typeof (NotSupportedException))]
99 public void ConvertFrom_Int32 ()
101 converter.ConvertFrom (10);
105 public void ConvertTo_MinValue ()
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");
118 public void ConvertTo_MaxValue ()
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");
131 public void ConvertToString_MinValue ()
133 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.InvariantCulture),
134 converter.ConvertToString (null, CultureInfo.InvariantCulture,
135 byte.MinValue), "#1");
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");
147 public void ConvertToString_MaxValue ()
149 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.InvariantCulture),
150 converter.ConvertToString (null, CultureInfo.InvariantCulture,
151 byte.MaxValue), "#1");
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");
163 public void ConvertFrom_InvalidValue ()
166 converter.ConvertFrom ("*1");
168 } catch (AssertionException) {
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");
178 public void ConvertFrom_InvalidValue_Invariant ()
181 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
183 } catch (AssertionException) {
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");
193 public void ConvertFrom_Base10_MinOverflow ()
195 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
196 CultureInfo.CurrentCulture);
199 converter.ConvertFrom (minOverflow);
201 } catch (AssertionException) {
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_Base10_MinOverflow_Invariant ()
213 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
214 CultureInfo.InvariantCulture);
217 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
220 } catch (AssertionException) {
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");
230 public void ConvertFrom_Base10_MaxOverflow ()
232 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
233 CultureInfo.CurrentCulture);
236 converter.ConvertFrom (maxOverflow);
238 } catch (AssertionException) {
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");
248 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
250 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
251 CultureInfo.InvariantCulture);
254 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
257 } catch (AssertionException) {
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");
267 public void ConvertFrom_Base16_MinOverflow ()
269 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
270 CultureInfo.CurrentCulture);
273 converter.ConvertFrom ("#" + minOverflow);
275 } catch (AssertionException) {
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");
285 public void ConvertFrom_Base16_MinOverflow_Invariant ()
287 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
288 CultureInfo.InvariantCulture);
291 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
294 } catch (AssertionException) {
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");
304 public void ConvertFrom_Base16_MaxOverflow ()
306 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
307 CultureInfo.CurrentCulture);
310 converter.ConvertFrom ("#" + maxOverflow);
312 } catch (AssertionException) {
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");
322 public void ConvertFrom_Base16_MaxOverflow_Invariant ()
324 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
325 CultureInfo.InvariantCulture);
328 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
331 } catch (AssertionException) {
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");
341 public void ConvertFromString_InvalidValue ()
344 converter.ConvertFromString ("*1");
346 } catch (AssertionException) {
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");
356 public void ConvertFromString_InvalidValue_Invariant ()
359 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
361 } catch (AssertionException) {
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");
371 public void ConvertFromString_Base10_MinOverflow ()
373 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
374 CultureInfo.CurrentCulture);
377 converter.ConvertFromString (minOverflow);
379 } catch (AssertionException) {
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");
389 public void ConvertFromString_Base10_MinOverflow_Invariant ()
391 string minOverflow = ((int) (byte.MinValue - 1)).ToString (
392 CultureInfo.InvariantCulture);
395 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
398 } catch (AssertionException) {
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");
408 public void ConvertFromString_Base10_MaxOverflow ()
410 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
411 CultureInfo.CurrentCulture);
414 converter.ConvertFromString (maxOverflow);
416 } catch (AssertionException) {
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");
426 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
428 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString (
429 CultureInfo.InvariantCulture);
432 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
435 } catch (AssertionException) {
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");
445 public void ConvertFromString_Base16_MinOverflow ()
447 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
448 CultureInfo.CurrentCulture);
451 converter.ConvertFromString ("#" + minOverflow);
453 } catch (AssertionException) {
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");
463 public void ConvertFromString_Base16_MinOverflow_Invariant ()
465 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x",
466 CultureInfo.InvariantCulture);
469 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
472 } catch (AssertionException) {
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");
482 public void ConvertFromString_Base16_MaxOverflow ()
484 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
485 CultureInfo.CurrentCulture);
488 converter.ConvertFromString ("#" + maxOverflow);
490 } catch (AssertionException) {
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");
500 public void ConvertFromString_Base16_MaxOverflow_Invariant ()
502 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x",
503 CultureInfo.InvariantCulture);
506 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
509 } catch (AssertionException) {
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");
519 private sealed class MyCultureInfo : CultureInfo
521 internal MyCultureInfo ()
526 public override object GetFormat (Type formatType)
528 if (formatType == typeof (NumberFormatInfo)) {
529 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
531 nfi.NegativeSign = "myNegativeSign";
532 return NumberFormatInfo.ReadOnly (nfi);
534 return base.GetFormat (formatType);
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 {
543 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
544 nfi.NegativeSign = "myNegativeSign";
547 set { throw new NotSupportedException (); }