public void AddMethod() { var name = "GenericMethod"; var attributes = (MethodAttributes)7; var genericParameterDeclarations = new[] { GenericParameterDeclarationObjectMother.Create() }; Func <GenericParameterContext, Type> returnTypeProvider = ctx => null; Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > parameterProvider = ctx => null; Func <MethodBodyCreationContext, Expression> bodyProvider = ctx => null; var fakeMethod = MutableMethodInfoObjectMother.Create(); _mutableMemberFactoryMock .Setup(mock => mock.CreateMethod(_mutableType, name, attributes, genericParameterDeclarations, returnTypeProvider, parameterProvider, bodyProvider)) .Returns(fakeMethod) .Verifiable(); var result = _mutableType.AddMethod(name, attributes, genericParameterDeclarations, returnTypeProvider, parameterProvider, bodyProvider); _mutableMemberFactoryMock.Verify(); Assert.That(result, Is.SameAs(fakeMethod)); Assert.That(_mutableType.AddedMethods, Is.EqualTo(new[] { result })); }
public void CreateMethod() { var name = "Method"; var attributes = MethodAttributes.Public; var baseConstraint = ReflectionObjectMother.GetSomeClassType(); var interfaceConstraint = ReflectionObjectMother.GetSomeInterfaceType(); GenericParameterContext genericParameterContext = null; Type firstGenericParameter = null; Func <GenericParameterContext, IEnumerable <Type> > constraintProvider = ctx => { genericParameterContext = ctx; Assert.That(ctx.GenericParameters, Has.Count.EqualTo(2)); Assert.That(ctx.GenericParameters[1].GenericParameterPosition, Is.EqualTo(1)); firstGenericParameter = ctx.GenericParameters[0]; Assert.That(firstGenericParameter.DeclaringMethod, Is.Null); Assert.That(firstGenericParameter.GenericParameterPosition, Is.EqualTo(0)); Assert.That(firstGenericParameter.Name, Is.EqualTo("T1")); Assert.That(firstGenericParameter.Namespace, Is.EqualTo(_mutableType.Namespace)); Assert.That(firstGenericParameter.GenericParameterAttributes, Is.EqualTo(GenericParameterAttributes.Covariant)); return(new[] { baseConstraint, interfaceConstraint }.AsOneTime()); }; var genericParameters = new[] { GenericParameterDeclarationObjectMother.Create("T1", GenericParameterAttributes.Covariant, constraintProvider), GenericParameterDeclarationObjectMother.Create() }; var returnType = typeof(IComparable); Func <GenericParameterContext, Type> returnTypeProvider = ctx => { Assert.That(ctx, Is.Not.Null.And.SameAs(genericParameterContext)); return(returnType); }; var parameter = ParameterDeclarationObjectMother.Create(name: "paramName"); Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > parameterProvider = ctx => { Assert.That(ctx, Is.Not.Null.And.SameAs(genericParameterContext)); return(new[] { parameter }.AsOneTime()); }; var fakeBody = ExpressionTreeObjectMother.GetSomeExpression(typeof(int)); Func <MethodBodyCreationContext, Expression> bodyProvider = ctx => { Assert.That(ctx.This.Type, Is.SameAs(_mutableType)); Assert.That(ctx.Parameters.Single().Name, Is.EqualTo("paramName")); Assert.That(ctx.IsStatic, Is.False); Assert.That(ctx.GenericParameters, Is.EqualTo(genericParameterContext.GenericParameters)); Assert.That(ctx.ReturnType, Is.SameAs(returnType)); Assert.That(ctx.HasBaseMethod, Is.False); return(fakeBody); }; var method = _factory.CreateMethod( _mutableType, name, attributes, genericParameters.AsOneTime(), returnTypeProvider, parameterProvider, bodyProvider); Assert.That(method.DeclaringType, Is.SameAs(_mutableType)); Assert.That(method.Name, Is.EqualTo(name)); Assert.That(method.Attributes, Is.EqualTo(attributes)); Assert.That(method.ReturnType, Is.SameAs(returnType)); Assert.That(method.BaseMethod, Is.Null); var returnParameter = method.ReturnParameter; Assertion.IsNotNull(returnParameter); Assert.That(returnParameter.Position, Is.EqualTo(-1)); Assert.That(returnParameter.Name, Is.Null); Assert.That(returnParameter.ParameterType, Is.SameAs(returnType)); Assert.That(returnParameter.Attributes, Is.EqualTo(ParameterAttributes.None)); Assert.That(method.GetGenericArguments(), Has.Length.EqualTo(2)); var actualFirstGenericParameter = method.GetGenericArguments()[0]; Assert.That(actualFirstGenericParameter, Is.SameAs(firstGenericParameter)); Assert.That(actualFirstGenericParameter.DeclaringMethod, Is.SameAs(method)); Assert.That(actualFirstGenericParameter.GetGenericParameterConstraints(), Is.EqualTo(new[] { baseConstraint, interfaceConstraint })); Assert.That(method.GetParameters().Single().Name, Is.EqualTo(parameter.Name)); var expectedBody = Expression.Convert(fakeBody, returnType); ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, method.Body); }