Beispiel #1
0
        private static void testCore(UnhandledExceptionEventHandler dele, int expectedTestCounter)
        {
            var w   = WeakDelegate.Create(dele);
            var otc = testConter;

            w(testSender, testArgs);
            Assert.AreEqual(expectedTestCounter, testConter - otc);
        }
Beispiel #2
0
        public void EmptyWorks()
        {
            var wd1 = WeakDelegate <Func <string> > .Create();

            Assert.Null(wd1.Invoker());
            var wd2 = WeakDelegate <Action <string> > .Create();

            wd2.Invoker("test");
            var wd3 = WeakDelegate <Action <string, int> > .Create();

            wd3.Invoker("test", 1);
        }
Beispiel #3
0
        public void DelegateIsWeak()
        {
            var wd1 = WeakDelegate <Func <int> > .Create();

            {
                var t = new SomeClass(5);
                wd1.Add(t.GetValue);
                Assert.Equal(5, wd1.Invoker());
            }
            GCEx.CollectAndWait();
            Assert.Equal(0, wd1.Invoker());
        }
Beispiel #4
0
        public void InvokerWorksForFunc1()
        {
            Func <int, int> sq   = x => x * x;
            Func <int, int> cube = x => x * x * x;

            var wd1 = WeakDelegate <Func <int, int> > .Create();

            wd1.Add(sq);
            Assert.Equal(4, wd1.Invoker(2));
            wd1.Add(cube);
            Assert.Equal(8, wd1.Invoker(2));
            wd1.Remove(cube);
            Assert.Equal(4, wd1.Invoker(2));
            wd1.Remove(sq);
            Assert.Equal(0, wd1.Invoker(2));
        }
Beispiel #5
0
        public void InvokerWorksForAction2()
        {
            var i = 0;
            // ReSharper disable once AccessToModifiedClosure
            Action <int, int> add = (x, y) => i += x + y;

            var wd1 = WeakDelegate <Action <int, int> > .Create();

            wd1.Add(add);
            wd1.Invoker(1, 2);
            Assert.Equal(3, i);
            wd1.Add(add);
            i = 0;
            wd1.Invoker(1, 2);
            Assert.Equal(6, i);
        }
Beispiel #6
0
        public void InvokerWorksForAction1()
        {
            var i = 0;
            // ReSharper disable once AccessToModifiedClosure
            Action <int> add = x => i += x;

            var wd1 = WeakDelegate <Action <int> > .Create();

            wd1.Add(add);
            wd1.Invoker(2);
            Assert.Equal(2, i);
            wd1.Add(add);
            i = 0;
            wd1.Invoker(1);
            Assert.Equal(2, i);
        }
Beispiel #7
0
 public void DelegateThrowsForStupidTypes()
 {
     Assert.Throws <TypeInitializationException>(() => WeakDelegate <string> .Create());
 }