Exemplo n.º 1
0
        public void Initialization_InvalidMemberDeclaringType()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute());
            var property    = NormalizingMemberInfoFromExpressionUtility.GetProperty((DerivedAttribute attr) => attr.PropertyInDerivedType);

            new CustomAttributeDeclaration(constructor, new object[0], new NamedArgumentDeclaration(property, 7));
        }
Exemplo n.º 2
0
        public override void SetUp()
        {
            base.SetUp();

            _signedType   = typeof(int);
            _unsignedType = CreateUnsignedType(TypeAttributes.Class, typeof(object));

            _signedInterfaceType   = typeof(IMarkerInterface);
            _unsignedInterfaceType = CreateUnsignedType(TypeAttributes.Interface | TypeAttributes.Abstract, baseType: null);

            _signedDelegateType   = typeof(Action);
            _unsignedDelegateType = typeof(Action <>).MakeGenericType(_unsignedType);

            var attributeCtor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute(null));

            _signedAttribute   = new CustomAttributeDeclaration(attributeCtor, new object[] { _signedType });
            _unsignedAttribute = new CustomAttributeDeclaration(attributeCtor, new object[] { _unsignedType });

            _signedField   = NormalizingMemberInfoFromExpressionUtility.GetField(() => DomainType.Field);
            _unsignedField = _unsignedType.GetField("field");

            _signedCtor   = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType());
            _unsignedCtor = _unsignedType.GetConstructors().Single();

            _signedMethod   = NormalizingMemberInfoFromExpressionUtility.GetMethod(() => DomainType.Method());
            _unsignedMethod = _unsignedType.GetMethod("method");

            _signedVarArgsMethod   = typeof(DomainType).GetMethod("VarArgsMethod");
            _unsignedVarArgsMethod = _unsignedType.GetMethod("varargs");
        }
Exemplo n.º 3
0
        public void Initialization_MemberDeclaringTypesAreAssignable()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute());
            var property    = typeof(DomainAttribute).GetProperty("Property");

            new CustomAttributeDeclaration(constructor, new object[0], new NamedArgumentDeclaration(property, 7));
        }
Exemplo n.º 4
0
        public void OverrideExisting_AddCustomAttribute()
        {
            var type = AssembleType <DomainType> (
                proxyType =>
            {
                var existingProperty = NormalizingMemberInfoFromExpressionUtility.GetProperty((DomainType obj) => obj.ExistingProperty);
                var getMethod        = proxyType.GetOrAddOverride(existingProperty.GetGetMethod());
                var setMethod        = proxyType.GetOrAddOverride(existingProperty.GetSetMethod());
                var property         = proxyType.AddProperty(existingProperty.Name, PropertyAttributes.None, getMethod, setMethod);

                var attributeCtor    = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute(""));
                var customAttributes = new CustomAttributeDeclaration(attributeCtor, new object[] { "derived" });
                property.AddCustomAttribute(customAttributes);
            });

            var newProperty = type.GetProperty("ExistingProperty");
            var instance    = (DomainType)Activator.CreateInstance(type);

            Assert.That(instance.ExistingProperty, Is.Null);
            Assert.That(newProperty.GetValue(instance, null), Is.Null);
            newProperty.SetValue(instance, "Test", null);
            Assert.That(instance.ExistingProperty, Is.EqualTo("Test"));
            Assert.That(newProperty.GetValue(instance, null), Is.EqualTo("Test"));

            var attributeArgs = Attribute.GetCustomAttributes(newProperty, inherit: true).Cast <AbcAttribute>().Select(a => a.Arg);

            Assert.That(attributeArgs, Is.EquivalentTo(new[] { "base", "derived" }));
        }
        private void AddAbcAttribute(IMutableInfo customAttributeTarget, string value)
        {
            var ctor      = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute(""));
            var attribute = new CustomAttributeDeclaration(ctor, new object[] { value });

            customAttributeTarget.AddCustomAttribute(attribute);
        }
Exemplo n.º 6
0
        private CustomAttributeDeclaration CreateMultipleAttribute(string value)
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new MultipleAttribute());
            var field       = NormalizingMemberInfoFromExpressionUtility.GetField((MultipleAttribute obj) => obj.String);

            return(new CustomAttributeDeclaration(constructor, new object[0], new NamedArgumentDeclaration(field, value)));
        }
Exemplo n.º 7
0
        public override void Participate(object id, IProxyTypeAssemblyContext proxyTypeAssemblyContext)
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new ModifiedAssembledTypeAttribute());
            var attribute   = new CustomAttributeDeclaration(constructor, new object[0]);

            proxyTypeAssemblyContext.ProxyType.AddCustomAttribute(attribute);
        }
Exemplo n.º 8
0
        public void SetCustomAttribute()
        {
            var attributeCtor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute(null));
            var property      = NormalizingMemberInfoFromExpressionUtility.GetProperty((AbcAttribute obj) => obj.StringProperty);
            var field         = NormalizingMemberInfoFromExpressionUtility.GetField((AbcAttribute obj) => obj.IntField);

            var wasCalled = false;
            Action <CustomAttributeBuilder> setCustomAttributeMethod = customAttributeBuilder =>
            {
                wasCalled = true;
                Assert.That(customAttributeBuilder, Is.Not.Null);

                CheckCustomAttributeBuilder(
                    customAttributeBuilder,
                    attributeCtor,
                    new object[] { typeof(int) },
                    new[] { property },
                    new object[] { "def" },
                    new[] { field },
                    new object[] { 8 });
            };
            var adapterBasePartialMock = MockRepository.GeneratePartialMock <BuilderAdapterBase> (setCustomAttributeMethod);
            var declaration            = new CustomAttributeDeclaration(
                attributeCtor, new object[] { typeof(int) }, new NamedArgumentDeclaration(property, "def"), new NamedArgumentDeclaration(field, 8));

            adapterBasePartialMock.SetCustomAttribute(declaration);

            Assert.That(wasCalled, Is.True);
        }
Exemplo n.º 9
0
        public void Initialization()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute((ValueType)null));
            var property    = NormalizingMemberInfoFromExpressionUtility.GetProperty((DomainAttribute obj) => obj.Property);
            var field       = NormalizingMemberInfoFromExpressionUtility.GetField((DomainAttribute obj) => obj.Field);

            var declaration = new CustomAttributeDeclaration(
                constructor,
                new object[] { 7 },
                new NamedArgumentDeclaration(property, 7),
                new NamedArgumentDeclaration(field, "value"));

            Assert.That(declaration.Type, Is.SameAs(typeof(DomainAttribute)));
            Assert.That(declaration.Constructor, Is.SameAs(constructor));
            Assert.That(declaration.ConstructorArguments, Is.EqualTo(new[] { 7 }));
            var actualNamedArguments   = declaration.NamedArguments.Select(na => new { na.MemberInfo, na.Value });
            var expectedNamedArguments =
                new[]
            {
                new { MemberInfo = (MemberInfo)property, Value = (object)7 },
                new { MemberInfo = (MemberInfo)field, Value = (object)"value" }
            };

            Assert.That(actualNamedArguments, Is.EqualTo(expectedNamedArguments));
        }
Exemplo n.º 10
0
        public void Constructor()
        {
            var result = GetCustomAttributeDataAdapter(MethodBase.GetCurrentMethod());

            var expected = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute(null));

            Assert.That(result.Constructor, Is.EqualTo(expected));
        }
        public void GetConstructorSignature()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType(7, out Dev <string> .Dummy));

            var result = SignatureDebugStringGenerator.GetConstructorSignature(constructor);

            Assert.That(result, Is.EqualTo("Void .ctor(Int32, String&)"));
        }
Exemplo n.º 12
0
        public void CallBaseConstructor_NotVisibleFromProxy()
        {
            var ctor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType());

            Assert.That(ctor, Is.Not.Null);

            _context.CallBaseConstructor();
        }
Exemplo n.º 13
0
        private static void CheckTypeIDDataExpression(Expression result, Type requestedType, Expression idPartExpression)
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AssembledTypeIDData("name", null));
            var expected    = Expression.New(
                constructor, Expression.Constant(requestedType.AssemblyQualifiedName), Expression.NewArrayInit(typeof(IFlatValue), idPartExpression));

            ExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
0
        public void Initialization_WithNullArgument()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute((ValueType)null));

            var declaration = new CustomAttributeDeclaration(constructor, new object[] { null });

            Assert.That(declaration.ConstructorArguments[0], Is.Null);
        }
Exemplo n.º 16
0
        public void GetConstructor()
        {
            var member = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType());

            var expected = typeof(DomainType).GetConstructor(Type.EmptyTypes);

            Assert.That(member, Is.EqualTo(expected));
        }
Exemplo n.º 17
0
            public Expression GetFlatValueExpressionForSerialization(object id)
            {
                Assert.That(id, Is.EqualTo("identifier"));
                GetFlattenedExpressionForSerializationWasCalled = true;

                var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new FlatValueStub("real value"));

                return(Expression.New(constructor, Expression.Constant("identifier")));
            }
Exemplo n.º 18
0
        public void ConstructorConstant()
        {
            var template    = NormalizingMemberInfoFromExpressionUtility.GetMethod((DomainType o) => o.ConstructorConstant());
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new object());

            var instance = ReturnMemberInfoViaConstantExpression(template, constructor);

            Assert.That(instance.ConstructorConstant(), Is.EqualTo(constructor));
        }
        public void Initialization_InvalidConstructorArgumentCount()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute((ValueType)null));

            Assert.That(
                () => new CustomAttributeDeclaration(constructor, new object[] { 7, 8 }),
                Throws.ArgumentException
                .With.ArgumentExceptionMessageEqualTo("Expected 1 constructor argument(s), but was 2.", "constructorArguments"));
        }
        public void GetMemberSignature_Constructor()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType());

            var result = MemberSignatureProvider.GetMemberSignature(constructor);

            Assert.That(result, Is.TypeOf <MethodSignature> ());
            Assert.That(result.ToString(), Is.EqualTo("System.Void()"));
        }
Exemplo n.º 21
0
        public void CallBaseConstructor()
        {
            var arguments = new Expression[] { Expression.Constant("string") };

            var result = _context.CallBaseConstructor(arguments.AsOneTime());

            var expectedCtor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType(""));

            CheckConstructorCall(expectedCtor, arguments, result);
        }
        public void Initialization_NonPublicConstructor()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute("internal"));

            Assert.That(
                () => new CustomAttributeDeclaration(constructor, new object[] { "ctorArg" }),
                Throws.ArgumentException
                .With.ArgumentExceptionMessageEqualTo(
                    "The attribute constructor 'Void .ctor(System.String)' is not a public instance constructor.", "constructor"));
        }
Exemplo n.º 23
0
        public void GetConstructor()
        {
            var parameterTypes = new[] { typeof(string), typeof(int) };

            var result = _finder.GetConstructor(_requestedType, parameterTypes, false, _assembledType);

            var expectedConstructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AssembledType("", 0));

            Assert.That(result, Is.EqualTo(expectedConstructor));
        }
        public void Initialization_InvalidNullArgument()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute(0));

            Assert.That(
                () => new CustomAttributeDeclaration(constructor, new object[] { null }),
                Throws.ArgumentException
                .With.ArgumentExceptionMessageEqualTo(
                    "Constructor parameter at position 0 of type 'System.Int32' cannot be null.", "constructorArguments"));
        }
Exemplo n.º 25
0
        public void Initialization()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAspect());

            var result = new TypeConstruction(typeof(DomainAspect));

            Assert.That(result.ConstructorInfo, Is.EqualTo(constructor));
            Assert.That(result.ConstructorArguments, Is.Empty);
            Assert.That(result.NamedArguments, Is.Empty);
        }
        public void Initialization_NoAttributeType()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new NonAttributeClass());

            Assert.That(
                () => new CustomAttributeDeclaration(constructor, new object[0]),
                Throws.ArgumentException
                .With.ArgumentExceptionMessageEqualTo(
                    "Type 'Remotion.TypePipe.UnitTests.MutableReflection.CustomAttributeDeclarationTest+NonAttributeClass' does not derive from 'System.Attribute'.",
                    "constructor"));
        }
Exemplo n.º 27
0
        public void CallBaseConstructor_NotVisibleFromProxy()
        {
            var ctor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType());

            Assert.That(ctor, Is.Not.Null);
            Assert.That(
                () => _context.CallBaseConstructor(),
                Throws.InstanceOf <MemberAccessException>()
                .With.Message.EqualTo(
                    "The matching constructor is not visible from the proxy type."));
        }
        public void Initialization_NonVisibleCustomAttributeType()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new PrivateCustomAttribute());

            Assert.That(
                () => new CustomAttributeDeclaration(constructor, new object[0]),
                Throws.ArgumentException
                .With.ArgumentExceptionMessageEqualTo(
                    "The attribute type 'Remotion.TypePipe.UnitTests.MutableReflection.CustomAttributeDeclarationTest+PrivateCustomAttribute' is not publicly "
                    + "visible.", "constructor"));
        }
        public void Initialization_InvalidConstructorArgumentType()
        {
            var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute((ValueType)null));

            Assert.That(
                () => new CustomAttributeDeclaration(constructor, new object[] { "string" }),
                Throws.ArgumentException
                .With.ArgumentExceptionMessageEqualTo(
                    "Item 0 of parameter 'constructorArguments' has the type 'System.String' instead of 'System.ValueType'.",
                    "constructorArguments"));
        }
Exemplo n.º 30
0
        private static IParticipant CreateFieldAddingParticipant()
        {
            var attributeConstructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new NonSerializedAttribute());

            return(CreateParticipant(
                       proxyType =>
            {
                proxyType.AddField("AddedIntField", FieldAttributes.Public, typeof(int));
                proxyType.AddField("AddedSkippedIntField", FieldAttributes.Public, typeof(int))
                .AddCustomAttribute(new CustomAttributeDeclaration(attributeConstructor, new object[0]));
            }));
        }