Merge pull request #654 from alesliehughes/master
[mono.git] / mcs / class / corlib / Test / System / AttributeTest.cs
index da399cde42f64483132ebd22623cc19392dc2c03..f8fe8cd7a001f294a54e2f7bdd0b0aec37e917d0 100644 (file)
@@ -12,7 +12,9 @@
 
 using System;
 using System.Reflection;
+#if !MONOTOUCH
 using System.Reflection.Emit;
+#endif
 using System.Runtime.InteropServices;
 using System.Threading;
 
@@ -82,6 +84,10 @@ namespace MonoTests.System
                        {
                        }
                }
+
+               class MyDerivedClassNoAttribute : MyClass
+               {
+               }
        }
 
        [TestFixture]
@@ -100,6 +106,7 @@ namespace MonoTests.System
                        Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (YourCustomAttribute), false), "#8");
                        Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClass), typeof (UnusedAttribute), false), "#9");
                        Assert.IsTrue (Attribute.IsDefined (typeof (MyClass).GetMethod ("ParamsMethod").GetParameters () [0], typeof (ParamArrayAttribute), false), "#10");
+                       Assert.IsFalse (Attribute.IsDefined (typeof (MyDerivedClassNoAttribute), typeof (MyCustomAttribute)), "#11");
                }
 
                [Test]
@@ -134,17 +141,9 @@ namespace MonoTests.System
                public void IsDefined_PropertyInfo_Override ()
                {
                        PropertyInfo pi = typeof (TestSub).GetProperty ("PropBase3");
-#if NET_2_0
                        Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
-#else
-                       Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute)), "#B1");
-#endif
                        Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), false), "#B2");
-#if NET_2_0
                        Assert.IsTrue (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
-#else
-                       Assert.IsFalse (Attribute.IsDefined (pi, typeof (PropTestAttribute), true), "#B3");
-#endif
                        Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute)), "#B4");
                        Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), false), "#B5");
                        Assert.IsFalse (Attribute.IsDefined (pi, typeof (ComVisibleAttribute), true), "#B6");
@@ -726,7 +725,6 @@ namespace MonoTests.System
                        Assert.IsFalse (a.Equals (null), "#8");
                }
 
-#if NET_2_0
                class UserType : TypeDelegator {
                        public int GetCattr1;
                        public int GetCattr2;
@@ -816,6 +814,14 @@ namespace MonoTests.System
                        Assert.AreEqual (typeof (TestFixtureAttribute), type.lastAttrType, "#8");
                }
 
+               [Test]
+               public void IsDefinedForPseudoAttribute ()
+               {                       
+                       Assert.IsTrue (Attribute.IsDefined (typeof (object), typeof(SerializableAttribute), true), "#1");
+                       Assert.IsFalse (Attribute.IsDefined (typeof (AttributeTest), typeof(SerializableAttribute), true), "#2");
+               }
+
+#if !MONOTOUCH
                [Test]
                public void GetCustomAttributeOnNewSreTypes ()
                {
@@ -864,7 +870,43 @@ namespace MonoTests.System
                        } catch (NotSupportedException) {}
                }
 #endif
+               [Test] //Regression test for #499569
+               public void GetCattrOnPropertyAndInheritance ()
+               {
+                       var m = typeof(Sub).GetProperty ("Name");
+                       var res = Attribute.GetCustomAttributes (m, typeof(MyAttribute), true);
+                       Assert.AreEqual (1, res.Length, "#1");
+               }
 
+               abstract class Abs
+               {
+                       public abstract string Name { get; set; }
+               }
+               
+               class Base: Abs
+               {
+                       [MyAttribute]
+                       public override string Name {
+                               get { return ""; }
+                               set {}
+                       }
+               }
+               
+               class Sub: Base
+               {
+                       public override string Name {
+                               get { return ""; }
+                               set {}
+                       }
+               }
+               
+               class MySubAttribute: MyAttribute
+               {
+               }
+               
+               class MyAttribute: Attribute
+               {
+               }
 
                private int GetAttributeCount (object[] attributes, Type attributeType)
                {
@@ -919,6 +961,43 @@ namespace MonoTests.System
                private class ClassC : ClassB
                {
                }
+
+               [Test]
+               public void EmptyNonOverridenGetHashCode ()
+               {
+                       MyAttribute a1 = new MyAttribute ();
+                       MyAttribute a2 = new MyAttribute ();
+                       Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical argument-less");
+                       Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "Empty/TypeId");
+
+                       MySubAttribute b1 = new MySubAttribute ();
+                       Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
+                       Assert.AreEqual (b1.GetHashCode (), b1.TypeId.GetHashCode (), "Empty/TypeId/Sub");
+               }
+
+               class MyOwnCustomAttribute : MyCustomAttribute {
+
+                       public MyOwnCustomAttribute (string s)
+                               : base (s)
+                       {
+                       }
+               }
+
+               [Test]
+               public void NonEmptyNonOverridenGetHashCode ()
+               {
+                       MyCustomAttribute a1 = new MyCustomAttribute (null);
+                       MyCustomAttribute a2 = new MyCustomAttribute (null);
+                       Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical arguments");
+                       Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "TypeId");
+
+                       MyCustomAttribute a3 = new MyCustomAttribute ("a");
+                       MyCustomAttribute a4 = new MyCustomAttribute ("b");
+                       Assert.AreNotEqual (a3.GetHashCode (), a4.GetHashCode (), "non-identical-arguments");
+
+                       MyOwnCustomAttribute b1 = new MyOwnCustomAttribute (null);
+                       Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
+               }
        }
 
        namespace ParamNamespace {
@@ -1047,5 +1126,48 @@ namespace MonoTests.System
                        Assert.AreEqual (1, attributes.Length);
                        Assert.AreEqual ("Derived.baz", attributes [0].Data);
                }
+
+               [AttributeUsage(AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Class)]
+               public class MyCAttr : Attribute {}
+
+               class Base {
+                       [MyCAttr]
+                       public override string ToString () { return null; }
+               }
+
+               class Derived : Base {
+                       public override string ToString () { return null; }
+               }
+
+               [Test] //one ton of bugs
+               public void GetCustomAttributesOnMethodOverride ()
+               {
+                       var m = typeof (Derived).GetMethod ("ToString");
+                       var attrs = Attribute.GetCustomAttributes (m, true);
+                       Assert.AreEqual (1, attrs.Length);      
+               }
+
+               class EvtBase
+               {
+                       public virtual event EventHandler Event {add {} remove {}}
+               }
+
+               class EvtOverride : EvtBase
+               {
+                       [MyCAttr]       
+                       public override event EventHandler Event {add {} remove {}}
+               }
+
+               class EvtChild : EvtOverride
+               {
+                       public override event EventHandler Event {add {} remove {}}
+               }
+
+               [Test] //Regression test for #662867
+               public void GetCustomAttributesOnEventOverride ()
+               {
+                       var attrs = Attribute.GetCustomAttributes (typeof(EvtChild).GetEvent ("Event"), true);
+                       Assert.AreEqual (1, attrs.Length);
+               }
        }
 }