Exemplo n.º 1
0
        public void AddProperty_ReadOnly_WriteOnly()
        {
            var staticGetMethod   = MutableMethodInfoObjectMother.Create(attributes: MethodAttributes.Static, returnType: typeof(int));
            var setMethod         = MutableMethodInfoObjectMother.Create(parameters: new[] { ParameterDeclarationObjectMother.Create(typeof(long)) });
            var readOnlyProperty  = MutablePropertyInfoObjectMother.Create(getMethod: staticGetMethod);
            var writeOnlyProperty = MutablePropertyInfoObjectMother.Create(setMethod: setMethod);

            Assert.That(readOnlyProperty.MutableSetMethod, Is.Null);
            Assert.That(writeOnlyProperty.MutableGetMethod, Is.Null);

            var methodBuilder = MockRepository.GenerateStub <IMethodBuilder>();

            _context.MethodBuilders.Add(readOnlyProperty.MutableGetMethod, methodBuilder);
            _context.MethodBuilders.Add(writeOnlyProperty.MutableSetMethod, methodBuilder);

            var propertyBuilderMock1 = MockRepository.GenerateStrictMock <IPropertyBuilder>();
            var propertyBuilderMock2 = MockRepository.GenerateStrictMock <IPropertyBuilder>();

            _typeBuilderMock
            .Expect(mock => mock.DefineProperty(readOnlyProperty.Name, readOnlyProperty.Attributes, staticGetMethod.CallingConvention, typeof(int), Type.EmptyTypes))
            .Return(propertyBuilderMock1);
            _typeBuilderMock
            .Expect(mock => mock.DefineProperty(writeOnlyProperty.Name, writeOnlyProperty.Attributes, setMethod.CallingConvention, typeof(long), Type.EmptyTypes))
            .Return(propertyBuilderMock2);
            propertyBuilderMock1.Expect(mock => mock.SetGetMethod(methodBuilder));
            propertyBuilderMock2.Expect(mock => mock.SetSetMethod(methodBuilder));

            _emitter.AddProperty(_context, readOnlyProperty);
            _emitter.AddProperty(_context, writeOnlyProperty);

            _typeBuilderMock.VerifyAllExpectations();
            propertyBuilderMock1.VerifyAllExpectations();
            propertyBuilderMock2.VerifyAllExpectations();
        }
Exemplo n.º 2
0
        public void AddProperty()
        {
            var name                = "Property";
            var attributes          = (PropertyAttributes)7;
            var returnType          = ReflectionObjectMother.GetSomeType();
            var parameters          = ParameterDeclarationObjectMother.CreateMultiple(2);
            var setMethodParameters = parameters.Concat(new[] { ParameterDeclarationObjectMother.Create(returnType) });
            var indexParameterTypes = parameters.Select(p => p.Type).ToArray();
            var getMethod           = MutableMethodInfoObjectMother.Create(returnType: returnType, parameters: parameters);
            var setMethod           = MutableMethodInfoObjectMother.Create(parameters: setMethodParameters);
            var property            = MutablePropertyInfoObjectMother.Create(name: name, attributes: attributes, getMethod: getMethod, setMethod: setMethod);

            var getMethodBuilder = MockRepository.GenerateStub <IMethodBuilder>();
            var setMethodBuilder = MockRepository.GenerateStub <IMethodBuilder>();

            _context.MethodBuilders.Add(getMethod, getMethodBuilder);
            _context.MethodBuilders.Add(setMethod, setMethodBuilder);

            var callingConventions  = CallingConventions.Standard | CallingConventions.HasThis;
            var propertyBuilderMock = MockRepository.GenerateStrictMock <IPropertyBuilder>();

            _typeBuilderMock
            .Expect(mock => mock.DefineProperty(name, attributes, callingConventions, returnType, indexParameterTypes))
            .Return(propertyBuilderMock);
            SetupDefineCustomAttribute(propertyBuilderMock, property);
            propertyBuilderMock.Expect(mock => mock.SetGetMethod(getMethodBuilder));
            propertyBuilderMock.Expect(mock => mock.SetSetMethod(setMethodBuilder));

            _emitter.AddProperty(_context, property);

            _typeBuilderMock.VerifyAllExpectations();
            propertyBuilderMock.VerifyAllExpectations();
        }
Exemplo n.º 3
0
        public void AddProperty_Simple_WriteOnlyProperty()
        {
            var type = ReflectionObjectMother.GetSomeType();
            Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => null;
            var fakeSetMethod = MutableMethodInfoObjectMother.Create(parameters: new[] { ParameterDeclarationObjectMother.Create(type) });
            var fakeProperty  = MutablePropertyInfoObjectMother.Create(setMethod: fakeSetMethod);

            Assert.That(fakeProperty.MutableGetMethod, Is.Null);
            _mutableMemberFactoryMock
            .Stub(stub => stub.CreateProperty(_mutableType, "Property", type, ParameterDeclaration.None, MethodAttributes.Public, null, setBodyProvider))
            .Return(fakeProperty);

            _mutableType.AddProperty("Property", type, setBodyProvider: setBodyProvider);

            Assert.That(_mutableType.AddedMethods, Is.EqualTo(new[] { fakeSetMethod }));
        }
Exemplo n.º 4
0
        public void AddProperty_Complex()
        {
            var name         = "Property";
            var attributes   = (PropertyAttributes)7;
            var getMethod    = MutableMethodInfoObjectMother.Create(attributes: MethodAttributes.Static);
            var setMethod    = MutableMethodInfoObjectMother.Create(attributes: MethodAttributes.Static);
            var fakeProperty = MutablePropertyInfoObjectMother.Create();

            _mutableMemberFactoryMock
            .Expect(mock => mock.CreateProperty(_mutableType, name, attributes, getMethod, setMethod))
            .Return(fakeProperty);

            var result = _mutableType.AddProperty(name, attributes, getMethod, setMethod);

            _mutableMemberFactoryMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeProperty));
            Assert.That(_mutableType.AddedProperties, Is.EqualTo(new[] { result }));
        }