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;
14 using NUnit.Framework;
16 namespace MonoTests.System.ComponentModel
19 public class TypeConverterTests
22 public void DefaultImplementation ()
24 BConverter converter = new BConverter ();
27 Assert.IsNull (converter.GetProperties (c), "#1");
28 Assert.IsNull (converter.GetProperties (null, c), "#2");
29 Assert.IsNull (converter.GetProperties (null, c, null), "#3");
31 Assert.IsNull (converter.GetProperties (null), "#4");
32 Assert.IsNull (converter.GetProperties (null, null), "#5");
33 Assert.IsNull (converter.GetProperties (null, null, null), "#6");
34 Assert.IsFalse (converter.GetCreateInstanceSupported (), "#7");
35 Assert.IsFalse (converter.GetCreateInstanceSupported (null), "#8");
36 Assert.IsFalse (converter.GetPropertiesSupported (), "#9");
37 Assert.IsFalse (converter.GetPropertiesSupported (null), "#10");
39 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#11");
40 Assert.IsTrue (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "#12");
41 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#13");
42 Assert.IsTrue (converter.CanConvertTo (null, typeof (string)), "#14");
47 [Ignore ("TD BUG ID: 7229")]
49 public void GetProperties ()
51 PropertyDescriptorCollection properties = null;
53 TypeConverter converter = TypeDescriptor.GetConverter (c);
55 Assert.AreEqual (typeof (AConverter).FullName, converter.GetType ().FullName, "#1");
57 properties = converter.GetProperties (c);
58 Assert.AreEqual (1, properties.Count, "#2");
59 Assert.AreEqual ("A", properties[0].Name, "#3");
61 // ensure collection is read-only
65 } catch (NotSupportedException) {
66 // read-only collection cannot be modified
69 properties = converter.GetProperties (null, c);
70 Assert.AreEqual (1, properties.Count, "#5");
71 Assert.AreEqual ("A", properties[0].Name, "#6");
74 // ensure collection is read-only
78 } catch (NotSupportedException) {
79 // read-only collection cannot be modified
82 properties = converter.GetProperties (null, c, null);
83 Assert.AreEqual (2, properties.Count, "#8");
85 // ensure collection is read-only
89 } catch (NotSupportedException) {
90 // read-only collection cannot be modified
93 properties = converter.GetProperties (null);
94 Assert.IsNull (properties, "#10");
96 properties = converter.GetProperties (null, null);
97 Assert.IsNull (properties, "#11");
99 properties = converter.GetProperties (null, null, null);
100 Assert.IsNull (properties, "#12");
104 public void GetConvertFromException ()
106 MockTypeConverter converter = new MockTypeConverter ();
109 converter.GetConvertFromException (null);
111 } catch (NotSupportedException ex) {
112 // MockTypeConverter cannot convert from (null)
113 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
114 Assert.IsNull (ex.InnerException, "#A3");
115 Assert.IsNotNull (ex.Message, "#A4");
116 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#A5");
117 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#A6");
121 converter.GetConvertFromException ("B");
123 } catch (NotSupportedException ex) {
124 // MockTypeConverter cannot convert from System.String
125 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
126 Assert.IsNull (ex.InnerException, "#B3");
127 Assert.IsNotNull (ex.Message, "#B4");
128 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
129 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
134 public void GetConvertToException ()
136 MockTypeConverter converter = new MockTypeConverter ();
139 converter.GetConvertToException (null, typeof (DateTime));
141 } catch (NotSupportedException ex) {
142 // 'MockTypeConverter' is unable to convert '(null)'
143 // to 'System.DateTime'
144 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
145 Assert.IsNull (ex.InnerException, "#A3");
146 Assert.IsNotNull (ex.Message, "#A4");
147 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (MockTypeConverter).Name + "'") != -1, "#A5");
148 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#A6");
149 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (DateTime).FullName + "'") != -1, "#A7");
153 converter.GetConvertToException ("B", typeof (DateTime));
155 } catch (NotSupportedException ex) {
156 // 'MockTypeConverter' is unable to convert 'System.String'
157 // to 'System.DateTime'
158 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
159 Assert.IsNull (ex.InnerException, "#B3");
160 Assert.IsNotNull (ex.Message, "#B4");
161 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
162 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
167 public class FooConverter<T> : TypeConverter
171 [TypeConverter (typeof (FooConverter<string>))]
172 public string FooProperty {
177 public void TestGenericTypeConverterInstantiation ()
179 Assert.IsNotNull (GetType ().GetProperty ("FooProperty").GetCustomAttributes (false));
183 [ExpectedException (typeof (NullReferenceException))]
184 public void GetConvertToException_DestinationType_Null ()
186 MockTypeConverter converter = new MockTypeConverter ();
187 converter.GetConvertToException ("B", (Type) null);
191 [TypeConverter (typeof (AConverter))]
205 public class MockTypeConverter : TypeConverter
207 public new Exception GetConvertFromException (object value)
209 return base.GetConvertFromException (value);
212 public new Exception GetConvertToException (object value, Type destinationType)
214 return base.GetConvertToException (value, destinationType);
218 public class AConverter : TypeConverter
220 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
223 return TypeDescriptor.GetProperties (value, attributes);
225 return base.GetProperties (context, value, attributes);
228 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
234 public class BConverter : TypeConverter