public void GetAllInterfaces() { var baseInterfaces = typeof(DomainType).GetInterfaces(); Assert.That(baseInterfaces, Is.Not.Empty); var addedInterface = ReflectionObjectMother.GetSomeInterfaceType(); _mutableType.AddInterface(addedInterface); Assert.That(_mutableType.GetAllInterfaces(), Is.EquivalentTo(new[] { addedInterface }.Concat(baseInterfaces))); }
public void AddInterface() { var baseInterface = typeof(DomainType).GetInterfaces().Single(); var addedInterface = ReflectionObjectMother.GetSomeInterfaceType(); _mutableType.AddInterface(addedInterface); Assert.That(_mutableType.AddedInterfaces, Is.EqualTo(new[] { addedInterface })); Assert.That(_mutableType.GetInterfaces(), Is.EqualTo(new[] { addedInterface, baseInterface })); _mutableType.AddInterface(baseInterface); // Base interface can be re-implemented. Assert.That(_mutableType.AddedInterfaces, Is.EqualTo(new[] { addedInterface, baseInterface })); Assert.That(_mutableType.GetInterfaces(), Is.EqualTo(new[] { addedInterface, baseInterface })); }
public void SetInterfaceConstraints() { var interfaceConstraint = ReflectionObjectMother.GetSomeInterfaceType(); var emittableInterfaceConstraint = ReflectionObjectMother.GetSomeOtherInterfaceType(); _operandProvider.Setup(mock => mock.GetEmittableType(interfaceConstraint)).Returns(emittableInterfaceConstraint).Verifiable(); _innerMock.Setup(mock => mock.SetInterfaceConstraints(new[] { emittableInterfaceConstraint })).Verifiable(); _decorator.SetInterfaceConstraints(new[] { interfaceConstraint }); _operandProvider.Verify(); _innerMock.Verify(); }
public void AddInterfaceImplementation() { var interfaceType = ReflectionObjectMother.GetSomeInterfaceType(); var emittableType = ReflectionObjectMother.GetSomeOtherType(); _operandProvider.Expect(mock => mock.GetEmittableType(interfaceType)).Return(emittableType); _innerMock.Expect(mock => mock.AddInterfaceImplementation(emittableType)); _decorator.AddInterfaceImplementation(interfaceType); _operandProvider.VerifyAllExpectations(); _innerMock.VerifyAllExpectations(); }
public void SetUp() { _position = 7; _name = "_parameter"; _namespace = "namespace"; _genericParameterAttributes = (GenericParameterAttributes)7; _parameter = new MutableGenericParameter(_position, _name, _namespace, _genericParameterAttributes); _baseTypeConstraint = typeof(DomainType); _interfaceConstraint = ReflectionObjectMother.GetSomeInterfaceType(); _constrainedParameter = MutableGenericParameterObjectMother.Create(constraints: new[] { _baseTypeConstraint, _interfaceConstraint }.AsOneTime()); }
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); }
public static void AddInterface(this MutableType mutableType, Type interfaceType = null) { interfaceType = interfaceType ?? ReflectionObjectMother.GetSomeInterfaceType(); mutableType.AddInterface(interfaceType); }