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;
35 using System.ComponentModel;
36 using System.ComponentModel.Design.Serialization;
37 using System.Globalization;
39 namespace MonoTests.System {
41 class UriTypeDescriptorContext : ITypeDescriptorContext {
43 public IContainer Container {
44 get { throw new NotImplementedException (); }
47 public object Instance {
48 get { throw new NotImplementedException (); }
51 public void OnComponentChanged ()
53 throw new NotImplementedException ();
56 public bool OnComponentChanging ()
58 throw new NotImplementedException ();
61 public PropertyDescriptor PropertyDescriptor {
62 get { throw new NotImplementedException (); }
65 public object GetService (Type serviceType)
67 throw new NotImplementedException ();
72 public class UriTypeConverterTest {
74 private const string url = "http://www.mono-project.com/";
75 private static Uri uri = new Uri (url);
77 private UriTypeConverter converter;
78 private UriTypeDescriptorContext context;
80 protected bool isWin32 = false;
85 converter = new UriTypeConverter ();
86 context = new UriTypeDescriptorContext ();
87 isWin32 = (Path.DirectorySeparatorChar == '\\');
91 [ExpectedException (typeof (ArgumentNullException))]
92 public void CanConvertFrom_Null ()
94 converter.CanConvertFrom (null);
98 public void CanConvertFrom ()
100 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "string");
101 Assert.IsTrue (converter.CanConvertFrom (typeof (Uri)), "Uri");
103 Assert.IsFalse (converter.CanConvertFrom (typeof (InstanceDescriptor)), "InstanceDescriptor");
105 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "InstanceDescriptor");
107 Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "object");
108 Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "int");
109 Assert.IsFalse (converter.CanConvertFrom (typeof (char)), "char");
110 Assert.IsFalse (converter.CanConvertFrom (typeof (DateTime)), "datetime");
114 [ExpectedException (typeof (ArgumentNullException))]
115 public void CanConvertFrom_Null_Null ()
117 converter.CanConvertFrom (null, null);
121 public void CanConvertFrom_Null_Type ()
123 Assert.IsTrue (converter.CanConvertFrom (null, typeof (string)), "string");
124 Assert.IsTrue (converter.CanConvertFrom (null, typeof (Uri)), "Uri");
126 Assert.IsFalse (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
128 Assert.IsTrue (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
130 Assert.IsFalse (converter.CanConvertFrom (null, typeof (object)), "object");
131 Assert.IsFalse (converter.CanConvertFrom (null, typeof (int)), "int");
132 Assert.IsFalse (converter.CanConvertFrom (null, typeof (char)), "char");
133 Assert.IsFalse (converter.CanConvertFrom (null, typeof (DateTime)), "datetime");
137 public void CanConvertFrom_TypeDescriptorContext_Type ()
139 Assert.IsTrue (converter.CanConvertFrom (context, typeof (string)), "string");
140 Assert.IsTrue (converter.CanConvertFrom (context, typeof (Uri)), "Uri");
142 Assert.IsFalse (converter.CanConvertFrom (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
144 Assert.IsTrue (converter.CanConvertFrom (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
146 Assert.IsFalse (converter.CanConvertFrom (context, typeof (object)), "object");
147 Assert.IsFalse (converter.CanConvertFrom (context, typeof (int)), "int");
148 Assert.IsFalse (converter.CanConvertFrom (context, typeof (char)), "char");
149 Assert.IsFalse (converter.CanConvertFrom (context, typeof (DateTime)), "datetime");
153 public void CanConvertTo ()
155 Assert.IsFalse (converter.CanConvertTo (null), "null");
157 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "string");
158 Assert.IsTrue (converter.CanConvertTo (typeof (Uri)), "Uri");
160 Assert.IsFalse (converter.CanConvertTo (typeof (InstanceDescriptor)), "InstanceDescriptor");
162 Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "InstanceDescriptor");
164 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "object");
165 Assert.IsFalse (converter.CanConvertTo (typeof (int)), "int");
166 Assert.IsFalse (converter.CanConvertTo (typeof (char)), "char");
167 Assert.IsFalse (converter.CanConvertTo (typeof (DateTime)), "datetime");
171 public void CanConvertTo_Null_Type ()
173 Assert.IsFalse (converter.CanConvertTo (null, null), "null");
175 Assert.IsTrue (converter.CanConvertTo (null, typeof (string)), "string");
176 Assert.IsTrue (converter.CanConvertTo (null, typeof (Uri)), "Uri");
178 Assert.IsFalse (converter.CanConvertTo (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
180 Assert.IsTrue (converter.CanConvertTo (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
182 Assert.IsFalse (converter.CanConvertTo (null, typeof (object)), "object");
183 Assert.IsFalse (converter.CanConvertTo (null, typeof (int)), "int");
184 Assert.IsFalse (converter.CanConvertTo (null, typeof (char)), "char");
185 Assert.IsFalse (converter.CanConvertTo (null, typeof (DateTime)), "datetime");
189 public void CanConvertTo_TypeDescriptorContext_Type ()
191 Assert.IsTrue (converter.CanConvertTo (context, typeof (string)), "string");
192 Assert.IsTrue (converter.CanConvertTo (context, typeof (Uri)), "Uri");
194 Assert.IsFalse (converter.CanConvertTo (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
196 Assert.IsTrue (converter.CanConvertTo (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
198 Assert.IsFalse (converter.CanConvertTo (context, typeof (object)), "object");
199 Assert.IsFalse (converter.CanConvertTo (context, typeof (int)), "int");
200 Assert.IsFalse (converter.CanConvertTo (context, typeof (char)), "char");
201 Assert.IsFalse (converter.CanConvertTo (context, typeof (DateTime)), "datetime");
205 public void ConvertFrom ()
207 object o = converter.ConvertFrom (url);
208 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
210 o = converter.ConvertFrom (uri);
211 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
214 o = converter.ConvertFrom ("");
215 Assert.IsNull (o, "null");
217 o = converter.ConvertFrom (converter.ConvertTo (uri, typeof (InstanceDescriptor)));
218 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
220 o = converter.ConvertFrom ("");
221 Assert.IsNotNull (o, "not null");
226 public void ConvertFromString ()
228 object o = converter.ConvertFrom ("~/SomeUri.txt");
229 Assert.IsFalse ((o as Uri).IsAbsoluteUri, "CFS_01");
230 Assert.AreEqual ("~/SomeUri.txt", (o as Uri).ToString (), "CFS_02");
232 o = converter.ConvertFrom ("/SomeUri.txt");
234 Assert.IsFalse ((o as Uri).IsAbsoluteUri, "CFS_03_WIN");
235 Assert.AreEqual ("/SomeUri.txt", (o as Uri).ToString (), "CFS_04_WIN");
237 Assert.IsTrue ((o as Uri).IsAbsoluteUri, "CFS_03_UNIX");
238 Assert.AreEqual ("file:///SomeUri.txt", (o as Uri).ToString (), "CFS_04_UNIX");
243 [ExpectedException (typeof (NotSupportedException))]
244 public void ConvertFrom_Bad ()
246 converter.ConvertFrom (new object ());
250 public void ConvertFrom_TypeDescriptorContext_Type ()
252 object o = converter.ConvertFrom (context, CultureInfo.CurrentCulture, url);
253 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
255 o = converter.ConvertFrom (context, CultureInfo.InvariantCulture, uri);
256 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
259 o = converter.ConvertFrom (context, null, converter.ConvertTo (uri, typeof (InstanceDescriptor)));
260 Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
265 [ExpectedException (typeof (NotSupportedException))]
266 public void ConvertFrom_TypeDescriptorContext_Bad ()
268 converter.ConvertFrom (context, CultureInfo.InvariantCulture, new object ());
272 public void ConvertTo ()
275 o = converter.ConvertTo (uri, typeof (Uri));
276 Assert.AreEqual (uri, (o as Uri), "uri-uri");
278 o = converter.ConvertTo (uri, typeof (string));
279 Assert.AreEqual (url, (o as String), "uri-string");
283 converter.ConvertTo (url, typeof (string));
284 Assert.Fail ("string-string");
285 } catch (NotSupportedException) {
289 converter.ConvertTo (uri, typeof (InstanceDescriptor));
290 Assert.Fail ("uri-InstanceDescriptor");
291 } catch (NotSupportedException) {
294 o = converter.ConvertTo (url, typeof (string));
295 Assert.AreEqual (url, (o as String), "string-string");
297 o = converter.ConvertTo (uri, typeof (InstanceDescriptor));
298 Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
300 InstanceDescriptor descriptor = (o as InstanceDescriptor);
302 o = converter.ConvertTo (descriptor, typeof (string));
303 Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
305 // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
309 [ExpectedException (typeof (NotSupportedException))]
310 public void ConvertTo_String_to_Uri ()
312 converter.ConvertTo (url, typeof (Uri));
316 [ExpectedException (typeof (NotSupportedException))]
317 public void ConvertTo_String_to_InstanceDescriptor ()
319 converter.ConvertTo (url, typeof (InstanceDescriptor));
323 public void ConvertTo_Bad ()
327 converter.ConvertTo (new object (), typeof (string));
328 Assert.Fail ("object");
329 } catch (NotSupportedException) {
332 Assert.AreEqual ("System.Object", converter.ConvertTo (new object (), typeof (string)), "object");
333 Assert.AreEqual ("4", converter.ConvertTo (4, typeof (string)), "int");
334 Assert.AreEqual ("c", converter.ConvertTo ('c', typeof (string)), "char");
335 Assert.AreEqual (String.Empty, converter.ConvertTo (null, typeof (string)), "null");
340 public void ConvertTo_TypeDescriptorContext ()
344 o = converter.ConvertTo (context, null, uri, typeof (string));
345 Assert.AreEqual (url, (o as String), "uri-string");
347 o = converter.ConvertTo (context, CultureInfo.InvariantCulture, uri, typeof (Uri));
348 Assert.AreEqual (uri, (o as Uri), "uri-uri");
352 o = converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (string));
353 Assert.AreEqual (url, (o as String), "string-string");
354 } catch (NotSupportedException) {
357 o = converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (string));
358 Assert.AreEqual (url, (o as String), "string-string");
360 o = converter.ConvertTo (context, CultureInfo.CurrentCulture, uri, typeof (InstanceDescriptor));
361 Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
363 InstanceDescriptor descriptor = (o as InstanceDescriptor);
365 o = converter.ConvertTo (context, CultureInfo.InvariantCulture, descriptor, typeof (string));
366 Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
368 // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
372 [ExpectedException (typeof (NotSupportedException))]
373 public void ConvertTo_TypeDescriptorContext_String_to_Uri ()
375 converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (Uri));
379 [ExpectedException (typeof (NotSupportedException))]
380 public void ConvertTo_TypeDescriptorContext_String_to_InstanceDescriptor ()
382 converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (InstanceDescriptor));
386 public void ConvertTo_TypeDescriptorContext_Bad ()
390 converter.ConvertTo (context, null, new object (), typeof (string));
391 Assert.Fail ("object");
392 } catch (NotSupportedException) {
395 Assert.AreEqual ("System.Object", converter.ConvertTo (context, null, new object (), typeof (string)), "object");
396 Assert.AreEqual ("4", converter.ConvertTo (context, CultureInfo.CurrentCulture, 4, typeof (string)), "int");
397 Assert.AreEqual ("c", converter.ConvertTo (context, CultureInfo.InvariantCulture, 'c', typeof (string)), "char");
398 Assert.AreEqual (String.Empty, converter.ConvertTo (null, null, null, typeof (string)), "null");
403 public void IsValid ()
405 Assert.IsFalse (converter.IsValid (null), "null");
407 // LAMESPEC: all strings are accepted
408 Assert.IsTrue (converter.IsValid (String.Empty), "empty");
409 Assert.IsTrue (converter.IsValid ("\\"), "\\");
410 Assert.IsTrue (converter.IsValid ("#%#%#"), "#%#%#");
411 Assert.IsTrue (converter.IsValid (".."), "..");
412 Assert.IsTrue (converter.IsValid (url), url);
414 Assert.IsTrue (converter.IsValid (uri), "uri");
416 Assert.IsFalse (converter.IsValid (new object ()), "object");
417 Assert.IsFalse (converter.IsValid (4), "int");
418 Assert.IsFalse (converter.IsValid ('c'), "char");
419 Assert.IsFalse (converter.IsValid (DateTime.Now), "datetime");