Esempio n. 1
0
        public void WireConstructorWithInitialization()
        {
            var counter    = MutableFieldInfoObjectMother.Create(_mutableType, type: typeof(int));
            var initMethod = MutableMethodInfoObjectMother.Create(
                _mutableType, parameters: new[] { ParameterDeclarationObjectMother.Create(typeof(InitializationSemantics)) });
            var initializationMembers = Tuple.Create <FieldInfo, MethodInfo> (counter, initMethod);
            var constructor           = MutableConstructorInfoObjectMother.Create(_mutableType);
            var oldBody = constructor.Body;

            _proxySerializationEnablerMock.Expect(mock => mock.IsDeserializationConstructor(constructor)).Return(false);

            _builder.WireConstructorWithInitialization(constructor, initializationMembers, _proxySerializationEnablerMock);

            _proxySerializationEnablerMock.VerifyAllExpectations();
            var expectedBody = Expression.Block(
                Expression.Assign(
                    Expression.Field(new ThisExpression(_mutableType), counter),
                    Expression.Add(Expression.Field(new ThisExpression(_mutableType), counter), Expression.Constant(1))),
                oldBody,
                Expression.Assign(
                    Expression.Field(new ThisExpression(_mutableType), counter),
                    Expression.Subtract(Expression.Field(new ThisExpression(_mutableType), counter), Expression.Constant(1))),
                Expression.IfThen(
                    Expression.Equal(Expression.Field(new ThisExpression(_mutableType), counter), Expression.Constant(0)),
                    Expression.Call(new ThisExpression(_mutableType), initMethod, Expression.Constant(InitializationSemantics.Construction))));

            ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, constructor.Body);
        }
        public void DelegatingMembers()
        {
            var emittableOperandProvider = MockRepository.GenerateStub <IEmittableOperandProvider>();
            var mutableConstructor       = MutableConstructorInfoObjectMother.Create();

            var helper = new DecoratorTestHelper <IConstructorBuilder> (_decorator, _innerMock);

            helper.CheckDelegation(d => d.RegisterWith(emittableOperandProvider, mutableConstructor));
        }
Esempio n. 3
0
        public void WireConstructorWithInitialization_Null()
        {
            var constructor = MutableConstructorInfoObjectMother.Create();
            var oldBody     = constructor.Body;

            _builder.WireConstructorWithInitialization(constructor, initializationMembers: null, proxySerializationEnabler: _proxySerializationEnablerMock);

            _proxySerializationEnablerMock.AssertWasNotCalled(mock => mock.IsDeserializationConstructor(constructor));
            Assert.That(constructor.Body, Is.SameAs(oldBody));
        }
        public void ToDebugString()
        {
            // Note: ToDebugString is defined in CustomConstructorInfo base class.
            var declaringType = MutableTypeObjectMother.Create(name: "Abc");
            var ctor          = MutableConstructorInfoObjectMother.Create(declaringType, parameters: new[] { new ParameterDeclaration(typeof(int), "p1") });

            var expected = "MutableConstructor = \"Void .ctor(Int32)\", DeclaringType = \"Abc\"";

            Assert.That(ctor.ToDebugString(), Is.EqualTo(expected));
        }
        public void SetBody_Static()
        {
            var typeInitializer = MutableConstructorInfoObjectMother.Create(attributes: MethodAttributes.Static);
            Func <ConstructorBodyModificationContext, Expression> bodyProvider = ctx =>
            {
                Assert.That(ctx.IsStatic, Is.True);
                return(Expression.Empty());
            };

            typeInitializer.SetBody(bodyProvider);
        }
Esempio n. 6
0
        public void AddConstructor_Static()
        {
            var ctor = MutableConstructorInfoObjectMother.Create(attributes: MethodAttributes.Static);
            var constructorBuilderStub = MockRepository.GenerateStub <IConstructorBuilder> ();
            var attributes             = MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;

            _typeBuilderMock.Expect(mock => mock.DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes)).Return(constructorBuilderStub);

            _emitter.AddConstructor(_context, ctor);

            _typeBuilderMock.VerifyAllExpectations();
        }
Esempio n. 7
0
        public void AddConstructor_Static()
        {
            Assert.That(_mutableType.MutableTypeInitializer, Is.Null);
            var typeInitializerFake = MutableConstructorInfoObjectMother.Create(attributes: MethodAttributes.Static);

            _mutableMemberFactoryMock.Stub(stub => stub.CreateConstructor(null, 0, null, null)).IgnoreArguments().Return(typeInitializerFake);

            var result = _mutableType.AddConstructor(0, ParameterDeclaration.None, ctx => Expression.Empty());

            Assert.That(result, Is.SameAs(typeInitializerFake));
            Assert.That(_mutableType.AddedConstructors, Is.Empty);
            Assert.That(_mutableType.MutableTypeInitializer, Is.SameAs(typeInitializerFake));
        }
Esempio n. 8
0
        public void WireConstructorWithInitialization_DeserializationConstructor()
        {
            var constructor           = MutableConstructorInfoObjectMother.Create();
            var oldBody               = constructor.Body;
            var initializationMembers = new Tuple <FieldInfo, MethodInfo> (null, null);

            _proxySerializationEnablerMock.Expect(x => x.IsDeserializationConstructor(constructor)).Return(true);

            _builder.WireConstructorWithInitialization(constructor, initializationMembers, _proxySerializationEnablerMock);

            _proxySerializationEnablerMock.VerifyAllExpectations();
            Assert.That(constructor.Body, Is.SameAs(oldBody));
        }
Esempio n. 9
0
        public void SetUp()
        {
            _delegateProviderMock = new Mock <IDelegateProvider> (MockBehavior.Strict);

            _provider = new EmittableOperandProvider(_delegateProviderMock.Object);

            _mutableType             = MutableTypeObjectMother.Create();
            _mutableGenericParameter = MutableGenericParameterObjectMother.Create();
            _mutableField            = MutableFieldInfoObjectMother.Create();
            _mutableConstructor      = MutableConstructorInfoObjectMother.Create();
            _mutableMethod           = MutableMethodInfoObjectMother.Create();

            _emittableType = ReflectionObjectMother.GetSomeType();
        }
Esempio n. 10
0
        public void AddConstructor_Static()
        {
            var ctor = MutableConstructorInfoObjectMother.Create(attributes: MethodAttributes.Static);
            var constructorBuilderStub = new Mock <IConstructorBuilder>();
            var attributes             = MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;

            _typeBuilderMock
            .Setup(mock => mock.DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes))
            .Returns(constructorBuilderStub.Object)
            .Verifiable();

            _emitter.AddConstructor(_context, ctor);

            _typeBuilderMock.Verify();
        }
Esempio n. 11
0
        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 }));
        }
Esempio n. 12
0
        public void AddTypeInitializer()
        {
            Assert.That(_mutableType.MutableTypeInitializer, Is.Null);
            Func <ConstructorBodyCreationContext, Expression> bodyProvider = ctx => null;
            var typeInitializerFake = MutableConstructorInfoObjectMother.Create(attributes: MethodAttributes.Static);

            _mutableMemberFactoryMock
            .Setup(mock => mock.CreateConstructor(_mutableType, MethodAttributes.Private | MethodAttributes.Static, ParameterDeclaration.None, bodyProvider))
            .Returns(typeInitializerFake)
            .Verifiable();

            var result = _mutableType.AddTypeInitializer(bodyProvider);

            Assert.That(result, Is.SameAs(typeInitializerFake));
            Assert.That(_mutableType.AddedConstructors, Is.Empty);
            Assert.That(_mutableType.MutableTypeInitializer, Is.SameAs(typeInitializerFake));
        }
Esempio n. 13
0
        public void AddConstructor_Static()
        {
            Assert.That(_mutableType.MutableTypeInitializer, Is.Null);
            var typeInitializerFake = MutableConstructorInfoObjectMother.Create(attributes: MethodAttributes.Static);

            _mutableMemberFactoryMock
            .Setup(
                stub => stub.CreateConstructor(
                    It.IsAny <MutableType>(),
                    It.IsAny <MethodAttributes>(),
                    It.IsAny <IEnumerable <ParameterDeclaration> >(),
                    It.IsAny <Func <ConstructorBodyCreationContext, Expression> >())).Returns(typeInitializerFake);

            var result = _mutableType.AddConstructor(0, ParameterDeclaration.None, ctx => Expression.Empty());

            Assert.That(result, Is.SameAs(typeInitializerFake));
            Assert.That(_mutableType.AddedConstructors, Is.Empty);
            Assert.That(_mutableType.MutableTypeInitializer, Is.SameAs(typeInitializerFake));
        }
Esempio n. 14
0
        public void DelegatingMembers()
        {
            var mappedType             = MutableTypeObjectMother.Create();
            var mappedGenericParameter = MutableGenericParameterObjectMother.Create();
            var mappedField            = MutableFieldInfoObjectMother.Create();
            var mappedConstructor      = MutableConstructorInfoObjectMother.Create();
            var mappedMethod           = MutableMethodInfoObjectMother.Create();

            var emittableType             = ReflectionObjectMother.GetSomeType();
            var emittableGenericParameter = ReflectionObjectMother.GetSomeGenericParameter();
            var emittableField            = ReflectionObjectMother.GetSomeField();
            var emittableConstructor      = ReflectionObjectMother.GetSomeConstructor();
            var emittableMethod           = ReflectionObjectMother.GetSomeMethod();

            var helper = new DecoratorTestHelper <IEmittableOperandProvider> (_decorator, _innerMock);

            helper.CheckDelegation(d => d.AddMapping(mappedType, emittableType));
            helper.CheckDelegation(d => d.AddMapping(mappedGenericParameter, emittableGenericParameter));
            helper.CheckDelegation(d => d.AddMapping(mappedField, emittableField));
            helper.CheckDelegation(d => d.AddMapping(mappedConstructor, emittableConstructor));
            helper.CheckDelegation(d => d.AddMapping(mappedMethod, emittableMethod));
        }
Esempio n. 15
0
        public void AddConstructor()
        {
            var constructor = MutableConstructorInfoObjectMother.Create(
                parameters: new[]
            {
                new ParameterDeclaration(typeof(string), "p1", ParameterAttributes.In),
                new ParameterDeclaration(typeof(int).MakeByRefType(), "p2", ParameterAttributes.Out)
            });
            var expectedAttributes         = constructor.Attributes;
            var expectedParameterTypes     = new[] { typeof(string), typeof(int).MakeByRefType() };
            var expectedCallingConventions = CallingConventions.Standard | CallingConventions.HasThis;

            var constructorBuilderMock = new Mock <IConstructorBuilder>();

            _typeBuilderMock
            .Setup(mock => mock.DefineConstructor(expectedAttributes, expectedCallingConventions, expectedParameterTypes))
            .Returns(constructorBuilderMock.Object)
            .Verifiable();
            constructorBuilderMock.Setup(mock => mock.RegisterWith(_emittableOperandProviderMock.Object, constructor)).Verifiable();

            SetupDefineCustomAttribute(constructorBuilderMock, constructor);
            var parameterBuilderMock = SetupDefineParameter(constructorBuilderMock, 1, "p1", ParameterAttributes.In);

            SetupDefineCustomAttribute(parameterBuilderMock, constructor.MutableParameters[0]);
            SetupDefineParameter(constructorBuilderMock, 2, "p2", ParameterAttributes.Out);

            Assert.That(_context.PostDeclarationsActionManager.Actions, Is.Empty);

            _emitter.AddConstructor(_context, constructor);

            _typeBuilderMock.Verify();
            constructorBuilderMock.Verify();
            parameterBuilderMock.Verify();

            Assert.That(_context.PostDeclarationsActionManager.Actions.Count(), Is.EqualTo(1));
            CheckBodyBuildAction(_context.PostDeclarationsActionManager.Actions.Single(), constructorBuilderMock, constructor);
        }
 public void SetUp()
 {
     _constructor = MutableConstructorInfoObjectMother.Create(parameters: ParameterDeclarationObjectMother.CreateMultiple(2));
 }