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");
47 public void GetProperties ()
49 PropertyDescriptorCollection properties = null;
51 TypeConverter converter = TypeDescriptor.GetConverter (c);
53 Assert.AreEqual (typeof (AConverter).FullName, converter.GetType ().FullName, "#1");
55 properties = converter.GetProperties (c);
56 Assert.AreEqual (1, properties.Count, "#2");
57 Assert.AreEqual ("A", properties[0].Name, "#3");
59 // ensure collection is read-only
63 } catch (NotSupportedException) {
64 // read-only collection cannot be modified
67 properties = converter.GetProperties (null, c);
68 Assert.AreEqual (1, properties.Count, "#5");
69 Assert.AreEqual ("A", properties[0].Name, "#6");
72 // ensure collection is read-only
76 } catch (NotSupportedException) {
77 // read-only collection cannot be modified
80 properties = converter.GetProperties (null, c, null);
81 Assert.AreEqual (2, properties.Count, "#8");
83 // ensure collection is read-only
87 } catch (NotSupportedException) {
88 // read-only collection cannot be modified
91 properties = converter.GetProperties (null);
92 Assert.IsNull (properties, "#10");
94 properties = converter.GetProperties (null, null);
95 Assert.IsNull (properties, "#11");
97 properties = converter.GetProperties (null, null, null);
98 Assert.IsNull (properties, "#12");
102 public void GetConvertFromException ()
104 MockTypeConverter converter = new MockTypeConverter ();
107 converter.GetConvertFromException (null);
109 } catch (NotSupportedException ex) {
110 // MockTypeConverter cannot convert from (null)
111 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
112 Assert.IsNull (ex.InnerException, "#A3");
113 Assert.IsNotNull (ex.Message, "#A4");
114 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#A5");
115 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#A6");
119 converter.GetConvertFromException ("B");
121 } catch (NotSupportedException ex) {
122 // MockTypeConverter cannot convert from System.String
123 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
124 Assert.IsNull (ex.InnerException, "#B3");
125 Assert.IsNotNull (ex.Message, "#B4");
126 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
127 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
132 public void GetConvertToException ()
134 MockTypeConverter converter = new MockTypeConverter ();
137 converter.GetConvertToException (null, typeof (DateTime));
139 } catch (NotSupportedException ex) {
140 // 'MockTypeConverter' is unable to convert '(null)'
141 // to 'System.DateTime'
142 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
143 Assert.IsNull (ex.InnerException, "#A3");
144 Assert.IsNotNull (ex.Message, "#A4");
145 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (MockTypeConverter).Name + "'") != -1, "#A5");
146 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#A6");
147 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (DateTime).FullName + "'") != -1, "#A7");
151 converter.GetConvertToException ("B", typeof (DateTime));
153 } catch (NotSupportedException ex) {
154 // 'MockTypeConverter' is unable to convert 'System.String'
155 // to 'System.DateTime'
156 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
157 Assert.IsNull (ex.InnerException, "#B3");
158 Assert.IsNotNull (ex.Message, "#B4");
159 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
160 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
165 public void ConvertToWithCulture ()
167 var culture = CultureInfo.CreateSpecificCulture ("sv-se");
169 var converter = TypeDescriptor.GetConverter (typeof (string));
170 Assert.AreEqual ("0,5", (string) converter.ConvertTo (null, culture, 0.5, typeof (string)));
173 public class FooConverter<T> : TypeConverter
177 [TypeConverter (typeof (FooConverter<string>))]
178 public string FooProperty {
183 public void TestGenericTypeConverterInstantiation ()
185 Assert.IsNotNull (GetType ().GetProperty ("FooProperty").GetCustomAttributes (false));
188 [ExpectedException (typeof (NullReferenceException))]
189 public void GetConvertToException_DestinationType_Null ()
191 MockTypeConverter converter = new MockTypeConverter ();
192 converter.GetConvertToException ("B", (Type) null);
196 public void IsValid ()
198 var tc = new TypeConverter ();
199 Assert.IsFalse (tc.IsValid (null));
203 [TypeConverter (typeof (AConverter))]
217 public class MockTypeConverter : TypeConverter
219 public new Exception GetConvertFromException (object value)
221 return base.GetConvertFromException (value);
224 public new Exception GetConvertToException (object value, Type destinationType)
226 return base.GetConvertToException (value, destinationType);
230 public class AConverter : TypeConverter
232 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
235 return TypeDescriptor.GetProperties (value, attributes);
237 return base.GetProperties (context, value, attributes);
240 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
246 public class BConverter : TypeConverter