Example #1
0
        public void AddEvent_Complex()
        {
            var eventAttributes = EventAttributes.SpecialName;
            var addMethod       = MutableMethodInfoObjectMother.Create();
            var removeMethod    = MutableMethodInfoObjectMother.Create();
            var raiseMethod     = MutableMethodInfoObjectMother.Create();
            var fakeEvent       = MutableEventInfoObjectMother.CreateWithAccessors();

            _mutableMemberFactoryMock
            .Expect(mock => mock.CreateEvent(_mutableType, "Event", eventAttributes, addMethod, removeMethod, raiseMethod))
            .Return(fakeEvent);

            var result = _mutableType.AddEvent("Event", eventAttributes, addMethod, removeMethod, raiseMethod);

            _mutableMemberFactoryMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeEvent));
            Assert.That(_mutableType.AddedEvents, Is.EqualTo(new[] { result }));
        }
Example #2
0
        public void AddEvent_NoRaiseMethod()
        {
            var event_ = MutableEventInfoObjectMother.CreateWithAccessors();

            Assert.That(event_.MutableRaiseMethod, Is.Null);

            var addMethodBuilder    = MockRepository.GenerateStub <IMethodBuilder>();
            var removeMethodBuilder = MockRepository.GenerateStub <IMethodBuilder>();

            _context.MethodBuilders.Add(event_.MutableAddMethod, addMethodBuilder);
            _context.MethodBuilders.Add(event_.MutableRemoveMethod, removeMethodBuilder);

            var eventBuilderMock = MockRepository.GenerateStrictMock <IEventBuilder>();

            _typeBuilderMock.Stub(stub => stub.DefineEvent(event_.Name, event_.Attributes, event_.EventHandlerType)).Return(eventBuilderMock);
            eventBuilderMock.Expect(mock => mock.SetAddOnMethod(addMethodBuilder));
            eventBuilderMock.Expect(mock => mock.SetRemoveOnMethod(removeMethodBuilder));

            _emitter.AddEvent(_context, event_);

            eventBuilderMock.AssertWasNotCalled(mock => mock.SetRaiseMethod(Arg <IMethodBuilder> .Is.Anything));
            eventBuilderMock.VerifyAllExpectations();
        }
Example #3
0
        public void AddEvent_Simple()
        {
            var name               = "Event";
            var handlerType        = ReflectionObjectMother.GetSomeType();
            var accessorAttributes = (MethodAttributes)7;
            Func <MethodBodyCreationContext, Expression> addBodyProvider    = ctx => null;
            Func <MethodBodyCreationContext, Expression> removeBodyProvider = ctx => null;
            Func <MethodBodyCreationContext, Expression> raiseBodyProvider  = ctx => null;

            var fakeEvent = MutableEventInfoObjectMother.CreateWithAccessors(createRaiseMethod: true);

            _mutableMemberFactoryMock
            .Expect(
                mock => mock.CreateEvent(_mutableType, name, handlerType, accessorAttributes, addBodyProvider, removeBodyProvider, raiseBodyProvider))
            .Return(fakeEvent);

            var result = _mutableType.AddEvent(name, handlerType, accessorAttributes, addBodyProvider, removeBodyProvider, raiseBodyProvider);

            _mutableMemberFactoryMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeEvent));
            Assert.That(_mutableType.AddedEvents, Is.EqualTo(new[] { result }));
            Assert.That(_mutableType.AddedMethods, Is.EqualTo(new[] { result.MutableAddMethod, result.MutableRemoveMethod, result.MutableRaiseMethod }));
        }
Example #4
0
        public void AddEvent_NoRaiseMethod()
        {
            var event_ = MutableEventInfoObjectMother.CreateWithAccessors();

            Assert.That(event_.MutableRaiseMethod, Is.Null);

            var addMethodBuilder    = new Mock <IMethodBuilder>();
            var removeMethodBuilder = new Mock <IMethodBuilder>();

            _context.MethodBuilders.Add(event_.MutableAddMethod, addMethodBuilder.Object);
            _context.MethodBuilders.Add(event_.MutableRemoveMethod, removeMethodBuilder.Object);

            var eventBuilderMock = new Mock <IEventBuilder> (MockBehavior.Strict);

            _typeBuilderMock.Setup(stub => stub.DefineEvent(event_.Name, event_.Attributes, event_.EventHandlerType)).Returns(eventBuilderMock.Object);
            eventBuilderMock.Setup(mock => mock.SetAddOnMethod(addMethodBuilder.Object)).Verifiable();
            eventBuilderMock.Setup(mock => mock.SetRemoveOnMethod(removeMethodBuilder.Object)).Verifiable();

            _emitter.AddEvent(_context, event_);

            eventBuilderMock.Verify(mock => mock.SetRaiseMethod(It.IsAny <IMethodBuilder>()), Times.Never());
            eventBuilderMock.Verify();
        }