2 // System.ComponentModel.CharConverter test cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (c) 2008 Gert Driesen
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 CharConverterTest
22 private CharConverter converter;
23 private string pattern;
28 converter = new CharConverter ();
30 DateTimeFormatInfo info = CultureInfo.CurrentCulture.DateTimeFormat;
31 pattern = info.ShortDatePattern + " " + info.ShortTimePattern;
35 public void CanConvertFrom ()
37 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
38 Assert.IsFalse (converter.CanConvertFrom (typeof (char)), "#2");
39 Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
40 Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#4");
41 Assert.IsFalse (converter.CanConvertFrom (typeof (char [])), "#5");
42 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#6");
46 public void CanConvertTo ()
48 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
49 Assert.IsFalse (converter.CanConvertTo (typeof (char)), "#2");
50 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#3");
51 Assert.IsFalse (converter.CanConvertTo (typeof (int)), "#4");
52 Assert.IsFalse (converter.CanConvertTo (typeof (char [])), "#5");
53 Assert.IsFalse (converter.CanConvertTo (typeof (InstanceDescriptor)), "#6");
57 public void ConvertFrom_String ()
61 c = (char) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
63 Assert.AreEqual ('\0', c, "#1");
65 c = (char) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
67 Assert.AreEqual ('e', c, "#2");
69 c = (char) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
71 Assert.AreEqual ('f', c, "#3");
75 public void ConvertFrom_String_Invalid ()
78 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
81 } catch (FormatException ex) {
82 // ef is not a valid value for Char
83 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#A2");
84 Assert.IsNull (ex.InnerException, "#A3");
85 Assert.IsNotNull (ex.Message, "#A4");
86 Assert.IsTrue (ex.Message.IndexOf (typeof (char).Name) != -1, "#A5");
87 Assert.IsTrue (ex.Message.IndexOf ("ef") != -1, "#A6");
91 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
94 } catch (FormatException ex) {
95 // \ref\n is not a valid value for Char
96 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
97 Assert.IsNull (ex.InnerException, "#B3");
98 Assert.IsNotNull (ex.Message, "#B4");
99 Assert.IsTrue (ex.Message.IndexOf (typeof (char).Name) != -1, "#B5");
100 Assert.IsTrue (ex.Message.IndexOf ("ef") != -1, "#B6");
105 public void ConvertFrom_Value_Null ()
108 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
111 } catch (NotSupportedException ex) {
112 // CharConverter cannot convert from (null)
113 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
114 Assert.IsNull (ex.InnerException, "#3");
115 Assert.IsNotNull (ex.Message, "#4");
116 Assert.IsTrue (ex.Message.IndexOf (typeof (CharConverter).Name) != -1, "#5");
117 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#6");
122 public void ConvertToString ()
126 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
128 Assert.AreEqual (" ", result, "#1");
130 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
132 Assert.AreEqual (string.Empty, result, "#2");
134 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
136 Assert.AreEqual ("f", result, "#3");
138 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
140 Assert.AreEqual (string.Empty, result, "#4");
142 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
143 new char [] { 'a', 'f' });
144 Assert.AreEqual ("System.Char[]", result, "#5");