2 // UriTypeConverterTest.cs - Unit tests for System.UriTypeConverter
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
34 using System.ComponentModel;
35 using System.ComponentModel.Design.Serialization;
36 using System.Globalization;
38 namespace MonoTests.System {
40 class UriTypeDescriptorContext : ITypeDescriptorContext {
42 public IContainer Container {
43 get { throw new NotImplementedException (); }
46 public object Instance {
47 get { throw new NotImplementedException (); }
50 public void OnComponentChanged ()
52 throw new NotImplementedException ();
55 public bool OnComponentChanging ()
57 throw new NotImplementedException ();
60 public PropertyDescriptor PropertyDescriptor {
61 get { throw new NotImplementedException (); }
64 public object GetService (Type serviceType)
66 throw new NotImplementedException ();
71 public class UriTypeConverterTest {
73 private const string url = "http://www.mono-project.com/";
74 private static Uri uri = new Uri (url);
76 private UriTypeConverter converter;
77 private UriTypeDescriptorContext context;
82 converter = new UriTypeConverter ();
83 context = new UriTypeDescriptorContext ();
87 [ExpectedException (typeof (ArgumentNullException))]
88 public void CanConvertFrom_Null ()
90 converter.CanConvertFrom (null);
94 public void CanConvertFrom ()
96 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "string");
97 Assert.IsTrue (converter.CanConvertFrom (typeof (Uri)), "Uri");
98 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "InstanceDescriptor");
100 Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "object");
101 Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "int");
102 Assert.IsFalse (converter.CanConvertFrom (typeof (char)), "char");
103 Assert.IsFalse (converter.CanConvertFrom (typeof (DateTime)), "datetime");
107 [ExpectedException (typeof (ArgumentNullException))]
108 public void CanConvertFrom_Null_Null ()
110 converter.CanConvertFrom (null, null);
114 public void CanConvertFrom_Null_Type ()
116 Assert.IsTrue (converter.CanConvertFrom (null, typeof (string)), "string");
117 Assert.IsTrue (converter.CanConvertFrom (null, typeof (Uri)), "Uri");
118 Assert.IsTrue (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
120 Assert.IsFalse (converter.CanConvertFrom (null, typeof (object)), "object");
121 Assert.IsFalse (converter.CanConvertFrom (null, typeof (int)), "int");
122 Assert.IsFalse (converter.CanConvertFrom (null, typeof (char)), "char");
123 Assert.IsFalse (converter.CanConvertFrom (null, typeof (DateTime)), "datetime");
127 public void CanConvertFrom_TypeDescriptorContext_Type ()
129 Assert.IsTrue (converter.CanConvertFrom (context, typeof (string)), "string");
130 Assert.IsTrue (converter.CanConvertFrom (context, typeof (Uri)), "Uri");
131 Assert.IsTrue (converter.CanConvertFrom (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
133 Assert.IsFalse (converter.CanConvertFrom (context, typeof (object)), "object");
134 Assert.IsFalse (converter.CanConvertFrom (context, typeof (int)), "int");
135 Assert.IsFalse (converter.CanConvertFrom (context, typeof (char)), "char");
136 Assert.IsFalse (converter.CanConvertFrom (context, typeof (DateTime)), "datetime");
140 public void CanConvertTo ()
142 Assert.IsFalse (converter.CanConvertTo (null), "null");
144 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "string");
145 Assert.IsTrue (converter.CanConvertTo (typeof (Uri)), "Uri");
146 Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "InstanceDescriptor");
148 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "object");
149 Assert.IsFalse (converter.CanConvertTo (typeof (int)), "int");
150 Assert.IsFalse (converter.CanConvertTo (typeof (char)), "char");
151 Assert.IsFalse (converter.CanConvertTo (typeof (DateTime)), "datetime");
155 public void CanConvertTo_Null_Type ()
157 Assert.IsFalse (converter.CanConvertTo (null, null), "null");
159 Assert.IsTrue (converter.CanConvertTo (null, typeof (string)), "string");
160 Assert.IsTrue (converter.CanConvertTo (null, typeof (Uri)), "Uri");
161 Assert.IsTrue (converter.CanConvertTo (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
163 Assert.IsFalse (converter.CanConvertTo (null, typeof (object)), "object");
164 Assert.IsFalse (converter.CanConvertTo (null, typeof (int)), "int");
165 Assert.IsFalse (converter.CanConvertTo (null, typeof (char)), "char");
166 Assert.IsFalse (converter.CanConvertTo (null, typeof (DateTime)), "datetime");
170 public void CanConvertTo_TypeDescriptorContext_Type ()
172 Assert.IsTrue (converter.CanConvertTo (context, typeof (string)), "string");
173 Assert.IsTrue (converter.CanConvertTo (context, typeof (Uri)), "Uri");
174 Assert.IsTrue (converter.CanConvertTo (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
176 Assert.IsFalse (converter.CanConvertTo (context, typeof (object)), "object");
177 Assert.IsFalse (converter.CanConvertTo (context, typeof (int)), "int");
178 Assert.IsFalse (converter.CanConvertTo (context, typeof (char)), "char");
179 Assert.IsFalse (converter.CanConvertTo (context, typeof (DateTime)), "datetime");
183 public void ConvertFrom ()
185 object o = converter.ConvertFrom (url);
186 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
188 o = converter.ConvertFrom (uri);
189 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
191 o = converter.ConvertFrom (converter.ConvertTo (uri, typeof (InstanceDescriptor)));
192 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
196 [ExpectedException (typeof (NotSupportedException))]
197 public void ConvertFrom_Bad ()
199 converter.ConvertFrom (new object ());
203 public void ConvertFrom_TypeDescriptorContext_Type ()
205 object o = converter.ConvertFrom (context, CultureInfo.CurrentCulture, url);
206 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
208 o = converter.ConvertFrom (context, CultureInfo.InvariantCulture, uri);
209 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
211 o = converter.ConvertFrom (context, null, converter.ConvertTo (uri, typeof (InstanceDescriptor)));
212 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
216 [ExpectedException (typeof (NotSupportedException))]
217 public void ConvertFrom_TypeDescriptorContext_Bad ()
219 converter.ConvertFrom (context, CultureInfo.InvariantCulture, new object ());
223 public void ConvertTo ()
225 object o = converter.ConvertTo (url, typeof (string));
226 Assert.AreEqual (url, (o as String), "string-string");
228 o = converter.ConvertTo (uri, typeof (string));
229 Assert.AreEqual (url, (o as String), "uri-string");
231 o = converter.ConvertTo (uri, typeof (Uri));
232 Assert.AreEqual (uri, (o as Uri), "uri-uri");
234 o = converter.ConvertTo (uri, typeof (InstanceDescriptor));
235 Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
237 InstanceDescriptor descriptor = (o as InstanceDescriptor);
239 o = converter.ConvertTo (descriptor, typeof (string));
240 Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
242 // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
246 [ExpectedException (typeof (NotSupportedException))]
247 public void ConvertTo_String_to_Uri ()
249 converter.ConvertTo (url, typeof (Uri));
253 [ExpectedException (typeof (NotSupportedException))]
254 public void ConvertTo_String_to_InstanceDescriptor ()
256 converter.ConvertTo (url, typeof (InstanceDescriptor));
260 public void ConvertTo_Bad ()
262 Assert.AreEqual ("System.Object", converter.ConvertTo (new object (), typeof (string)), "object");
263 Assert.AreEqual ("4", converter.ConvertTo (4, typeof (string)), "int");
264 Assert.AreEqual ("c", converter.ConvertTo ('c', typeof (string)), "char");
265 Assert.AreEqual (String.Empty, converter.ConvertTo (null, typeof (string)), "null");
269 public void ConvertTo_TypeDescriptorContext ()
271 object o = converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (string));
272 Assert.AreEqual (url, (o as String), "string-string");
274 o = converter.ConvertTo (context, null, uri, typeof (string));
275 Assert.AreEqual (url, (o as String), "uri-string");
277 o = converter.ConvertTo (context, CultureInfo.InvariantCulture, uri, typeof (Uri));
278 Assert.AreEqual (uri, (o as Uri), "uri-uri");
280 o = converter.ConvertTo (context, CultureInfo.CurrentCulture, uri, typeof (InstanceDescriptor));
281 Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
283 InstanceDescriptor descriptor = (o as InstanceDescriptor);
285 o = converter.ConvertTo (context, CultureInfo.InvariantCulture, descriptor, typeof (string));
286 Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
288 // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
292 [ExpectedException (typeof (NotSupportedException))]
293 public void ConvertTo_TypeDescriptorContext_String_to_Uri ()
295 converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (Uri));
299 [ExpectedException (typeof (NotSupportedException))]
300 public void ConvertTo_TypeDescriptorContext_String_to_InstanceDescriptor ()
302 converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (InstanceDescriptor));
306 public void ConvertTo_TypeDescriptorContext_Bad ()
308 Assert.AreEqual ("System.Object", converter.ConvertTo (context, null, new object (), typeof (string)), "object");
309 Assert.AreEqual ("4", converter.ConvertTo (context, CultureInfo.CurrentCulture, 4, typeof (string)), "int");
310 Assert.AreEqual ("c", converter.ConvertTo (context, CultureInfo.InvariantCulture, 'c', typeof (string)), "char");
311 Assert.AreEqual (String.Empty, converter.ConvertTo (null, null, null, typeof (string)), "null");
315 public void IsValid ()
317 Assert.IsFalse (converter.IsValid (null), "null");
319 // LAMESPEC: all strings are accepted
320 Assert.IsTrue (converter.IsValid (String.Empty), "empty");
321 Assert.IsTrue (converter.IsValid ("\\"), "\\");
322 Assert.IsTrue (converter.IsValid ("#%#%#"), "#%#%#");
323 Assert.IsTrue (converter.IsValid (".."), "..");
324 Assert.IsTrue (converter.IsValid (url), url);
326 Assert.IsTrue (converter.IsValid (uri), "uri");
328 Assert.IsFalse (converter.IsValid (new object ()), "object");
329 Assert.IsFalse (converter.IsValid (4), "int");
330 Assert.IsFalse (converter.IsValid ('c'), "char");
331 Assert.IsFalse (converter.IsValid (DateTime.Now), "datetime");