2 // Copyright (C) 2010 Novell Inc. http://novell.com
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 using System.Collections.Generic;
25 using System.ComponentModel;
26 using System.Globalization;
27 using System.Reflection;
29 using System.Windows.Markup;
31 using System.Xaml.Schema;
33 using NUnit.Framework;
35 namespace MonoTests.System.Xaml.Schema
38 public class XamlTypeTypeConverterTest
40 XamlTypeTypeConverter c = new XamlTypeTypeConverter ();
41 XamlSchemaContext sctx = new XamlSchemaContext (null, null);
44 public void CanConvertFrom ()
46 Assert.IsFalse (c.CanConvertFrom (null, typeof (XamlType)), "#1");
47 Assert.IsTrue (c.CanConvertFrom (null, typeof (string)), "#2");
48 Assert.IsFalse (c.CanConvertFrom (null, typeof (int)), "#3");
49 Assert.IsFalse (c.CanConvertFrom (null, typeof (object)), "#4");
50 Assert.IsFalse (c.CanConvertFrom (new DummyValueSerializerContext (), typeof (XamlType)), "#5");
54 public void CanConvertTo ()
56 Assert.IsFalse (c.CanConvertTo (null, typeof (XamlType)), "#1");
57 Assert.IsTrue (c.CanConvertTo (null, typeof (string)), "#2");
58 Assert.IsFalse (c.CanConvertTo (null, typeof (int)), "#3");
59 Assert.IsFalse (c.CanConvertTo (null, typeof (object)), "#4");
60 Assert.IsFalse (c.CanConvertTo (new DummyValueSerializerContext (), typeof (XamlType)), "#5");
63 // ConvertFrom() is not supported in either way.
66 [ExpectedException (typeof (NotSupportedException))]
67 public void ConvertFrom ()
69 c.ConvertFrom (null, null, XamlLanguage.String);
73 [ExpectedException (typeof (NotSupportedException))]
74 public void ConvertFrom2 ()
76 c.ConvertFrom (null, null, "System.Int32");
80 [ExpectedException (typeof (NotSupportedException))]
81 public void ConvertXamlTypeToXamlType ()
83 Assert.AreEqual ("", c.ConvertTo (null, null, XamlLanguage.String, typeof (XamlType)), "#1");
87 [ExpectedException (typeof (NotSupportedException))]
88 public void ConvertXamlTypeToXamlType2 ()
90 Assert.AreEqual ("", c.ConvertTo (new DummyValueSerializerContext (), null, XamlLanguage.String, typeof (XamlType)), "#1");
94 [ExpectedException (typeof (NotSupportedException))]
95 public void ConvertXamlTypeToType ()
97 c.ConvertTo (null, null, XamlLanguage.String, typeof (Type));
101 public void ConvertXamlTypeToString ()
103 // ... so, it does not seem to just call XamlType.ToString(), but rather first try to use UnderlyingType if possible.
104 Assert.AreEqual ("System.String", c.ConvertTo (null, null, XamlLanguage.String, typeof (string)), "#1"); // huh?
105 Assert.AreEqual ("System.Windows.Markup.TypeExtension", c.ConvertTo (null, null, XamlLanguage.Type, typeof (string)), "#1"); // huh?
106 Assert.AreEqual ("{urn:foo}Foo", c.ConvertTo (null, null, new XamlType ("urn:foo", "Foo", null, sctx), typeof (string)), "#2");
110 public void ConvertStringToString ()
112 Assert.AreEqual ("foo", c.ConvertTo (null, CultureInfo.InvariantCulture, "foo", typeof (string)), "#1");
116 public void ConvertIntToString ()
118 Assert.AreEqual ("5", c.ConvertTo (null, null, 5, typeof (string)), "#1");
122 [ExpectedException (typeof (NotSupportedException))]
123 public void ConvertStringToXamlType ()
125 Assert.AreEqual ("", c.ConvertTo (new DummyValueSerializerContext (), null, "System.String", typeof (XamlType)), "#1");