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;
25 using System.Collections.Generic;
26 using System.ComponentModel;
29 using System.Reflection;
30 using System.Windows.Markup;
32 using System.Xaml.Schema;
33 using NUnit.Framework;
34 using MonoTests.System.Xaml;
36 using Category = NUnit.Framework.CategoryAttribute;
38 namespace MonoTests.System.Windows.Markup
41 public class ValueSerializerTest
43 static ValueSerializerTest ()
45 std_types = new List<XamlType> (XamlLanguage.AllTypes);
46 std_types.Sort ((t1, t2) => String.CompareOrdinal (t1.Name, t2.Name));
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"};
54 public void SerializerInAllTypes ()
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);
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));
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)};
71 public void GetSerializerForAllTypes ()
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);
82 Assert.Fail ("Missing serializer for " + t.Name);
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))
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);
96 // The funny thing also applies to CanConvertToString() and ConvertToString().
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);
108 public void GetSerializerFor ()
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.
128 public void DefaultImplementation ()
130 var v = new MyValueSerializer ();
133 foreach (var val in test_values) {
134 Assert.IsFalse (v.CanConvertToString (val, null), "CanConvertTo." + val);
136 v.ConvertToString (val, null);
137 Assert.Fail ("ConvertTo." + val);
138 } catch (NotSupportedException) {
142 // The funny thing also applies to CanConvertToString() and ConvertToString().
145 foreach (var str in test_strings) {
146 Assert.IsFalse (v.CanConvertFromString (str, null), "CanConvertFrom." + str);
148 v.ConvertFromString (str, null);
149 Assert.Fail ("ConvertFrom." + str);
150 } catch (NotSupportedException) {
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");
159 class MyValueSerializer : ValueSerializer
161 public Exception CallGetConvertFromException (object value)
163 return GetConvertFromException (value);
166 public Exception CallGetConvertToException (object value, Type destinationType)
168 return GetConvertToException (value, destinationType);