public void Initialization() { var declaringType = MutableTypeObjectMother.Create(); var attributes = (MethodAttributes)7 | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; var parameters = ParameterDeclarationObjectMother.CreateMultiple(2); var body = ExpressionTreeObjectMother.GetSomeExpression(typeof(void)); var ctor = new MutableConstructorInfo(declaringType, attributes, parameters.AsOneTime(), body); Assert.That(ctor.DeclaringType, Is.SameAs(declaringType)); Assert.That(ctor.MutableDeclaringType, Is.SameAs(declaringType)); Assert.That(ctor.Name, Is.EqualTo(".ctor")); var actualParameters = ctor.GetParameters(); Assert.That(actualParameters, Has.Length.EqualTo(2)); CustomParameterInfoTest.CheckParameter(actualParameters[0], ctor, 0, parameters[0].Name, parameters[0].Type, parameters[0].Attributes); CustomParameterInfoTest.CheckParameter(actualParameters[1], ctor, 1, parameters[1].Name, parameters[1].Type, parameters[1].Attributes); Assert.That(ctor.MutableParameters, Is.EqualTo(actualParameters)); var paramExpressions = ctor.ParameterExpressions; Assert.That(paramExpressions, Has.Count.EqualTo(2)); Assert.That(paramExpressions[0], Has.Property("Name").EqualTo(parameters[0].Name).And.Property("Type").SameAs(parameters[0].Type)); Assert.That(paramExpressions[1], Has.Property("Name").EqualTo(parameters[1].Name).And.Property("Type").SameAs(parameters[1].Type)); Assert.That(ctor.Body, Is.SameAs(body)); }
public void CreateProperty_Providers_ThrowsIfAlreadyExists() { var factory = new PropertyFactory(new MethodFactory(new RelatedMethodFinder())); Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => Expression.Empty(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2); var property = _mutableType.AddProperty("Property", typeof(int), indexParameters, setBodyProvider: setBodyProvider); Assert.That( () => factory.CreateProperty(_mutableType, "OtherName", property.PropertyType, indexParameters, 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty(_mutableType, property.Name, typeof(string), indexParameters, 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty( _mutableType, property.Name, property.PropertyType, ParameterDeclarationObjectMother.CreateMultiple(3), 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty(_mutableType, property.Name, property.PropertyType, indexParameters, 0, null, setBodyProvider), Throws.InvalidOperationException.With.Message.EqualTo("Property with equal name and signature already exists.")); }
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 SetBody() { var declaringType = MutableTypeObjectMother.Create(); var attribtes = MethodAttributes.Virtual; // Methods which have a base method must be virtual. var returnType = typeof(object); var parameters = ParameterDeclarationObjectMother.CreateMultiple(2); var baseMethod = ReflectionObjectMother.GetSomeVirtualMethod(); // Base method must be virtual. var genericParameters = new[] { MutableGenericParameterObjectMother.Create() }; var method = MutableMethodInfoObjectMother.Create( declaringType, "Method", attribtes, returnType, parameters, baseMethod, genericParameters: genericParameters); var fakeBody = ExpressionTreeObjectMother.GetSomeExpression(typeof(int)); Func <MethodBodyModificationContext, Expression> bodyProvider = ctx => { Assert.That(ctx.DeclaringType, Is.SameAs(declaringType)); Assert.That(ctx.IsStatic, Is.False); Assert.That(ctx.Parameters, Is.EqualTo(method.ParameterExpressions).And.Not.Empty); Assert.That(ctx.GenericParameters, Is.EqualTo(genericParameters)); Assert.That(ctx.ReturnType, Is.SameAs(returnType)); Assert.That(ctx.BaseMethod, Is.SameAs(baseMethod)); Assert.That(ctx.PreviousBody, Is.SameAs(method.Body)); return(fakeBody); }; method.SetBody(bodyProvider); var expectedBody = Expression.Convert(fakeBody, returnType); ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, method.Body); }
public void SetUp() { _declaringType = CustomTypeObjectMother.Create(); _attributes = (MethodAttributes)7 | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; _parameters = ParameterDeclarationObjectMother.CreateMultiple(2); _constructor = new ConstructorOnCustomType(_declaringType, _attributes, _parameters); }
public void SetUp() { _declaringType = CustomTypeObjectMother.Create(); _name = "Method"; _attributes = (MethodAttributes)7; _typeArguments = new[] { ReflectionObjectMother.GetSomeType() }; _returnType = ReflectionObjectMother.GetSomeType(); _parameters = ParameterDeclarationObjectMother.CreateMultiple(2); _method = new MethodOnCustomType(_declaringType, _name, _attributes, _typeArguments, _returnType, _parameters); }
public void SetUp() { _declaringType = MutableTypeObjectMother.Create(); _name = "Property"; _attributes = (PropertyAttributes)7; _type = ReflectionObjectMother.GetSomeType(); _indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2); _getMethod = MutableMethodInfoObjectMother.Create(returnType: _type, parameters: _indexParameters); _setMethod = MutableMethodInfoObjectMother.Create(parameters: _indexParameters.Concat(new[] { ParameterDeclarationObjectMother.Create(_type) })); _property = new MutablePropertyInfo(_declaringType, _name, _attributes, _getMethod, _setMethod); }
private void CopyMethodBodyAndCheckResult(BodyContextBase context, MethodAttributes methodAttributes) { var parameter = ParameterDeclarationObjectMother.CreateMultiple(2); var constantBodyPart = ExpressionTreeObjectMother.GetSomeExpression(typeof(int)); var body = Expression.Block(parameter[0].Expression, parameter[1].Expression, constantBodyPart); var methodToCopy = MutableMethodInfoObjectMother.Create( declaringType: _mutableType, attributes: methodAttributes, returnType: typeof(int), parameters: parameter, body: body); var arguments = parameter.Select(p => ExpressionTreeObjectMother.GetSomeExpression(p.Type)).ToArray(); var result = context.CopyMethodBody(methodToCopy, arguments.AsOneTime()); var expectedBody = Expression.Block(arguments[0], arguments[1], constantBodyPart); ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, result); }
public void CreateMethod_Static() { var name = "StaticMethod"; var attributes = MethodAttributes.Static; var returnType = ReflectionObjectMother.GetSomeType(); var parameterDeclarations = ParameterDeclarationObjectMother.CreateMultiple(2); Func <MethodBodyCreationContext, Expression> bodyProvider = ctx => { Assert.That(ctx.IsStatic, Is.True); return(ExpressionTreeObjectMother.GetSomeExpression(returnType)); }; var method = CallCreateMethod(_mutableType, name, attributes, returnType, parameterDeclarations, bodyProvider); Assert.That(method.IsStatic, Is.True); }
public void AddConstructor() { var attributes = (MethodAttributes)7; var parameters = ParameterDeclarationObjectMother.CreateMultiple(2); Func <ConstructorBodyCreationContext, Expression> bodyProvider = ctx => null; var fakeConstructor = MutableConstructorInfoObjectMother.Create(); _mutableMemberFactoryMock .Expect(mock => mock.CreateConstructor(_mutableType, attributes, parameters, bodyProvider)) .Return(fakeConstructor); var result = _mutableType.AddConstructor(attributes, parameters, bodyProvider); _mutableMemberFactoryMock.VerifyAllExpectations(); Assert.That(result, Is.SameAs(fakeConstructor)); Assert.That(_mutableType.AddedConstructors, Is.EqualTo(new[] { result })); }
public void CreateMethod_ThrowsIfAlreadyExists() { Func <MethodBodyCreationContext, Expression> bodyProvider = ctx => Expression.Empty(); var method = _mutableType.AddMethod("Method", 0, typeof(void), ParameterDeclarationObjectMother.CreateMultiple(2), bodyProvider); var methodParameters = method.GetParameters().Select(p => new ParameterDeclaration(p.ParameterType, p.Name, p.Attributes)); Assert.That(() => CallCreateMethod(_mutableType, "OtherName", 0, method.ReturnType, methodParameters, bodyProvider), Throws.Nothing); Assert.That( () => CallCreateMethod(_mutableType, method.Name, 0, typeof(int), methodParameters, ctx => Expression.Constant(7)), Throws.Nothing); Assert.That( () => CallCreateMethod(_mutableType, method.Name, 0, method.ReturnType, ParameterDeclarationObjectMother.CreateMultiple(3), bodyProvider), Throws.Nothing); Assert.That( () => CallCreateMethod(_mutableType, method.Name, 0, method.ReturnType, methodParameters, bodyProvider), Throws.InvalidOperationException.With.Message.EqualTo("Method with equal name and signature already exists.")); }
public void CreateConstructor_ThrowsIfAlreadyExists() { _mutableType.AddConstructor(parameters: ParameterDeclaration.None); Func <ConstructorBodyCreationContext, Expression> bodyProvider = ctx => Expression.Empty(); Assert.That( () => _factory.CreateConstructor(_mutableType, 0, ParameterDeclarationObjectMother.CreateMultiple(2), bodyProvider), Throws.Nothing); Assert.That( () => _factory.CreateConstructor(_mutableType, MethodAttributes.Static, ParameterDeclaration.None, bodyProvider), Throws.Nothing); Assert.That( () => _factory.CreateConstructor(_mutableType, 0, ParameterDeclaration.None, bodyProvider), Throws.InvalidOperationException.With.Message.EqualTo("Constructor with equal signature already exists.")); }
public void CreateProperty_Accessors_ThrowsIfAlreadyExists() { var returnType = ReflectionObjectMother.GetSomeType(); var getMethod = MutableMethodInfoObjectMother.Create(declaringType: _mutableType, returnType: returnType); var property = _mutableType.AddProperty2("Property", getMethod: getMethod); Assert.That(() => _factory.CreateProperty(_mutableType, "OtherName", 0, getMethod, null), Throws.Nothing); var differentPropertyType = ReflectionObjectMother.GetSomeOtherType(); var getMethod2 = MutableMethodInfoObjectMother.Create(declaringType: _mutableType, returnType: differentPropertyType); Assert.That(() => _factory.CreateProperty(_mutableType, property.Name, 0, getMethod2, null), Throws.Nothing); var differentIndexParameters = ParameterDeclarationObjectMother.CreateMultiple(2); var getMethod3 = MutableMethodInfoObjectMother.Create(declaringType: _mutableType, returnType: returnType, parameters: differentIndexParameters); Assert.That(() => _factory.CreateProperty(_mutableType, property.Name, 0, getMethod3, null), Throws.Nothing); Assert.That( () => _factory.CreateProperty(_mutableType, property.Name, 0, getMethod, null), Throws.InvalidOperationException.With.Message.EqualTo("Property with equal name and signature already exists.")); }
public void Initialization() { var declaringType = MutableTypeObjectMother.Create(); var name = "abc"; var attributes = (MethodAttributes)7 | MethodAttributes.Virtual; var returnType = ReflectionObjectMother.GetSomeType(); var parameters = ParameterDeclarationObjectMother.CreateMultiple(2); var baseMethod = ReflectionObjectMother.GetSomeVirtualMethod(); var body = ExpressionTreeObjectMother.GetSomeExpression(returnType); var method = new MutableMethodInfo( declaringType, name, attributes, new MutableGenericParameter[0], returnType, parameters.AsOneTime(), baseMethod, body); Assert.That(method.DeclaringType, Is.SameAs(declaringType)); Assert.That(method.MutableDeclaringType, Is.SameAs(declaringType)); Assert.That(method.Name, Is.EqualTo(name)); Assert.That(method.Attributes, Is.EqualTo(attributes)); Assert.That(method.IsGenericMethod, Is.False); CustomParameterInfoTest.CheckParameter(method.ReturnParameter, method, -1, null, returnType, ParameterAttributes.None); Assert.That(method.MutableReturnParameter, Is.SameAs(method.ReturnParameter)); var actualParameters = method.GetParameters(); Assert.That(actualParameters, Has.Length.EqualTo(2)); CustomParameterInfoTest.CheckParameter(actualParameters[0], method, 0, parameters[0].Name, parameters[0].Type, parameters[0].Attributes); CustomParameterInfoTest.CheckParameter(actualParameters[1], method, 1, parameters[1].Name, parameters[1].Type, parameters[1].Attributes); Assert.That(method.MutableParameters, Is.EqualTo(actualParameters)); var paramExpressions = method.ParameterExpressions; Assert.That(paramExpressions, Has.Count.EqualTo(2)); Assert.That(paramExpressions[0], Has.Property("Name").EqualTo(parameters[0].Name).And.Property("Type").SameAs(parameters[0].Type)); Assert.That(paramExpressions[1], Has.Property("Name").EqualTo(parameters[1].Name).And.Property("Type").SameAs(parameters[1].Type)); Assert.That(method.BaseMethod, Is.SameAs(baseMethod)); Assert.That(method.Body, Is.SameAs(body)); }
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)); }
public void CreateProperty_Providers() { var name = "Property"; var propertyType = ReflectionObjectMother.GetSomeType(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2).ToList(); var accessorAttributes = (MethodAttributes)7; var setterParameters = indexParameters.Concat(new[] { ParameterDeclarationObjectMother.Create(propertyType, "value") }).ToList(); Func <MethodBodyCreationContext, Expression> getBodyProvider = ctx => null; Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => null; var fakeGetMethod = MutableMethodInfoObjectMother.Create(returnType: propertyType, parameters: indexParameters); var fakeSetMethod = MutableMethodInfoObjectMother.Create(parameters: setterParameters); _methodFactoryMock .Setup( mock => mock.CreateMethod( _mutableType, "get_Property", accessorAttributes | MethodAttributes.SpecialName, GenericParameterDeclaration.None, It.IsAny <Func <GenericParameterContext, Type> >(), It.IsAny <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >(), getBodyProvider)) .Callback( ( MutableType declaringType, string nameArgument, MethodAttributes attributes, IEnumerable <GenericParameterDeclaration> genericParameters, Func <GenericParameterContext, Type> returnTypeProvider, Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > parameterProvider, Func <MethodBodyCreationContext, Expression> bodyProvider) => { var returnType = returnTypeProvider(null); Assert.That(returnType, Is.SameAs(propertyType)); var parameters = parameterProvider(null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(indexParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(indexParameters.Select(p => p.Name))); }) .Returns(fakeGetMethod); _methodFactoryMock .Setup( mock => mock.CreateMethod( _mutableType, "set_Property", (accessorAttributes | MethodAttributes.SpecialName), GenericParameterDeclaration.None, It.IsAny <Func <GenericParameterContext, Type> >(), It.IsAny <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >(), setBodyProvider)) .Callback( ( MutableType declaringType, string nameArgument, MethodAttributes attributes, IEnumerable <GenericParameterDeclaration> genericParameters, Func <GenericParameterContext, Type> returnTypeProvider, Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > parameterProvider, Func <MethodBodyCreationContext, Expression> bodyProvider) => { var returnType = returnTypeProvider(null); Assert.That(returnType, Is.SameAs(typeof(void))); var parameters = parameterProvider(null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(setterParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(setterParameters.Select(p => p.Name))); }) .Returns(fakeSetMethod) .Verifiable(); var result = _factory.CreateProperty( _mutableType, name, propertyType, indexParameters.AsOneTime(), accessorAttributes, getBodyProvider, setBodyProvider); _methodFactoryMock.Verify(); Assert.That(result.DeclaringType, Is.SameAs(_mutableType)); Assert.That(result.Name, Is.EqualTo(name)); Assert.That(result.Attributes, Is.EqualTo(PropertyAttributes.None)); Assert.That(result.PropertyType, Is.SameAs(propertyType)); Assert.That(result.MutableGetMethod, Is.SameAs(fakeGetMethod)); Assert.That(result.MutableSetMethod, Is.SameAs(fakeSetMethod)); }
public void SetUp() { _constructor = MutableConstructorInfoObjectMother.Create(parameters: ParameterDeclarationObjectMother.CreateMultiple(2)); }
public void CreateConstructor_ThrowsIfStaticAndNonEmptyParameters() { _factory.CreateConstructor(_mutableType, MethodAttributes.Static, ParameterDeclarationObjectMother.CreateMultiple(1), ctx => null); }
public void CreateConstructor_ThrowsIfStaticAndNonEmptyParameters() { Assert.That( () => _factory.CreateConstructor(_mutableType, MethodAttributes.Static, ParameterDeclarationObjectMother.CreateMultiple(1), ctx => null), Throws.ArgumentException .With.ArgumentExceptionMessageEqualTo("A type initializer (static constructor) cannot have parameters.", "parameters")); }
public void CreateProperty_Providers() { var name = "Property"; var propertyType = ReflectionObjectMother.GetSomeType(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2).ToList(); var accessorAttributes = (MethodAttributes)7; var setterParameters = indexParameters.Concat(new[] { ParameterDeclarationObjectMother.Create(propertyType, "value") }).ToList(); Func <MethodBodyCreationContext, Expression> getBodyProvider = ctx => null; Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => null; var fakeGetMethod = MutableMethodInfoObjectMother.Create(returnType: propertyType, parameters: indexParameters); var fakeSetMethod = MutableMethodInfoObjectMother.Create(parameters: setterParameters); _methodFactoryMock .Expect( mock => mock.CreateMethod( Arg.Is(_mutableType), Arg.Is("get_Property"), Arg.Is(accessorAttributes | MethodAttributes.SpecialName), Arg.Is(GenericParameterDeclaration.None), Arg <Func <GenericParameterContext, Type> > .Is.Anything, Arg <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > > .Is.Anything, Arg.Is(getBodyProvider))) .WhenCalled( mi => { var returnType = mi.Arguments[4].As <Func <GenericParameterContext, Type> >() (null); Assert.That(returnType, Is.SameAs(propertyType)); var parameters = mi.Arguments[5].As <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >() (null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(indexParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(indexParameters.Select(p => p.Name))); }) .Return(fakeGetMethod); _methodFactoryMock .Expect( mock => mock.CreateMethod( Arg.Is(_mutableType), Arg.Is("set_Property"), Arg.Is(accessorAttributes | MethodAttributes.SpecialName), Arg.Is(GenericParameterDeclaration.None), Arg <Func <GenericParameterContext, Type> > .Is.Anything, Arg <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > > .Is.Anything, Arg.Is(setBodyProvider))) .WhenCalled( mi => { var returnType = mi.Arguments[4].As <Func <GenericParameterContext, Type> >() (null); Assert.That(returnType, Is.SameAs(typeof(void))); var parameters = mi.Arguments[5].As <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >() (null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(setterParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(setterParameters.Select(p => p.Name))); }) .Return(fakeSetMethod); var result = _factory.CreateProperty( _mutableType, name, propertyType, indexParameters.AsOneTime(), accessorAttributes, getBodyProvider, setBodyProvider); _methodFactoryMock.VerifyAllExpectations(); Assert.That(result.DeclaringType, Is.SameAs(_mutableType)); Assert.That(result.Name, Is.EqualTo(name)); Assert.That(result.Attributes, Is.EqualTo(PropertyAttributes.None)); Assert.That(result.PropertyType, Is.SameAs(propertyType)); Assert.That(result.MutableGetMethod, Is.SameAs(fakeGetMethod)); Assert.That(result.MutableSetMethod, Is.SameAs(fakeSetMethod)); }