Exemplo n.º 1
0
        public void GetEmittableConstructor()
        {
            var constructor          = ReflectionObjectMother.GetSomeConstructor();
            var emittableConstructor = ReflectionObjectMother.GetSomeConstructor();

            CheckGetEmittable(p => p.GetEmittableConstructor(constructor), emittableConstructor, emittableConstructor.DeclaringType);
        }
Exemplo n.º 2
0
        public void SetUp()
        {
            _memberSelectorMock = MockRepository.GenerateStrictMock <IMemberSelector>();

            _name       = "TypeName";
            _namespace  = "MyNamespace";
            _attributes = (TypeAttributes)7;

            _customType = new TestableCustomType(
                _name,
                _namespace,
                _attributes,
                genericTypeDefinition: null,
                typeArguments: Type.EmptyTypes)
            {
                NestedTypes  = new[] { ReflectionObjectMother.GetSomeType() },
                Interfaces   = new[] { typeof(IDisposable) },
                Fields       = new[] { ReflectionObjectMother.GetSomeField() },
                Constructors = new[] { ReflectionObjectMother.GetSomeConstructor() },
                Methods      = new[] { ReflectionObjectMother.GetSomeMethod() },
                Properties   = new[] { ReflectionObjectMother.GetSomeProperty() },
                Events       = new[] { ReflectionObjectMother.GetSomeEvent() }
            };
            _customType.SetMemberSelector(_memberSelectorMock);

            _typeArgument = ReflectionObjectMother.GetSomeType();
            _genericTypeUnderlyingDefinition = typeof(IList <>);
            _genericType = CustomTypeObjectMother.Create(
                name: "GenericType`1", genericTypeDefinition: _genericTypeUnderlyingDefinition, typeArguments: new[] { _typeArgument });

            _typeParameter         = ReflectionObjectMother.GetSomeGenericParameter();
            _genericTypeDefinition = CustomTypeObjectMother.Create(name: "GenericTypeDefinition`1", typeArguments: new[] { _typeParameter });
        }
Exemplo n.º 3
0
        public void IsDeserializationConstructor()
        {
            var ctor1 = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new SerializableInterfaceType(null, new StreamingContext()));
            var ctor2 = ReflectionObjectMother.GetSomeConstructor();

            Assert.That(_enabler.IsDeserializationConstructor(ctor1), Is.True);
            Assert.That(_enabler.IsDeserializationConstructor(ctor2), Is.False);
        }
        public void Adapt_Constructor()
        {
            var ctor = ReflectionObjectMother.GetSomeConstructor();

            var adapter = NonVirtualCallMethodInfoAdapter.Adapt(ctor);

            Assert.That(adapter.AdaptedMethod, Is.TypeOf <ConstructorAsMethodInfoAdapter>());
            var constructorAsMethodInfoAdapter = (ConstructorAsMethodInfoAdapter)adapter.AdaptedMethod;

            Assert.That(constructorAsMethodInfoAdapter.AdaptedConstructor, Is.SameAs(ctor));
        }
Exemplo n.º 5
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 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();
        }
Exemplo n.º 7
0
        public void GetConstructors()
        {
            Assert.That(_customType.Constructors, Is.Not.Null.And.Not.Empty);
            var bindingAttr = BindingFlags.NonPublic;
            var fakeResult  = new[] { ReflectionObjectMother.GetSomeConstructor() };

            _memberSelectorMock.Expect(mock => mock.SelectMethods(_customType.Constructors, bindingAttr, _customType)).Return(fakeResult);

            var result = _customType.GetConstructors(bindingAttr);

            _memberSelectorMock.VerifyAllExpectations();
            Assert.That(result, Is.EqualTo(fakeResult));
        }
Exemplo n.º 8
0
        public void GetEmittableXXX_RuntimeInfos()
        {
            var type             = ReflectionObjectMother.GetSomeType();
            var genericParameter = ReflectionObjectMother.GetSomeGenericParameter();
            var field            = ReflectionObjectMother.GetSomeField();
            var ctor             = ReflectionObjectMother.GetSomeConstructor();
            var method           = ReflectionObjectMother.GetSomeMethod();

            Assert.That(_provider.GetEmittableType(type), Is.SameAs(type));
            Assert.That(_provider.GetEmittableType(genericParameter), Is.SameAs(genericParameter));
            Assert.That(_provider.GetEmittableField(field), Is.SameAs(field));
            Assert.That(_provider.GetEmittableConstructor(ctor), Is.SameAs(ctor));
            Assert.That(_provider.GetEmittableMethod(method), Is.SameAs(method));
        }
Exemplo n.º 9
0
        public void EmitCall_MethodInfo_BaseConstructorMethodInfo_EmptyOptionalParameters()
        {
            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));

            var constructorAsMethodInfoAdapter = new ConstructorAsMethodInfoAdapter(constructor);

            _decorator.EmitCall(OpCodes.Call, constructorAsMethodInfoAdapter, Type.EmptyTypes);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
Exemplo n.º 10
0
        public void EmitCall_MethodInfo_BaseConstructorMethodInfo_NullOptionalParameters()
        {
            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();

            var constructorAsMethodInfoAdapter = new ConstructorAsMethodInfoAdapter(constructor);

            _decorator.EmitCall(OpCodes.Call, constructorAsMethodInfoAdapter, null);

            _innerILGeneratorMock.Verify();
        }
Exemplo n.º 11
0
        public void GetEmittableXXX_Mutable()
        {
            var emittableType             = ReflectionObjectMother.GetSomeType();
            var emittableGenericParameter = ReflectionObjectMother.GetSomeGenericParameter();
            var emittableField            = ReflectionObjectMother.GetSomeField();
            var emittableConstructor      = ReflectionObjectMother.GetSomeConstructor();
            var emittableMethod           = ReflectionObjectMother.GetSomeMethod();

            _provider.AddMapping(_mutableType, emittableType);
            _provider.AddMapping(_mutableGenericParameter, emittableGenericParameter);
            _provider.AddMapping(_mutableField, emittableField);
            _provider.AddMapping(_mutableConstructor, emittableConstructor);
            _provider.AddMapping(_mutableMethod, emittableMethod);

            Assert.That(_provider.GetEmittableType(_mutableType), Is.SameAs(emittableType));
            Assert.That(_provider.GetEmittableType(_mutableGenericParameter), Is.SameAs(emittableGenericParameter));
            Assert.That(_provider.GetEmittableField(_mutableField), Is.SameAs(emittableField));
            Assert.That(_provider.GetEmittableConstructor(_mutableConstructor), Is.SameAs(emittableConstructor));
            Assert.That(_provider.GetEmittableMethod(_mutableMethod), Is.SameAs(emittableMethod));
        }
Exemplo n.º 12
0
        public void GetConstructorImpl(Binder inputBinder, Binder expectedBinder)
        {
            Assert.That(_customType.Constructors, Is.Not.Null.And.Not.Empty);
            var callingConvention = CallingConventions.Any;
            var bindingAttr       = BindingFlags.NonPublic;
            var typesOrNull       = new[] { ReflectionObjectMother.GetSomeType() };
            var modifiersOrNull   = new[] { new ParameterModifier(1) };
            var fakeResult        = ReflectionObjectMother.GetSomeConstructor();

            _memberSelectorMock
            .Setup(mock => mock.SelectSingleMethod(_customType.Constructors, expectedBinder, bindingAttr, null, _customType, typesOrNull, modifiersOrNull))
            .Returns(fakeResult)
            .Verifiable();

            var arguments = new object[] { bindingAttr, inputBinder, callingConvention, typesOrNull, modifiersOrNull };
            var result    = (ConstructorInfo)PrivateInvoke.InvokeNonPublicMethod(_customType, "GetConstructorImpl", arguments);

            _memberSelectorMock.Verify();
            Assert.That(result, Is.SameAs(fakeResult));
        }
Exemplo n.º 13
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));
        }
Exemplo n.º 14
0
        public void SelectSingleMethod_ForConstructors_NameIsNotConsidered()
        {
            var constructors    = new[] { CreateConstructorStub(MethodAttributes.Assembly), CreateConstructorStub(MethodAttributes.Family) };
            var bindingFlags    = (BindingFlags)1;
            var typesOrNull     = new[] { typeof(int), typeof(string) };
            var modifiersOrNull = new[] { new ParameterModifier(2) };

            _bindingFlagsEvaluatorMock.Expect(mock => mock.HasRightAttributes(constructors[0].Attributes, bindingFlags)).Return(false);
            _bindingFlagsEvaluatorMock.Expect(mock => mock.HasRightAttributes(constructors[1].Attributes, bindingFlags)).Return(true);

            var binderMock = MockRepository.GenerateStrictMock <Binder>();
            var fakeResult = ReflectionObjectMother.GetSomeConstructor();

            binderMock
            .Expect(mock => mock.SelectMethod(bindingFlags, new MethodBase[] { constructors[1] }, typesOrNull, modifiersOrNull))
            .Return(fakeResult);

            var result = _selector.SelectSingleMethod(constructors, binderMock, bindingFlags, null, _someDeclaringType, typesOrNull, modifiersOrNull);

            _bindingFlagsEvaluatorMock.VerifyAllExpectations();
            binderMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeResult));
        }
Exemplo n.º 15
0
        public void SelectSingleMethod_ForConstructors_NameIsNotConsidered()
        {
            var constructors    = new[] { CreateConstructorStub(MethodAttributes.Assembly), CreateConstructorStub(MethodAttributes.Family) };
            var bindingFlags    = (BindingFlags)1;
            var typesOrNull     = new[] { typeof(int), typeof(string) };
            var modifiersOrNull = new[] { new ParameterModifier(2) };

            _bindingFlagsEvaluatorMock.Setup(mock => mock.HasRightAttributes(constructors[0].Attributes, bindingFlags)).Returns(false).Verifiable();
            _bindingFlagsEvaluatorMock.Setup(mock => mock.HasRightAttributes(constructors[1].Attributes, bindingFlags)).Returns(true).Verifiable();

            var binderMock = new Mock <Binder> (MockBehavior.Strict);
            var fakeResult = ReflectionObjectMother.GetSomeConstructor();

            binderMock
            .Setup(mock => mock.SelectMethod(bindingFlags, It.Is <MethodBase[]> (param => param.SequenceEqual(new MethodBase[] { constructors[1] })), typesOrNull, modifiersOrNull))
            .Returns(fakeResult)
            .Verifiable();

            var result = _selector.SelectSingleMethod(constructors, binderMock.Object, bindingFlags, null, _someDeclaringType, typesOrNull, modifiersOrNull);

            _bindingFlagsEvaluatorMock.Verify();
            binderMock.Verify();
            Assert.That(result, Is.SameAs(fakeResult));
        }
        public void SetUp()
        {
            var adaptedConstructor = ReflectionObjectMother.GetSomeConstructor();

            _adapter = new ConstructorAsMethodInfoAdapter(adaptedConstructor);
        }