* MethodBuilderTest.cs: Split up GetCustomAttributes test and marked
authorGert Driesen <drieseng@users.sourceforge.net>
Mon, 21 May 2007 18:54:02 +0000 (18:54 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Mon, 21 May 2007 18:54:02 +0000 (18:54 -0000)
the one for a baked type as NotWorking. Added test for
SetCustomAttribute with SuppressUnmanagedCodeSecurity attribute.
* TypeBuilderTest.cs: Split up HasElementType test and marked the
baked variant as NotWorking on the 1.0 profile. Enabled additional
test for bug #81640. Added test for SetCustomAttribute with
SuppressUnmanagedCodeSecurity attribute.
* TypeBuilder.cs: Changed HasElementTypeImpl to return false on 2.0
profile when type is not baked.

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

mcs/class/corlib/System.Reflection.Emit/ChangeLog
mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs
mcs/class/corlib/Test/System.Reflection.Emit/ChangeLog
mcs/class/corlib/Test/System.Reflection.Emit/MethodBuilderTest.cs
mcs/class/corlib/Test/System.Reflection.Emit/TypeBuilderTest.cs

index 1c6efff5e833c1cc4c968e914658be09c3349f7c..eb5dfdb8ad8a39f3a27414d1f711b593adb2a41b 100644 (file)
@@ -1,3 +1,8 @@
+2007-05-21  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * TypeBuilder.cs: Changed HasElementTypeImpl to return false on 2.0
+       profile when type is not baked.
+
 2007-05-21  Jb Evain  <jb@nurv.fr>
 
        * TypeBuilder.cs
index 325c2fcd114fb219c306e2363e787c419fc88221..c385605b22924a8ce021e02769ff5f10193b5dd7 100644 (file)
@@ -1144,8 +1144,15 @@ namespace System.Reflection.Emit {
                        throw not_supported ();
                }
 
-               protected override bool HasElementTypeImpl () {
+               protected override bool HasElementTypeImpl ()
+               {
+#if NET_2_0
+                       // a TypeBuilder can never represent an array, pointer
+                       if (!is_created)
+                               return false;
+#else
                        check_created ();
+#endif
                        return created.HasElementType;
                }
 
index 02cbc08aadcf97e531715e3eaa95a5e4ab9d75a4..450efc12b4850fff9144b1a2b5842c2bc86455b8 100644 (file)
@@ -1,3 +1,13 @@
+2007-05-21  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * MethodBuilderTest.cs: Split up GetCustomAttributes test and marked
+       the one for a baked type as NotWorking. Added test for 
+       SetCustomAttribute with SuppressUnmanagedCodeSecurity attribute.
+       * TypeBuilderTest.cs: Split up HasElementType test and marked the
+       baked variant as NotWorking on the 1.0 profile. Enabled additional
+       test for bug #81640. Added test for SetCustomAttribute with
+       SuppressUnmanagedCodeSecurity attribute.
+
 2007-05-16  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * TypeBuilderTest.cs: Added GetFields test for bug #81368. Enabled
index 124f7a3ffd097f445c2c9cb6c71f532cab713ad3..145014aaa8a400c40c7aeffee99dc940ccbd3cc7 100644 (file)
@@ -450,7 +450,7 @@ namespace MonoTests.System.Reflection.Emit
                }
 
                [Test]
-               public void TestGetCustomAttributes ()
+               public void TestGetCustomAttributes_Incomplete ()
                {
                        MethodBuilder mb = genClass.DefineMethod (
                                genMethodName (), 0, typeof (void),
@@ -469,6 +469,29 @@ namespace MonoTests.System.Reflection.Emit
                        }
                }
 
+               [Test]
+               [Category ("NotWorking")]
+               public void TestGetCustomAttributes_Complete ()
+               {
+                       MethodBuilder mb = genClass.DefineMethod (
+                               genMethodName (), 0, typeof (void),
+                               new Type [1] { typeof (int) });
+                       mb.GetILGenerator ().Emit (OpCodes.Ret);
+                       genClass.CreateType ();
+
+                       try {
+                               mb.GetCustomAttributes (true);
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException) {
+                       }
+
+                       try {
+                               mb.GetCustomAttributes (null, true);
+                               Assert.Fail ("#2");
+                       } catch (NotSupportedException) {
+                       }
+               }
+
                [Test]
                public void TestSetCustomAttribute ()
                {
@@ -562,6 +585,30 @@ namespace MonoTests.System.Reflection.Emit
                        }
                }
 
+               [Test]
+               public void SetCustomAttribute_SuppressUnmanagedCodeSecurity ()
+               {
+                       string mname = genMethodName ();
+
+                       TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+                       MethodBuilder mb = tb.DefineMethod (mname, MethodAttributes.Public,
+                               typeof (void), new Type [] { typeof (int), typeof (string) });
+                       ConstructorInfo attrCtor = typeof (SuppressUnmanagedCodeSecurityAttribute).
+                               GetConstructor (new Type [0]);
+                       CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (
+                               attrCtor, new object [0]);
+                       Assert.IsTrue ((mb.Attributes & MethodAttributes.HasSecurity) == 0, "#1");
+                       mb.SetCustomAttribute (caBuilder);
+                       //Assert.IsTrue ((mb.Attributes & MethodAttributes.HasSecurity) == 0, "#2");
+                       mb.GetILGenerator ().Emit (OpCodes.Ret);
+                       Type emittedType = tb.CreateType ();
+                       MethodInfo emittedMethod = emittedType.GetMethod (mname);
+                       Assert.AreEqual (MethodAttributes.HasSecurity, emittedMethod.Attributes & MethodAttributes.HasSecurity, "#3");
+                       //Assert.IsTrue ((mb.Attributes & MethodAttributes.HasSecurity) == 0, "#4");
+                       object [] emittedAttrs = emittedMethod.GetCustomAttributes (typeof (SuppressUnmanagedCodeSecurityAttribute), true);
+                       Assert.AreEqual (1, emittedAttrs.Length, "#5");
+               }
+
                [AttributeUsage (AttributeTargets.Parameter)]
                class PrivateAttribute : Attribute
                {
index dc5abe46d7b27fa55517261d532cc51a2737b39f..a33cbcf42f317e536351b251579c221938ca72c9 100644 (file)
@@ -191,13 +191,47 @@ namespace MonoTests.System.Reflection.Emit
                }
 
                [Test]
-               [ExpectedException (typeof (NotSupportedException))]
-               public void TestHasElementType ()
+               public void TestHasElementType_Incomplete ()
                {
                        // According to the MSDN docs, this member works, but in reality, it
                        // returns a NotSupportedException
                        TypeBuilder tb = module.DefineType (genTypeName ());
-                       bool b = tb.HasElementType;
+#if NET_2_0
+                       Assert.IsFalse (tb.HasElementType);
+#else
+                       try {
+                               bool b = tb.HasElementType;
+                               Assert.Fail ("#1: " + b);
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+#endif
+               }
+
+               [Test]
+#if ONLY_1_1
+               [Category ("NotWorking")]
+#endif
+               public void TestHasElementType_Complete ()
+               {
+                       // According to the MSDN docs, this member works, but in reality, it
+                       // returns a NotSupportedException
+                       TypeBuilder tb = module.DefineType (genTypeName ());
+                       tb.CreateType ();
+#if NET_2_0
+                       Assert.IsFalse (tb.HasElementType);
+#else
+                       try {
+                               bool b = tb.HasElementType;
+                               Assert.Fail ("#1: " + b);
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+#endif
                }
 
                [Test]
@@ -1396,7 +1430,6 @@ namespace MonoTests.System.Reflection.Emit
                }
 
                [Test] // bug #81640
-               [Category ("NotWorking")]
                public void TestGetFieldComplete_Type ()
                {
                        TypeBuilder tb = module.DefineType (genTypeName ());
@@ -1788,6 +1821,24 @@ namespace MonoTests.System.Reflection.Emit
                        enumBuilder.CreateType ();
                }
 
+               [Test]
+               public void SetCustomAttribute_SuppressUnmanagedCodeSecurity ()
+               {
+                       TypeBuilder tb = module.DefineType (genTypeName ());
+                       ConstructorInfo attrCtor = typeof (SuppressUnmanagedCodeSecurityAttribute).
+                               GetConstructor (new Type [0]);
+                       CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (
+                               attrCtor, new object [0]);
+                       Assert.IsTrue ((tb.Attributes & TypeAttributes.HasSecurity) == 0, "#1");
+                       tb.SetCustomAttribute (caBuilder);
+                       //Assert.IsTrue ((tb.Attributes & TypeAttributes.HasSecurity) == 0, "#2");
+                       Type emittedType = tb.CreateType ();
+                       Assert.AreEqual (TypeAttributes.HasSecurity, emittedType.Attributes & TypeAttributes.HasSecurity, "#3");
+                       //Assert.IsTrue ((tb.Attributes & TypeAttributes.HasSecurity) == 0, "#4");
+                       object [] emittedAttrs = emittedType.GetCustomAttributes (typeof (SuppressUnmanagedCodeSecurityAttribute), true);
+                       Assert.AreEqual (1, emittedAttrs.Length, "#5");
+               }
+
                private void DefineStringProperty (TypeBuilder tb, string propertyName, string fieldName, MethodAttributes methodAttribs)
                {
                        // define the field holding the property value