Merge branch 'xml-fixes' of https://github.com/myeisha/mono into myeisha-xml-fixes
[mono.git] / mcs / class / System.Xaml / Test / System.Windows.Markup / ValueSerializerTest.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;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.IO;
28 using System.Linq;
29 using System.Reflection;
30 using System.Windows.Markup;
31 using System.Xaml;
32 using System.Xaml.Schema;
33 using NUnit.Framework;
34 using MonoTests.System.Xaml;
35
36 using Category = NUnit.Framework.CategoryAttribute;
37
38 namespace MonoTests.System.Windows.Markup
39 {
40         [TestFixture]
41         public class ValueSerializerTest
42         {
43                 static ValueSerializerTest ()
44                 {
45                         std_types = new List<XamlType> (XamlLanguage.AllTypes);
46                         std_types.Sort ((t1, t2) => String.CompareOrdinal (t1.Name, t2.Name));
47                 }
48
49                 static readonly List<XamlType> std_types;
50                 object [] test_values = {null, true, "test", 3, 'x', 5.5, -1.414m, (byte) 255, new Uri ("urn:foo"), new NullExtension (), new object (), new PropertyDefinition (), new Reference ("Foo"), new StaticExtension (), TimeSpan.FromMinutes (5), new TypeExtension ("TypeExt"), new XData () { Text = "test xdata"} }; // can we instantiate MemberDefinition?
51                 string [] test_strings = {String.Empty, "True", "test", "3", "x", "5.5", "-1.414", "255", "urn:foo", "System.Windows.Markup.NullExtension", "System.Object", "System.Windows.Markup.PropertyDefinition", "System.Windows.Markup.Reference", "System.Windows.Markup.StaticExtension", "00:05:00", "System.Windows.Markup.TypeExtension", "System.Windows.Markup.XData"};
52
53                 [Test]
54                 public void SerializerInAllTypes ()
55                 {
56                         var sctx = new XamlSchemaContext (new Assembly [] { typeof (XamlType).Assembly });
57                         foreach (var t in std_types) {
58                                 if (t != XamlLanguage.String) {
59                                         Assert.IsNull (t.ValueSerializer, "IsNull? " + t.Name);
60                                         continue;
61                                 }
62                                 var v = t.ValueSerializer.ConverterInstance;
63                                 foreach (var val in test_values)
64                                         Assert.IsTrue (v.CanConvertToString (val, null), t.Name + "_" + (val != null ? val.GetType () : null));
65                         }
66                 }
67
68                 static readonly Type [] no_ser_types = {typeof (object), typeof (ArrayExtension), typeof (MemberDefinition), typeof (NullExtension), typeof (PropertyDefinition), typeof (Reference), typeof (StaticExtension), typeof (TypeExtension), typeof (XData)};
69
70                 [Test]
71                 public void GetSerializerForAllTypes ()
72                 {
73                         // Serializers from GetSerializerFor() returns very 
74                         // different results from predefined ValueSerializer.
75                         foreach (var t in std_types) {
76                                 var v = ValueSerializer.GetSerializerFor (t.UnderlyingType, null);
77                                 if (no_ser_types.Any (ti => ti == t.UnderlyingType)) {
78                                         Assert.IsNull (v, "NoSerializer_" + t.Name);
79                                         continue;
80                                 }
81                                 else if (v == null)
82                                         Assert.Fail ("Missing serializer for " + t.Name);
83
84                                 // String ValueSerializer is the only exceptional one that mostly fails ConvertToString().
85                                 // For remaining types, ConvertToString() should succeed.
86                                 // What is funny or annoying here is, that always return true for CanConvertToString() while everything fails at ConvertToString() on .NET.
87                                 if (t.UnderlyingType == typeof (string))
88                                         continue;
89
90                                 int i = 0;
91                                 foreach (var val in test_values) {
92                                         Assert.IsTrue (v.CanConvertToString (val, null), t.Name + "_" + (val != null ? val.GetType () : null));
93                                         Assert.AreEqual (test_strings [i++], v.ConvertToString (val, null), "value-" + t.Name + "_" + val);
94                                 }
95
96                                 // The funny thing also applies to CanConvertToString() and ConvertToString().
97
98                                 i = 0;
99                                 foreach (var str in test_strings) {
100                                         Assert.IsTrue (v.CanConvertFromString (str, null), t.Name + "_" + str);
101                                         // FIXME: add tests for this large matrix someday.
102                                         //Assert.AreEqual (test_values [i++], v.ConvertFromString (str, null), "value-" + t.Name + "_" + str);
103                                 }
104                         }
105                 }
106
107                 [Test]
108                 public void GetSerializerFor ()
109                 {
110                         Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (Array)), "#1");
111                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (Uri)), "#2");
112                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (Type)), "#3"); // has no TypeConverter (undocumented behavior)
113                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (string)), "#4"); // documented as special
114                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (DateTime)), "#5"); // documented as special
115                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (bool)), "#6"); // has no TypeConverter (undocumented behavior)
116                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (byte)), "#7"); // has no TypeConverter (undocumented behavior)
117                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (char)), "#8"); // has no TypeConverter (undocumented behavior)
118                         Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (DBNull)), "#9"); // TypeCode.DBNull
119                         Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (object)), "#10");
120                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (TimeSpan)), "#11"); // has no TypeConverter (undocumented behavior), TypeCode.Object -> unexpectedly has non-null serializer!
121                         Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (DateTimeOffset)), "#12"); // has no TypeConverter (undocumented behavior), TypeCode.Object -> expected
122                         Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (MyExtension)), "#13");
123                         Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (MyExtension4)), "#14"); // has TypeConverter.
124                         Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (XamlType)), "#15"); // While there is XamlTypeTypeConverter, it is not used on XamlType.
125                 }
126
127                 [Test]
128                 public void DefaultImplementation ()
129                 {
130                         var v = new MyValueSerializer ();
131
132                         int i = 0;
133                         foreach (var val in test_values) {
134                                 Assert.IsFalse (v.CanConvertToString (val, null), "CanConvertTo." + val);
135                                 try {
136                                         v.ConvertToString (val, null);
137                                         Assert.Fail ("ConvertTo." + val);
138                                 } catch (NotSupportedException) {
139                                 }
140                         }
141
142                         // The funny thing also applies to CanConvertToString() and ConvertToString().
143
144                         i = 0;
145                         foreach (var str in test_strings) {
146                                 Assert.IsFalse (v.CanConvertFromString (str, null), "CanConvertFrom." + str);
147                                 try {
148                                         v.ConvertFromString (str, null);
149                                         Assert.Fail ("ConvertFrom." + str);
150                                 } catch (NotSupportedException) {
151                                 }
152                         }
153                         
154                         Assert.AreEqual (typeof (NotSupportedException), v.CallGetConvertFromException (null).GetType (), "#1");
155                         Assert.AreEqual (typeof (NotSupportedException), v.CallGetConvertToException (null, typeof (int)).GetType (), "#2");
156                         Assert.IsFalse (v.TypeReferences (null, null).GetEnumerator ().MoveNext (), "#3");
157                 }
158
159                 class MyValueSerializer : ValueSerializer
160                 {
161                         public Exception CallGetConvertFromException (object value)
162                         {
163                                 return GetConvertFromException (value);
164                         }
165
166                         public Exception CallGetConvertToException (object value, Type destinationType)
167                         {
168                                 return GetConvertToException (value, destinationType);
169                         }
170                 }
171         }
172 }