Exemple #1
0
        private void SetupDefineCustomAttribute <T> (Mock <T> customAttributeTargetBuilderMock, IMutableInfo mutableInfo) where T : class, ICustomAttributeTargetBuilder
        {
            var declaration = CustomAttributeDeclarationObjectMother.Create();

            mutableInfo.AddCustomAttribute(declaration);
            customAttributeTargetBuilderMock.Setup(mock => mock.SetCustomAttribute(declaration)).Verifiable();
        }
        private void SetupDefineCustomAttribute(ICustomAttributeTargetBuilder customAttributeTargetBuilderMock, IMutableInfo mutableInfo)
        {
            var declaration = CustomAttributeDeclarationObjectMother.Create();

            mutableInfo.AddCustomAttribute(declaration);
            customAttributeTargetBuilderMock.Expect(mock => mock.SetCustomAttribute(declaration));
        }
Exemple #3
0
        public void GetCustomAttributeData()
        {
            var customAttributes = new[] { CustomAttributeDeclarationObjectMother.Create() };
            var evt = CustomEventInfoObjectMother.Create(customAttributes: customAttributes);
            var evtInstantiation = new EventOnTypeInstantiation(_declaringType, evt, _addMethod, _removeMethod, _raiseMethod);

            Assert.That(evtInstantiation.GetCustomAttributeData(), Is.EqualTo(customAttributes));
        }
Exemple #4
0
        public void Attributes_NonSerialized()
        {
            Assert.That(_field.IsNotSerialized, Is.False);

            _field.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create(typeof(NonSerializedAttribute)));

            Assert.That(_field.IsNotSerialized, Is.True);
        }
Exemple #5
0
        public void GetCustomAttributeData()
        {
            var customAttributes  = new[] { CustomAttributeDeclarationObjectMother.Create() };
            var ctor              = CustomConstructorInfoObjectMother.Create(customAttributes: customAttributes);
            var ctorInstantiation = new ConstructorOnTypeInstantiation(_declaringType, ctor);

            Assert.That(ctorInstantiation.GetCustomAttributeData(), Is.EqualTo(customAttributes));
        }
        public void GetCustomAttributeData()
        {
            var customAttributes        = new[] { CustomAttributeDeclarationObjectMother.Create() };
            var genericMethodDefinition = CustomMethodInfoObjectMother.Create(typeArguments: new[] { _typeArgument }, customAttributes: customAttributes);
            var instantiation           = MethodInstantiationObjectMother.Create(genericMethodDefinition);

            Assert.That(instantiation.GetCustomAttributeData(), Is.EqualTo(customAttributes));
        }
        public void GetCustomAttributeData()
        {
            var customAttributes      = new[] { CustomAttributeDeclarationObjectMother.Create() };
            var property              = CustomPropertyInfoObjectMother.Create(customAttributes: customAttributes);
            var propertyInstantiation = new PropertyOnTypeInstantiation(_declaringType, property, _getMethod, _setMethod);

            Assert.That(propertyInstantiation.GetCustomAttributeData(), Is.EqualTo(customAttributes));
        }
        public void GetCustomAttributeData()
        {
            var customAttributes    = new[] { CustomAttributeDeclarationObjectMother.Create() };
            var method              = CustomMethodInfoObjectMother.Create(customAttributes: customAttributes);
            var methodInstantiation = new MethodOnTypeInstantiation(_declaringType, method);

            Assert.That(methodInstantiation.GetCustomAttributeData(), Is.EqualTo(customAttributes));
        }
        public void AddCustomAttribute()
        {
            var declaration = CustomAttributeDeclarationObjectMother.Create();

            _container.AddCustomAttribute(declaration);

            Assert.That(_container.AddedCustomAttributes, Is.EqualTo(new[] { declaration }));
        }
Exemple #10
0
        public void CustomAttributeMethods()
        {
            var declaration = CustomAttributeDeclarationObjectMother.Create(typeof(ObsoleteAttribute));

            _mutableType.AddCustomAttribute(declaration);

            Assert.That(_mutableType.AddedCustomAttributes, Is.EqualTo(new[] { declaration }));
            Assert.That(_mutableType.GetCustomAttributeData().Select(a => a.Type), Is.EquivalentTo(new[] { typeof(ObsoleteAttribute) }));
        }
Exemple #11
0
        public void SetUp()
        {
            var customAttributes = new[] { CustomAttributeDeclarationObjectMother.Create() };

            _elementType = CustomTypeObjectMother.Create(
                name: "Abc", @namespace: "MyNs", typeArguments: new[] { typeof(int) }, customAttributeDatas: customAttributes);

            _type = new ByRefType(_elementType);
        }
Exemple #12
0
        public void GetCustomAttributes()
        {
            var datas = new[] { CustomAttributeDeclarationObjectMother.Create(typeof(ObsoleteAttribute)) };

            _providerMock.Setup(mock => mock.GetCustomAttributeData(_randomInherit)).Returns(datas).Verifiable();

            var result = CustomAttributeFinder.GetCustomAttributes(_providerMock.Object, _randomInherit);

            Assert.That(result.Select(a => a.GetType()), Is.EqualTo(new[] { typeof(ObsoleteAttribute) }));
        }
        public void CustomAttributeMethods()
        {
            _constructor.CustomAttributeDatas = new[] { CustomAttributeDeclarationObjectMother.Create(typeof(ObsoleteAttribute)) };

            Assert.That(_constructor.GetCustomAttributes(false).Select(a => a.GetType()), Is.EqualTo(new[] { typeof(ObsoleteAttribute) }));
            Assert.That(_constructor.GetCustomAttributes(typeof(NonSerializedAttribute), false), Is.Empty);

            Assert.That(_constructor.IsDefined(typeof(ObsoleteAttribute), false), Is.True);
            Assert.That(_constructor.IsDefined(typeof(NonSerializedAttribute), false), Is.False);
        }
        public void GetCustomAttributeData()
        {
            var member          = ReflectionObjectMother.GetSomeProperty();
            var customAttribute = CustomAttributeDeclarationObjectMother.Create();
            var parameter       = CustomParameterInfoObjectMother.Create(customAttributes: new ICustomAttributeData[] { customAttribute });

            var wrapper = new PropertyParameterInfoWrapper(member, parameter);

            Assert.That(wrapper.GetCustomAttributeData(), Is.EqualTo(new[] { customAttribute }));
        }
        public void GetCustomAttributeData()
        {
            var customAttributes = new[] { CustomAttributeDeclarationObjectMother.Create() };
            var member           = MethodOnTypeInstantiationObjectMother.Create();
            var parameter        = CustomParameterInfoObjectMother.Create(member, customAttributes: customAttributes);

            var parameterInstantiation = new MemberParameterOnInstantiation(member, parameter);

            Assert.That(parameterInstantiation.GetCustomAttributeData(), Is.EqualTo(customAttributes));
        }
Exemple #16
0
        public void SetUp()
        {
            var customAttributes = new[] { CustomAttributeDeclarationObjectMother.Create() };

            _elementType = CustomTypeObjectMother.Create(name: "Abc", @namespace: "MyNs", customAttributeDatas: customAttributes);
            _rank        = 2;

            _type = new TestableArrayTypeBase(_elementType, _rank);

            _realArrayTypeForComparison = typeof(string[]);
        }
        public void GetCustomAttributes_NewInstance()
        {
            var datas = new ICustomAttributeData[] { CustomAttributeDeclarationObjectMother.Create() };

            _providerMock.Expect(mock => mock.GetCustomAttributeData(_randomInherit)).Return(datas).Repeat.Twice();

            var attribute1 = CustomAttributeFinder.GetCustomAttributes(_providerMock, _randomInherit).Single();
            var attribute2 = CustomAttributeFinder.GetCustomAttributes(_providerMock, _randomInherit).Single();

            Assert.That(attribute1, Is.Not.SameAs(attribute2));
        }
        public void CustomAttributeMethods()
        {
            var customAttribute = CustomAttributeDeclarationObjectMother.Create(typeof(ObsoleteAttribute));
            var property        = CustomPropertyInfoObjectMother.Create(customAttributes: new[] { customAttribute });

            Assert.That(property.GetCustomAttributes(false).Select(a => a.GetType()), Is.EqualTo(new[] { typeof(ObsoleteAttribute) }));
            Assert.That(property.GetCustomAttributes(typeof(NonSerializedAttribute), false), Is.Empty);

            Assert.That(property.IsDefined(typeof(ObsoleteAttribute), false), Is.True);
            Assert.That(property.IsDefined(typeof(NonSerializedAttribute), false), Is.False);
        }
        public void GetCustomAttributes_Filter()
        {
            var datas = new[] { CustomAttributeDeclarationObjectMother.Create(typeof(DerivedAttribute)) };

            _providerMock.Expect(mock => mock.GetCustomAttributeData(_randomInherit)).Return(datas).Repeat.Times(4);

            Assert.That(CustomAttributeFinder.GetCustomAttributes(_providerMock, typeof(UnrelatedAttribute), _randomInherit), Is.Empty);
            Assert.That(CustomAttributeFinder.GetCustomAttributes(_providerMock, typeof(DerivedAttribute), _randomInherit), Has.Length.EqualTo(1));
            Assert.That(CustomAttributeFinder.GetCustomAttributes(_providerMock, typeof(BaseAttribute), _randomInherit), Has.Length.EqualTo(1));
            Assert.That(CustomAttributeFinder.GetCustomAttributes(_providerMock, typeof(IBaseAttributeInterface), _randomInherit), Has.Length.EqualTo(1));
        }
        public void IsDefined()
        {
            var datas = new[] { CustomAttributeDeclarationObjectMother.Create(typeof(DerivedAttribute)) };

            _providerMock.Expect(mock => mock.GetCustomAttributeData(_randomInherit)).Return(datas).Repeat.Times(4);

            Assert.That(CustomAttributeFinder.IsDefined(_providerMock, typeof(UnrelatedAttribute), _randomInherit), Is.False);
            Assert.That(CustomAttributeFinder.IsDefined(_providerMock, typeof(DerivedAttribute), _randomInherit), Is.True);
            Assert.That(CustomAttributeFinder.IsDefined(_providerMock, typeof(BaseAttribute), _randomInherit), Is.True);
            Assert.That(CustomAttributeFinder.IsDefined(_providerMock, typeof(IBaseAttributeInterface), _randomInherit), Is.True);
        }
Exemple #21
0
        public void GetAttributeFlagsImpl_Serializable()
        {
            var proxyType = MutableTypeObjectMother.Create(memberSelector: _memberSelectorMock);

            _memberSelectorMock.Stub(stub => stub.SelectMethods <MethodInfo> (null, 0, null)).IgnoreArguments().Return(new MethodInfo[0]);
            Assert.That(proxyType.IsTypePipeSerializable(), Is.False);

            proxyType.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create(typeof(SerializableAttribute)));

            Assert.That(proxyType.IsTypePipeSerializable(), Is.True);
        }
        public void AddCustomAttribute_AlreadyPresent()
        {
            _container.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create(typeof(SingleAttribute)));
            _container.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create(typeof(MultipleAttribute)));

            Assert.That(
                () => _container.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create(typeof(SingleAttribute))),
                Throws.InvalidOperationException.With.Message.EqualTo(
                    "Attribute of type 'SingleAttribute' (with AllowMultiple = false) is already present."));
            Assert.That(() => _container.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create(typeof(MultipleAttribute))), Throws.Nothing);
        }
Exemple #23
0
        public void GetCustomAttributes_NewInstance()
        {
            var datas = new ICustomAttributeData[] { CustomAttributeDeclarationObjectMother.Create() };

            _providerMock.Setup(mock => mock.GetCustomAttributeData(_randomInherit)).Returns(datas);

            var attribute1 = CustomAttributeFinder.GetCustomAttributes(_providerMock.Object, _randomInherit).Single();
            var attribute2 = CustomAttributeFinder.GetCustomAttributes(_providerMock.Object, _randomInherit).Single();

            Assert.That(attribute1, Is.Not.SameAs(attribute2));
            _providerMock.Verify(mock => mock.GetCustomAttributeData(_randomInherit), Times.Exactly(2));
        }
Exemple #24
0
        public void GetAttributeFlagsImpl_Serializable()
        {
            var proxyType = MutableTypeObjectMother.Create(memberSelector: _memberSelectorMock.Object);

            _memberSelectorMock
            .Setup(stub => stub.SelectMethods <MethodInfo> (It.IsAny <IEnumerable <MethodInfo> >(), It.IsAny <BindingFlags>(), It.IsAny <Type>()))
            .Returns(new MethodInfo[0]);
            Assert.That(proxyType.IsTypePipeSerializable(), Is.False);

            proxyType.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create(typeof(SerializableAttribute)));

            Assert.That(proxyType.IsTypePipeSerializable(), Is.True);
        }
Exemple #25
0
        public void DefineTypeFacets()
        {
            var typeInitializer = _mutableType.AddTypeInitializer(ctx => Expression.Empty());

            var instanceInitialization = ExpressionTreeObjectMother.GetSomeExpression();

            _mutableType.AddInitialization(ctx => instanceInitialization);

            var customAttribute = CustomAttributeDeclarationObjectMother.Create();

            _mutableType.AddCustomAttribute(customAttribute);

            var @interface = typeof(IDisposable);

            _mutableType.AddInterface(@interface);

            var field       = _mutableType.AddField();
            var constructor = _mutableType.AddConstructor();
            var method      = _mutableType.AddMethod();
            var property    = _mutableType.AddProperty();
            var event_      = _mutableType.AddEvent();

            using (_mockRepository.Ordered())
            {
                var context = PopulateContext(_generator, 2);

                _typeBuilderMock.Expect(mock => mock.SetParent(_mutableType.BaseType));

                _memberEmitterMock.Expect(mock => mock.AddConstructor(context, typeInitializer));

                _initializationBuilderMock.Expect(mock => mock.CreateInitializationMembers(_mutableType)).Return(_fakeInitializationMembers);
                _proxySerializationEnablerMock.Expect(mock => mock.MakeSerializable(_mutableType, _fakeInitializationMethod));

                _typeBuilderMock.Expect(mock => mock.SetCustomAttribute(customAttribute));
                _typeBuilderMock.Expect(mock => mock.AddInterfaceImplementation(@interface));
                _memberEmitterMock.Expect(mock => mock.AddField(context, field));
                _initializationBuilderMock.Expect(
                    mock => mock.WireConstructorWithInitialization(constructor, _fakeInitializationMembers, _proxySerializationEnablerMock));
                _memberEmitterMock.Expect(mock => mock.AddConstructor(context, constructor));
                _memberEmitterMock.Expect(mock => mock.AddMethod(context, method));
                SetupExpectationsForAccessors(_memberEmitterMock, _mutableType.AddedMethods.Except(new[] { method }));
                _memberEmitterMock.Expect(mock => mock.AddProperty(context, property));
                _memberEmitterMock.Expect(mock => mock.AddEvent(context, event_));
            }
            _mockRepository.ReplayAll();

            _generator.DefineTypeFacets();

            _mockRepository.VerifyAll();
        }
Exemple #26
0
        public void FlushCodeToDisk_WithException_ReturnsAssemblyContextToPool()
        {
            var expectedException = new Exception();
            var assemblyAttribute = CustomAttributeDeclarationObjectMother.Create();

            var generatedCodeFlusherMock1 = new Mock <IGeneratedCodeFlusher> (MockBehavior.Strict);
            var assemblyContext1          = new AssemblyContext(new Mock <IMutableTypeBatchCodeGenerator> (MockBehavior.Strict).Object, generatedCodeFlusherMock1.Object);

            var generatedCodeFlusherMock2 = new Mock <IGeneratedCodeFlusher> (MockBehavior.Strict);
            var assemblyContext2          = new AssemblyContext(new Mock <IMutableTypeBatchCodeGenerator> (MockBehavior.Strict).Object, generatedCodeFlusherMock2.Object);

            var generatedCodeFlusherMock3 = new Mock <IGeneratedCodeFlusher> (MockBehavior.Strict);
            var assemblyContext3          = new AssemblyContext(new Mock <IMutableTypeBatchCodeGenerator> (MockBehavior.Strict).Object, generatedCodeFlusherMock3.Object);

            _assemblyContextPool
            .Setup(mock => mock.DequeueAll())
            .Returns(new[] { assemblyContext1, assemblyContext2, assemblyContext3 })
            .Verifiable();

            generatedCodeFlusherMock1
            .Setup(mock => mock.FlushCodeToDisk(It.IsAny <IEnumerable <CustomAttributeDeclaration> >()))
            .Returns("path1")
            .Verifiable();

            generatedCodeFlusherMock2
            .Setup(mock => mock.FlushCodeToDisk(It.IsAny <IEnumerable <CustomAttributeDeclaration> >()))
            .Throws(expectedException)
            .Verifiable();

            generatedCodeFlusherMock3
            .Setup(mock => mock.FlushCodeToDisk(It.IsAny <IEnumerable <CustomAttributeDeclaration> >()))
            .Returns("path3")
            .Verifiable();

            _assemblyContextPool.Setup(mock => mock.Enqueue(assemblyContext1)).Verifiable();
            _assemblyContextPool.Setup(mock => mock.Enqueue(assemblyContext2)).Verifiable();
            _assemblyContextPool.Setup(mock => mock.Enqueue(assemblyContext3)).Verifiable();

            var aggregateException = Assert.Throws <AggregateException> (() => _manager.FlushCodeToDisk(new[] { assemblyAttribute }));

            Assert.That(aggregateException.InnerExceptions, Is.EquivalentTo(new[] { expectedException }));

            _assemblyContextPool.Verify();
            generatedCodeFlusherMock1.Verify();
            generatedCodeFlusherMock2.Verify();
            generatedCodeFlusherMock3.Verify();
        }
Exemple #27
0
        public void FlushCodeToDisk_WithException_ReturnsAssemblyContextToPool()
        {
            var expectedException = new Exception();
            var assemblyAttribute = CustomAttributeDeclarationObjectMother.Create();

            var generatedCodeFlusherMock1 = MockRepository.GenerateStrictMock <IGeneratedCodeFlusher>();
            var assemblyContext1          = new AssemblyContext(MockRepository.GenerateStrictMock <IMutableTypeBatchCodeGenerator>(), generatedCodeFlusherMock1);

            var generatedCodeFlusherMock2 = MockRepository.GenerateStrictMock <IGeneratedCodeFlusher>();
            var assemblyContext2          = new AssemblyContext(MockRepository.GenerateStrictMock <IMutableTypeBatchCodeGenerator>(), generatedCodeFlusherMock2);

            var generatedCodeFlusherMock3 = MockRepository.GenerateStrictMock <IGeneratedCodeFlusher>();
            var assemblyContext3          = new AssemblyContext(MockRepository.GenerateStrictMock <IMutableTypeBatchCodeGenerator>(), generatedCodeFlusherMock3);

            _assemblyContextPool
            .Expect(mock => mock.DequeueAll())
            .Return(new[] { assemblyContext1, assemblyContext2, assemblyContext3 });

            generatedCodeFlusherMock1
            .Expect(mock => mock.FlushCodeToDisk(Arg <IEnumerable <CustomAttributeDeclaration> > .Is.Anything))
            .Return("path1");

            generatedCodeFlusherMock2
            .Expect(mock => mock.FlushCodeToDisk(Arg <IEnumerable <CustomAttributeDeclaration> > .Is.Anything))
            .Throw(expectedException);

            generatedCodeFlusherMock3
            .Expect(mock => mock.FlushCodeToDisk(Arg <IEnumerable <CustomAttributeDeclaration> > .Is.Anything))
            .Return("path3");

            _assemblyContextPool.Expect(mock => mock.Enqueue(assemblyContext1));
            _assemblyContextPool.Expect(mock => mock.Enqueue(assemblyContext2));
            _assemblyContextPool.Expect(mock => mock.Enqueue(assemblyContext3));

            var aggregateException = Assert.Throws <AggregateException> (() => _manager.FlushCodeToDisk(new[] { assemblyAttribute }));

            Assert.That(aggregateException.InnerExceptions, Is.EquivalentTo(new[] { expectedException }));

            _assemblyContextPool.VerifyAllExpectations();
            generatedCodeFlusherMock1.VerifyAllExpectations();
            generatedCodeFlusherMock2.VerifyAllExpectations();
            generatedCodeFlusherMock3.VerifyAllExpectations();
        }
Exemple #28
0
        public void FlushCodeToDisk()
        {
            DefineSomeType();
            var assemblyAttribute   = CustomAttributeDeclarationObjectMother.Create();
            var fakeAssemblyPath    = "fake path";
            var assemblyBuilderMock = MockRepository.GenerateStrictMock <IAssemblyBuilder>();

            _moduleBuilderMock.Expect(mock => mock.AssemblyBuilder).Return(assemblyBuilderMock);
            assemblyBuilderMock.Expect(mock => mock.SetCustomAttribute(assemblyAttribute));
            assemblyBuilderMock.Expect(mock => mock.SaveToDisk()).Return(fakeAssemblyPath);
            var previousDebugInfoGenerator = _flusher.DebugInfoGenerator;

            var result = _flusher.FlushCodeToDisk(new[] { assemblyAttribute });

            _moduleBuilderMock.VerifyAllExpectations();
            assemblyBuilderMock.VerifyAllExpectations();
            Assert.That(result, Is.EqualTo(fakeAssemblyPath));
            Assert.That(_flusher.DebugInfoGenerator, Is.Not.EqualTo(previousDebugInfoGenerator));
        }
        public void IsModified_Constructor_AddedAttribute()
        {
            _addedCtor.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create());

            Assert.That(_tracker.IsModified(), Is.True);
        }
Exemple #30
0
 public static void ModifyField(MutableFieldInfo mutableField)
 {
     mutableField.AddCustomAttribute(CustomAttributeDeclarationObjectMother.Create());
 }