예제 #1
0
        public void WithEventEmitter_WhenEmitsEvent_CallsListenersWithEventNameAndArguments()
        {
            var callList = new List <int>();

            var em = new TestEventEmitter(DefaultLogger.LoggerInstance);

            TestEventEmitterArgs listener1Args = null;

            void Listener1(TestEventEmitterArgs args)
            {
                callList.Add(1);
                em.Off(1, Listener2);
                em.On(1, Listener4);
                listener1Args = args;
            }

            void Listener2(TestEventEmitterArgs args)
            {
                callList.Add(2);
                em.Off(1, Listener3);
                throw new Exception("should not be hit");
            }

            void Listener3(TestEventEmitterArgs args)
            {
                callList.Add(3);
            }

            void Listener4(TestEventEmitterArgs args)
            {
                callList.Add(4);
            }

            void Listener5(TestEventEmitterArgs args)
            {
                callList.Add(5);
                em.Off(1, Listener5);
            }

            em.On(1, Listener1);
            em.On(1, Listener2);
            em.On(1, Listener3);
            em.On(1, Listener5);

            // Listener1 is called first, it subscribes Listener4
            // Listener2 is removed by Listener1, but should still be called
            // Listener3 is called
            // Listener4 is should not be called as it was added by Listener1
            // Listener5 is called
            em.DoDummyEmit(1, "emit1");
            callList.Count.Should().Be(4);
            callList[0].Should().Be(1);
            callList[1].Should().Be(2);
            callList[2].Should().Be(3);
            callList[3].Should().Be(5);
            listener1Args.Message.Should().Be("emit1");

            callList = new List <int>();

            // Listener1 is called first, it subscribes Listener4. Listener4 now has 2 subscriptions, but only 1 should fire here
            // Listener2 was already removed by Listener, so it is not called
            // Listener3 was removed by Listener2, it should not be called
            // Listener4 is called once
            // Listener5 is not called as it removed itself previously
            em.DoDummyEmit(1, "emit2");
            callList.Count.Should().Be(2);
            callList[0].Should().Be(1);
            callList[1].Should().Be(4);
            listener1Args.Message.Should().Be("emit2");

            callList = new List <int>();

            void Listener6(TestEventEmitterArgs args)
            {
                callList.Add(6);
                em.Off();
            }

            em.On(1, Listener6);
            em.DoDummyEmit(1, "emit3");
            callList.Count.Should().Be(4);
            callList[0].Should().Be(1);
            callList[1].Should().Be(4);
            callList[2].Should().Be(4);
            callList[3].Should().Be(6);
            listener1Args.Message.Should().Be("emit3");

            callList = new List <int>();

            // Listener6 removed all listeners
            em.DoDummyEmit(1, "emit4");
            callList.Count.Should().Be(0);
            listener1Args.Message.Should().Be("emit3");
        }
예제 #2
0
        public void WithEventEmitter_WhenOff_DeRegistersForEvents()
        {
            var    em      = new TestEventEmitter(DefaultLogger.LoggerInstance);
            string message = string.Empty;
            int    counter = 0;

            void Reset()
            {
                counter = 0;
            }

            void Listener1(TestEventEmitterArgs args)
            {
                counter++;
                message = args.Message;
            }

            void Listener2(TestEventEmitterArgs args)
            {
                counter++;
                message = args.Message;
            }

            // if called with no arguments, it removes all registrations, for all events and listeners
            em.On(1, Listener1);
            em.On(2, Listener1);
            em.On(3, Listener1);
            em.DoDummyEmit(1, "off");
            message.Should().Be("off");
            counter.Should().Be(1);
            Reset();

            em.Off();
            em.DoDummyEmit(1, "off");
            em.DoDummyEmit(2, "off");
            em.DoDummyEmit(3, "off");
            counter.Should().Be(0);
            Reset();

            em.On(1, Listener1);
            em.On(2, Listener2);
            em.On(1, Listener2);
            em.DoDummyEmit(1, "off");
            message.Should().Be("off");
            counter.Should().Be(2);
            Reset();

            em.Off();
            em.DoDummyEmit(1, "off");
            em.DoDummyEmit(2, "off");
            counter.Should().Be(0);
            Reset();

            // if called only with a listener, it removes all registrations matching the given listener,
            // regardless of whether they are associated with an event or not
            em.On(1, Listener1);
            em.On(1, Listener2);
            em.DoDummyEmit(1, "off");
            counter.Should().Be(2);
            Reset();

            em.Off(Listener2);
            em.DoDummyEmit(1, "off");
            counter.Should().Be(1);
            Reset();

            em.Off(Listener1);
            em.DoDummyEmit(1, "off");
            counter.Should().Be(0);

            // If called with a specific event and a listener,
            // it removes all registrations that match both the given listener and the given event
            em.On(1, Listener1);
            em.On(1, Listener2);
            em.On(2, Listener1);
            Reset();

            em.DoDummyEmit(1, "off");
            counter.Should().Be(2);
            Reset();

            em.DoDummyEmit(2, "off");
            counter.Should().Be(1);
            Reset();

            // no handler for this event, so this should have no effect
            em.Off(3, Listener1);
            em.DoDummyEmit(1, "off");
            counter.Should().Be(2);
            Reset();

            // no handler for this listener, so this should have no effect
            em.Off(2, Listener2);
            em.DoDummyEmit(1, "off");
            counter.Should().Be(2);
            Reset();

            // remove handler 1 of 2 remaining
            em.Off(1, Listener1);
            em.DoDummyEmit(1, "off");
            counter.Should().Be(1);
            Reset();

            // remove the final handler
            em.Off(1, Listener2);
            em.DoDummyEmit(1, "off");
            counter.Should().Be(0);
        }