Exemplo n.º 1
0
        public void Handler_ContextShouldNot_BeChanged()
        {
            //Arrange
            var handler = new Handler();
            var context = new Context()
            {
                Data = 0
            };

            //Act
            //Assert
            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                Assert.AreEqual(ctx.Data, 0);
                ctx.Data += 1;
            });
            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                Assert.AreEqual(ctx.Data, 0);
                ctx.Data += 1;
            });
            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                Assert.AreEqual(ctx.Data, 0);
                ctx.Data += 1;
            });

            handler.Dispatch <ContextfulEvent, Context>(context);

            Assert.AreEqual(context.Data, 0);
        }
Exemplo n.º 2
0
        public void Handler_ShouldThrow_WhenSubscribing_IfSubscriptionIsNull()
        {
            //Arrange
            var handler = new Handler();
            ISubscription <ContextlessEvent> subscription = null;

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Subscribe(subscription));
        }
Exemplo n.º 3
0
        public void Handler_ShouldThrowException_WhenUnsubscribing_WhenSubscriptionIsNull()
        {
            //Arrange
            var handler = new Handler();
            ContextfulSubscription subscription = null;

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Subscribe(subscription));
        }
Exemplo n.º 4
0
        public void Handler_ShouldThrowException_WhenUnsubscribing_WhenEventIsNotSubscribed()
        {
            //Arrange
            var handler      = new Handler();
            var subscription = new ContextfulSubscription();

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Unsubscribe(subscription));
        }
Exemplo n.º 5
0
        public void Handler_ShouldThrowException_WhenSubscribedTwice()
        {
            //Arrange
            var handler      = new Handler();
            var subscription = new ContextlessSubscription();

            handler.Subscribe(subscription);

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Subscribe(subscription));
        }
Exemplo n.º 6
0
        public void Handler_ShouldNotify_ContexlessSubscribe()
        {
            //Arrange
            var handler          = new Handler();
            var subscriptionMock = new Mock <ContextlessSubscription>();

            subscriptionMock.Setup(m => m.Handle());
            handler.Subscribe(subscriptionMock.Object);

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            subscriptionMock.Verify(x => x.Handle(), Times.Once());
        }
Exemplo n.º 7
0
        public void Handler_ShouldThrow_WhenSubscriptionForDualEvent_IsNotImplementedProperly()
        {
            //Arrange
            var handler = new Handler();
            var context = new DualContext();
            var subs    = new InvalidDualSubscription();

            handler.Subscribe(subs);

            //Act
            handler.Dispatch <DualEvent, DualContext>(context);

            //Assert
            Assert.ThrowsException <InvalidOperationException>(() => handler.Dispatch <DualEvent>());
        }
Exemplo n.º 8
0
        public void Handler_ShouldNotify_ContexlessLambda()
        {
            //Arrange
            var subscriptionCalled = false;
            var handler            = new Handler();

            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionCalled = true;
            });

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            Assert.IsTrue(subscriptionCalled);
        }
Exemplo n.º 9
0
        public void Handler_ShouldDispatchAll_WhenSubscriptionForDualEvent_IsImplementedProperly()
        {
            //Arrange
            var subscriptionsHandled = 0;
            var handler = new Handler();
            var context = new DualContext();
            var subs    = new ValidDualSubscription(() => subscriptionsHandled++);

            handler.Subscribe <DualEvent>(subs);

            //Act
            handler.Dispatch <DualEvent, DualContext>(context);
            handler.Dispatch <DualEvent>();

            //Assert
            Assert.AreEqual(2, subscriptionsHandled);
        }
Exemplo n.º 10
0
        public void Handler_ShouldNotNotify_WhenSubscriptionRemoved()
        {
            //Arrange
            var subscriptionCalled = false;
            var handler            = new Handler();
            var subscription       = handler.Subscribe <ContextlessEvent>(() => {
                subscriptionCalled = true;
            });

            handler.Unsubscribe(subscription);

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            Assert.IsFalse(subscriptionCalled);
        }
Exemplo n.º 11
0
        public void Handler_ShouldNotify_ContexfulSubscribe()
        {
            //Arrange
            var handler          = new Handler();
            var subscriptionMock = new Mock <ContextfulSubscription>();
            var context          = new Context()
            {
                Data = 42
            };

            subscriptionMock.Setup(m => m.Handle(It.IsAny <Context>()));
            handler.Subscribe(subscriptionMock.Object);

            //Act
            handler.Dispatch <ContextfulEvent, Context>(context);

            //Assert
            subscriptionMock.Verify(x => x.Handle(context), Times.Once());
        }
Exemplo n.º 12
0
        public void Handler_ShouldNotify_ContexfulLambda()
        {
            //Arrange
            var subscriptionCalled = false;
            var handler            = new Handler();
            var context            = new Context()
            {
                Data = 42
            };

            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                subscriptionCalled = true;
            });

            //Act
            handler.Dispatch <ContextfulEvent, Context>(context);

            //Assert
            Assert.IsTrue(subscriptionCalled);
        }
Exemplo n.º 13
0
        public void Handler_ShouldNotify_AllSubscriptions()
        {
            //Arrange
            var subscriptionsHandled = 0;
            var handler = new Handler();

            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionsHandled++;
            });
            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionsHandled++;
            });
            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionsHandled++;
            });

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            Assert.AreEqual(subscriptionsHandled, 3);
        }
Exemplo n.º 14
0
        public void Handler_ShouldNotThrow_WhenSubscriptionForDualEvent_IsImplementedProperly()
        {
            //Arrange
            var timesDispatched = 0;
            var handler         = new Handler();
            var context         = new DualContext();
            var subs            = new ValidDualSubscription(() => { timesDispatched++; });

            handler.Subscribe <DualEvent>(subs);

            //Act
            //Assert
            try
            {
                handler.Dispatch <DualEvent, DualContext>(context);
                handler.Dispatch <DualEvent>();
            }
            catch (Exception e)
            {
                Assert.Fail($"Expected no exception, but got {e.Message}");
            }
            Assert.AreEqual(timesDispatched, 2);
        }