예제 #1
0
        public void Initialize_WithTypeOnly()
        {
            var type = ReflectionObjectMother.GetSomeClassType();

            var result = new TypeAssemblyResult(type);

            Assert.That(result.Type, Is.SameAs(type));
            Assert.That(result.AdditionalTypes, Is.Empty);
        }
예제 #2
0
        public void Initialize_WithTypeAndAdditionalTypes()
        {
            var type            = ReflectionObjectMother.GetSomeClassType();
            var additionalTypes = MockRepository.GenerateStrictMock <IReadOnlyDictionary <object, Type> >();

            var result = new TypeAssemblyResult(type, additionalTypes);

            Assert.That(result.Type, Is.SameAs(type));
            Assert.That(result.AdditionalTypes, Is.SameAs(additionalTypes));
        }
예제 #3
0
        public void Initialize_WithTypeAndAdditionalTypes()
        {
            var type            = ReflectionObjectMother.GetSomeClassType();
            var additionalTypes = new Mock <IReadOnlyDictionary <object, Type> > (MockBehavior.Strict);

            var result = new TypeAssemblyResult(type, additionalTypes.Object);

            Assert.That(result.Type, Is.SameAs(type));
            Assert.That(result.AdditionalTypes, Is.SameAs(additionalTypes.Object));
        }
예제 #4
0
        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);
        }