Esempio n. 1
0
        public static BoxAndCastExpression GetSomeBoxAndCastExpression()
        {
            var operand = GetSomeExpression();
            var type    = ReflectionObjectMother.GetSomeType();

            return(new BoxAndCastExpression(operand, type));
        }
Esempio n. 2
0
        public static UnboxExpression GetSomeUnboxExpression()
        {
            var operand = GetSomeExpression();
            var type    = ReflectionObjectMother.GetSomeType();

            return(new UnboxExpression(operand, type));
        }
        public void CacheMiss_AddsAdditionalTypesToCacheBeforeReturningAssemblyContextToPool()
        {
            var assemblyContext  = CreateAssemblyContext();
            var typeID           = AssembledTypeIDObjectMother.Create();
            var additionalTypeID = new object();
            var additionalType   = ReflectionObjectMother.GetSomeType();

            _assemblyContextPoolMock.Setup(mock => mock.Dequeue()).Returns(assemblyContext).Verifiable();

            _typeAssemblerMock
            .Setup(mock => mock.AssembleType(It.IsAny <AssembledTypeID>(), It.IsAny <IParticipantState>(), It.IsAny <IMutableTypeBatchCodeGenerator>()))
            .Returns(new TypeAssemblyResult(_assembledType, new Dictionary <object, Type> {
                { additionalTypeID, additionalType }
            }));

            _assemblyContextPoolMock
            .Setup(mock => mock.Enqueue(assemblyContext))
            .Callback((AssemblyContext _) => Assert.That(_additionalTypes[additionalTypeID].Value, Is.SameAs(additionalType)))
            .Verifiable();

            var result = _cache.GetOrCreateType(typeID);

            _assemblyContextPoolMock.Verify();
            Assert.That(result, Is.SameAs(_assembledType));
        }
        public void CacheMiss_AddsAdditionalTypesToCache_OverridesPreviouslyCachedValue()
        {
            var assemblyContext       = CreateAssemblyContext();
            var additionalTypeID      = new object();
            var additionalType        = ReflectionObjectMother.GetSomeType();
            var otherAdditionalTypeID = new object();
            var otherAdditionalType   = ReflectionObjectMother.GetSomeType();

            _additionalTypes.Add(otherAdditionalTypeID, new Lazy <Type> (() => null, LazyThreadSafetyMode.None));

            _assemblyContextPoolMock.Expect(mock => mock.Dequeue()).Return(assemblyContext);

            _typeAssemblerMock
            .Expect(mock => mock.AssembleAdditionalType(null, null, null))
            .IgnoreArguments()
            .Return(new TypeAssemblyResult(additionalType, new Dictionary <object, Type> {
                { otherAdditionalTypeID, otherAdditionalType }
            }));

            _assemblyContextPoolMock
            .Expect(mock => mock.Enqueue(assemblyContext))
            .WhenCalled(mi => Assert.That(_additionalTypes[otherAdditionalTypeID].Value, Is.SameAs(otherAdditionalType)));

            var result = _cache.GetOrCreateAdditionalType(additionalTypeID);

            _assemblyContextPoolMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(additionalType));
        }
Esempio n. 5
0
        public void GetEmittableType()
        {
            var type          = ReflectionObjectMother.GetSomeType();
            var emittableType = ReflectionObjectMother.GetSomeType();

            CheckGetEmittable(p => p.GetEmittableType(type), emittableType, emittableType);
        }
Esempio n. 6
0
        public void Initialization_TypeArguments()
        {
            var elementType = CustomTypeObjectMother.Create(typeArguments: new[] { ReflectionObjectMother.GetSomeType() });
            var type        = ArrayTypeBaseObjectMother.Create(elementType);

            Assert.That(type.GetGenericArguments(), Is.Empty);
        }
Esempio n. 7
0
        public void CacheMiss_AddsAdditionalTypesToCacheBeforeReturningAssemblyContextToPool()
        {
            var assemblyContext  = CreateAssemblyContext();
            var typeID           = AssembledTypeIDObjectMother.Create();
            var additionalTypeID = new object();
            var additionalType   = ReflectionObjectMother.GetSomeType();

            _assemblyContextPoolMock.Expect(mock => mock.Dequeue()).Return(assemblyContext);

            _typeAssemblerMock
            .Expect(mock => mock.AssembleType(new AssembledTypeID(), null, null))
            .IgnoreArguments()
            .Return(new TypeAssemblyResult(_assembledType, new Dictionary <object, Type> {
                { additionalTypeID, additionalType }
            }));

            _assemblyContextPoolMock
            .Expect(mock => mock.Enqueue(assemblyContext))
            .WhenCalled(mi => Assert.That(_additionalTypes[additionalTypeID].Value, Is.SameAs(additionalType)));

            var result = _cache.GetOrCreateType(typeID);

            _assemblyContextPoolMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(_assembledType));
        }
Esempio n. 8
0
        public void GetPropertyImpl(Binder inputBinder, Binder expectedBinder)
        {
            Assert.That(_customType.Properties, Is.Not.Null.And.Not.Empty);
            var name             = "some name";
            var bindingAttr      = BindingFlags.NonPublic;
            var returnTypeOrNull = ReflectionObjectMother.GetSomeType();
            var typesOrNull      = new[] { ReflectionObjectMother.GetSomeType() };
            var modifiersOrNull  = new[] { new ParameterModifier(1) };

            var fakeResult = ReflectionObjectMother.GetSomeProperty();

            _memberSelectorMock
            .Setup(
                mock => mock.SelectSingleProperty(
                    _customType.Properties,
                    expectedBinder,
                    bindingAttr,
                    name,
                    _customType,
                    returnTypeOrNull,
                    typesOrNull,
                    modifiersOrNull))
            .Returns(fakeResult)
            .Verifiable();

            var arguments = new object[] { name, bindingAttr, inputBinder, returnTypeOrNull, typesOrNull, modifiersOrNull };
            var result    = (PropertyInfo)PrivateInvoke.InvokeNonPublicMethod(_customType, "GetPropertyImpl", arguments);

            _memberSelectorMock.Verify();
            Assert.That(result, Is.SameAs(fakeResult));
        }
        public void SetUp()
        {
            _returnType    = ReflectionObjectMother.GetSomeType();
            _parameterType = ReflectionObjectMother.GetSomeOtherType();

            _type = new DelegateTypePlaceholder(_returnType, new[] { _parameterType }.AsOneTime());
        }
        public void SetUp()
        {
            _type          = ReflectionObjectMother.GetSomeType();
            _flatValueStub = new FlatValueStub();

            _data = new AssembledTypeIDData(_type.AssemblyQualifiedName, new IFlatValue[] { _flatValueStub, null });
        }
Esempio n. 11
0
        public void AddEvent_Simple_NoRaiseBodyProvider()
        {
            var handlerType = ReflectionObjectMother.GetSomeType();
            Func <MethodBodyCreationContext, Expression> addBodyProvider    = ctx => null;
            Func <MethodBodyCreationContext, Expression> removeBodyProvider = ctx => null;

            var addRemoveParameters = new[] { new ParameterDeclaration(typeof(Func <int, string>), "handler") };
            var addMethod           = MutableMethodInfoObjectMother.Create(returnType: typeof(void), parameters: addRemoveParameters);
            var removeMethod        = MutableMethodInfoObjectMother.Create(returnType: typeof(void), parameters: addRemoveParameters);
            var fakeEvent           = MutableEventInfoObjectMother.Create(addMethod: addMethod, removeMethod: removeMethod);

            Assert.That(fakeEvent.MutableRaiseMethod, Is.Null);
            _mutableMemberFactoryMock
            .Setup(
                stub => stub.CreateEvent(
                    It.IsAny <MutableType>(),
                    It.IsAny <string>(),
                    It.IsAny <Type>(),
                    It.IsAny <MethodAttributes>(),
                    It.IsAny <Func <MethodBodyCreationContext, Expression> >(),
                    It.IsAny <Func <MethodBodyCreationContext, Expression> >(),
                    It.IsAny <Func <MethodBodyCreationContext, Expression> >()))
            .Returns(fakeEvent);

            var result = _mutableType.AddEvent("Event", handlerType, addBodyProvider: addBodyProvider, removeBodyProvider: removeBodyProvider);

            Assert.That(_mutableType.AddedMethods, Is.EqualTo(new[] { result.MutableAddMethod, result.MutableRemoveMethod }));
        }
        public void CacheMiss_DoesNotAddRequestedAdditionalTypeToCacheAtSameTimeAsOtherAdditionalTypes()
        {
            var assemblyContext       = CreateAssemblyContext();
            var additionalTypeID      = new object();
            var additionalType        = ReflectionObjectMother.GetSomeType();
            var otherAdditionalTypeID = new object();
            var otherAdditionalType   = ReflectionObjectMother.GetSomeType();

            _assemblyContextPoolMock.Setup(mock => mock.Dequeue()).Returns(assemblyContext).Verifiable();

            _typeAssemblerMock
            .Setup(mock => mock.AssembleAdditionalType(It.IsAny <object>(), It.IsAny <IParticipantState>(), It.IsAny <IMutableTypeBatchCodeGenerator>()))
            .Returns(
                new TypeAssemblyResult(
                    additionalType,
                    new Dictionary <object, Type> {
                { additionalTypeID, additionalType }, { otherAdditionalTypeID, otherAdditionalType }
            }));

            _assemblyContextPoolMock.Setup(mock => mock.Enqueue(assemblyContext)).Verifiable();

            var result = _cache.GetOrCreateAdditionalType(additionalTypeID);

            _typeAssemblerMock.Verify();
            _assemblyContextPoolMock.Verify();
            Assert.That(result, Is.SameAs(additionalType));
            Assert.That(_additionalTypes[additionalTypeID].IsValueCreated, Is.True);
            Assert.That(_additionalTypes[additionalTypeID].Value, Is.SameAs(additionalType));
        }
Esempio n. 13
0
        public void CreateProperty_Providers_WriteOnly()
        {
            var type = ReflectionObjectMother.GetSomeType();
            Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => ExpressionTreeObjectMother.GetSomeExpression(typeof(void));
            var fakeSetMethod = MutableMethodInfoObjectMother.Create(parameters: new[] { ParameterDeclarationObjectMother.Create(type) });

            _methodFactoryMock
            .Setup(
                stub => stub.CreateMethod(
                    It.IsAny <MutableType>(),
                    "set_Property",
                    It.IsAny <MethodAttributes>(),
                    It.IsAny <IEnumerable <GenericParameterDeclaration> >(),
                    It.IsAny <Func <GenericParameterContext, Type> >(),
                    It.IsAny <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >(),
                    It.IsAny <Func <MethodBodyCreationContext, Expression> >()))
            .Returns(fakeSetMethod);

            var result = _factory.CreateProperty(
                _mutableType,
                "Property",
                type,
                ParameterDeclaration.None,
                0,
                getBodyProvider: null,
                setBodyProvider: setBodyProvider);

            Assert.That(result.MutableGetMethod, Is.Null);
        }
Esempio n. 14
0
        public void SetUp()
        {
            _operand = ExpressionTreeObjectMother.GetSomeExpression();
            _type    = ReflectionObjectMother.GetSomeType();

            _expression = new BoxAndCastExpression(_operand, _type);
        }
        public void CacheMiss_AddsAdditionalTypesToCache_OverridesPreviouslyCachedValue()
        {
            var assemblyContext       = CreateAssemblyContext();
            var additionalTypeID      = new object();
            var additionalType        = ReflectionObjectMother.GetSomeType();
            var otherAdditionalTypeID = new object();
            var otherAdditionalType   = ReflectionObjectMother.GetSomeType();

            _additionalTypes.Add(otherAdditionalTypeID, new Lazy <Type> (() => null, LazyThreadSafetyMode.None));

            _assemblyContextPoolMock.Setup(mock => mock.Dequeue()).Returns(assemblyContext).Verifiable();

            _typeAssemblerMock
            .Setup(mock => mock.AssembleAdditionalType(It.IsAny <object>(), It.IsAny <IParticipantState>(), It.IsAny <IMutableTypeBatchCodeGenerator>()))
            .Returns(new TypeAssemblyResult(additionalType, new Dictionary <object, Type> {
                { otherAdditionalTypeID, otherAdditionalType }
            }));

            _assemblyContextPoolMock
            .Setup(mock => mock.Enqueue(assemblyContext))
            .Callback((AssemblyContext _) => Assert.That(_additionalTypes[otherAdditionalTypeID].Value, Is.SameAs(otherAdditionalType)))
            .Verifiable();

            var result = _cache.GetOrCreateAdditionalType(additionalTypeID);

            _assemblyContextPoolMock.Verify();
            Assert.That(result, Is.SameAs(additionalType));
        }
Esempio n. 16
0
        public void AddProperty()
        {
            var name                = "Property";
            var attributes          = (PropertyAttributes)7;
            var returnType          = ReflectionObjectMother.GetSomeType();
            var parameters          = ParameterDeclarationObjectMother.CreateMultiple(2);
            var setMethodParameters = parameters.Concat(new[] { ParameterDeclarationObjectMother.Create(returnType) });
            var indexParameterTypes = parameters.Select(p => p.Type).ToArray();
            var getMethod           = MutableMethodInfoObjectMother.Create(returnType: returnType, parameters: parameters);
            var setMethod           = MutableMethodInfoObjectMother.Create(parameters: setMethodParameters);
            var property            = MutablePropertyInfoObjectMother.Create(name: name, attributes: attributes, getMethod: getMethod, setMethod: setMethod);

            var getMethodBuilder = MockRepository.GenerateStub <IMethodBuilder>();
            var setMethodBuilder = MockRepository.GenerateStub <IMethodBuilder>();

            _context.MethodBuilders.Add(getMethod, getMethodBuilder);
            _context.MethodBuilders.Add(setMethod, setMethodBuilder);

            var callingConventions  = CallingConventions.Standard | CallingConventions.HasThis;
            var propertyBuilderMock = MockRepository.GenerateStrictMock <IPropertyBuilder>();

            _typeBuilderMock
            .Expect(mock => mock.DefineProperty(name, attributes, callingConventions, returnType, indexParameterTypes))
            .Return(propertyBuilderMock);
            SetupDefineCustomAttribute(propertyBuilderMock, property);
            propertyBuilderMock.Expect(mock => mock.SetGetMethod(getMethodBuilder));
            propertyBuilderMock.Expect(mock => mock.SetSetMethod(setMethodBuilder));

            _emitter.AddProperty(_context, property);

            _typeBuilderMock.VerifyAllExpectations();
            propertyBuilderMock.VerifyAllExpectations();
        }
Esempio n. 17
0
        public void SetUp()
        {
            _operand = ExpressionTreeObjectMother.GetSomeExpression();
            _type    = ReflectionObjectMother.GetSomeType();

            _expressionPartialMock = MockRepository.GeneratePartialMock <UnaryExpressionBase> (_operand, _type);
        }
Esempio n. 18
0
        public void SelectSingleProperty()
        {
            var properties =
                new[]
            {
                CreatePropertyStub("Property1", accessors: new[] { CreateMethodStub() }),
                CreatePropertyStub("Property2", accessors: new[] { CreateMethodStub(attributes: MethodAttributes.Assembly) }),
                CreatePropertyStub("Property2", accessors: new[] { CreateMethodStub(attributes: MethodAttributes.Public) })
            };
            var bindingFlags = (BindingFlags)1;
            var propertyType = ReflectionObjectMother.GetSomeType();
            var indexerTypes = new[] { ReflectionObjectMother.GetSomeOtherType() };
            var modifiers    = new[] { new ParameterModifier(2) };

            _bindingFlagsEvaluatorMock.Expect(mock => mock.HasRightAttributes(MethodAttributes.Assembly, bindingFlags)).Return(true);
            _bindingFlagsEvaluatorMock.Expect(mock => mock.HasRightAttributes(MethodAttributes.Public, bindingFlags)).Return(true);

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

            binderMock.Expect(mock => mock.SelectProperty(bindingFlags, new[] { properties[1], properties[2] }, propertyType, indexerTypes, modifiers))
            .Return(fakeResult);

            var result = _selector.SelectSingleProperty(
                properties, binderMock, bindingFlags, "Property2", _someDeclaringType, propertyType, indexerTypes, modifiers);

            _bindingFlagsEvaluatorMock.VerifyAllExpectations();
            binderMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeResult));
        }
        public void CacheMiss_DoesNotAddRequestedAdditionalTypeToCacheAtSameTimeAsOtherAdditionalTypes()
        {
            var assemblyContext       = CreateAssemblyContext();
            var additionalTypeID      = new object();
            var additionalType        = ReflectionObjectMother.GetSomeType();
            var otherAdditionalTypeID = new object();
            var otherAdditionalType   = ReflectionObjectMother.GetSomeType();

            _assemblyContextPoolMock.Expect(mock => mock.Dequeue()).Return(assemblyContext);

            _typeAssemblerMock
            .Expect(mock => mock.AssembleAdditionalType(null, null, null))
            .IgnoreArguments()
            .Return(
                new TypeAssemblyResult(
                    additionalType,
                    new Dictionary <object, Type> {
                { additionalTypeID, additionalType }, { otherAdditionalTypeID, otherAdditionalType }
            }));

            _assemblyContextPoolMock.Expect(mock => mock.Enqueue(assemblyContext));

            var result = _cache.GetOrCreateAdditionalType(additionalTypeID);

            _typeAssemblerMock.VerifyAllExpectations();
            _assemblyContextPoolMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(additionalType));
            Assert.That(_additionalTypes[additionalTypeID].IsValueCreated, Is.True);
            Assert.That(_additionalTypes[additionalTypeID].Value, Is.SameAs(additionalType));
        }
Esempio n. 20
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 });
        }
Esempio n. 21
0
        public void GetTypeCodeFast()
        {
            var runtimeType = ReflectionObjectMother.GetSomeType();
            var customType  = CustomTypeObjectMother.Create();

            Assert.That(runtimeType.GetTypePipeTypeCode(), Is.EqualTo(Type.GetTypeCode(runtimeType)));
            Assert.That(customType.GetTypePipeTypeCode(), Is.EqualTo(TypeCode.Object));
        }
Esempio n. 22
0
        public void Initialization_NullName()
        {
            var type        = ReflectionObjectMother.GetSomeType();
            var declaration = new ParameterDeclaration(type, name: null);

            Assert.That(declaration.Name, Is.Null);
            Assert.That(declaration.Expression.Name, Is.Null);
        }
Esempio n. 23
0
        public void IsRuntimeType()
        {
            var runtimeType = ReflectionObjectMother.GetSomeType();
            var customType  = CustomTypeObjectMother.Create();

            Assert.That(runtimeType.IsRuntimeType(), Is.True);
            Assert.That(customType.IsRuntimeType(), Is.False);
        }
        public static MutableFieldInfo Create(
            MutableType declaringType = null, string name = "_newField", Type type = null, FieldAttributes attributes = (FieldAttributes)7)
        {
            declaringType = declaringType ?? MutableTypeObjectMother.Create();
            type          = type ?? ReflectionObjectMother.GetSomeType();

            return(new MutableFieldInfo(declaringType, name, type, attributes));
        }
Esempio n. 25
0
        public void MemberType_NestedType_ForNotNullDeclaringType()
        {
            var declaringType = ReflectionObjectMother.GetSomeType();

            _customType.Invoke("SetDeclaringType", declaringType);

            Assert.That(_customType.MemberType, Is.EqualTo(MemberTypes.NestedType));
        }
Esempio n. 26
0
        public void SetUp()
        {
            _bindingFlagsEvaluatorMock = MockRepository.GenerateStrictMock <IBindingFlagsEvaluator>();

            _selector = new MemberSelector(_bindingFlagsEvaluatorMock);

            _someDeclaringType = ReflectionObjectMother.GetSomeType();
        }
Esempio n. 27
0
        public static MutableParameterInfo Create(
            MemberInfo member = null, int position = 7, string name = "abc", Type type = null, ParameterAttributes attributes = (ParameterAttributes)7)
        {
            member = member ?? ReflectionObjectMother.GetSomeMember();
            type   = type ?? ReflectionObjectMother.GetSomeType();

            return(new MutableParameterInfo(member, position, name, type, attributes));
        }
Esempio n. 28
0
        public void ReturnType()
        {
            var type = ReflectionObjectMother.GetSomeType();

            _customMethod.ReturnParameter_ = CustomParameterInfoObjectMother.Create(type: type);

            Assert.That(_customMethod.ReturnType, Is.SameAs(type));
        }
Esempio n. 29
0
        public void SetUp()
        {
            _bindingFlagsEvaluatorMock = new Mock <IBindingFlagsEvaluator> (MockBehavior.Strict);

            _selector = new MemberSelector(_bindingFlagsEvaluatorMock.Object);

            _someDeclaringType = ReflectionObjectMother.GetSomeType();
        }
Esempio n. 30
0
        public void SetUp()
        {
            var attributeTarget = ReflectionObjectMother.GetSomeType();

            _key1 = new CustomAttributeDataCacheKey(attributeTarget, inherit: true);
            _key2 = new CustomAttributeDataCacheKey(ReflectionObjectMother.GetSomeParameter(), true);
            _key3 = new CustomAttributeDataCacheKey(attributeTarget, false);
            _key4 = new CustomAttributeDataCacheKey(attributeTarget, true);
        }