Example #1
0
        public void GetPropertyValueTest()
        {
            string propertyName = "IntPropNonNullable";
            int expected = (new Random()).Next(int.MaxValue);
            object obj = new ReflectionUtilsMockObject() { IntPropNonNullable = expected };
            object actual = obj.GetPropertyValue(propertyName);
            Assert.AreEqual(expected, actual);

            propertyName = "SubObject.IntPropNonNullable";
            actual = obj.GetPropertyValue(propertyName);
            Assert.AreEqual(expected, actual);
        }
Example #2
0
        public void CopyMatchingObjectPropertiesToTest()
        {
            var source = new
            {
                IntPropNonNullable = 1,
                IntPropNullable = (int?)null,
                StringProp = "Source String",
                DateTimeProp = DateTime.Now,
                DateRangeProp = new DateRange(DateTime.Today, DateTime.Now),
                SubObject = new
                {
                    IntPropNonNullable = 1,
                    IntPropNullable = (int?)null,
                    StringProp = "Source String",
                },
                StringArrayProp = new string[3] {"Matt", "Joanne", "Robert"}
            };
            var dest = new ReflectionUtilsMockObject()
            {
                IntPropNonNullable = 10,
                IntPropNullable = 20,
                StringProp = "Destination String",
                DateTimeProp = DateTime.Now.AddDays(1),
                DateRangeProp = new DateRange(DateTime.Today.AddDays(1), DateTime.Now.AddDays(1)),
                StringArrayProp = (string[])null
            };
            Assert.AreNotEqual(source.IntPropNonNullable, dest.IntPropNonNullable, "IntPropNonNullable");
            Assert.AreNotEqual(source.IntPropNullable, dest.IntPropNullable, "IntPropNullable");
            Assert.AreNotEqual(source.StringProp, dest.StringProp, "StringProp");
            Assert.AreNotEqual(source.StringProp, dest.StringProp, "DateTimeProp");
            Assert.AreNotEqual(source.StringProp, dest.StringProp, "DateRangeProp");
            Assert.AreNotEqual(source.SubObject.IntPropNonNullable, dest.IntPropNonNullable, "SubObject.IntPropNonNullable");
            Assert.AreNotEqual(source.SubObject.IntPropNullable, dest.IntPropNullable, "SubObject.IntPropNullable");
            Assert.AreNotEqual(source.SubObject.StringProp, dest.StringProp, "SubObject.StringProp");
            Assert.AreNotEqual(source.SubObject.StringProp, dest.StringProp, "SubObject.DateTimeProp");
            Assert.AreNotEqual(source.SubObject.StringProp, dest.StringProp, "SubObject.DateRangeProp");

            ObjectExtensions.CopyMatchingObjectPropertiesTo(source, dest);

            Assert.AreEqual(source.IntPropNonNullable, dest.IntPropNonNullable, "IntPropNonNullable");
            Assert.AreEqual(source.IntPropNullable, dest.IntPropNullable, "IntPropNullable");
            Assert.AreEqual(source.StringProp, dest.StringProp, "StringProp");
            Assert.AreEqual(source.DateTimeProp, dest.DateTimeProp, "DateTimeProp");
            Assert.AreEqual(source.DateRangeProp, dest.DateRangeProp, "DateRangeProp");
            Assert.AreEqual(source.SubObject.IntPropNonNullable, dest.IntPropNonNullable, "SubObject.IntPropNonNullable");
            Assert.AreEqual(source.SubObject.IntPropNullable, dest.IntPropNullable, "SubObject.IntPropNullable");
            Assert.AreEqual(source.SubObject.StringProp, dest.StringProp, "SubObject.StringProp");
            Assert.AreEqual(source.SubObject.StringProp, dest.StringProp, "SubObject.DateTimeProp");
            Assert.AreEqual(source.SubObject.StringProp, dest.StringProp, "SubObject.DateRangeProp");
            Assert.AreEqual(source.StringArrayProp[0], dest.StringArrayProp[0], "StringArrayProp[0]");
            Assert.AreEqual(source.StringArrayProp[1], dest.StringArrayProp[1], "StringArrayProp[1]");
            Assert.AreEqual(source.StringArrayProp[2], dest.StringArrayProp[2], "StringArrayProp[2]");
        }
Example #3
0
        public void SetPropertyValueTest()
        {
            string propertyName = "IntPropNonNullable";
            int random = (new Random()).Next(int.MaxValue);
            int expected = random - (new Random()).Next(int.MaxValue);
            object obj = new ReflectionUtilsMockObject() { IntPropNonNullable = random };
            obj.SetPropertyValue(propertyName, expected);
            object actual = ((ReflectionUtilsMockObject)obj).IntPropNonNullable;
            Assert.AreEqual(expected, actual);

            propertyName = "SubObject.IntPropNonNullable";
            obj.SetPropertyValue(propertyName, expected);
            actual = ((ReflectionUtilsMockObject)obj).SubObject.IntPropNonNullable;
            Assert.AreEqual(expected, actual);
        }
 public ReflectionUtilsMockObject(ReflectionUtilsMockObject parent = null)
 {
     _Parent = parent;
 }
Example #5
0
 public void GetMethodsWithAttributeNotIneritedTest()
 {
     object instance = new ReflectionUtilsMockObject();
     Type attributeType = typeof(ReflectionUtilsMockAttribute);
     MethodInfo[] actual = ObjectExtensions.GetMethodsWithAttribute(instance, attributeType, false);
     Assert.AreEqual(1, actual.Length);
     Assert.AreEqual("VoidMethodWithAttribute", actual[0].Name);
 }
Example #6
0
 public void GetMethodsWithAttributeInvalidAttributeTest()
 {
     object instance = new ReflectionUtilsMockObject();
     Type attributeType = typeof(object);
     ObjectExtensions.GetMethodsWithAttribute(instance, attributeType, false);
 }