TARGET_JVM adjustments.
[mono.git] / mcs / class / corlib / Test / System.Reflection / MethodInfoTest.cs
index 74826578f818e0fbfdba678ef6d8ce501d03a0c4..86cb680d49405abab7e08382a18f06a249906257 100644 (file)
@@ -40,11 +40,12 @@ using System.Collections.Generic;
 namespace MonoTests.System.Reflection
 {
        [TestFixture]
-       public class MethodInfoTest : Assertion
+       public class MethodInfoTest
        {
+#if !TARGET_JVM
                [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
                public static extern void dllImportMethod ();
-
+#endif
                [MethodImplAttribute(MethodImplOptions.PreserveSig)]
                public void preserveSigMethod () {
                }
@@ -62,15 +63,15 @@ namespace MonoTests.System.Reflection
 
                        DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
 
-                       AssertEquals (CallingConvention.Winapi, attr.CallingConvention);
-                       AssertEquals ("foo", attr.EntryPoint);
-                       AssertEquals ("libfoo", attr.Value);
-                       AssertEquals (CharSet.Unicode, attr.CharSet);
-                       AssertEquals (false, attr.ExactSpelling);
-                       AssertEquals (true, attr.PreserveSig);
-                       AssertEquals (true, attr.SetLastError);
-                       AssertEquals (true, attr.BestFitMapping);
-                       AssertEquals (true, attr.ThrowOnUnmappableChar);
+                       Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
+                       Assert.AreEqual ("foo", attr.EntryPoint, "#2");
+                       Assert.AreEqual ("libfoo", attr.Value, "#3");
+                       Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
+                       Assert.AreEqual (false, attr.ExactSpelling, "#5");
+                       Assert.AreEqual (true, attr.PreserveSig, "#6");
+                       Assert.AreEqual (true, attr.SetLastError, "#7");
+                       Assert.AreEqual (true, attr.BestFitMapping, "#8");
+                       Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
 
                        PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
 
@@ -79,6 +80,18 @@ namespace MonoTests.System.Reflection
                          MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
                        */
                }
+
+               [return: MarshalAs (UnmanagedType.Interface)]
+               public void ReturnTypeMarshalAs () {
+               }
+
+               [Test]
+               [Category ("TargetJvmNotWorking")]
+               public void ReturnTypePseudoCustomAttributes () {
+                       MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
+
+                       Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
+               }
 #endif
 
                public static int foo (int i, int j)
@@ -100,14 +113,14 @@ namespace MonoTests.System.Reflection
                        MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
                        object[] parms = new object[] {1};
                        met.Invoke (null, parms);
-                       AssertEquals (2, parms [0]);
+                       Assert.AreEqual (2, parms[0]);
                }
 
                public static void ByRefTest (ref int a1)
                {
                        if (a1 == 1)
                                a1 = 2;
-               }          
+               }
 
                static int byref_arg;
 
@@ -117,6 +130,9 @@ namespace MonoTests.System.Reflection
                }
 
                [Test]
+#if ONLY_1_1
+               [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
+#endif
                public void ByrefVtypeInvoke ()
                {
                        MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
@@ -124,14 +140,14 @@ namespace MonoTests.System.Reflection
                        object o = 1;
                        object[] args = new object [] { o };
                        mi.Invoke (null, args);
-                       AssertEquals (1, byref_arg);
-                       AssertEquals (1, o);
-                       AssertEquals (5, args [0]);
+                       Assert.AreEqual (1, byref_arg, "#A1");
+                       Assert.AreEqual (1, o, "#A2");
+                       Assert.AreEqual (5, args[0], "#A3");
 
                        args [0] = null;
                        mi.Invoke (null, args);
-                       AssertEquals (0, byref_arg);
-                       AssertEquals (5, args [0]);
+                       Assert.AreEqual (0, byref_arg, "#B1");
+                       Assert.AreEqual (5, args[0], "#B2");
                }
 
                public void HeyHey (out string out1, ref string ref1)
@@ -142,35 +158,51 @@ namespace MonoTests.System.Reflection
                [Test] // bug #76541
                public void ToStringByRef ()
                {
-                       AssertEquals ("Void HeyHey(System.String ByRef, System.String ByRef)",
+                       Assert.AreEqual ("Void HeyHey(System.String ByRef, System.String ByRef)",
                                this.GetType ().GetMethod ("HeyHey").ToString ());
                }
 
+               class GBD_A         { public virtual     void f () {} }
+               class GBD_B : GBD_A { public override    void f () {} }
+               class GBD_C : GBD_B { public override    void f () {} }
+               class GBD_D : GBD_C { public new virtual void f () {} }
+               class GBD_E : GBD_D { public override    void f () {} }
+
+               [Test]
+               public void GetBaseDefinition ()
+               {
+                       Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
+                       Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
+                       Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
+               }
+
 #if NET_2_0
+#if !TARGET_JVM // MethodBody is not supported for TARGET_JVM
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void GetMethodBody_Abstract () {
-                       typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
+                       MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
+                       Assert.IsNull (mb);
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void GetMethodBody_Runtime () {
-                       typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
+                       MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
+                       Assert.IsNull (mb);
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void GetMethodBody_Pinvoke () {
-                       typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
+                       MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
+                       Assert.IsNull (mb);
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void GetMethodBody_Icall () {
                        foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
-                               if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0)
-                                       mi.GetMethodBody ();
+                               if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
+                                       MethodBody mb = mi.GetMethodBody ();
+                                       Assert.IsNull (mb);
+                               }
                }
 
                public static void locals_method () {
@@ -187,21 +219,37 @@ namespace MonoTests.System.Reflection
                public void GetMethodBody () {
                        MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
 
-                       Assert (mb.InitLocals);
-                       Assert (mb.LocalSignatureMetadataToken > 0);
+                       Assert.IsTrue (mb.InitLocals, "#1");
+                       Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
 
                        IList<LocalVariableInfo> locals = mb.LocalVariables;
 
                        // This might break with different compilers etc.
-                       AssertEquals (2, locals.Count);
+                       Assert.AreEqual (2, locals.Count, "#3");
 
-                       Assert ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])));
+                       Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
                        if (locals [0].LocalType == typeof (byte[]))
-                               AssertEquals (false, locals [0].IsPinned);
+                               Assert.AreEqual (false, locals [0].IsPinned, "#5");
                        else
-                               AssertEquals (false, locals [1].IsPinned);
+                               Assert.AreEqual (false, locals [1].IsPinned, "#6");
+               }
+#endif // TARGET_JVM
+
+               public int return_parameter_test () {
+                       return 0;
                }
 
+               [Test]
+               public void ReturnParameter () {
+                       ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
+
+                       Assert.AreEqual (typeof (int), pi.ParameterType);
+                       Assert.AreEqual (-1, pi.Position);
+                       // This fails on MS
+                       //Assert.AreEqual (True, pi.IsRetval);
+               }
+
+#if !TARGET_JVM // ReflectionOnly is not supported yet on TARGET_JVM
                [Test]
                [ExpectedException (typeof (InvalidOperationException))]
                public void InvokeOnRefOnlyAssembly ()
@@ -212,9 +260,29 @@ namespace MonoTests.System.Reflection
                        
                        m.Invoke (null, new object [0]);
                }
+#endif // TARGET_JVM
 
-               public void MakeGenericMethodArgsMismatchFoo<T> () {}
+               [Test]
+               public void InvokeGenericVtype ()
+               {
+                       KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
+                       Type type = kvp.GetType ();
+                       Type [] arguments = type.GetGenericArguments ();
+                       MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
+                       MethodInfo generic_method = method.MakeGenericMethod (arguments);
+                       kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
+
+                       Assert.AreEqual ("a", kvp.Key);
+                       Assert.AreEqual (21, kvp.Value);
+               }
 
+        public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
+        {
+                       return kvp;
+        }
+
+               public void MakeGenericMethodArgsMismatchFoo<T> () {}
+           
                [Test]
                [ExpectedException (typeof (ArgumentException))]
                public void MakeGenericMethodArgsMismatch ()
@@ -233,8 +301,60 @@ namespace MonoTests.System.Reflection
                public void NullableTests ()
                {
                        MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
-                       AssertEquals (102, mi.Invoke (null, new object [] { 102 }));
-                       AssertEquals (null, mi.Invoke (null, new object [] { null }));
+                       Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
+                       Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
+               }
+
+               public static void foo_generic<T> () {
+               }
+
+               [Test]
+               public void IsGenericMethod ()
+               {
+                       MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
+                       Assert.AreEqual (true, mi.IsGenericMethod, "#1");
+                       MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
+                       Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
+
+                       MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
+                       Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
+               }
+
+               class A<T> {
+
+                       public static void Foo<T2> (T2 i) {
+                       }
+
+                       public static void Bar () {
+                       }
+
+                       public class B {
+                               public static void Baz () {
+                               }
+                       }
+               }
+
+               [Test]
+               public void ContainsGenericParameters ()
+               {
+                       // Non-generic method in open generic type
+                       Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
+                       // open generic method in closed generic type
+                       Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
+                       // non-generic method in closed generic type
+                       Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
+                       // closed generic method in closed generic type
+                       Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
+                       // non-generic method in non-generic nested type of closed generic type
+                       Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
+                       // non-generic method in non-generic nested type of open generic type
+                       Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
+               }
+
+               class GenericHelper<T>
+               {
+                       public void Test (T t)
+                       { }
                }
 #endif
        }