コード例 #1
0
        public static void AddRemoveMultipleIndividualHandlersFromSameSubscriber()
        {
            var subscriber = new TestEventHandler();
            var collection = new EventSubscriberCollection();

            Assert.True(collection.Add <NamedEvent>(subscriber));
            Assert.False(collection.Add <NamedEvent>(subscriber));

            Assert.True(collection.Add <TestEvent>(subscriber));
            Assert.False(collection.Add <TestEvent>(subscriber));

            Assert.True(collection.Remove <NamedEvent>(subscriber));
            Assert.False(collection.Remove <NamedEvent>(subscriber));

            Assert.True(collection.Remove <TestEvent>(subscriber));
            Assert.False(collection.Remove <TestEvent>(subscriber));
        }
コード例 #2
0
        public static void MixedIndividualOrCompleteAdditionRemoval()
        {
            var subscriber = new TestEventHandler();
            var collection = new EventSubscriberCollection();

            // add individually
            Assert.True(collection.Add <NamedEvent>(subscriber));
            Assert.True(collection.Add <TestEvent>(subscriber));

            // remove all
            Assert.True(collection.RemoveAll(subscriber));

            // check individually
            Assert.False(collection.Remove <NamedEvent>(subscriber));
            Assert.False(collection.Remove <TestEvent>(subscriber));


            // add all
            Assert.True(collection.AddAll(subscriber));

            // remove individually
            Assert.True(collection.Remove <NamedEvent>(subscriber));
            Assert.True(collection.Remove <TestEvent>(subscriber));

            // check all
            Assert.False(collection.RemoveAll(subscriber));


            // add all
            Assert.True(collection.AddAll(subscriber));

            // remove some
            Assert.True(collection.Remove <NamedEvent>(subscriber));

            // remove rest
            Assert.True(collection.RemoveAll(subscriber));

            // check
            Assert.False(collection.RemoveAll(subscriber));
        }
コード例 #3
0
        public static void AddRemoveIndividualEventHandlers()
        {
            var subscriber = new TestEventHandler();
            var collection = new EventSubscriberCollection();

            // invalid argument tests
            Assert.Throws <ArgumentNullException>(() => collection.Add <EventBase>(null, weakRef: true));
            Assert.Throws <ArgumentNullException>(() => collection.Add <EventBase>(null, weakRef: false));
            Assert.Throws <ArgumentNullException>(() => collection.Remove <EventBase>(null));

            // can not remove, what's not there
            Assert.False(collection.Remove <NamedEvent>(subscriber));
            Assert.False(collection.Remove <TestEvent>(subscriber));

            // add
            Assert.True(collection.Add <NamedEvent>(subscriber, weakRef: false));
            Assert.False(collection.Add <NamedEvent>(subscriber, weakRef: false)); // subscribers only added once
            Assert.False(collection.Add <NamedEvent>(subscriber, weakRef: true));  // the type of reference used does not change this

            // remove
            Assert.True(collection.Remove <NamedEvent>(subscriber));
            Assert.False(collection.Remove <NamedEvent>(subscriber));
        }