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(); }
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(); }
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 })); }
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 })); }
public void AddProperty_Simple() { var name = "Property"; var type = ReflectionObjectMother.GetSomeType(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2); var accessorAttributes = (MethodAttributes)7; Func <MethodBodyCreationContext, Expression> getBodyProvider = ctx => null; Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => null; var fakeProperty = MutablePropertyInfoObjectMother.CreateReadWrite(); _mutableMemberFactoryMock .Expect(mock => mock.CreateProperty(_mutableType, name, type, indexParameters, accessorAttributes, getBodyProvider, setBodyProvider)) .Return(fakeProperty); var result = _mutableType.AddProperty(name, type, indexParameters, accessorAttributes, getBodyProvider, setBodyProvider); _mutableMemberFactoryMock.VerifyAllExpectations(); Assert.That(result, Is.SameAs(fakeProperty)); Assert.That(_mutableType.AddedProperties, Is.EqualTo(new[] { result })); Assert.That(_mutableType.AddedMethods, Is.EqualTo(new[] { result.MutableGetMethod, result.MutableSetMethod })); Assert.That(result.MutableGetMethod.Attributes, Is.EqualTo(accessorAttributes)); Assert.That(result.MutableSetMethod.Attributes, Is.EqualTo(accessorAttributes)); }