Merge pull request #1949 from lewurm/fixtype
[mono.git] / mcs / class / corlib / Test / System.Reflection / PropertyInfoTest.cs
index 14543b85007486c9825aaf1c001c5f899ebf6952..dd38e16b7e52e7588d05cdbb637b9ae47da88b5b 100644 (file)
@@ -31,7 +31,9 @@ using System;
 using System.Reflection;
 using System.Runtime.InteropServices;
 using System.Threading;
+#if !MONOTOUCH
 using System.Reflection.Emit;
+#endif
 using System.IO;
 
 using NUnit.Framework;
@@ -46,6 +48,7 @@ namespace MonoTests.System.Reflection
                {
                        Type type = typeof (TestClass);
                        PropertyInfo property = type.GetProperty ("ReadOnlyProperty");
+                       Assert.IsNotNull (property.Module, "#0");
 
                        MethodInfo [] methods = property.GetAccessors (true);
                        Assert.AreEqual (1, methods.Length, "#A1");
@@ -96,7 +99,6 @@ namespace MonoTests.System.Reflection
                        methods = property.GetAccessors ();
                        Assert.AreEqual (0, methods.Length, "#H");
 
-#if NET_2_0
                        property = typeof (TestClass).GetProperty ("PrivateSetter");
 
                        methods = property.GetAccessors (true);
@@ -115,7 +117,6 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (1, methods.Length, "#J1");
                        Assert.IsNotNull (methods [0], "#J2");
                        Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#J3");
-#endif
                }
 
                [Test]
@@ -354,7 +355,7 @@ namespace MonoTests.System.Reflection
                                get { return 99; }
                        }
                }
-
+#if !MONOTOUCH
                [Test]
                public void ConstantValue () {
                        /*This test looks scary because we can't generate a default value with C# */
@@ -399,8 +400,8 @@ namespace MonoTests.System.Reflection
                        } catch (InvalidOperationException) {
                        }
                }
+#endif
 
-#if NET_2_0
                public class A<T>
                {
                        public string Property {
@@ -436,8 +437,13 @@ namespace MonoTests.System.Reflection
                        PropertyInfo property = type.GetProperty ("Property");
                        Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
                }
-#endif
 
+               [Test]
+               public void ToStringTest ()
+               {
+                       var pa = typeof (TestC).GetProperty ("Item");
+                       Assert.AreEqual ("Int32 Item [Double[]]", pa.ToString ());
+               }
 
                static bool HasAttribute (object [] attrs, Type attributeType)
                {
@@ -473,12 +479,10 @@ namespace MonoTests.System.Reflection
                                set { }
                        }
 
-#if NET_2_0
                        public string PrivateSetter {
                                get { return null; }
                                private set { }
                        }
-#endif
                }
 
                [Test] // bug #633671
@@ -494,8 +498,20 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (typeof (ClassWithNullableDateTime), siblingProperty.DeclaringType, "#3");
                        Assert.AreEqual (typeof (InheritsFromClassWithNullableDateTime), siblingProperty.ReflectedType, "#4");
                }
-               
-       
+
+               class Super { public long A { get; private set; } }
+
+               class Sub : Super { }
+
+               [Test]
+               public void PrivateSetterFromDerivedType ()
+               {
+                       var prop = typeof (Sub).GetProperty ("A");
+                       Assert.AreEqual (1, prop.GetAccessors (true).Length, "#1");
+                       Assert.IsFalse (prop.CanWrite, "#2");
+                       Assert.IsNull (prop.GetSetMethod (true), "#3");
+               }
+
                public class ClassWithNullableDateTime
                {
                        public DateTime? Property1 { get; set; }
@@ -504,5 +520,40 @@ namespace MonoTests.System.Reflection
                public class InheritsFromClassWithNullableDateTime : ClassWithNullableDateTime
                {
                }
+
+               public static int ThrowingProperty {
+                       get {
+                               throw new ObjectDisposedException("TestClass");
+                       }
+               }
+
+               [Test]
+               public void GetException () {
+                       var prop = typeof(PropertyInfoTest).GetProperty("ThrowingProperty");
+                       try {
+                               prop.GetValue (null, null);
+                               Assert.Fail ();
+                       } catch (TargetInvocationException ex) {
+                               Assert.IsTrue (ex.InnerException is ObjectDisposedException);
+                       }
+               }
+
+               public class DefaultValueTest
+               {
+                       public string this[int val, string param = "test"]
+                       {
+                               get{ return val + param; }
+                       }
+               }
+
+
+               [Test]
+               public void PropertyWithDefaultValue ()
+               {
+                       var parameters = typeof (DefaultValueTest).GetProperty ("Item").GetIndexParameters ();
+                       var defaultParam = parameters[parameters.Length - 1];
+                       Assert.AreEqual ("param", defaultParam.Name, "#1");
+                       Assert.AreEqual ("test", defaultParam.DefaultValue, "#2");
+               }
        }
 }