2 // System.ComponentModel.TypeConverter 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 TypeConverterTests
23 public void DefaultImplementation ()
25 BConverter converter = new BConverter ();
28 Assert.IsNull (converter.GetProperties (c), "#1");
29 Assert.IsNull (converter.GetProperties (null, c), "#2");
30 Assert.IsNull (converter.GetProperties (null, c, null), "#3");
32 Assert.IsNull (converter.GetProperties (null), "#4");
33 Assert.IsNull (converter.GetProperties (null, null), "#5");
34 Assert.IsNull (converter.GetProperties (null, null, null), "#6");
35 Assert.IsFalse (converter.GetCreateInstanceSupported (), "#7");
36 Assert.IsFalse (converter.GetCreateInstanceSupported (null), "#8");
37 Assert.IsFalse (converter.GetPropertiesSupported (), "#9");
38 Assert.IsFalse (converter.GetPropertiesSupported (null), "#10");
40 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#11");
41 Assert.IsTrue (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "#12");
42 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#13");
43 Assert.IsTrue (converter.CanConvertTo (null, typeof (string)), "#14");
48 [Ignore ("TD BUG ID: 7229")]
50 public void GetProperties ()
52 PropertyDescriptorCollection properties = null;
54 TypeConverter converter = TypeDescriptor.GetConverter (c);
56 Assert.AreEqual (typeof (AConverter).FullName, converter.GetType ().FullName, "#1");
58 properties = converter.GetProperties (c);
59 Assert.AreEqual (1, properties.Count, "#2");
60 Assert.AreEqual ("A", properties[0].Name, "#3");
62 // ensure collection is read-only
66 } catch (NotSupportedException) {
67 // read-only collection cannot be modified
70 properties = converter.GetProperties (null, c);
71 Assert.AreEqual (1, properties.Count, "#5");
72 Assert.AreEqual ("A", properties[0].Name, "#6");
75 // ensure collection is read-only
79 } catch (NotSupportedException) {
80 // read-only collection cannot be modified
83 properties = converter.GetProperties (null, c, null);
84 Assert.AreEqual (2, properties.Count, "#8");
86 // ensure collection is read-only
90 } catch (NotSupportedException) {
91 // read-only collection cannot be modified
94 properties = converter.GetProperties (null);
95 Assert.IsNull (properties, "#10");
97 properties = converter.GetProperties (null, null);
98 Assert.IsNull (properties, "#11");
100 properties = converter.GetProperties (null, null, null);
101 Assert.IsNull (properties, "#12");
105 public void GetConvertFromException ()
107 MockTypeConverter converter = new MockTypeConverter ();
110 converter.GetConvertFromException (null);
112 } catch (NotSupportedException ex) {
113 // MockTypeConverter cannot convert from (null)
114 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
115 Assert.IsNull (ex.InnerException, "#A3");
116 Assert.IsNotNull (ex.Message, "#A4");
117 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#A5");
118 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#A6");
122 converter.GetConvertFromException ("B");
124 } catch (NotSupportedException ex) {
125 // MockTypeConverter cannot convert from System.String
126 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
127 Assert.IsNull (ex.InnerException, "#B3");
128 Assert.IsNotNull (ex.Message, "#B4");
129 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
130 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
135 public void GetConvertToException ()
137 MockTypeConverter converter = new MockTypeConverter ();
140 converter.GetConvertToException (null, typeof (DateTime));
142 } catch (NotSupportedException ex) {
143 // 'MockTypeConverter' is unable to convert '(null)'
144 // to 'System.DateTime'
145 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
146 Assert.IsNull (ex.InnerException, "#A3");
147 Assert.IsNotNull (ex.Message, "#A4");
148 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (MockTypeConverter).Name + "'") != -1, "#A5");
149 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#A6");
150 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (DateTime).FullName + "'") != -1, "#A7");
154 converter.GetConvertToException ("B", typeof (DateTime));
156 } catch (NotSupportedException ex) {
157 // 'MockTypeConverter' is unable to convert 'System.String'
158 // to 'System.DateTime'
159 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
160 Assert.IsNull (ex.InnerException, "#B3");
161 Assert.IsNotNull (ex.Message, "#B4");
162 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
163 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
168 public void ConvertToWithCulture ()
170 var culture = CultureInfo.CreateSpecificCulture ("sv-se");
172 var converter = TypeDescriptor.GetConverter (typeof (string));
173 Assert.AreEqual ("0,5", (string) converter.ConvertTo (null, culture, 0.5, typeof (string)));
176 public class FooConverter<T> : TypeConverter
180 [TypeConverter (typeof (FooConverter<string>))]
181 public string FooProperty {
186 public void TestGenericTypeConverterInstantiation ()
188 Assert.IsNotNull (GetType ().GetProperty ("FooProperty").GetCustomAttributes (false));
191 [ExpectedException (typeof (NullReferenceException))]
192 public void GetConvertToException_DestinationType_Null ()
194 MockTypeConverter converter = new MockTypeConverter ();
195 converter.GetConvertToException ("B", (Type) null);
199 [TypeConverter (typeof (AConverter))]
213 public class MockTypeConverter : TypeConverter
215 public new Exception GetConvertFromException (object value)
217 return base.GetConvertFromException (value);
220 public new Exception GetConvertToException (object value, Type destinationType)
222 return base.GetConvertToException (value, destinationType);
226 public class AConverter : TypeConverter
228 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
231 return TypeDescriptor.GetProperties (value, attributes);
233 return base.GetProperties (context, value, attributes);
236 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
242 public class BConverter : TypeConverter