Merge branch 'xml-fixes' of https://github.com/myeisha/mono into myeisha-xml-fixes
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml.Schema / XamlTypeTypeConverterTest.cs
1 //
2 // Copyright (C) 2010 Novell Inc. http://novell.com
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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.
22 //
23 using System;
24 using System.Collections.Generic;
25 using System.ComponentModel;
26 using System.Globalization;
27 using System.Reflection;
28 using System.Text;
29 using System.Windows.Markup;
30 using System.Xaml;
31 using System.Xaml.Schema;
32 using System.Xml;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Xaml.Schema
36 {
37         [TestFixture]
38         public class XamlTypeTypeConverterTest
39         {
40                 XamlTypeTypeConverter c = new XamlTypeTypeConverter ();
41                 XamlSchemaContext sctx = new XamlSchemaContext (null, null);
42
43                 [Test]
44                 public void CanConvertFrom ()
45                 {
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");
51                 }
52
53                 [Test]
54                 public void CanConvertTo ()
55                 {
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");
61                 }
62
63                 // ConvertFrom() is not supported in either way.
64
65                 [Test]
66                 [ExpectedException (typeof (NotSupportedException))]
67                 public void ConvertFrom ()
68                 {
69                         c.ConvertFrom (null, null, XamlLanguage.String);
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (NotSupportedException))]
74                 public void ConvertFrom2 ()
75                 {
76                         c.ConvertFrom (null, null, "System.Int32");
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (NotSupportedException))]
81                 public void ConvertXamlTypeToXamlType ()
82                 {
83                         Assert.AreEqual ("", c.ConvertTo (null, null, XamlLanguage.String, typeof (XamlType)), "#1");
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (NotSupportedException))]
88                 public void ConvertXamlTypeToXamlType2 ()
89                 {
90                         Assert.AreEqual ("", c.ConvertTo (new DummyValueSerializerContext (), null, XamlLanguage.String, typeof (XamlType)), "#1");
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (NotSupportedException))]
95                 public void ConvertXamlTypeToType ()
96                 {
97                         c.ConvertTo (null, null, XamlLanguage.String, typeof (Type));
98                 }
99
100                 [Test]
101                 public void ConvertXamlTypeToString ()
102                 {
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");
107                 }
108
109                 [Test]
110                 public void ConvertStringToString ()
111                 {
112                         Assert.AreEqual ("foo", c.ConvertTo (null, CultureInfo.InvariantCulture, "foo", typeof (string)), "#1");
113                 }
114
115                 [Test]
116                 public void ConvertIntToString ()
117                 {
118                         Assert.AreEqual ("5", c.ConvertTo (null, null, 5, typeof (string)), "#1");
119                 }
120
121                 [Test]
122                 [ExpectedException (typeof (NotSupportedException))]
123                 public void ConvertStringToXamlType ()
124                 {
125                         Assert.AreEqual ("", c.ConvertTo (new DummyValueSerializerContext (), null, "System.String", typeof (XamlType)), "#1");
126                 }
127         }
128 }