* TypeTest.cs: Added IsDefined tests for bug #82431. Added tests for
authorGert Driesen <drieseng@users.sourceforge.net>
Wed, 15 Aug 2007 08:04:42 +0000 (08:04 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Wed, 15 Aug 2007 08:04:42 +0000 (08:04 -0000)
GetCustomAttributes.
* MonoCustomAttrs.cs: In IsDefined, only walk inheritance chain if
both AttributeUsage.Inherited and inherit are true. Fixed bug #82431.

svn path=/trunk/mcs/; revision=84139

mcs/class/corlib/System/ChangeLog
mcs/class/corlib/System/MonoCustomAttrs.cs
mcs/class/corlib/Test/System/ChangeLog
mcs/class/corlib/Test/System/TypeTest.cs

index ae1474fab2391401ad1a6151414ffd3e8b6c3254..22c97aa9d6d08e494888b6716bf1c08e5b7e548d 100644 (file)
@@ -1,3 +1,8 @@
+2007-08-15  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * MonoCustomAttrs.cs: In IsDefined, only walk inheritance chain if
+       both AttributeUsage.Inherited and inherit are true. Fixed bug #82431.
+
 2007-08-13  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * ArgumentException.cs: Ignore zero-length ParamName for Message.
index a28627c056b47342f5f689b7fff28b3500857a1c..b58429309fed83888089da5925a2d8cce1787759 100644 (file)
@@ -273,8 +273,11 @@ namespace System
                                                return true;
                        }
 
+                       AttributeUsageAttribute usage = RetrieveAttributeUsage (
+                               attributeType);
+
                        ICustomAttributeProvider btype;
-                       if (inherit && ((btype = GetBase (obj)) != null))
+                       if (usage.Inherited && inherit && ((btype = GetBase (obj)) != null))
                                return IsDefined (btype, attributeType, inherit);
 
                        return false;
index c614650234ebb8107b30ac8fb5211459107550b5..31b5e2d89b85fbadf63bc3a4ef7724aa808e328c 100644 (file)
@@ -1,3 +1,8 @@
+2007-08-15  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * TypeTest.cs: Added IsDefined tests for bug #82431. Added tests for
+       GetCustomAttributes.
+
 2007-08-13  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * ArgumentExceptionTest.cs: Added test for ctor (string, string).
index c2c808f7d7d3158b1b2702034af6205631802c7f..116792b003f71131f10dfbc7fd7c7ba7394528ab 100644 (file)
@@ -589,12 +589,116 @@ PublicKeyToken=b77a5c561934e089"));
                        Assert.IsNull (typeof (TakesInt).GetConstructor (new Type[1] { typeof (object) }));
                }
 
-               // bug #76150
                [Test]
+               public void GetCustomAttributes_All ()
+               {
+                       object [] attrs = typeof (A).GetCustomAttributes (false);
+                       Assert.AreEqual (2, attrs.Length, "#A1");
+                       Assert.AreEqual (typeof (FooAttribute), attrs [0].GetType (), "#A2");
+                       Assert.AreEqual (typeof (VolatileModifier), attrs [1].GetType (), "#A3");
+
+                       attrs = typeof (BA).GetCustomAttributes (false);
+                       Assert.AreEqual (1, attrs.Length, "#B1");
+                       Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#B2");
+
+                       attrs = typeof (BA).GetCustomAttributes (true);
+                       Assert.AreEqual (2, attrs.Length, "#C1");
+                       Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#C2");
+                       Assert.AreEqual (typeof (VolatileModifier), attrs [1].GetType (), "#C3");
+
+                       attrs = typeof (CA).GetCustomAttributes (false);
+                       Assert.AreEqual (0, attrs.Length, "#D");
+
+                       attrs = typeof (CA).GetCustomAttributes (true);
+                       Assert.AreEqual (1, attrs.Length, "#E1");
+                       Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#E2");
+               }
+
+               [Test]
+               public void GetCustomAttributes_Type ()
+               {
+                       object [] attrs = typeof (A).GetCustomAttributes (
+                               typeof (VolatileModifier), false);
+                       Assert.AreEqual (1, attrs.Length, "#A1");
+                       Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#A2");
+
+                       attrs = typeof (A).GetCustomAttributes (
+                               typeof (NemerleAttribute), false);
+                       Assert.AreEqual (1, attrs.Length, "#B1");
+                       Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#B2");
+
+                       attrs = typeof (A).GetCustomAttributes (
+                               typeof (FooAttribute), false);
+                       Assert.AreEqual (1, attrs.Length, "#C1");
+                       Assert.AreEqual (typeof (FooAttribute), attrs [0].GetType (), "#C2");
+
+                       attrs = typeof (BA).GetCustomAttributes (
+                               typeof (VolatileModifier), false);
+                       Assert.AreEqual (0, attrs.Length, "#D");
+
+                       attrs = typeof (BA).GetCustomAttributes (
+                               typeof (VolatileModifier), true);
+                       Assert.AreEqual (1, attrs.Length, "#E1");
+                       Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#E2");
+
+                       attrs = typeof (BA).GetCustomAttributes (
+                               typeof (NemerleAttribute), false);
+                       Assert.AreEqual (0, attrs.Length, "#F");
+
+                       attrs = typeof (BA).GetCustomAttributes (
+                               typeof (NemerleAttribute), true);
+                       Assert.AreEqual (1, attrs.Length, "#G1");
+                       Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#G2");
+
+                       attrs = typeof (BA).GetCustomAttributes (
+                               typeof (FooAttribute), false);
+                       Assert.AreEqual (1, attrs.Length, "#H1");
+                       Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#H2");
+
+                       attrs = typeof (BA).GetCustomAttributes (
+                               typeof (FooAttribute), true);
+                       Assert.AreEqual (1, attrs.Length, "#I1");
+                       Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#I2");
+
+                       attrs = typeof (A).GetCustomAttributes (
+                               typeof (string), false);
+                       Assert.AreEqual (0, attrs.Length, "#J");
+               }
+
+               [Test] // bug #76150 and #82431
                public void IsDefined ()
                {
-                       Assert.IsTrue (typeof (A).IsDefined (typeof (NemerleAttribute), false), "#1");
-                       Assert.IsTrue (typeof (A).IsDefined (typeof (VolatileModifier), false), "#2");
+                       Assert.IsTrue (typeof (A).IsDefined (typeof (NemerleAttribute), false), "#A1");
+                       Assert.IsTrue (typeof (A).IsDefined (typeof (VolatileModifier), false), "#A2");
+                       Assert.IsTrue (typeof (A).IsDefined (typeof (FooAttribute), false), "#A3");
+                       Assert.IsFalse (typeof (A).IsDefined (typeof (BarAttribute), false), "#A4");
+
+                       Assert.IsFalse (typeof (BA).IsDefined (typeof (NemerleAttribute), false), "#B1");
+                       Assert.IsFalse (typeof (BA).IsDefined (typeof (VolatileModifier), false), "#B2");
+                       Assert.IsTrue (typeof (BA).IsDefined (typeof (FooAttribute), false), "#B3");
+                       Assert.IsTrue (typeof (BA).IsDefined (typeof (BarAttribute), false), "#B4");
+                       Assert.IsTrue (typeof (BA).IsDefined (typeof (NemerleAttribute), true), "#B5");
+                       Assert.IsTrue (typeof (BA).IsDefined (typeof (VolatileModifier), true), "#B6");
+                       Assert.IsTrue (typeof (BA).IsDefined (typeof (FooAttribute), true), "#B7");
+                       Assert.IsTrue (typeof (BA).IsDefined (typeof (BarAttribute), true), "#B8");
+
+                       Assert.IsFalse (typeof (CA).IsDefined (typeof (NemerleAttribute), false), "#C1");
+                       Assert.IsFalse (typeof (CA).IsDefined (typeof (VolatileModifier), false), "#C2");
+                       Assert.IsFalse (typeof (CA).IsDefined (typeof (FooAttribute), false), "#C3");
+                       Assert.IsFalse (typeof (CA).IsDefined (typeof (BarAttribute), false), "#C4");
+                       Assert.IsTrue (typeof (CA).IsDefined (typeof (NemerleAttribute), true), "#C5");
+                       Assert.IsTrue (typeof (CA).IsDefined (typeof (VolatileModifier), true), "#C6");
+                       Assert.IsFalse (typeof (CA).IsDefined (typeof (FooAttribute), true), "#C7");
+                       Assert.IsFalse (typeof (CA).IsDefined (typeof (BarAttribute), true), "#C8");
+
+                       Assert.IsFalse (typeof (BBA).IsDefined (typeof (NemerleAttribute), false), "#D1");
+                       Assert.IsFalse (typeof (BBA).IsDefined (typeof (VolatileModifier), false), "#D2");
+                       Assert.IsFalse (typeof (BBA).IsDefined (typeof (FooAttribute), false), "#D3");
+                       Assert.IsFalse (typeof (BBA).IsDefined (typeof (BarAttribute), false), "#D4");
+                       Assert.IsTrue (typeof (BBA).IsDefined (typeof (NemerleAttribute), true), "#D5");
+                       Assert.IsTrue (typeof (BBA).IsDefined (typeof (VolatileModifier), true), "#D6");
+                       Assert.IsFalse (typeof (BBA).IsDefined (typeof (FooAttribute), true), "#D7");
+                       Assert.IsFalse (typeof (BBA).IsDefined (typeof (BarAttribute), true), "#D8");
                }
 
                [Test]
@@ -666,7 +770,7 @@ PublicKeyToken=b77a5c561934e089"));
                        name.Name = "enumtest";
                        AssemblyBuilder assembly = 
                                AppDomain.CurrentDomain.DefineDynamicAssembly (
-                                                                                                                          name, access);
+                                       name, access);
 
                        ModuleBuilder module = assembly.DefineDynamicModule 
                                ("m", "enumtest.dll");
@@ -867,8 +971,31 @@ PublicKeyToken=b77a5c561934e089"));
                { }
 
                [VolatileModifier]
+               [FooAttribute]
                class A { }
 
+               [AttributeUsage (AttributeTargets.Class, Inherited=false)]
+               public class FooAttribute : Attribute
+               {
+               }
+
+               public class BarAttribute : FooAttribute
+               {
+               }
+
+               [BarAttribute]
+               class BA : A
+               {
+               }
+
+               class BBA : BA
+               {
+               }
+
+               class CA : A
+               {
+               }
+
                struct FooStruct {
                }