New test.
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / MethodBuilderTest.cs
index 8db0b3a51d04a69a0ef3633ddd55b1c4be323767..3f1b82fc38e0e1a59ed3436c03661853df29ed16 100644 (file)
@@ -123,7 +123,7 @@ public class MethodBuilderTest : Assertion
        public void TestMethodHandleComplete () {
                MethodBuilder mb = genClass.DefineMethod (
                        genMethodName (), 0, typeof (void), new Type [0]);
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                genClass.CreateType ();
 
                RuntimeMethodHandle handle = mb.MethodHandle;
@@ -192,14 +192,14 @@ public class MethodBuilderTest : Assertion
                try {
                        mb.CreateMethodBody (new byte[1], -1);
                        Fail ();
-               } catch (ArgumentException) {
+               } catch (ArgumentOutOfRangeException) {
                }
 
                // Check arguments 2.
                try {
                        mb.CreateMethodBody (new byte[1], 2);
                        Fail ();
-               } catch (ArgumentException) {
+               } catch (ArgumentOutOfRangeException) {
                }
 
                mb.CreateMethodBody (new byte[2], 1);
@@ -236,7 +236,7 @@ public class MethodBuilderTest : Assertion
                        new Type[2] {
                        typeof(int), typeof(int)
                });
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                tb.CreateType ();
                mb.DefineParameter (-5, ParameterAttributes.None, "param1");
        }
@@ -251,7 +251,7 @@ public class MethodBuilderTest : Assertion
                        new Type[2] {
                        typeof(int), typeof(int)
                });
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                tb.CreateType ();
                mb.DefineParameter (1, ParameterAttributes.None, "param1");
        }
@@ -285,8 +285,7 @@ public class MethodBuilderTest : Assertion
                mb.DefineParameter (1, 0, "param1");
                mb.DefineParameter (2, 0, null);
 
-               // Can not be called on a created type
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                tb.CreateType ();
                try {
                        mb.DefineParameter (1, 0, "param1");
@@ -296,6 +295,31 @@ public class MethodBuilderTest : Assertion
                }
        }
 
+       [Test]
+#if NET_2_0
+       // MS.NET 2.x no longer allows a zero length method body
+       // to be emitted
+       [ExpectedException (typeof (InvalidOperationException))]
+#endif
+       public void ZeroLengthBodyTest1 ()
+       {
+               MethodBuilder mb = genClass.DefineMethod (
+                       genMethodName (), 0, typeof (void), 
+                       new Type [2] { typeof(int), typeof(int) });
+               mb.CreateMethodBody (new byte[2], 0);
+               genClass.CreateType ();
+       }
+
+       // A zero length method body can be created
+       [Test]
+       public void ZeroLengthBodyTest2 ()
+       {
+               MethodBuilder mb = genClass.DefineMethod (
+                       genMethodName (), 0, typeof (void), 
+                       new Type [2] { typeof(int), typeof(int) });
+               mb.CreateMethodBody (new byte[2], 0);
+       }
+
        [Test]
        public void TestHashCode ()
        {
@@ -364,8 +388,7 @@ public class MethodBuilderTest : Assertion
                                          MethodImplAttributes.OPTIL, 
                                          mb.GetMethodImplementationFlags ());
 
-               // Can not be called on a created type
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                mb.SetImplementationFlags (MethodImplAttributes.Managed);
                tb.CreateType ();
                try {
@@ -512,6 +535,53 @@ public class MethodBuilderTest : Assertion
                }
        }
 
+       [AttributeUsage (AttributeTargets.Parameter)]
+       class PrivateAttribute : Attribute {
+
+               public PrivateAttribute () {
+               }
+       }
+
+       [Test]
+       public void GetCustomAttributes () {
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+               MethodBuilder mb = tb.DefineMethod ("foo", MethodAttributes.Public, 
+                                                                                       typeof (void),
+                                                                                       new Type [1] {typeof(int)});
+               mb.GetILGenerator ().Emit (OpCodes.Ret);
+
+               Type attrType = typeof (ObsoleteAttribute);
+               ConstructorInfo ctorInfo =
+                       attrType.GetConstructor (new Type [] { typeof (String) });
+
+               mb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
+
+               // Check that attributes not accessible are not returned
+               mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (PrivateAttribute).GetConstructor (new Type [0]), new object [] { }));
+
+               Type t = tb.CreateType ();
+
+               // Try the created type
+               {
+                       MethodInfo mi = t.GetMethod ("foo");
+                       object[] attrs = mi.GetCustomAttributes (true);
+
+                       AssertEquals (1, attrs.Length);
+                       Assert (attrs [0] is ObsoleteAttribute);
+                       AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
+               }
+
+               // Try the type builder
+               {
+                       MethodInfo mi = tb.GetMethod ("foo");
+                       object[] attrs = mi.GetCustomAttributes (true);
+
+                       AssertEquals (1, attrs.Length);
+                       Assert (attrs [0] is ObsoleteAttribute);
+                       AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
+               }
+       }
+
        [Test]
        [ExpectedException (typeof (InvalidOperationException))]
        public void TestAddDeclarativeSecurityAlreadyCreated () {
@@ -551,8 +621,7 @@ public class MethodBuilderTest : Assertion
                        try {
                                mb.AddDeclarativeSecurity (action, set);
                                Fail ();
-                       }
-                       catch (ArgumentException) {
+                       } catch (ArgumentOutOfRangeException) {
                        }
                }
        }