Merge pull request #273 from joncham/bug-getpid
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml / XamlTypeTest.cs
old mode 100644 (file)
new mode 100755 (executable)
index 60ba8b3..923d6d4
@@ -144,6 +144,7 @@ namespace MonoTests.System.Xaml
                        Assert.IsFalse (t.IsArray, "#5");
                        Assert.IsFalse (t.IsGeneric, "#6");
                        Assert.IsTrue (t.IsPublic, "#7");
+                       Assert.AreEqual (0, t.GetAllMembers ().Count, "#8");
                }
 
                [Test]
@@ -200,6 +201,8 @@ namespace MonoTests.System.Xaml
                        var t2 = new XamlType (t1.PreferredXamlNamespace, t1.Name, null, sctx);
                        // not sure if it always returns false for different .ctor comparisons...
                        Assert.IsFalse (t1 == t2, "#3");
+                       
+                       Assert.AreNotEqual (XamlLanguage.Type, new XamlSchemaContext ().GetXamlType (typeof (Type)), "#4");
                }
 
                [Test]
@@ -209,17 +212,34 @@ namespace MonoTests.System.Xaml
                        Assert.IsFalse (t.IsArray, "#1.1");
                        Assert.IsFalse (t.IsCollection, "#1.2");
                        Assert.IsNull (t.ItemType, "#1.3");
+
                        t = new XamlType (typeof (ArrayList), sctx);
                        Assert.IsFalse (t.IsArray, "#2.1");
                        Assert.IsTrue (t.IsCollection, "#2.2");
                        Assert.IsNotNull (t.ItemType, "#2.3");
                        Assert.AreEqual ("Object", t.ItemType.Name, "#2.4");
+
                        t = new XamlType (typeof (int []), sctx);
                        Assert.IsTrue (t.IsArray, "#3.1");
-                       // why?
                        Assert.IsFalse (t.IsCollection, "#3.2");
                        Assert.IsNotNull (t.ItemType, "#3.3");
                        Assert.AreEqual (typeof (int), t.ItemType.UnderlyingType, "#3.4");
+
+                       t = new XamlType (typeof (IList), sctx);
+                       Assert.IsFalse (t.IsArray, "#4.1");
+                       Assert.IsTrue (t.IsCollection, "#4.2");
+                       Assert.IsNotNull (t.ItemType, "#4.3");
+                       Assert.AreEqual (typeof (object), t.ItemType.UnderlyingType, "#4.4");
+
+                       t = new XamlType (typeof (ICollection), sctx); // it is not a XAML collection.
+                       Assert.IsFalse (t.IsArray, "#5.1");
+                       Assert.IsFalse (t.IsCollection, "#5.2");
+                       Assert.IsNull (t.ItemType, "#5.3");
+
+                       t = new XamlType (typeof (ArrayExtension), sctx);
+                       Assert.IsFalse (t.IsArray, "#6.1");
+                       Assert.IsFalse (t.IsCollection, "#6.2");
+                       Assert.IsNull (t.ItemType, "#6.3");
                }
 
                [Test]
@@ -233,12 +253,23 @@ namespace MonoTests.System.Xaml
                        Assert.IsTrue (t.IsDictionary, "#2.1");
                        Assert.IsFalse (t.IsCollection, "#2.1-2");
                        Assert.IsNotNull (t.KeyType, "#2.2");
+                       Assert.IsNotNull (t.ItemType, "#2.2-2");
                        Assert.AreEqual ("Object", t.KeyType.Name, "#2.3");
+                       Assert.AreEqual ("Object", t.ItemType.Name, "#2.3-2");
                        t = new XamlType (typeof (Dictionary<int,string>), sctx);
                        Assert.IsTrue (t.IsDictionary, "#3.1");
                        Assert.IsFalse (t.IsCollection, "#3.1-2");
                        Assert.IsNotNull (t.KeyType, "#3.2");
+                       Assert.IsNotNull (t.ItemType, "#3.2-2");
                        Assert.AreEqual ("Int32", t.KeyType.Name, "#3.3");
+                       Assert.AreEqual ("String", t.ItemType.Name, "#3.3-2");
+
+                       var ml = t.GetAllMembers ();
+                       Assert.AreEqual (2, ml.Count, "#3.4");
+                       Assert.IsTrue (ml.Any (mi => mi.Name == "Keys"), "#3.4-2");
+                       Assert.IsTrue (ml.Any (mi => mi.Name == "Values"), "#3.4-3");
+                       Assert.IsNotNull (t.GetMember ("Keys"), "#3.4-4");
+                       Assert.IsNotNull (t.GetMember ("Values"), "#3.4-5");
                }
 
                public class TestClass1
@@ -344,8 +375,7 @@ namespace MonoTests.System.Xaml
                        Assert.IsFalse (t.IsAmbient, "#22");
                        Assert.IsNull (t.AllowedContentTypes, "#23");
                        Assert.IsNull (t.ContentWrappers, "#24");
-                       // FIXME: enable this when we fixed TypeConverter for Type.
-                       //Assert.IsNotNull (t.TypeConverter, "#25"); // TypeTypeConverter
+                       Assert.IsNotNull (t.TypeConverter, "#25"); // TypeTypeConverter
                        Assert.IsNull (t.ValueSerializer, "#26");
                        Assert.IsNull (t.ContentProperty, "#27");
                        //Assert.IsNull (t.DeferringLoader, "#28");
@@ -569,10 +599,19 @@ namespace MonoTests.System.Xaml
                public void GetXamlNamespaces ()
                {
                        var xt = new XamlType (typeof (string), new XamlSchemaContext (null, null));
-                       var l = xt.GetXamlNamespaces ();
-                       Assert.AreEqual (2, l.Count, "#1");
-                       Assert.AreEqual (XamlLanguage.Xaml2006Namespace, l [0], "#2");
-                       Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", l [1], "#3");
+                       var l = xt.GetXamlNamespaces ().ToList ();
+                       l.Sort ();
+                       Assert.AreEqual (2, l.Count, "#1-1");
+                       Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", l [0], "#1-2");
+                       Assert.AreEqual (XamlLanguage.Xaml2006Namespace, l [1], "#1-3");
+
+                       xt = new XamlType (typeof (TypeExtension), new XamlSchemaContext (null, null));
+                       l = xt.GetXamlNamespaces ().ToList ();
+                       l.Sort ();
+                       Assert.AreEqual (3, l.Count, "#2-1");
+                       Assert.AreEqual ("clr-namespace:System.Windows.Markup;assembly=System.Xaml", l [0], "#2-2");
+                       Assert.AreEqual (XamlLanguage.Xaml2006Namespace, l [1], "#2-3");
+                       Assert.AreEqual (XamlLanguage.Xaml2006Namespace, l [2], "#2-4"); // ??
                }
                
                [Test]
@@ -588,6 +627,9 @@ namespace MonoTests.System.Xaml
                        Assert.IsNotNull (xm, "#3");
                        xm = xt.GetAliasedProperty (XamlLanguage.Lang);
                        Assert.IsNotNull (xm, "#4");
+                       
+                       xt = new XamlType (typeof (Dictionary<int,string>), xt.SchemaContext);
+                       Assert.IsNull (xt.GetAliasedProperty (XamlLanguage.Key), "#5");
                }
 
                [Test]
@@ -641,28 +683,217 @@ namespace MonoTests.System.Xaml
                        // wow, so it returns some meaningless method parameters.
                        Assert.IsNotNull (new XamlType (typeof (MyXamlType), sctx).GetPositionalParameters (3), "#1");
                }
-       }
+               
+               [Test]
+               public void ListMembers ()
+               {
+                       var xt = new XamlType (typeof (List<int>), sctx);
+                       var ml = xt.GetAllMembers ().ToArray ();
+                       Assert.AreEqual (1, ml.Length, "#1");
+                       Assert.IsNotNull (xt.GetMember ("Capacity"), "#2");
+               }
+               
+               [Test]
+               public void ComplexPositionalParameters ()
+               {
+                       var xt = new XamlType (typeof (ComplexPositionalParameterWrapper), sctx);
+               }
+               
+               [Test]
+               public void CustomArrayExtension ()
+               {
+                       var xt = new XamlType (typeof (MyArrayExtension), sctx);
+                       var xm = xt.GetMember ("Items");
+                       Assert.IsNotNull (xt.GetAllMembers ().FirstOrDefault (m => m.Name == "Items"), "#0");
+                       Assert.IsNotNull (xm, "#1");
+                       Assert.IsFalse (xm.IsReadOnly, "#2"); // Surprisingly it is False. Looks like XAML ReadOnly is true only if it lacks set accessor. Having private member does not make it ReadOnly.
+                       Assert.IsTrue (xm.Type.IsCollection, "#3");
+                       Assert.IsFalse (xm.Type.IsConstructible, "#4");
+               }
+               
+               [Test]
+               public void ContentIncluded ()
+               {
+                       var xt = new XamlType (typeof (ContentIncludedClass), sctx);
+                       var xm = xt.GetMember ("Content");
+                       Assert.AreEqual (xm, xt.ContentProperty, "#1");
+                       Assert.IsTrue (xt.GetAllMembers ().Contains (xm), "#2");
+               }
+               
+               [Test]
+               public void NamedItem ()
+               {
+                       var xt = new XamlType (typeof (NamedItem), sctx);
+                       var e = xt.GetAllMembers ().GetEnumerator ();
+                       Assert.IsTrue (e.MoveNext (), "#1");
+                       Assert.AreEqual (xt.GetMember ("ItemName"), e.Current, "#2");
+                       Assert.IsTrue (e.MoveNext (), "#3");
+                       Assert.AreEqual (xt.GetMember ("References"), e.Current, "#4");
+                       Assert.IsFalse (e.MoveNext (), "#5");
+               }
 
-       class MyXamlType : XamlType
-       {
-               public MyXamlType (string fullName, IList<XamlType> typeArguments, XamlSchemaContext context)
-                       : base (fullName, typeArguments, context)
+               [Test]
+               public void CanAssignTo ()
                {
+                       foreach (var xt1 in XamlLanguage.AllTypes)
+                               foreach (var xt2 in XamlLanguage.AllTypes)
+                                       Assert.AreEqual (xt1.UnderlyingType.IsAssignableFrom (xt2.UnderlyingType), xt2.CanAssignTo (xt1), "{0} to {1}", xt1, xt2);
+                       Assert.IsTrue (XamlLanguage.Type.CanAssignTo (XamlLanguage.Object), "x#1"); // specific test
+                       Assert.IsFalse (new MyXamlType ("MyFooBar", null, sctx).CanAssignTo (XamlLanguage.String), "x#2"); // custom type to string -> false
+                       Assert.IsTrue (new MyXamlType ("MyFooBar", null, sctx).CanAssignTo (XamlLanguage.Object), "x#3"); // custom type to object -> true!
                }
-       }
 
-       public class ArgumentAttributed
-       {
-               public ArgumentAttributed (string s1, string s2)
+               [Test]
+               public void IsXData ()
+               {
+                       Assert.IsFalse (XamlLanguage.XData.IsXData, "#1"); // yes, it is false.
+                       Assert.IsTrue (sctx.GetXamlType (typeof (XmlSerializable)).IsXData, "#2");
+               }
+               
+               [Test]
+               public void XDataMembers ()
                {
-                       Arg1 = s1;
-                       Arg2 = s2;
+                       var xt = sctx.GetXamlType (typeof (XmlSerializableWrapper));
+                       Assert.IsNotNull (xt.GetMember ("Value"), "#1"); // it is read-only, so if wouldn't be retrieved if it were not XData.
+
+                       Assert.IsNotNull (XamlLanguage.XData.GetMember ("XmlReader"), "#2"); // it is returned, but ignored by XamlObjectReader.
+                       Assert.IsNotNull (XamlLanguage.XData.GetMember ("Text"), "#3");
                }
 
-               [ConstructorArgument ("s1")]
-               public string Arg1 { get; set; }
+               [Test]
+               public void AttachableProperty ()
+               {
+                       var xt = new XamlType (typeof (Attachable), sctx);
+                       var apl = xt.GetAllAttachableMembers ();
+                       Assert.IsTrue (apl.Any (ap => ap.Name == "Foo"), "#1");
+                       Assert.IsTrue (apl.Any (ap => ap.Name == "Protected"), "#2");
+                       // oh? SetBaz() has non-void return value, but it seems ignored.
+                       Assert.IsTrue (apl.Any (ap => ap.Name == "Baz"), "#3");
+                       Assert.AreEqual (4, apl.Count, "#4");
+                       Assert.IsTrue (apl.All (ap => ap.IsAttachable), "#5");
+                       var x = apl.First (ap => ap.Name == "X");
+                       Assert.IsTrue (x.IsEvent, "#6");
+               }
 
-               [ConstructorArgument ("s2")]
-               public string Arg2 { get; set; }
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void AttachablePropertySetValueNullObject ()
+               {
+                       var xt = new XamlType (typeof (Attachable), sctx);
+                       var apl = xt.GetAllAttachableMembers ();
+                       var foo = apl.First (ap => ap.Name == "Foo");
+                       Assert.IsTrue (foo.IsAttachable, "#7");
+                       foo.Invoker.SetValue (null, "xxx");
+               }
+
+               [Test]
+               public void AttachablePropertySetValueSuccess ()
+               {
+                       var xt = new XamlType (typeof (Attachable), sctx);
+                       var apl = xt.GetAllAttachableMembers ();
+                       var foo = apl.First (ap => ap.Name == "Foo");
+                       Assert.IsTrue (foo.IsAttachable, "#7");
+                       var obj = new object ();
+                       foo.Invoker.SetValue (obj, "xxx"); // obj is non-null, so valid.
+                       // FIXME: this line should be unnecessary.
+                       AttachablePropertyServices.RemoveProperty (obj, new AttachableMemberIdentifier (foo.Type.UnderlyingType, foo.Name));
+               }
+
+               [Test]
+               public void ReadOnlyPropertyContainer ()
+               {
+                       var xt = new XamlType (typeof (ReadOnlyPropertyContainer), sctx);
+                       var xm = xt.GetMember ("Bar");
+                       Assert.IsNotNull (xm, "#1");
+                       Assert.IsFalse (xm.IsWritePublic, "#2");
+               }
+
+               [Test]
+               public void UnknownType ()
+               {
+                       var xt = new XamlType ("urn:foo", "MyUnknown", null, sctx);
+                       Assert.IsTrue (xt.IsUnknown, "#1");
+                       Assert.IsNotNull (xt.BaseType, "#2");
+                       Assert.IsFalse (xt.BaseType.IsUnknown, "#3");
+                       Assert.AreEqual (typeof (object), xt.BaseType.UnderlyingType, "#4");
+               }
+
+               [Test] // wrt bug #680385
+               public void DerivedListMembers ()
+               {
+                       var xt = sctx.GetXamlType (typeof (XamlTest.Configurations));
+                       Assert.IsTrue (xt.GetAllMembers ().Any (xm => xm.Name == "Active"), "#1"); // make sure that the member name is Active, not Configurations.Active ...
+               }
+
+               [Test]
+               public void EnumType ()
+               {
+                       var xt = sctx.GetXamlType (typeof (EnumValueType));
+                       Assert.IsTrue (xt.IsConstructible, "#1");
+                       Assert.IsFalse (xt.IsNullable, "#2");
+                       Assert.IsFalse (xt.IsUnknown, "#3");
+                       Assert.IsFalse (xt.IsUsableDuringInitialization, "#4");
+                       Assert.IsNotNull (xt.TypeConverter, "#5");
+               }
+
+               [Test]
+               public void CollectionContentProperty ()
+               {
+                       var xt = sctx.GetXamlType (typeof (CollectionContentProperty));
+                       var p = xt.ContentProperty;
+                       Assert.IsNotNull (p, "#1");
+                       Assert.AreEqual ("ListOfItems", p.Name, "#2");
+               }
+
+               [Test]
+               public void AmbientPropertyContainer ()
+               {
+                       var xt = sctx.GetXamlType (typeof (SecondTest.ResourcesDict));
+                       Assert.IsTrue (xt.IsAmbient, "#1");
+                       var l = xt.GetAllMembers ().ToArray ();
+                       Assert.AreEqual (2, l.Length, "#2");
+                       // FIXME: enable when string representation difference become compatible
+                       // Assert.AreEqual ("System.Collections.Generic.Dictionary(System.Object, System.Object).Keys", l [0].ToString (), "#3");
+                       // Assert.AreEqual ("System.Collections.Generic.Dictionary(System.Object, System.Object).Values", l [1].ToString (), "#4");
+               }
+
+               [Test]
+               public void NullableContainer ()
+               {
+                       var xt = sctx.GetXamlType (typeof (NullableContainer));
+                       Assert.IsFalse (xt.IsGeneric, "#1");
+                       Assert.IsTrue (xt.IsNullable, "#2");
+                       var xm = xt.GetMember ("TestProp");
+                       Assert.IsTrue (xm.Type.IsGeneric, "#3");
+                       Assert.IsTrue (xm.Type.IsNullable, "#4");
+                       Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", xm.Type.PreferredXamlNamespace, "#5");
+                       Assert.AreEqual (1, xm.Type.TypeArguments.Count, "#6");
+                       Assert.AreEqual (XamlLanguage.Int32, xm.Type.TypeArguments [0], "#7");
+                       Assert.IsNotNull (xm.Type.TypeConverter, "#8");
+                       Assert.IsNotNull (xm.Type.TypeConverter.ConverterInstance, "#9");
+
+                       var obj = new NullableContainer ();
+                       xm.Invoker.SetValue (obj, 5);
+                       xm.Invoker.SetValue (obj, null);
+               }
+
+               [Test]
+               public void DerivedCollectionAndDictionary ()
+               {
+                       var xt = sctx.GetXamlType (typeof (IList<int>));
+                       Assert.IsTrue (xt.IsCollection, "#1");
+                       Assert.IsFalse (xt.IsDictionary, "#2");
+                       xt = sctx.GetXamlType (typeof (IDictionary<EnumValueType,int>));
+                       Assert.IsTrue (xt.IsDictionary, "#3");
+                       Assert.IsFalse (xt.IsCollection, "#4");
+               }
+       }
+
+       class MyXamlType : XamlType
+       {
+               public MyXamlType (string fullName, IList<XamlType> typeArguments, XamlSchemaContext context)
+                       : base (fullName, typeArguments, context)
+               {
+               }
        }
 }