Add another couple of MarkupExtension related tests.
authorAtsushi Eno <atsushi@ximian.com>
Thu, 28 Oct 2010 12:06:01 +0000 (21:06 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Thu, 28 Oct 2010 12:06:01 +0000 (21:06 +0900)
mcs/class/System.Xaml/Test/System.Windows.Markup/ArrayExtensionTest.cs
mcs/class/System.Xaml/Test/System.Xaml/XamlObjectReaderTest.cs
mcs/class/System.Xaml/Test/System.Xaml/XamlTypeTest.cs

index 02ab144cd5602ff81c569acb65d03a23c8f08222..45ed983d73209321bac262ebd6037771765f939d 100644 (file)
@@ -60,6 +60,15 @@ namespace MonoTests.System.Windows.Markup
                        x.AddChild ("test");
                }
 
+               [Test]
+               public void AddChild2 ()
+               {
+                       var x = new ArrayExtension (new int [0]);
+                       x.AddChild (new object ());
+                       x.AddChild (5);
+                       x.AddChild ("test");
+               }
+
                [Test]
                public void AddInconsistent ()
                {
index 5f5cb30b961e66ad0b527aa759ff9fab3735addd..121787e7495e670ffc093f19ca924a6b0a667164 100644 (file)
@@ -275,7 +275,8 @@ namespace MonoTests.System.Xaml
                        Assert.IsTrue (r.Read (), "#36");
                        Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "#37");
                        // static Foo is not included in GetAllXembers() return value.
-                       // nonpublic Baz does not appear either.
+                       // ReadOnly is not included in GetAllMembers() return value neither.
+                       // nonpublic Baz is a member, but does not appear in the reader.
 
                        Assert.IsTrue (r.Read (), "#51");
                        Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#52");
@@ -686,9 +687,28 @@ namespace MonoTests.System.Xaml
                        var r = new XamlObjectReader (obj);
                        Read_ArrayOrArrayExtension (r, obj);
                }
+               
+               [Test]
+               public void Read_MyArrayExtension ()
+               {
+                       var obj = new MyArrayExtension (new int [] {5, -3, 0});
+                       var r = new XamlObjectReader (obj);
+                       Read_ArrayOrArrayExtensionOrMyArrayExtension (r, obj, typeof (MyArrayExtension));
+               }
 
                void Read_ArrayOrArrayExtension (XamlObjectReader r, object instance)
                {
+                       Read_ArrayOrArrayExtensionOrMyArrayExtension (r, instance, typeof (ArrayExtension));
+               }
+
+               void Read_ArrayOrArrayExtensionOrMyArrayExtension (XamlObjectReader r, object instance, Type extType)
+               {
+                       if (extType == typeof (MyArrayExtension)) {
+                               Assert.IsTrue (r.Read (), "#1");
+                               Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#2");
+                               Assert.IsNotNull (r.Namespace, "#3");
+                               Assert.AreEqual (String.Empty, r.Namespace.Prefix, "#3-2");
+                       }
                        Assert.IsTrue (r.Read (), "#11");
                        Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#12");
                        Assert.IsNotNull (r.Namespace, "#13");
@@ -697,7 +717,7 @@ namespace MonoTests.System.Xaml
 
                        Assert.IsTrue (r.Read (), "#21");
                        Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#22");
-                       var xt = new XamlType (typeof (ArrayExtension), r.SchemaContext);
+                       var xt = new XamlType (extType, r.SchemaContext);
                        Assert.AreEqual (xt, r.Type, "#23");
                        Assert.AreEqual (instance, r.Instance, "#26"); // different between Array and ArrayExtension. Also, different from Type and TypeExtension (Type returns TypeExtension, while Array remains to return Array)
 
@@ -1383,6 +1403,9 @@ namespace MonoTests.System.Xaml
                public static string Foo { get; set; }
                public string Bar { get; set; }
                public string Baz { internal get; set; }
+               public string ReadOnly {
+                       get { return Foo; }
+               }
        }
 
        public class MyExtension : MarkupExtension
index b49aa71f15ec9dd0442a17f999ec003ef0186f83..e4fe4700fcfefc676f4cac912f94396b3ee49a86 100644 (file)
@@ -676,6 +676,12 @@ namespace MonoTests.System.Xaml
                {
                        var xt = new XamlType (typeof (ComplexPositionalParameterWrapper), sctx);
                }
+               
+               [Test]
+               public void CustomArrayExtension ()
+               {
+                       var xt = new XamlType (typeof (MyArrayExtension), sctx);
+               }
        }
 
        class MyXamlType : XamlType
@@ -730,4 +736,41 @@ namespace MonoTests.System.Xaml
        {
                public string Foo { get; set; }
        }
+       
+       //[MarkupExtensionReturnType (typeof (Array))]
+       //[ContentProperty ("Items")]  ... so, these attributes do not affect XamlObjectReader.
+       public class MyArrayExtension : MarkupExtension
+       {
+               public MyArrayExtension ()
+               {
+                       Items = new ArrayList ();
+               }
+
+               public MyArrayExtension (Array array)
+               {
+                       this.Items = array;
+                       this.Type = array.GetType ().GetElementType ();
+               }
+               
+               public MyArrayExtension (Type type)
+                       : this ()
+               {
+                       this.Type = type;
+               }
+               
+               [ConstructorArgument ("type")]
+               public Type Type { get; set; }
+
+               public IList Items { get; private set; }
+               
+               public override object ProvideValue (IServiceProvider serviceProvider)
+               {
+                       if (Type == null)
+                               throw new InvalidOperationException ("Type property must be set before calling ProvideValue method");
+
+                       Array a = Array.CreateInstance (Type, Items.Count);
+                       Items.CopyTo (a, 0);
+                       return a;
+               }
+       }
 }