Exemplo n.º 1
0
        public void ClassWithProtectedProperties_ProtectedAccessorsShouldNotBeAvailable()
        {
            // Arrange:
            // const string altogetherProtectedProperty = "ImAltogetherProtected"; // Not expected to be available either as a source or destination property.
            const string propertyWithProtectedGetter  = "MyGetterIsProtected";
            var          propertyWithProtectedSetter  = MemberExpressions.GetMemberInfo <ClassWithProtectedProperties>(c => c.MySetterIsProtected).Name;
            var          altogetherAccessibleProperty = MemberExpressions.GetMemberInfo <ClassWithProtectedProperties>(c => c.ImAltogetherAccessible).Name;

            var expectedSourcePropertyNames     = new[] { propertyWithProtectedSetter, altogetherAccessibleProperty };
            var expectedDestinationProperyNames = new[] { propertyWithProtectedGetter, altogetherAccessibleProperty };

            // Act:
            var mappingCollection                 = new MappingCollection <ClassWithProtectedProperties, ClassWithProtectedProperties, object>(null);
            var availableSourcePropertyNames      = mappingCollection.Unmapped.Source.Select(sourceMember => sourceMember.Name).ToArray();
            var availableDestinationPropertyNames = mappingCollection.Unmapped.Destination.Select(destMember => destMember.Name).ToArray();

            // Assert:
            Array.Sort(expectedSourcePropertyNames);
            Array.Sort(availableSourcePropertyNames);
            Assert.AreEqual(expectedSourcePropertyNames, availableSourcePropertyNames);

            Array.Sort(expectedDestinationProperyNames);
            Array.Sort(availableDestinationPropertyNames);
            Assert.AreEqual(expectedDestinationProperyNames, availableDestinationPropertyNames);
        }
Exemplo n.º 2
0
        public void GetMemberInfo_DeepMemberChain_WithCast()
        {
            var memberInfo = MemberExpressions.GetMemberInfo <DeepClass>(c => ((string)c.String).Length);

            Assert.IsNotNull(memberInfo);
            Assert.AreEqual(MemberExpressions.GetMemberInfo <string>(c => c.Length), memberInfo);
        }
Exemplo n.º 3
0
        public void GetMemberInfo_LambdaExpression_Basic()
        {
            var info = (PropertyInfo)MemberExpressions.GetMemberInfo((ClassParent c) => c.Property1);

            Assert.AreEqual(typeof(ClassParent), info.ReflectedType);
            Assert.AreEqual(typeof(int), info.PropertyType);
        }
Exemplo n.º 4
0
        public void GetMemberInfo_ClassParent_SimpleField1()
        {
            var info = (FieldInfo)MemberExpressions.GetMemberInfo <ClassParent>(c => c.Field1);

            Assert.IsNotNull(info);
            Assert.AreEqual(typeof(ClassParent), info.ReflectedType);
            Assert.AreEqual("Field1", info.Name);
        }
Exemplo n.º 5
0
        public void GetMemberInfo_InheritedMember_ReturnsInheritingClassType()
        {
            var       instance = new ClassChild1();
            const int number   = 10;

            var info = (PropertyInfo)MemberExpressions.GetMemberInfo <ClassChild1>(c => c.Property1);

            Assert.AreEqual(typeof(ClassChild1), info.ReflectedType);
            info.SetValue(instance, number, null);
            Assert.AreEqual(number, instance.Property1);
        }
        public void Create_SimpleType_NamedMember()
        {
            var session = new BuildSession(null, null, new Random(0));
            var obj     = new SimpleType();

            session.PushObject(new ObjectBuildRecord(typeof(SimpleType), obj, true));
            session.PushMember(MemberExpressions.GetMemberInfo <SimpleType>(c => c.NamedMember));

            Assert.AreEqual("NamedMember0", Generator.CreateRecord(typeof(string), null, session).Object);
            Assert.AreEqual("NamedMember1", Generator.CreateRecord(typeof(string), null, session).Object);
            Assert.AreEqual("NamedMember2", Generator.CreateRecord(typeof(string), null, session).Object);
        }
Exemplo n.º 7
0
        public void GetMemberChain_DeepMemberChain_WithCast()
        {
            var memberInfo = MemberExpressions.GetExpressionChain <DeepClass>(c => ((string)c.String).Length);
            var expected   = new[]
            {
                MemberExpressions.GetMemberInfo <DeepClass>(c => c.String),
                MemberExpressions.GetMemberInfo <string>(c => c.Length)
            };

            Assert.IsNotNull(memberInfo);
            Assert.AreEqual(expected, memberInfo);
        }
Exemplo n.º 8
0
        public void GetMemberInfo_InheritedMember_NewProperty_BaseStillAccessible()
        {
            var       instance = new SpecificInterface();
            const int number   = 10;

            var info = (PropertyInfo)MemberExpressions.GetMemberInfo <IGeneralInterface>(c => c.Property1);

            Assert.AreEqual(typeof(IGeneralInterface), info.ReflectedType);
            Assert.AreEqual(typeof(object), info.PropertyType);

            info.SetValue(instance, number, null);
            Assert.AreEqual(number, instance.Property1);
        }
Exemplo n.º 9
0
        public void ResolveSource_MappingCollection_ReturnsNullIfNoMemberFound()
        {
            var searchMember = MemberExpressions.GetMemberInfo <ClassWithSeveralPropertiesDest>(c => c.Property2);
            var members      = new[]
            {
                MemberExpressions.GetMemberInfo <ClassWithSeveralPropertiesDest>(c => c.Property1),
                MemberExpressions.GetMemberInfo <ClassWithSeveralPropertiesDest>(c => c.Property3)
            };
            var mappingCollection = new Mock <IMappingCollection <object, object, object> >();

            mappingCollection.Setup(m => m.MemberResolvers).Returns(new PriorityList <IMemberResolver> {
                new IgnoreCaseNameMatcher()
            });
            mappingCollection.Setup(m => m.Unmapped.Source).Returns(members);
            Assert.IsNull(mappingCollection.Object.ResolveSource(searchMember));
        }
Exemplo n.º 10
0
 public IMappingCollection <TFrom, TTo, TContext> Ignore <TMemberType>(Expression <Func <TTo, TMemberType> > expression)
 {
     return(IgnoreMember(MemberExpressions.GetMemberInfo(expression)));
 }
Exemplo n.º 11
0
 public void GetMemberInfo_NonGeneric_NullMember_ThrowsException()
 {
     Assert.Throws <ArgumentNullException>(() => MemberExpressions.GetMemberInfo(null));
 }
Exemplo n.º 12
0
 public void GetMemberInfo_NonPropertyOrFieldType_ThrowsException()
 {
     Assert.Throws <MemberExpressionException>(() => MemberExpressions.GetMemberInfo <DeepClass>(c => c.String.Length + 1));
 }
Exemplo n.º 13
0
        public void GetAccessor_Property()
        {
            var accessor = MemberExpressions.GetMemberInfo <NormalPropertiesAndFields>(c => c.ValidProperty).GetAccessor();

            Assert.IsInstanceOf <PropertyAccessor <NormalPropertiesAndFields, int> >(accessor);
        }
Exemplo n.º 14
0
        public void GetAccessor_Field()
        {
            var accessor = MemberExpressions.GetMemberInfo <NormalPropertiesAndFields>(c => c.ValidField).GetAccessor();

            Assert.IsInstanceOf <FieldAccessor>(accessor);
        }
Exemplo n.º 15
0
 public void ReturnType_Property()
 {
     Assert.AreEqual(typeof(string), MemberExpressions.GetMemberInfo <DemoClass>(c => c.Property).ReturnType());
 }
Exemplo n.º 16
0
 public void ReturnType_Field()
 {
     Assert.AreEqual(typeof(int), MemberExpressions.GetMemberInfo <DemoClass>(c => c.Field).ReturnType());
 }