Пример #1
0
        public void SetGenericParameterConstraints_ValueTypeBaseConstraint()
        {
            var message   = "A generic parameter cannot be constrained by a value type.";
            var paramName = "constraints";

            Assert.That(
                () => _parameter.SetGenericParameterConstraints(new[] { ReflectionObjectMother.GetSomeValueType() }),
                Throws.ArgumentException.With.ArgumentExceptionMessageEqualTo(message, paramName));
            Assert.That(
                () => _parameter.SetGenericParameterConstraints(new[] { typeof(ValueType) }),
                Throws.ArgumentException.With.ArgumentExceptionMessageEqualTo(message, paramName));
        }
Пример #2
0
        public void MakeTypePipeGenericType_MakesGenericTypeWithCustomTypeArgument()
        {
            var genericTypeDefinition = typeof(Dictionary <,>);
            var runtimeType           = ReflectionObjectMother.GetSomeType();
            var customType            = CustomTypeObjectMother.Create();

            var result = genericTypeDefinition.MakeTypePipeGenericType(runtimeType, customType);

            Assert.That(result.IsGenericType, Is.True);
            Assert.That(result.IsGenericTypeDefinition, Is.False);
            Assert.That(result.GetGenericArguments(), Is.EqualTo(new[] { runtimeType, customType }));
        }
Пример #3
0
        public void GetTypeIDForAssembledType()
        {
            var assembledType = ReflectionObjectMother.GetSomeType();
            var fakeTypeID    = AssembledTypeIDObjectMother.Create();

            _typeAssemblerMock.Expect(mock => mock.ExtractTypeID(assembledType)).Return(fakeTypeID);

            var result = _service.GetTypeIDForAssembledType(assembledType);

            _typeAssemblerMock.VerifyAllExpectations();
            Assert.That(result, Is.EqualTo(fakeTypeID));
        }
        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);
        }
Пример #5
0
        public void IsAssembledType()
        {
            var type       = ReflectionObjectMother.GetSomeType();
            var fakeResult = BooleanObjectMother.GetRandomBoolean();

            _typeAssemblerMock.Expect(mock => mock.IsAssembledType(type)).Return(fakeResult);

            var result = _service.IsAssembledType(type);

            _typeAssemblerMock.VerifyAllExpectations();
            Assert.That(result, Is.EqualTo(fakeResult));
        }
Пример #6
0
        public void GetRequestedType()
        {
            var assembledType     = ReflectionObjectMother.GetSomeType();
            var fakeRequestedType = ReflectionObjectMother.GetSomeOtherType();

            _typeAssemblerMock.Expect(mock => mock.GetRequestedType(assembledType)).Return(fakeRequestedType);

            var result = _service.GetRequestedType(assembledType);

            _typeAssemblerMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeRequestedType));
        }
Пример #7
0
        public void GetAssembledType_AssembledTypeID()
        {
            var typeID            = AssembledTypeIDObjectMother.Create();
            var fakeAssembledType = ReflectionObjectMother.GetSomeOtherType();

            _typeCacheMock.Expect(mock => mock.GetOrCreateType(Arg <AssembledTypeID> .Matches(id => id.Equals(typeID)))).Return(fakeAssembledType);

            var result = _service.GetAssembledType(typeID);

            _typeCacheMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeAssembledType));
        }
        public void Instantiate_CustomGenericTypeDefinition()
        {
            var typeParameter = ReflectionObjectMother.GetSomeGenericParameter();
            var customGenericTypeDefinition = CustomTypeObjectMother.Create(typeArguments: new[] { typeParameter });
            var instantiationInfo           = new TypeInstantiationInfo(customGenericTypeDefinition, new[] { _customType });

            var result = _context.Instantiate(instantiationInfo);

            Assert.That(result, Is.TypeOf <TypeInstantiation>());
            Assert.That(result.GetGenericTypeDefinition(), Is.SameAs(customGenericTypeDefinition));
            Assert.That(result.GetGenericArguments(), Is.EqualTo(new[] { _customType }));
        }
Пример #9
0
        public void GetFlattenedExpressionForSerialization_ProviderNotCalledForNull()
        {
            var requestedType = ReflectionObjectMother.GetSomeType();
            var typeID        = AssembledTypeIDObjectMother.Create(requestedType, new object[] { null });

            var result = _provider.GetAssembledTypeIDDataExpression(typeID);

            _identifierProviderMock.AssertWasNotCalled(mock => mock.GetFlatValueExpressionForSerialization(Arg <object> .Is.Anything));
            var expectedIdPartExpression = Expression.Constant(null, typeof(IFlatValue));

            CheckTypeIDDataExpression(result, requestedType, expectedIdPartExpression);
        }
Пример #10
0
        public void ComputeTypeID()
        {
            var requestedType = ReflectionObjectMother.GetSomeType();

            _identifierProviderMock.Stub(_ => _.GetID(requestedType)).Return("abc");

            var result = _provider.ComputeTypeID(requestedType);

            var expectedTypeID = new AssembledTypeID(requestedType, new object[] { "abc" });

            Assert.That(result, Is.EqualTo(expectedTypeID));
        }
Пример #11
0
        public void GetFlattenedExpressionForSerialization()
        {
            var requestedType    = ReflectionObjectMother.GetSomeType();
            var typeID           = AssembledTypeIDObjectMother.Create(requestedType, new object[] { "abc" });
            var idPartExpression = ExpressionTreeObjectMother.GetSomeExpression(typeof(IFlatValue));

            _identifierProviderMock.Stub(_ => _.GetFlatValueExpressionForSerialization("abc")).Return(idPartExpression);

            var result = _provider.GetAssembledTypeIDDataExpression(typeID);

            CheckTypeIDDataExpression(result, requestedType, idPartExpression);
        }
Пример #12
0
        public void Initialization()
        {
            var proxyType      = new LoadedProxy(typeof(int), typeof(string));
            var additionalType = ReflectionObjectMother.GetSomeOtherType();
            var state          = new ParticipantState();

            var context = new LoadedTypesContext(new[] { proxyType }.AsOneTime(), new[] { additionalType }.AsOneTime(), state);

            Assert.That(context.ProxyTypes, Is.EqualTo(new[] { proxyType }));
            Assert.That(context.AdditionalTypes, Is.EqualTo(new[] { additionalType }));
            Assert.That(context.ParticipantState, Is.SameAs(state));
        }
        public void Instantiate_CustomGenericMethodDefinition()
        {
            var typeParameter = ReflectionObjectMother.GetSomeGenericParameter();
            var customGenericMethodDefinition = CustomMethodInfoObjectMother.Create(typeArguments: new[] { typeParameter });
            var instantiationInfo             = new MethodInstantiationInfo(customGenericMethodDefinition, new[] { _runtimeType });

            var result = instantiationInfo.Instantiate();

            Assert.That(result, Is.TypeOf <MethodInstantiation>());
            Assert.That(result.GetGenericMethodDefinition(), Is.EqualTo(instantiationInfo.GenericMethodDefinition));
            Assert.That(result.GetGenericArguments(), Is.EqualTo(instantiationInfo.TypeArguments));
        }
Пример #14
0
        public void GetTypeIDForRequestedType()
        {
            var fakeRequestedType = ReflectionObjectMother.GetSomeOtherType();
            var fakeTypeID        = AssembledTypeIDObjectMother.Create();

            _typeAssemblerMock.Setup(mock => mock.ComputeTypeID(fakeRequestedType)).Returns(fakeTypeID).Verifiable();

            var result = _service.GetTypeIDForRequestedType(fakeRequestedType);

            _typeAssemblerMock.Verify();
            Assert.That(result, Is.EqualTo(fakeTypeID));
        }
Пример #15
0
        public void GetAdditionalType()
        {
            var additionalTypeID   = new object();
            var fakeAdditionalType = ReflectionObjectMother.GetSomeType();

            _typeCacheMock.Expect(mock => mock.GetOrCreateAdditionalType(additionalTypeID)).Return(fakeAdditionalType);

            var result = _service.GetAdditionalType(additionalTypeID);

            _typeCacheMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeAdditionalType));
        }
        public static ParameterOnCustomMember Create(
            MemberInfo member = null,
            int position      = 7,
            string name       = "param",
            Type type         = null,
            ParameterAttributes attributes = (ParameterAttributes)7)
        {
            member = member ?? NormalizingMemberInfoFromExpressionUtility.GetMethod(() => UnspecifiedMember());
            type   = type ?? ReflectionObjectMother.GetSomeType();

            return(new ParameterOnCustomMember(member, position, name, type, attributes));
        }
Пример #17
0
        public void Initialization()
        {
            var member     = ReflectionObjectMother.GetSomeMember();
            var position   = 7;
            var name       = "abc";
            var type       = ReflectionObjectMother.GetSomeType();
            var attributes = (ParameterAttributes)7;

            var parameter = new MutableParameterInfo(member, position, name, type, attributes);

            CustomParameterInfoTest.CheckParameter(parameter, member, position, name, type, attributes);
        }
Пример #18
0
        public void GetAssembledType_AssembledTypeID()
        {
            var typeID            = AssembledTypeIDObjectMother.Create();
            var fakeAssembledType = ReflectionObjectMother.GetSomeOtherType();

            _typeCacheMock.Setup(mock => mock.GetOrCreateType(It.Is <AssembledTypeID> (id => id.Equals(typeID)))).Returns(fakeAssembledType).Verifiable();

            var result = _service.GetAssembledType(typeID);

            _typeCacheMock.Verify();
            Assert.That(result, Is.SameAs(fakeAssembledType));
        }
Пример #19
0
        public void AddExplicitOverride()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            Func <MethodBodyCreationContext, Expression> bodyProvider = ctx => null;
            var fakeMethod = MutableMethodInfoObjectMother.Create();

            _mutableMemberFactoryMock.Expect(mock => mock.CreateExplicitOverride(_mutableType, method, bodyProvider)).Return(fakeMethod);

            var result = _mutableType.AddExplicitOverride(method, bodyProvider);

            Assert.That(result, Is.SameAs(fakeMethod));
            Assert.That(_mutableType.AddedMethods, Is.EqualTo(new[] { result }));
        }
Пример #20
0
        public void SetUp()
        {
            _declaringType   = MutableTypeObjectMother.Create();
            _name            = "Property";
            _attributes      = (PropertyAttributes)7;
            _type            = ReflectionObjectMother.GetSomeType();
            _indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2);
            _getMethod       = MutableMethodInfoObjectMother.Create(returnType: _type, parameters: _indexParameters);
            _setMethod       =
                MutableMethodInfoObjectMother.Create(parameters: _indexParameters.Concat(new[] { ParameterDeclarationObjectMother.Create(_type) }));

            _property = new MutablePropertyInfo(_declaringType, _name, _attributes, _getMethod, _setMethod);
        }
Пример #21
0
        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 }));
        }
Пример #22
0
        public void Emit_ConstructorInfo()
        {
            var constructor          = ReflectionObjectMother.GetSomeConstructor();
            var fakeEmittableOperand = MockRepository.GenerateStub <ConstructorInfo>();

            _emittableOperandProviderStub.Stub(stub => stub.GetEmittableConstructor(constructor)).Return(fakeEmittableOperand);

            _innerILGeneratorMock.Expect(mock => mock.Emit(OpCodes.Call, fakeEmittableOperand));

            _decorator.Emit(OpCodes.Call, constructor);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
        public void CacheHit()
        {
            var additionalTypeID = new object();
            var additionalType   = ReflectionObjectMother.GetSomeType();

            _additionalTypes.Add(additionalTypeID, new Lazy <Type> (() => additionalType, LazyThreadSafetyMode.None));

            var result = _cache.GetOrCreateAdditionalType(additionalTypeID);

            _typeAssemblerMock.VerifyAllExpectations();
            _assemblyContextPoolMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(additionalType));
        }
Пример #24
0
        public void Emit_MethodInfo()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            var fakeEmittableOperand = MockRepository.GenerateStub <MethodInfo> ();

            _emittableOperandProviderStub.Stub(stub => stub.GetEmittableMethod(method)).Return(fakeEmittableOperand);

            _innerILGeneratorMock.Expect(mock => mock.Emit(OpCodes.Call, fakeEmittableOperand));

            _decorator.Emit(OpCodes.Call, method);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
Пример #25
0
        public void BeginCatchBlock()
        {
            var exceptionType        = ReflectionObjectMother.GetSomeType();
            var fakeEmittableOperand = ReflectionObjectMother.GetSomeOtherType();

            _emittableOperandProviderStub.Stub(stub => stub.GetEmittableType(exceptionType)).Return(fakeEmittableOperand);

            _innerILGeneratorMock.Expect(mock => mock.BeginCatchBlock(fakeEmittableOperand));

            _decorator.BeginCatchBlock(exceptionType);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
Пример #26
0
        public void Emit_FieldInfo()
        {
            var field = ReflectionObjectMother.GetSomeField();
            var fakeEmittableOperand = MockRepository.GenerateStub <FieldInfo> ();

            _emittableOperandProviderStub.Stub(stub => stub.GetEmittableField(field)).Return(fakeEmittableOperand);

            _innerILGeneratorMock.Expect(mock => mock.Emit(OpCodes.Ldfld, fakeEmittableOperand));

            _decorator.Emit(OpCodes.Ldfld, field);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
Пример #27
0
        public void Emit_Type()
        {
            var type = ReflectionObjectMother.GetSomeType();
            var fakeEmittableOperand = MockRepository.GenerateStub <Type> ();

            _emittableOperandProviderStub.Stub(stub => stub.GetEmittableType(type)).Return(fakeEmittableOperand);

            _innerILGeneratorMock.Expect(mock => mock.Emit(OpCodes.Ldtoken, fakeEmittableOperand));

            _decorator.Emit(OpCodes.Ldtoken, type);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
Пример #28
0
        public void SetParameters()
        {
            var parameterType          = ReflectionObjectMother.GetSomeType();
            var emittableParameterType = ReflectionObjectMother.GetSomeOtherType();

            _operandProviderMock.Expect(mock => mock.GetEmittableType(parameterType)).Return(emittableParameterType);
            _innerMock.Expect(mock => mock.SetParameters(new[] { emittableParameterType }));

            _decorator.SetParameters(new[] { parameterType });

            _operandProviderMock.VerifyAllExpectations();
            _innerMock.VerifyAllExpectations();
        }
Пример #29
0
        public void SetReturnType()
        {
            var returnType          = ReflectionObjectMother.GetSomeType();
            var emittableReturnType = ReflectionObjectMother.GetSomeOtherType();

            _operandProviderMock.Expect(mock => mock.GetEmittableType(returnType)).Return(emittableReturnType);
            _innerMock.Expect(mock => mock.SetReturnType(emittableReturnType));

            _decorator.SetReturnType(returnType);

            _operandProviderMock.VerifyAllExpectations();
            _innerMock.VerifyAllExpectations();
        }
Пример #30
0
        public void Emit_ConstructorInfo()
        {
            var constructor          = ReflectionObjectMother.GetSomeConstructor();
            var fakeEmittableOperand = new Mock <ConstructorInfo>().Object;

            _emittableOperandProviderStub.Setup(stub => stub.GetEmittableConstructor(constructor)).Returns(fakeEmittableOperand);

            _innerILGeneratorMock.Setup(mock => mock.Emit(OpCodes.Call, fakeEmittableOperand)).Verifiable();

            _decorator.Emit(OpCodes.Call, constructor);

            _innerILGeneratorMock.Verify();
        }