X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem%2FAttributeTest.cs;h=160b9b98df29899dc9d770d6ade3b7142c759963;hb=438e3b5ac335f69a620d2db738626fca1d0c2980;hp=dddf0aff7cc5f1aa34f6b156e797d3c41cf696ba;hpb=9982c34eefb04f88fc27d6eb7adb340dd0951f33;p=mono.git diff --git a/mcs/class/corlib/Test/System/AttributeTest.cs b/mcs/class/corlib/Test/System/AttributeTest.cs index dddf0aff7cc..160b9b98df2 100644 --- a/mcs/class/corlib/Test/System/AttributeTest.cs +++ b/mcs/class/corlib/Test/System/AttributeTest.cs @@ -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"); @@ -815,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 () { @@ -862,7 +876,7 @@ namespace MonoTests.System Assert.Fail ("#1"); } catch (NotSupportedException) {} } - +#endif [Test] //Regression test for #499569 public void GetCattrOnPropertyAndInheritance () { @@ -991,6 +1005,33 @@ namespace MonoTests.System 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 { @@ -1028,6 +1069,12 @@ namespace MonoTests.System { } } + + class Multiple { + public void Bar ([Foo] [Bar] string multiple, [Bar] string bar) + { + } + } } [TestFixture] @@ -1119,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 (); + Assert.AreEqual (typeof(ParamNamespace.FooAttribute), foo.GetType ()); + var bar = parameter.GetCustomAttribute (); + Assert.AreEqual (typeof(ParamNamespace.BarAttribute), bar.GetType ()); + } + + [Test] + public void MultipleParameterAttributes2 () + { + var parameter = GetParameter (typeof(ParamNamespace.Multiple), "Bar", "bar"); + var foo = parameter.GetCustomAttribute (); + 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); + } } }