Exemplo n.º 1
0
        public virtual void SetUp()
        {
            _mockRepository = new MockRepository();

            _mutableType = MutableTypeObjectMother.Create();
            _nestedTypeCodeGeneratorFactoryMock = _mockRepository.StrictMock <IMutableNestedTypeCodeGeneratorFactory>();
            _codeGeneratorMock             = _mockRepository.StrictMock <IReflectionEmitCodeGenerator>();
            _emittableOperandProviderMock  = _mockRepository.StrictMock <IEmittableOperandProvider> ();
            _memberEmitterMock             = _mockRepository.StrictMock <IMemberEmitter>();
            _initializationBuilderMock     = _mockRepository.StrictMock <IInitializationBuilder>();
            _proxySerializationEnablerMock = _mockRepository.StrictMock <IProxySerializationEnabler>();

            _generator = new MutableTypeCodeGenerator(
                _mutableType,
                _nestedTypeCodeGeneratorFactoryMock,
                _codeGeneratorMock,
                _emittableOperandProviderMock,
                _memberEmitterMock,
                _initializationBuilderMock,
                _proxySerializationEnablerMock);

            _typeBuilderMock        = _mockRepository.StrictMock <ITypeBuilder>();
            _debugInfoGeneratorMock = _mockRepository.StrictMock <DebugInfoGenerator>();

            _fakeInitializationField   = ReflectionObjectMother.GetSomeField();
            _fakeInitializationMethod  = ReflectionObjectMother.GetSomeMethod();
            _fakeInitializationMembers = Tuple.Create(_fakeInitializationField, _fakeInitializationMethod);
        }
Exemplo n.º 2
0
        public static MethodOnTypeInstantiation Create(TypeInstantiation declaringType = null, MethodInfo method = null)
        {
            declaringType = declaringType ?? TypeInstantiationObjectMother.Create();
            method        = method ?? ReflectionObjectMother.GetSomeMethod();

            return(new MethodOnTypeInstantiation(declaringType, method));
        }
Exemplo n.º 3
0
        public void SelectSingleMethod()
        {
            var methods =
                new[]
            {
                CreateMethodStub("Method1", MethodAttributes.Assembly),
                CreateMethodStub("This method is filtered because of its name", MethodAttributes.Family),
                CreateMethodStub("Method1", MethodAttributes.Public)
            };
            var bindingFlags    = (BindingFlags)1;
            var typesOrNull     = new[] { typeof(int), typeof(string) };
            var modifiersOrNull = new[] { new ParameterModifier(2) };

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

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

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

            var result = _selector.SelectSingleMethod(methods, binderMock, bindingFlags, "Method1", _someDeclaringType, typesOrNull, modifiersOrNull);

            _bindingFlagsEvaluatorMock.VerifyAllExpectations();
            binderMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeResult));
        }
Exemplo n.º 4
0
        public void GetEmittableMethod()
        {
            var method          = ReflectionObjectMother.GetSomeMethod();
            var emittableMethod = ReflectionObjectMother.GetSomeNonGenericMethod();

            CheckGetEmittable(p => p.GetEmittableMethod(method), emittableMethod, emittableMethod.DeclaringType);
        }
Exemplo n.º 5
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.º 6
0
        public void SelectSingleMethod()
        {
            var methods =
                new[]
            {
                CreateMethodStub("Method1", MethodAttributes.Assembly),
                CreateMethodStub("This method is filtered because of its name", MethodAttributes.Family),
                CreateMethodStub("Method1", MethodAttributes.Public)
            };
            var bindingFlags    = (BindingFlags)1;
            var typesOrNull     = new[] { typeof(int), typeof(string) };
            var modifiersOrNull = new[] { new ParameterModifier(2) };

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

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

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

            var result = _selector.SelectSingleMethod(methods, binderMock.Object, bindingFlags, "Method1", _someDeclaringType, typesOrNull, modifiersOrNull);

            _bindingFlagsEvaluatorMock.Verify();
            binderMock.Verify();
            Assert.That(result, Is.SameAs(fakeResult));
        }
Exemplo n.º 7
0
        public void IsRunetimeMethodInfo()
        {
            var runtimeMethod = ReflectionObjectMother.GetSomeMethod();
            var customMethod  = CustomMethodInfoObjectMother.Create();

            Assert.That(runtimeMethod.IsRuntimeMethodInfo(), Is.True);
            Assert.That(customMethod.IsRuntimeMethodInfo(), Is.False);
        }
        public void Adapt_Method()
        {
            var method = ReflectionObjectMother.GetSomeMethod();

            var adapter = NonVirtualCallMethodInfoAdapter.Adapt(method);

            Assert.That(adapter.AdaptedMethod, Is.SameAs(method));
        }
        public void GetEmittableMethod_MethodBuilder()
        {
            var method          = ReflectionObjectMother.GetSomeMethod();
            var emittableMethod = ReflectionEmitObjectMother.CreateMethodBuilder();

            Assert.That(emittableMethod.GetGenericArguments(), Is.Null);
            _innerMock.Stub(stub => stub.GetEmittableMethod(method)).Return(emittableMethod);
            _typeAnalyzerMock.Stub(stub => stub.IsStrongNamed(emittableMethod.DeclaringType)).Return(true);

            Assert.That(() => _decorator.GetEmittableMethod(method), Throws.Nothing);
        }
Exemplo n.º 10
0
        public void SetUp()
        {
            _declaringType     = MutableTypeObjectMother.Create();
            _isStatic          = BooleanObjectMother.GetRandomBoolean();
            _parameters        = new[] { Expression.Parameter(typeof(string)) };
            _genericParameters = new[] { ReflectionObjectMother.GetSomeGenericParameter() };
            _returnType        = ReflectionObjectMother.GetSomeType();
            _baseMethod        = ReflectionObjectMother.GetSomeMethod();

            _context = new TestableMethodBodyContextBase(
                _declaringType, _isStatic, _parameters.AsOneTime(), _genericParameters.AsOneTime(), _returnType, _baseMethod);
        }
Exemplo n.º 11
0
        public void GetMethods()
        {
            Assert.That(_customType.Methods, Is.Not.Null.And.Not.Empty);
            var bindingAttr = BindingFlags.NonPublic;
            var fakeResult  = new[] { ReflectionObjectMother.GetSomeMethod() };

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

            var result = _customType.GetMethods(bindingAttr);

            _memberSelectorMock.VerifyAllExpectations();
            Assert.That(result, Is.EqualTo(fakeResult));
        }
Exemplo n.º 12
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();
        }
Exemplo n.º 13
0
        public void Emit_MethodInfo()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            var fakeEmittableOperand = new Mock <MethodInfo>().Object;

            _emittableOperandProviderStub.Setup(stub => stub.GetEmittableMethod(method)).Returns(fakeEmittableOperand);

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

            _decorator.Emit(OpCodes.Call, method);

            _innerILGeneratorMock.Verify();
        }
Exemplo n.º 14
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 }));
        }
Exemplo n.º 15
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.º 16
0
        public void GetEmittableMethod_GenericArguments()
        {
            var method          = ReflectionObjectMother.GetSomeMethod();
            var emittableMethod = NormalizingMemberInfoFromExpressionUtility.GetMethod(() => GenericMethod <int>());

            _innerMock.Setup(stub => stub.GetEmittableMethod(method)).Returns(emittableMethod);
            _typeAnalyzerMock.Setup(mock => mock.IsStrongNamed(emittableMethod.DeclaringType)).Returns(true).Verifiable();
            _typeAnalyzerMock.Setup(mock => mock.IsStrongNamed(typeof(int))).Returns(true).Verifiable();

            var result = _decorator.GetEmittableMethod(method);

            _typeAnalyzerMock.Verify();
            Assert.That(result, Is.SameAs(emittableMethod));
        }
Exemplo n.º 17
0
        private void CheckExplicitOverrideAction(Action testedAction, MethodInfo overriddenMethod, MutableMethodInfo overridingMethod)
        {
            var fakeOverriddenMethod = ReflectionObjectMother.GetSomeMethod();
            var fakeOverridingMethod = ReflectionObjectMother.GetSomeMethod();

            _emittableOperandProviderMock.Setup(mock => mock.GetEmittableMethod(overriddenMethod)).Returns(fakeOverriddenMethod).Verifiable();
            _emittableOperandProviderMock.Setup(mock => mock.GetEmittableMethod(overridingMethod)).Returns(fakeOverridingMethod).Verifiable();

            _typeBuilderMock.Setup(mock => mock.DefineMethodOverride(fakeOverridingMethod, fakeOverriddenMethod)).Verifiable();

            testedAction();

            _emittableOperandProviderMock.Verify();
            _typeBuilderMock.Verify();
        }
Exemplo n.º 18
0
        public void Emit_MethodInfo_BaseCallMethodInfo_TurnsCallvirt_IntoCall()
        {
            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));

            var baseAdaptedMethod = new NonVirtualCallMethodInfoAdapter(method);

            _decorator.Emit(OpCodes.Callvirt, baseAdaptedMethod);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
Exemplo n.º 19
0
        public void Emit_MethodInfo_BaseCallMethodInfo_TurnsCallvirt_IntoCall()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            var fakeEmittableOperand = new Mock <MethodInfo>().Object;

            _emittableOperandProviderStub.Setup(stub => stub.GetEmittableMethod(method)).Returns(fakeEmittableOperand);

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

            var baseAdaptedMethod = new NonVirtualCallMethodInfoAdapter(method);

            _decorator.Emit(OpCodes.Callvirt, baseAdaptedMethod);

            _innerILGeneratorMock.Verify();
        }
        public void SetUp()
        {
            _declaringType     = MutableTypeObjectMother.Create();
            _isStatic          = BooleanObjectMother.GetRandomBoolean();
            _parameters        = new[] { Expression.Parameter(typeof(int)), Expression.Parameter(typeof(object)) };
            _baseMethod        = ReflectionObjectMother.GetSomeMethod();
            _genericParameters = new[] { ReflectionObjectMother.GetSomeGenericParameter() };
            _returnType        = ReflectionObjectMother.GetSomeType();
            _previousBody      = Expression.Block(_parameters[0], _parameters[1]);

            _context = new MethodBodyModificationContext(
                _declaringType, _isStatic, _parameters.AsOneTime(), _genericParameters.AsOneTime(), _returnType, _baseMethod, _previousBody);
            _contextWithoutPreviousBody = new MethodBodyModificationContext(
                _declaringType, _isStatic, _parameters, _genericParameters, _returnType, _baseMethod, null);
        }
Exemplo n.º 21
0
        public void GetOrAddOverride_RetrievesExistingOverride()
        {
            var baseMethod     = ReflectionObjectMother.GetSomeMethod();
            var fakeOverride   = MutableMethodInfoObjectMother.Create();
            var isNewlyCreated = false;

            _mutableMemberFactoryMock
            .Setup(mock => mock.GetOrCreateOverride(_mutableType, baseMethod, out isNewlyCreated))
            .Returns(fakeOverride)
            .Verifiable();

            var result = _mutableType.GetOrAddOverride(baseMethod);

            Assert.That(result, Is.SameAs(fakeOverride));
            Assert.That(_mutableType.AddedMethods, Has.No.Member(result));
        }
        public void GetBaseDefinition_ForStandardMethodInfo_Cached()
        {
            var standardMethodInfoMock = MockRepository.GenerateStrictMock <MethodInfo>();

            var fakeMethodDefinition = ReflectionObjectMother.GetSomeMethod();

            standardMethodInfoMock.Expect(mock => mock.GetBaseDefinition()).Return(fakeMethodDefinition).Repeat.Once();

            var result1 = MethodBaseDefinitionCache.GetBaseDefinition(standardMethodInfoMock);
            var result2 = MethodBaseDefinitionCache.GetBaseDefinition(standardMethodInfoMock);

            standardMethodInfoMock.VerifyAllExpectations();

            Assert.That(result1, Is.SameAs(fakeMethodDefinition));
            Assert.That(result2, Is.SameAs(fakeMethodDefinition));
        }
Exemplo n.º 23
0
        public void EmitCall_MethodInfo_BaseCallMethodInfo()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            var optionalParameterTypes = new[] { ReflectionObjectMother.GetSomeType() };
            var fakeEmittableOperand   = MockRepository.GenerateStub <MethodInfo> ();

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

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

            var baseAdaptedMethod = new NonVirtualCallMethodInfoAdapter(method);

            _decorator.EmitCall(OpCodes.Call, baseAdaptedMethod, optionalParameterTypes);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
Exemplo n.º 24
0
        public void GetBaseDefinition_ForStandardMethodInfo_Cached()
        {
            var standardMethodInfoMock = new Mock <MethodInfo> (MockBehavior.Strict);

            var fakeMethodDefinition = ReflectionObjectMother.GetSomeMethod();

            standardMethodInfoMock.Setup(mock => mock.GetBaseDefinition()).Returns(fakeMethodDefinition).Verifiable();

            var result1 = MethodBaseDefinitionCache.GetBaseDefinition(standardMethodInfoMock.Object);
            var result2 = MethodBaseDefinitionCache.GetBaseDefinition(standardMethodInfoMock.Object);

            standardMethodInfoMock.Verify();

            Assert.That(result1, Is.SameAs(fakeMethodDefinition));
            Assert.That(result2, Is.SameAs(fakeMethodDefinition));
        }
Exemplo n.º 25
0
        public void GetEmittableMethod_MethodBuilder()
        {
            var method          = ReflectionObjectMother.GetSomeMethod();
            var emittableMethod = ReflectionEmitObjectMother.CreateMethodBuilder();

#if NETFRAMEWORK
            // MethodBuilder in .NET Framework returns null for non-generic types.
            // MethodInfo returns empty, same as MethodInfo and MethodBuilder do in .NET 5.
            Assert.That(emittableMethod.GetGenericArguments(), Is.Null);
#else
            Assert.That(emittableMethod.GetGenericArguments(), Is.Empty);
#endif
            _innerMock.Setup(stub => stub.GetEmittableMethod(method)).Returns(emittableMethod);
            _typeAnalyzerMock.Setup(stub => stub.IsStrongNamed(emittableMethod.DeclaringType)).Returns(true);

            Assert.That(() => _decorator.GetEmittableMethod(method), Throws.Nothing);
        }
Exemplo n.º 26
0
        public void SetUp()
        {
            var identifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ());

            _nonPublicMethod          = ReflectionObjectMother.GetSomeNonPublicMethod();
            _publicMethod             = ReflectionObjectMother.GetSomePublicMethod();
            _wrapperOrInterfaceMethod = ReflectionObjectMother.GetSomeMethod();
            _concreteMixinType        = new ConcreteMixinType(
                identifier,
                typeof(object),
                typeof(IServiceProvider),
                new Dictionary <MethodInfo, MethodInfo> {
                { _nonPublicMethod, _wrapperOrInterfaceMethod }
            },
                new Dictionary <MethodInfo, MethodInfo> {
                { _nonPublicMethod, _wrapperOrInterfaceMethod }
            });
        }
Exemplo n.º 27
0
        public void GetOrAddImplementation_RetrievesExistingImplementation()
        {
            var interfaceMethod    = ReflectionObjectMother.GetSomeMethod();
            var fakeImplementation = MutableMethodInfoObjectMother.Create();

            _mutableMemberFactoryMock
            .Expect(
                mock => mock.GetOrCreateImplementation(
                    Arg.Is(_mutableType),
                    Arg.Is(interfaceMethod),
                    out Arg <bool> .Out(false).Dummy))
            .Return(fakeImplementation);

            var result = _mutableType.GetOrAddImplementation(interfaceMethod);

            Assert.That(result, Is.SameAs(fakeImplementation));
            Assert.That(_mutableType.AddedMethods, Has.No.Member(result));
        }
Exemplo n.º 28
0
        public void GetOrAddOverride_RetrievesExistingOverride()
        {
            var baseMethod   = ReflectionObjectMother.GetSomeMethod();
            var fakeOverride = MutableMethodInfoObjectMother.Create();

            _mutableMemberFactoryMock
            .Expect(
                mock => mock.GetOrCreateOverride(
                    Arg.Is(_mutableType),
                    Arg.Is(baseMethod),
                    out Arg <bool> .Out(false).Dummy))
            .Return(fakeOverride);

            var result = _mutableType.GetOrAddOverride(baseMethod);

            Assert.That(result, Is.SameAs(fakeOverride));
            Assert.That(_mutableType.AddedMethods, Has.No.Member(result));
        }
Exemplo n.º 29
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.º 30
0
        public void GetOrAddImplementation_RetrievesExistingImplementation()
        {
            var interfaceMethod    = ReflectionObjectMother.GetSomeMethod();
            var fakeImplementation = MutableMethodInfoObjectMother.Create();
            var isNewlyCreated     = false;

            _mutableMemberFactoryMock
            .Setup(
                mock => mock.GetOrCreateImplementation(
                    _mutableType,
                    interfaceMethod,
                    out isNewlyCreated))
            .Returns(fakeImplementation)
            .Verifiable();

            var result = _mutableType.GetOrAddImplementation(interfaceMethod);

            Assert.That(result, Is.SameAs(fakeImplementation));
            Assert.That(_mutableType.AddedMethods, Has.No.Member(result));
        }