Пример #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);
        }
Пример #2
0
        public void DelegatingMembers()
        {
            var emittableOperandProvider = MockRepository.GenerateStub <IEmittableOperandProvider>();
            var mutableField             = MutableFieldInfoObjectMother.Create();

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

            helper.CheckDelegation(d => d.RegisterWith(emittableOperandProvider, mutableField));
        }
        public void MakeSerializable_SerializableInterfaceType_SerializedFields()
        {
            var       dummyField    = _serializableInterfaceProxy.AddField("input field", FieldAttributes.Private, typeof(int));
            var       fakeFieldType = ReflectionObjectMother.GetSomeType();
            FieldInfo fakeField     = MutableFieldInfoObjectMother.Create(_serializableInterfaceProxy, type: fakeFieldType);
            var       fakeMapping   = new[] { Tuple.Create("fake key", fakeField) };

            _serializableFieldFinderMock
            .Setup(mock => mock.GetSerializableFieldMapping(It.Is <IEnumerable <FieldInfo> > (fields => fields.SequenceEqual(new[] { dummyField }))))
            .Returns(fakeMapping)
            .Verifiable();

            _enabler.MakeSerializable(_serializableInterfaceProxy, _someInitializationMethod);

            _serializableFieldFinderMock.Verify();
            Assert.That(_serializableInterfaceProxy.AddedInterfaces, Is.Empty);
            Assert.That(_serializableInterfaceProxy.AddedMethods, Has.Count.EqualTo(1));

            var baseMethod =
                NormalizingMemberInfoFromExpressionUtility.GetMethod((SerializableInterfaceType obj) => obj.GetObjectData(null, new StreamingContext()));
            var method             = _serializableInterfaceProxy.AddedMethods.Single();
            var expectedMethodBody = Expression.Block(
                typeof(void),
                Expression.Call(
                    new ThisExpression(_serializableInterfaceProxy),
                    NonVirtualCallMethodInfoAdapter.Adapt(baseMethod),
                    method.ParameterExpressions.Cast <Expression>()),
                Expression.Call(
                    method.ParameterExpressions[0],
                    "AddValue",
                    Type.EmptyTypes,
                    Expression.Constant("fake key"),
                    Expression.Field(new ThisExpression(_serializableInterfaceProxy), fakeField)));

            ExpressionTreeComparer.CheckAreEqualTrees(expectedMethodBody, method.Body);

            var baseCtor         = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new SerializableInterfaceType(null, new StreamingContext()));
            var ctor             = _serializableInterfaceProxy.AddedConstructors.Single();
            var getValueMethod   = NormalizingMemberInfoFromExpressionUtility.GetMethod((SerializationInfo obj) => obj.GetValue("", null));
            var expectedCtorBody = Expression.Block(
                typeof(void),
                Expression.Call(
                    new ThisExpression(_serializableInterfaceProxy),
                    NonVirtualCallMethodInfoAdapter.Adapt(baseCtor),
                    ctor.ParameterExpressions.Cast <Expression>()),
                Expression.Assign(
                    Expression.Field(new ThisExpression(_serializableInterfaceProxy), fakeField),
                    Expression.Convert(
                        Expression.Call(ctor.ParameterExpressions[0], getValueMethod, Expression.Constant("fake key"), Expression.Constant(fakeFieldType)),
                        fakeFieldType)));

            ExpressionTreeComparer.CheckAreEqualTrees(expectedCtorBody, ctor.Body);
        }
Пример #4
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();
        }
Пример #5
0
        public void AddField()
        {
            var name       = "_newField";
            var type       = ReflectionObjectMother.GetSomeType();
            var attributes = (FieldAttributes)7;
            var fakeField  = MutableFieldInfoObjectMother.Create(_mutableType);

            _mutableMemberFactoryMock.Expect(mock => mock.CreateField(_mutableType, name, type, attributes)).Return(fakeField);

            var result = _mutableType.AddField(name, attributes, type);

            _mutableMemberFactoryMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeField));
            Assert.That(_mutableType.AddedFields, Is.EqualTo(new[] { result }));
        }
Пример #6
0
        public void AddField()
        {
            var field            = MutableFieldInfoObjectMother.Create();
            var fieldBuilderMock = MockRepository.GenerateStrictMock <IFieldBuilder>();

            _typeBuilderMock
            .Expect(mock => mock.DefineField(field.Name, field.FieldType, field.Attributes))
            .Return(fieldBuilderMock);
            fieldBuilderMock.Expect(mock => mock.RegisterWith(_emittableOperandProviderMock, field));
            SetupDefineCustomAttribute(fieldBuilderMock, field);

            _emitter.AddField(_context, field);

            _typeBuilderMock.VerifyAllExpectations();
            fieldBuilderMock.VerifyAllExpectations();
        }
Пример #7
0
        public void AddField()
        {
            var field            = MutableFieldInfoObjectMother.Create();
            var fieldBuilderMock = new Mock <IFieldBuilder> (MockBehavior.Strict);

            _typeBuilderMock
            .Setup(mock => mock.DefineField(field.Name, field.FieldType, field.Attributes))
            .Returns(fieldBuilderMock.Object)
            .Verifiable();
            fieldBuilderMock.Setup(mock => mock.RegisterWith(_emittableOperandProviderMock.Object, field)).Verifiable();
            SetupDefineCustomAttribute(fieldBuilderMock, field);

            _emitter.AddField(_context, field);

            _typeBuilderMock.Verify();
            fieldBuilderMock.Verify();
        }
Пример #8
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));
        }
Пример #9
0
 private void StubFilterWithSerializedFields(MutableType declaringType)
 {
     _serializableFieldFinderMock
     .Stub(stub => stub.GetSerializableFieldMapping(Arg <IEnumerable <FieldInfo> > .Is.Anything))
     .Return(new[] { Tuple.Create <string, FieldInfo> ("someField", MutableFieldInfoObjectMother.Create(declaringType)) });
 }
Пример #10
0
 public void SetUp()
 {
     _field = MutableFieldInfoObjectMother.Create();
 }