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");
47 public void ConvertFrom_MinValue ()
49 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#8000"), "#1");
50 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x8000"), "#2");
51 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X8000"), "#3");
52 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x8000"), "#4");
53 Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X8000"), "#5");
57 public void ConvertFrom_MaxValue ()
59 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fff"), "#1");
60 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFF"), "#2");
61 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fff"), "#3");
62 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFF"), "#4");
63 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fff"), "#5");
64 Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFF"), "#6");
68 public void ConvertToString ()
70 CultureInfo culture = new MyCultureInfo ();
71 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
73 Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (short) -5), "#1");
74 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
78 public void ConvertFromString ()
80 CultureInfo culture = new MyCultureInfo ();
81 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
83 Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
87 [ExpectedException (typeof (NotSupportedException))]
88 public void ConvertFrom_Object ()
90 converter.ConvertFrom (new object ());
94 [ExpectedException (typeof (NotSupportedException))]
95 public void ConvertFrom_Int16 ()
97 converter.ConvertFrom ((short) 10);
101 [ExpectedException (typeof (NotSupportedException))]
102 public void ConvertFrom_Int32 ()
104 converter.ConvertFrom (10);
108 public void ConvertTo_MinValue ()
110 Assert.AreEqual (short.MinValue.ToString (CultureInfo.InvariantCulture),
111 converter.ConvertTo (null, CultureInfo.InvariantCulture, short.MinValue,
112 typeof (string)), "#1");
113 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
114 converter.ConvertTo (null, CultureInfo.CurrentCulture, short.MinValue,
115 typeof (string)), "#2");
116 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
117 converter.ConvertTo (short.MinValue, typeof (string)), "#3");
121 public void ConvertTo_MaxValue ()
123 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.InvariantCulture),
124 converter.ConvertTo (null, CultureInfo.InvariantCulture, short.MaxValue,
125 typeof (string)), "#1");
126 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
127 converter.ConvertTo (null, CultureInfo.CurrentCulture, short.MaxValue,
128 typeof (string)), "#2");
129 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
130 converter.ConvertTo (short.MaxValue, typeof (string)), "#3");
134 public void ConvertToString_MinValue ()
136 Assert.AreEqual (short.MinValue.ToString (CultureInfo.InvariantCulture),
137 converter.ConvertToString (null, CultureInfo.InvariantCulture,
138 short.MinValue), "#1");
140 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
141 converter.ConvertToString (null, short.MinValue), "#2");
142 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
143 converter.ConvertToString (null, CultureInfo.CurrentCulture,
144 short.MinValue), "#3");
145 Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
146 converter.ConvertToString (short.MinValue), "#4");
150 public void ConvertToString_MaxValue ()
152 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.InvariantCulture),
153 converter.ConvertToString (null, CultureInfo.InvariantCulture,
154 short.MaxValue), "#1");
156 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
157 converter.ConvertToString (null, short.MaxValue), "#2");
158 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
159 converter.ConvertToString (null, CultureInfo.CurrentCulture,
160 short.MaxValue), "#3");
161 Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
162 converter.ConvertToString (short.MaxValue), "#4");
166 public void ConvertFrom_InvalidValue ()
169 converter.ConvertFrom ("*1");
171 } catch (AssertionException) {
173 } catch (Exception ex) {
174 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
175 Assert.IsNotNull (ex.InnerException, "#3");
176 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
181 public void ConvertFrom_InvalidValue_Invariant ()
184 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
186 } catch (AssertionException) {
188 } catch (Exception ex) {
189 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
190 Assert.IsNotNull (ex.InnerException, "#3");
191 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
196 public void ConvertFrom_Base10_MinOverflow ()
198 string minOverflow = ((int) (short.MinValue - 1)).ToString (
199 CultureInfo.CurrentCulture);
202 converter.ConvertFrom (minOverflow);
204 } catch (AssertionException) {
206 } catch (Exception ex) {
207 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
208 Assert.IsNotNull (ex.InnerException, "#3");
209 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
214 public void ConvertFrom_Base10_MinOverflow_Invariant ()
216 string minOverflow = ((int) (short.MinValue - 1)).ToString (
217 CultureInfo.InvariantCulture);
220 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
223 } catch (AssertionException) {
225 } catch (Exception ex) {
226 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
227 Assert.IsNotNull (ex.InnerException, "#3");
228 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
233 public void ConvertFrom_Base10_MaxOverflow ()
235 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
236 CultureInfo.CurrentCulture);
239 converter.ConvertFrom (maxOverflow);
241 } catch (AssertionException) {
243 } catch (Exception ex) {
244 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
245 Assert.IsNotNull (ex.InnerException, "#3");
246 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
251 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
253 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
254 CultureInfo.InvariantCulture);
257 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
260 } catch (AssertionException) {
262 } catch (Exception ex) {
263 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
264 Assert.IsNotNull (ex.InnerException, "#3");
265 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
270 public void ConvertFrom_Base16_MinOverflow ()
272 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
273 CultureInfo.CurrentCulture);
276 converter.ConvertFrom ("#" + minOverflow);
278 } catch (AssertionException) {
280 } catch (Exception ex) {
281 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
282 Assert.IsNotNull (ex.InnerException, "#3");
283 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
288 public void ConvertFrom_Base16_MinOverflow_Invariant ()
290 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
291 CultureInfo.InvariantCulture);
294 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
297 } catch (AssertionException) {
299 } catch (Exception ex) {
300 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
301 Assert.IsNotNull (ex.InnerException, "#3");
302 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#4");
307 public void ConvertFrom_Base16_MaxOverflow ()
309 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
310 CultureInfo.CurrentCulture);
311 Assert.AreEqual (-32768, converter.ConvertFrom (null, CultureInfo.CurrentCulture,
312 "#" + maxOverflow), "#1");
314 maxOverflow = ((int) (ushort.MaxValue)).ToString ("x",
315 CultureInfo.CurrentCulture);
316 Assert.AreEqual (-1, converter.ConvertFrom (null, CultureInfo.CurrentCulture,
317 "#" + maxOverflow), "#2");
319 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
320 CultureInfo.CurrentCulture);
323 converter.ConvertFrom (null, CultureInfo.CurrentCulture,
326 } catch (AssertionException) {
328 } catch (Exception ex) {
329 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
330 Assert.IsNotNull (ex.InnerException, "#5");
331 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
336 public void ConvertFrom_Base16_MaxOverflow_Invariant ()
338 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
339 CultureInfo.InvariantCulture);
340 Assert.AreEqual (-32768, converter.ConvertFrom (null, CultureInfo.InvariantCulture,
341 "#" + maxOverflow), "#1");
343 maxOverflow = ((int) (ushort.MaxValue)).ToString ("x",
344 CultureInfo.InvariantCulture);
345 Assert.AreEqual (-1, converter.ConvertFrom (null, CultureInfo.InvariantCulture,
346 "#" + maxOverflow), "#2");
348 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
349 CultureInfo.InvariantCulture);
352 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
355 } catch (AssertionException) {
357 } catch (Exception ex) {
358 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
359 Assert.IsNotNull (ex.InnerException, "#5");
360 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
365 public void ConvertFromString_InvalidValue ()
368 converter.ConvertFromString ("*1");
370 } catch (AssertionException) {
372 } catch (Exception ex) {
373 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
374 Assert.IsNotNull (ex.InnerException, "#3");
375 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
380 public void ConvertFromString_InvalidValue_Invariant ()
383 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
385 } catch (AssertionException) {
387 } catch (Exception ex) {
388 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
389 Assert.IsNotNull (ex.InnerException, "#3");
390 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
395 public void ConvertFromString_Base10_MinOverflow ()
397 string minOverflow = ((int) (short.MinValue - 1)).ToString (
398 CultureInfo.CurrentCulture);
401 converter.ConvertFromString (minOverflow);
403 } catch (AssertionException) {
405 } catch (Exception ex) {
406 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
407 Assert.IsNotNull (ex.InnerException, "#3");
408 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
413 public void ConvertFromString_Base10_MinOverflow_Invariant ()
415 string minOverflow = ((int) (short.MinValue - 1)).ToString (
416 CultureInfo.InvariantCulture);
419 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
422 } catch (AssertionException) {
424 } catch (Exception ex) {
425 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
426 Assert.IsNotNull (ex.InnerException, "#3");
427 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
432 public void ConvertFromString_Base10_MaxOverflow ()
434 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
435 CultureInfo.CurrentCulture);
438 converter.ConvertFromString (maxOverflow);
440 } catch (AssertionException) {
442 } catch (Exception ex) {
443 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
444 Assert.IsNotNull (ex.InnerException, "#3");
445 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
450 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
452 string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
453 CultureInfo.InvariantCulture);
456 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
459 } catch (AssertionException) {
461 } catch (Exception ex) {
462 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
463 Assert.IsNotNull (ex.InnerException, "#3");
464 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
469 public void ConvertFromString_Base16_MinOverflow ()
471 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
472 CultureInfo.CurrentCulture);
475 converter.ConvertFromString ("#" + minOverflow);
477 } catch (AssertionException) {
479 } catch (Exception ex) {
480 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
481 Assert.IsNotNull (ex.InnerException, "#3");
482 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
487 public void ConvertFromString_Base16_MinOverflow_Invariant ()
489 string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
490 CultureInfo.InvariantCulture);
493 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
496 } catch (AssertionException) {
498 } catch (Exception ex) {
499 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
500 Assert.IsNotNull (ex.InnerException, "#3");
501 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
506 public void ConvertFromString_Base16_MaxOverflow ()
508 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
509 CultureInfo.CurrentCulture);
510 Assert.AreEqual (-32768, converter.ConvertFromString ("#" + maxOverflow), "#1");
512 maxOverflow = (ushort.MaxValue).ToString ("x",
513 CultureInfo.CurrentCulture);
514 Assert.AreEqual (-1, converter.ConvertFromString ("#" + maxOverflow), "#2");
516 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
517 CultureInfo.CurrentCulture);
520 converter.ConvertFromString ("#" + maxOverflow);
522 } catch (AssertionException) {
524 } catch (Exception ex) {
525 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
526 Assert.IsNotNull (ex.InnerException, "#5");
527 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
532 public void ConvertFromString_Base16_MaxOverflow_Invariant ()
534 string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
535 CultureInfo.CurrentCulture);
536 Assert.AreEqual (-32768, converter.ConvertFromString ("#" + maxOverflow), "#1");
538 maxOverflow = (ushort.MaxValue + 1).ToString ("x",
539 CultureInfo.CurrentCulture);
542 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
545 } catch (AssertionException) {
547 } catch (Exception ex) {
548 Assert.AreEqual (typeof (Exception), ex.GetType (), "#3");
549 Assert.IsNotNull (ex.InnerException, "#4");
550 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#5");
555 private sealed class MyCultureInfo : CultureInfo
557 internal MyCultureInfo ()
562 public override object GetFormat (Type formatType)
564 if (formatType == typeof (NumberFormatInfo)) {
565 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
567 nfi.NegativeSign = "myNegativeSign";
568 return NumberFormatInfo.ReadOnly (nfi);
570 return base.GetFormat (formatType);