Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[mono.git] / mcs / class / corlib / Test / System / AttributeTest.cs
index da399cde42f64483132ebd22623cc19392dc2c03..160b9b98df29899dc9d770d6ade3b7142c759963 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,17 @@ namespace MonoTests.System
                        {
                        }
                }
+
+               class MyDerivedClassNoAttribute : MyClass
+               {
+               }
+
+               internal class AttributeWithTypeId : Attribute
+               {
+                       public override object TypeId {
+                               get { return this; }
+                       }
+               }
        }
 
        [TestFixture]
@@ -100,6 +113,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 +148,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 +732,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 +821,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 +877,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 +968,70 @@ 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");
+               }
+
+               [Test]
+               public void GetHashCodeWithOverriddenTypeId ()
+               {
+                       //check for not throwing stack overflow exception
+                       AttributeWithTypeId a = new AttributeWithTypeId ();
+                       a.GetHashCode ();
+               }
+
+               class ArrayAttribute : Attribute
+               {
+#pragma warning disable 414
+                       int[] array;
+#pragma warning restore
+
+                       public ArrayAttribute (int[] array)
+                       {
+                               this.array = array;
+                       }
+               }
+
+               [Test]
+               public void ArrayFieldsEquality ()
+               {
+                       Assert.IsTrue (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 2 })));
+                       Assert.IsFalse (new ArrayAttribute (new int[] { 1, 2 }).Equals (new ArrayAttribute (new int[] { 1, 1 })));
+               }
        }
 
        namespace ParamNamespace {
@@ -956,6 +1069,12 @@ namespace MonoTests.System
                        {
                        }
                }
+
+               class Multiple {
+                       public void Bar ([Foo] [Bar] string multiple, [Bar] string bar)
+                       {
+                       }
+               }
        }
 
        [TestFixture]
@@ -1047,5 +1166,66 @@ namespace MonoTests.System
                        Assert.AreEqual (1, attributes.Length);
                        Assert.AreEqual ("Derived.baz", attributes [0].Data);
                }
+
+               [Test]
+               public void MultipleParameterAttributes ()
+               {
+                       var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "multiple");
+                       var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
+                       Assert.AreEqual (typeof(ParamNamespace.FooAttribute), foo.GetType ());
+                       var bar = parameter.GetCustomAttribute<ParamNamespace.BarAttribute> ();
+                       Assert.AreEqual (typeof(ParamNamespace.BarAttribute), bar.GetType ());
+               }
+
+               [Test]
+               public void MultipleParameterAttributes2 ()
+               {
+                       var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "bar");
+                       var foo = parameter.GetCustomAttribute<ParamNamespace.FooAttribute> ();
+                       Assert.IsNull (foo);
+               }
+
+               [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);
+               }
        }
 }