Exemple #1
0
        public void CoerceDelegate_WhenSourceDelegateIsAssignableToTargetDelegateType_ReturnsSameDelegate()
        {
            VoidDelegate1 sourceDelegate = () => { };

            var targetDelegate = (VoidDelegate1)ProxyUtils.CoerceDelegate(typeof(VoidDelegate1), sourceDelegate);

            Assert.AreSame(sourceDelegate, targetDelegate);
        }
Exemple #2
0
        public void CoerceDelegate_Void_To_Void()
        {
            bool          called = false;
            VoidDelegate1 d1     = () => { called = true; };
            var           d2     = (VoidDelegate2)ProxyUtils.CoerceDelegate(typeof(VoidDelegate2), d1);

            d2();

            Assert.IsTrue(called);
        }
Exemple #3
0
        public void CoerceDelegate_IntInt_To_IntInt()
        {
            bool            called = false;
            IntIntDelegate1 d1     = x => { called = true; return(x * x); };
            var             d2     = (IntIntDelegate2)ProxyUtils.CoerceDelegate(typeof(IntIntDelegate2), d1);

            int result = d2(4);

            Assert.IsTrue(called);
            Assert.AreEqual(16, result);
        }
Exemple #4
0
        public void CoerceDelegate_ObjectRefObject_To_ObjectRefObject()
        {
            bool called = false;
            ObjectRefObjectDelegate1 d1 = (ref object x) => { called = true; int y = (int)x; x = y + 1; return(y * y); };
            var d2 = (ObjectRefObjectDelegate2)ProxyUtils.CoerceDelegate(typeof(ObjectRefObjectDelegate2), d1);

            object arg    = 4;
            object result = d2(ref arg);

            Assert.IsTrue(called);
            Assert.AreEqual(16, result);
            Assert.AreEqual(5, arg);
        }
Exemple #5
0
        public void CoerceDelegate_IntRefInt_To_ObjectRefObject()
        {
            bool called           = false;
            IntRefIntDelegate1 d1 = (ref int x) => { called = true; return(x * x++); };
            var d2 = (ObjectRefObjectDelegate1)ProxyUtils.CoerceDelegate(typeof(ObjectRefObjectDelegate1), d1);

            object arg    = 4;
            object result = d2(ref arg);

            Assert.IsTrue(called);
            Assert.AreEqual(16, result);
            Assert.AreEqual(5, arg);
        }
Exemple #6
0
 public void CoerceDelegate_WhenDelegateTypesIncompatible_Throws()
 {
     Assert.Multiple(() =>
     {
         Assert.Throws <InvalidOperationException>(() => ProxyUtils.CoerceDelegate(
                                                       typeof(VoidDelegate1), new IntDelegate1(() => 42)));
         Assert.Throws <InvalidOperationException>(() => ProxyUtils.CoerceDelegate(
                                                       typeof(IntDelegate1), new IntIntDelegate1(x => 42)));
         Assert.Throws <InvalidOperationException>(() => ProxyUtils.CoerceDelegate(
                                                       typeof(IntIntDelegate1), new IntOutIntDelegate1((out int x) => { x = 42; return(42); })));
         Assert.Throws <InvalidOperationException>(() => ProxyUtils.CoerceDelegate(
                                                       typeof(IntIntDelegate1), new IntRefIntDelegate1((ref int x) => { x = 42; return(42); })));
         Assert.Throws <InvalidOperationException>(() => ProxyUtils.CoerceDelegate(
                                                       typeof(IntOutIntDelegate1), new IntRefIntDelegate1((ref int x) => { x = 42; return(42); })));
     });
 }
Exemple #7
0
        public void CoerceDelegate_EventHandler_To_EventHandler()
        {
            bool                  called          = false;
            object                actualSender    = null;
            EventArgs             actualEventArgs = null;
            EventHandlerDelegate1 d1 = (sender, e) => { called = true; actualSender = sender; actualEventArgs = e; };
            var d2 = (EventHandlerDelegate2)ProxyUtils.CoerceDelegate(typeof(EventHandlerDelegate2), d1);

            object    expectedSender    = this;
            EventArgs expectedEventArgs = EventArgs.Empty;

            d2(expectedSender, expectedEventArgs);

            Assert.IsTrue(called);
            Assert.AreSame(expectedSender, actualSender);
            Assert.AreSame(expectedEventArgs, actualEventArgs);
        }
Exemple #8
0
 public void CoerceDelegate_WhenSourceDelegateIsNull_ReturnsNull()
 {
     Assert.IsNull(ProxyUtils.CoerceDelegate(typeof(VoidDelegate1), null));
 }
Exemple #9
0
 public void CoerceDelegate_WhenTargetDelegateTypeIsNotAValidDelegateType_Throws()
 {
     Assert.Throws <ArgumentException>(() => ProxyUtils.CoerceDelegate(typeof(object), new VoidDelegate1(() => { })));
 }
Exemple #10
0
 public void CoerceDelegate_WhenTargetDelegateTypeIsNull_Throws()
 {
     Assert.Throws <ArgumentNullException>(() => ProxyUtils.CoerceDelegate(null, new VoidDelegate1(() => { })));
 }