Merge pull request #853 from echampet/onclick
[mono.git] / mcs / class / corlib / Test / System / DelegateTest.cs
index 54b7ec13fa5f4ec340e821dc96d23cf95e013606..2458b9831a608b1993d7e8682370f6bad6132a17 100644 (file)
@@ -17,7 +17,6 @@ namespace MonoTests.System
        [TestFixture]
        public class DelegateTest
        {
-#if NET_2_0
                
                public class GenericClass<T> {
                        public void Method<K> (T t, K k) {}
@@ -51,7 +50,6 @@ namespace MonoTests.System
                        Assert.IsNotNull (method, "#1");
                        Assert.AreEqual (target, method, "#2");
                }
-#endif
 
                [Test] // CreateDelegate (Type, MethodInfo)
                public void CreateDelegate1_Method_Static ()
@@ -73,25 +71,11 @@ namespace MonoTests.System
                {
                        C c = new C ();
                        MethodInfo mi = typeof (C).GetMethod ("M");
-#if NET_2_0
                        Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
                        Assert.AreSame (mi, dg.Method, "#1");
                        Assert.IsNull (dg.Target, "#2");
                        D d = (D) dg;
                        d (c);
-#else
-                       try {
-                               Delegate.CreateDelegate (typeof (D), mi);
-                               Assert.Fail ("#1");
-                       } catch (ArgumentException ex) {
-                               // Method must be a static method
-                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
-                               Assert.IsNull (ex.InnerException, "#3");
-                               Assert.IsNotNull (ex.Message, "#4");
-                               Assert.IsNotNull (ex.ParamName, "#5");
-                               Assert.AreEqual ("method", ex.ParamName, "#6");
-                       }
-#endif
                }
 
                [Test] // CreateDelegate (Type, MethodInfo)
@@ -584,7 +568,6 @@ namespace MonoTests.System
                        }
                }
 
-#if NET_2_0
                [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
                public void CreateDelegate9 ()
                {
@@ -851,7 +834,6 @@ namespace MonoTests.System
                }
 
                [Test]
-               [Category ("TargetJvmNotWorking")]
                public void CoContraVariance ()
                {
                        CoContraVariantDelegate d = (CoContraVariantDelegate)
@@ -1087,6 +1069,27 @@ namespace MonoTests.System
                        action_int (42);
                }
 
+               struct FooStruct {
+                       public int i, j, k, l;
+
+                       public int GetProp (int a, int b, int c, int d) {
+                               return i;
+                       }
+               }
+
+               delegate int ByRefDelegate (ref FooStruct s, int a, int b, int c, int d);
+
+#if MONOTOUCH
+               [Category ("NotWorking")]
+#endif
+               [Test]
+               public void CallVirtVType ()
+               {
+                       var action = (ByRefDelegate)Delegate.CreateDelegate (typeof (ByRefDelegate), null, typeof (FooStruct).GetMethod ("GetProp"));
+                       var s = new FooStruct () { i = 42 };
+                       Assert.AreEqual (42, action (ref s, 1, 2, 3, 4));
+               }
+
                class Foo {
 
                        public void Bar ()
@@ -1118,16 +1121,26 @@ namespace MonoTests.System
                }
 
                [Test] // #664205
-               public void DynamicInvokeNullTarget ()
+               public void DynamicInvokeClosedStatic ()
                {
                        var d1 = Delegate.CreateDelegate (typeof(Func<int>), null, typeof(DelegateTest).GetMethod ("DynamicInvokeClosedStaticDelegate_CB"));
-                       Assert.AreEqual (4, d1.DynamicInvoke ());
+                       Assert.AreEqual (1, d1.DynamicInvoke (), "#1");
+
+                       var d2 = Delegate.CreateDelegate (typeof(Func<int>), "arg", typeof(DelegateTest).GetMethod ("DynamicInvokeClosedStaticDelegate_CB"));
+                       Assert.AreEqual (2, d2.DynamicInvoke (), "#2");
                }
 
-               public static int DynamicInvokeClosedStaticDelegate_CB (object instance)
+               public static int DynamicInvokeClosedStaticDelegate_CB (string instance)
                {
-                       Assert.IsNull (instance);
-                       return 4;
+                       switch (instance) {
+                       case null:
+                               return 1;
+                       case "arg":
+                               return 2;
+                       default:
+                               Assert.Fail ();
+                               return -1;
+                       }
                }
 
                [Test]
@@ -1168,7 +1181,6 @@ namespace MonoTests.System
                        throw new NotSupportedException ();
                }
 
-#endif
                public static void CreateDelegateOfStaticMethodBoundToNull_Helper (object[] args) {}
 
                [Test]
@@ -1367,17 +1379,56 @@ namespace MonoTests.System
                        } catch (ArgumentException) {}
                }
 
-               private static Func<Int32, Int32, bool> Int32D = (x, y) => (x & y) == y;
-
                [Test]
                public void EnumBaseTypeConversion () {
+                       Func<int, int, bool> dm = Int32D2;
                        var d =
-                               Delegate.CreateDelegate(typeof (Func<StringComparison,
-                                                                                               StringComparison, bool>), Int32D.Method) as
+                               Delegate.CreateDelegate(typeof (Func<StringComparison, StringComparison, bool>), dm.Method) as
                                Func<StringComparison, StringComparison, bool>; 
                        Assert.IsTrue (d (0, 0));
                }
 
+#if !MONOTOUCH
+               public static void DynInvokeWithClosedFirstArg (object a, object b)
+               {
+               }
+
+               [Test]
+               public void DynamicInvokeClosedOverNullDelegate () {
+                       var dm = new DynamicMethod ("test", typeof (Delegate), null);
+                       var il = dm.GetILGenerator ();
+                       il.Emit (OpCodes.Ldnull);
+                       il.Emit (OpCodes.Ldftn, GetType ().GetMethod ("DynInvokeWithClosedFirstArg"));
+                       il.Emit (OpCodes.Newobj, typeof (Action<object>).GetConstructors ()[0]);
+                       il.Emit (OpCodes.Ret);
+
+                       var f = (Func <object>) dm.CreateDelegate (typeof (Func <object>));
+                       Action<object> ac = (Action<object>)f();
+                       ac.DynamicInvoke (new object[] { "oi" });
+                       ac.DynamicInvoke (new object[] { null });
+               }
+
+               [Test]
+               public void DynamicInvokeFirstArgBoundDelegate () {
+                       var dm = new DynamicMethod ("test", typeof (Delegate), null);
+                       var il = dm.GetILGenerator ();
+                       il.Emit (OpCodes.Ldstr, "test");
+                       il.Emit (OpCodes.Ldftn, GetType ().GetMethod ("DynInvokeWithClosedFirstArg"));
+                       il.Emit (OpCodes.Newobj, typeof (Action<object>).GetConstructors ()[0]);
+                       il.Emit (OpCodes.Ret);
+
+                       var f = (Func <object>) dm.CreateDelegate (typeof (Func <object>));
+                       Action<object> ac = (Action<object>)f();
+                       ac.DynamicInvoke (new object[] { "oi" });
+                       ac.DynamicInvoke (new object[] { null });
+               }
+#endif
+
+               static bool Int32D2 (int x, int y)
+               {
+                       return (x & y) == y; 
+               }
+
                public class B {
 
                        public virtual string retarg3 (string s) {