2 // System.ComponentModel.Int16Converter 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 Int16ConverterTests
22 private Int16Converter converter;
27 converter = new Int16Converter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (short)), "#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 (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#8000"), "#1");
51 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x8000"), "#2");
52 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X8000"), "#3");
53 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x8000"), "#4");
54 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X8000"), "#5");
58 public void ConvertFrom_MaxValue ()
60 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fff"), "#1");
61 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFF"), "#2");
62 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fff"), "#3");
63 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFF"), "#4");
64 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fff"), "#5");
65 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFF"), "#6");
69 public void ConvertToString ()
71 CultureInfo culture = new MyCultureInfo ();
72 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
74 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (short) -5), "#1");
75 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
79 public void ConvertFromString ()
81 CultureInfo culture = new MyCultureInfo ();
82 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
84 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
88 [ExpectedException (typeof (NotSupportedException))]
89 public void ConvertFrom_Object ()
91 converter.ConvertFrom (new object ());
95 [ExpectedException (typeof (NotSupportedException))]
96 public void ConvertFrom_Int16 ()
98 converter.ConvertFrom ((short) 10);
102 [ExpectedException (typeof (NotSupportedException))]
103 public void ConvertFrom_Int32 ()
105 converter.ConvertFrom (10);
109 public void ConvertTo_MinValue ()
111 Assert.AreEqual (short.MinValue.ToString (CultureInfo.InvariantCulture),
112 converter.ConvertTo (null, CultureInfo.InvariantCulture, short.MinValue,
113 typeof (string)), "#1");
114 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
115 converter.ConvertTo (null, CultureInfo.CurrentCulture, short.MinValue,
116 typeof (string)), "#2");
117 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
118 converter.ConvertTo (short.MinValue, typeof (string)), "#3");
122 public void ConvertTo_MaxValue ()
124 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.InvariantCulture),
125 converter.ConvertTo (null, CultureInfo.InvariantCulture, short.MaxValue,
126 typeof (string)), "#1");
127 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
128 converter.ConvertTo (null, CultureInfo.CurrentCulture, short.MaxValue,
129 typeof (string)), "#2");
130 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
131 converter.ConvertTo (short.MaxValue, typeof (string)), "#3");
135 public void ConvertToString_MinValue ()
137 Assert.AreEqual (short.MinValue.ToString (CultureInfo.InvariantCulture),
138 converter.ConvertToString (null, CultureInfo.InvariantCulture,
139 short.MinValue), "#1");
141 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
142 converter.ConvertToString (null, short.MinValue), "#2");
143 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
144 converter.ConvertToString (null, CultureInfo.CurrentCulture,
145 short.MinValue), "#3");
146 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
147 converter.ConvertToString (short.MinValue), "#4");
151 public void ConvertToString_MaxValue ()
153 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.InvariantCulture),
154 converter.ConvertToString (null, CultureInfo.InvariantCulture,
155 short.MaxValue), "#1");
157 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
158 converter.ConvertToString (null, short.MaxValue), "#2");
159 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
160 converter.ConvertToString (null, CultureInfo.CurrentCulture,
161 short.MaxValue), "#3");
162 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
163 converter.ConvertToString (short.MaxValue), "#4");
167 public void ConvertFrom_InvalidValue ()
170 converter.ConvertFrom ("*1");
172 } catch (AssertionException) {
174 } catch (Exception ex) {
175 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
176 Assert.IsNotNull (ex.InnerException, "#3");
177 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
182 public void ConvertFrom_InvalidValue_Invariant ()
185 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
187 } catch (AssertionException) {
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");
197 public void ConvertFrom_Base10_MinOverflow ()
199 string minOverflow = ((int) (short.MinValue - 1)).ToString (
200 CultureInfo.CurrentCulture);
203 converter.ConvertFrom (minOverflow);
205 } catch (AssertionException) {
207 } catch (Exception ex) {
208 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
209 Assert.IsNotNull (ex.InnerException, "#3");
210 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
215 public void ConvertFrom_Base10_MinOverflow_Invariant ()
217 string minOverflow = ((int) (short.MinValue - 1)).ToString (
218 CultureInfo.InvariantCulture);
221 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
224 } catch (AssertionException) {
226 } catch (Exception ex) {
227 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
228 Assert.IsNotNull (ex.InnerException, "#3");
229 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
234 public void ConvertFrom_Base10_MaxOverflow ()
236 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
237 CultureInfo.CurrentCulture);
240 converter.ConvertFrom (maxOverflow);
242 } catch (AssertionException) {
244 } catch (Exception ex) {
245 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
246 Assert.IsNotNull (ex.InnerException, "#3");
247 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
252 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
254 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
255 CultureInfo.InvariantCulture);
258 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
261 } catch (AssertionException) {
263 } catch (Exception ex) {
264 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
265 Assert.IsNotNull (ex.InnerException, "#3");
266 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
271 public void ConvertFrom_Base16_MinOverflow ()
273 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
274 CultureInfo.CurrentCulture);
277 converter.ConvertFrom ("#" + minOverflow);
279 } catch (AssertionException) {
281 } catch (Exception ex) {
282 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
283 Assert.IsNotNull (ex.InnerException, "#3");
284 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
289 public void ConvertFrom_Base16_MinOverflow_Invariant ()
291 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
292 CultureInfo.InvariantCulture);
295 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
298 } catch (AssertionException) {
300 } catch (Exception ex) {
301 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
302 Assert.IsNotNull (ex.InnerException, "#3");
303 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#4");
308 public void ConvertFrom_Base16_MaxOverflow ()
310 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
311 CultureInfo.CurrentCulture);
312 Assert.AreEqual (-32768, converter.ConvertFrom (null, CultureInfo.CurrentCulture,
313 "#" + maxOverflow), "#1");
315 maxOverflow = ((int) (ushort.MaxValue)).ToString ("x",
316 CultureInfo.CurrentCulture);
317 Assert.AreEqual (-1, converter.ConvertFrom (null, CultureInfo.CurrentCulture,
318 "#" + maxOverflow), "#2");
320 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
321 CultureInfo.CurrentCulture);
324 converter.ConvertFrom (null, CultureInfo.CurrentCulture,
327 } catch (AssertionException) {
329 } catch (Exception ex) {
330 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
331 Assert.IsNotNull (ex.InnerException, "#5");
332 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
337 public void ConvertFrom_Base16_MaxOverflow_Invariant ()
339 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
340 CultureInfo.InvariantCulture);
341 Assert.AreEqual (-32768, converter.ConvertFrom (null, CultureInfo.InvariantCulture,
342 "#" + maxOverflow), "#1");
344 maxOverflow = ((int) (ushort.MaxValue)).ToString ("x",
345 CultureInfo.InvariantCulture);
346 Assert.AreEqual (-1, converter.ConvertFrom (null, CultureInfo.InvariantCulture,
347 "#" + maxOverflow), "#2");
349 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
350 CultureInfo.InvariantCulture);
353 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
356 } catch (AssertionException) {
358 } catch (Exception ex) {
359 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
360 Assert.IsNotNull (ex.InnerException, "#5");
361 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
366 public void ConvertFromString_InvalidValue ()
369 converter.ConvertFromString ("*1");
371 } catch (AssertionException) {
373 } catch (Exception ex) {
374 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
375 Assert.IsNotNull (ex.InnerException, "#3");
376 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
381 public void ConvertFromString_InvalidValue_Invariant ()
384 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
386 } catch (AssertionException) {
388 } catch (Exception ex) {
389 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
390 Assert.IsNotNull (ex.InnerException, "#3");
391 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
396 public void ConvertFromString_Base10_MinOverflow ()
398 string minOverflow = ((int) (short.MinValue - 1)).ToString (
399 CultureInfo.CurrentCulture);
402 converter.ConvertFromString (minOverflow);
404 } catch (AssertionException) {
406 } catch (Exception ex) {
407 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
408 Assert.IsNotNull (ex.InnerException, "#3");
409 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
414 public void ConvertFromString_Base10_MinOverflow_Invariant ()
416 string minOverflow = ((int) (short.MinValue - 1)).ToString (
417 CultureInfo.InvariantCulture);
420 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
423 } catch (AssertionException) {
425 } catch (Exception ex) {
426 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
427 Assert.IsNotNull (ex.InnerException, "#3");
428 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
433 public void ConvertFromString_Base10_MaxOverflow ()
435 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
436 CultureInfo.CurrentCulture);
439 converter.ConvertFromString (maxOverflow);
441 } catch (AssertionException) {
443 } catch (Exception ex) {
444 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
445 Assert.IsNotNull (ex.InnerException, "#3");
446 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
451 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
453 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
454 CultureInfo.InvariantCulture);
457 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
460 } catch (AssertionException) {
462 } catch (Exception ex) {
463 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
464 Assert.IsNotNull (ex.InnerException, "#3");
465 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
470 public void ConvertFromString_Base16_MinOverflow ()
472 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
473 CultureInfo.CurrentCulture);
476 converter.ConvertFromString ("#" + minOverflow);
478 } catch (AssertionException) {
480 } catch (Exception ex) {
481 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
482 Assert.IsNotNull (ex.InnerException, "#3");
483 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
488 public void ConvertFromString_Base16_MinOverflow_Invariant ()
490 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
491 CultureInfo.InvariantCulture);
494 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
497 } catch (AssertionException) {
499 } catch (Exception ex) {
500 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
501 Assert.IsNotNull (ex.InnerException, "#3");
502 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
507 public void ConvertFromString_Base16_MaxOverflow ()
509 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
510 CultureInfo.CurrentCulture);
511 Assert.AreEqual (-32768, converter.ConvertFromString ("#" + maxOverflow), "#1");
513 maxOverflow = (ushort.MaxValue).ToString ("x",
514 CultureInfo.CurrentCulture);
515 Assert.AreEqual (-1, converter.ConvertFromString ("#" + maxOverflow), "#2");
517 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
518 CultureInfo.CurrentCulture);
521 converter.ConvertFromString ("#" + maxOverflow);
523 } catch (AssertionException) {
525 } catch (Exception ex) {
526 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
527 Assert.IsNotNull (ex.InnerException, "#5");
528 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
533 public void ConvertFromString_Base16_MaxOverflow_Invariant ()
535 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
536 CultureInfo.CurrentCulture);
537 Assert.AreEqual (-32768, converter.ConvertFromString ("#" + maxOverflow), "#1");
539 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
540 CultureInfo.CurrentCulture);
543 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
546 } catch (AssertionException) {
548 } catch (Exception ex) {
549 Assert.AreEqual (typeof (Exception), ex.GetType (), "#3");
550 Assert.IsNotNull (ex.InnerException, "#4");
551 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#5");
556 private sealed class MyCultureInfo : CultureInfo
558 internal MyCultureInfo ()
563 public override object GetFormat (Type formatType)
565 if (formatType == typeof (NumberFormatInfo)) {
566 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
568 nfi.NegativeSign = "myNegativeSign";
569 return NumberFormatInfo.ReadOnly (nfi);
571 return base.GetFormat (formatType);
576 // adding this override in 1.x shows different result in .NET (it is ignored).
577 // Some compatibility kids might want to fix this issue.
578 public override NumberFormatInfo NumberFormat {
580 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
581 nfi.NegativeSign = "myNegativeSign";
584 set { throw new NotSupportedException (); }