Ejemplo n.º 1
0
        public void ObjectMapConfig_CustomPropertyMappingRule()
        {
            var config = new ObjectMapConfig();
            config.PropertyMapRules.Clear();
            var rule = new SuffixPropertyMappingRule("Nullable");
            config.PropertyMapRules.Add(rule);

            var u1 = new User();
            var u2 = config.Map(u1, new User());

            Assert.AreEqual(u1.Name, u2.Name);
            Assert.AreEqual(u1.Int32, u2.Int32Nullable);
            Assert.AreEqual(u1.Decimal, u2.DecimalNullable);
            Assert.AreEqual(u1.DateTime, u2.DateTimeNullable);
            Assert.AreEqual(u1.DayOfWeek, u2.DayOfWeekNullable);
        }
Ejemplo n.º 2
0
        public void ObjectMapConfig_CustomStructConverter()
        {
            var config = new ObjectMapConfig();
            config.AddConverter(DayOfWeekConverter);

            config.PropertyMapRules.Clear();
            var rule = new PropertyNameMappingRule();
            rule.PropertyNameMaps.Add("Value", "DayOfWeek");
            config.PropertyMapRules.Add(rule);

            var u1 = new User();
            u1.Value = "Friday";
            var u2 = config.Map(u1, new User());

            Assert.AreEqual(DayOfWeek.Friday, u2.DayOfWeek);
        }
Ejemplo n.º 3
0
        public void ObjectMapConfig_CustomClassConverter()
        {
            var config = new ObjectMapConfig();
            config.AddConverter<MapPoint>(MapPointConverter);

            config.PropertyMapRules.Clear();
            var rule = new PropertyNameMappingRule();
            rule.PropertyNameMaps.Add("Value", "MapPoint");
            config.PropertyMapRules.Add(rule);

            var u1 = new User();
            u1.Value = "23, 45";
            var u2 = config.Map(u1, new User());

            Assert.AreEqual(23m, u2.MapPoint.Latitude);
            Assert.AreEqual(45m, u2.MapPoint.Longitude);
        }
Ejemplo n.º 4
0
        public void ObjectMapConfig_DefaultTypeConverter()
        {
            var config = new ObjectMapConfig();
            var rules = config.PropertyMapRules.ToArray();
            config.PropertyMapRules.Clear();

            config.PropertyMapRules.Clear();
            var rule = new PropertyNameMappingRule();
            rule.PropertyNameMaps.Add("Value", "Int32");
            rule.PropertyNameMaps.Add("Value", "DateTime");
            rule.PropertyNameMaps.Add("Value", "Decimal");
            rule.PropertyNameMaps.Add("Value", "DayOfWeek");
            config.PropertyMapRules.Add(rule);

            var u1 = new User();
            {
                u1.Value = "23";
                var u2 = config.Map(u1, new User());
                Assert.AreEqual(23, u2.Int32);
            }
            {
                u1.Value = "2014/12/17 00:00:00";
                var u2 = config.Map(u1, new User());
                Assert.AreEqual(new DateTime(2014, 12, 17), u2.DateTime);
            }
            {
                u1.Value = "23.4";
                var u2 = config.Map(u1, new User());
                Assert.AreEqual(23.4m, u2.Decimal);
            }
            {
                u1.Value = "Friday";
                var u2 = config.Map(u1, new User());
                Assert.AreEqual(DayOfWeek.Friday, u2.DayOfWeek);
            }
        }
Ejemplo n.º 5
0
        public void ObjectMapConfig_RemovePropertyMap()
        {
            var config = new ObjectMapConfig();
            config.PropertyMapRules.Clear();
            var rule = new SuffixPropertyMappingRule("Nullable");
            config.PropertyMapRules.Add(rule);
            config.RemovePropertyMap<User, User>(new String[] { "DecimalNullable", "DateTimeNullable", "DayOfWeekNullable" }, null);

            var u1 = new User();
            var u2 = config.Map(u1, new User());

            Assert.AreEqual(u1.Name, u2.Name);
            Assert.AreEqual(u1.Int32, u2.Int32Nullable);
            Assert.IsNull(u2.DecimalNullable);
            Assert.IsNull(u2.DateTimeNullable);
            Assert.IsNull(u2.DayOfWeekNullable);

            Assert.AreEqual(u1.MapPoint.Latitude, u2.MapPoint.Latitude);
            Assert.AreEqual(u1.MapPoint.Longitude, u2.MapPoint.Longitude);
        }
Ejemplo n.º 6
0
        public void ObjectMapConfig_Map_Object_Object_SetNullablePropertyToNull()
        {
            var config = new ObjectMapConfig();

            var u1 = new User();
            u1.DecimalNullable = null;
            var u2 = config.Map(u1, new User() { DecimalNullable = 23.4m });

            Assert.IsNull(u2.DecimalNullable);
        }
Ejemplo n.º 7
0
        public void ObjectMapConfig_Map_Object_Object_Convert_Failure()
        {
            var config = new ObjectMapConfig();
            config.PropertyMapRules.Clear();

            var rule = new PropertyNameMappingRule();
            rule.PropertyNameMaps.Add("Value", "Decimal");
            config.PropertyMapRules.Add(rule);

            var u1 = new User();
            u1.Value = "abc";
            var u2 = config.Map(u1, new User());
            //Not changed...
            Assert.AreEqual(20.4m, u2.Decimal);
        }
Ejemplo n.º 8
0
        public void ObjectMapConfig_Map_Object_Object()
        {
            var config = new ObjectMapConfig();

            var u1 = new User();

            var u2 = config.Map(u1, new User());

            Assert.AreEqual(u1.Name, u2.Name);
            Assert.AreEqual(u1.Int32, u2.Int32);
            Assert.AreEqual(u1.Decimal, u2.Decimal);
            Assert.AreEqual(u1.DateTime, u2.DateTime);
            Assert.AreEqual(u1.DayOfWeek, u2.DayOfWeek);

            Assert.AreEqual(u1.MapPoint.Latitude, u2.MapPoint.Latitude);
            Assert.AreEqual(u1.MapPoint.Longitude, u2.MapPoint.Longitude);
        }
Ejemplo n.º 9
0
        public void ObjectMapConfig_Map_Object_Dictionary()
        {
            var config = new ObjectMapConfig();

            var u1 = new User();
            var d = config.Map(u1, new Dictionary<String, Object>());

            Assert.AreEqual(u1.Name, d["Name"]);
            Assert.AreEqual(u1.Int32, d["Int32"]);
            Assert.AreEqual(u1.Decimal, d["Decimal"]);
            Assert.AreEqual(u1.DateTime, d["DateTime"]);
            Assert.AreEqual(u1.DayOfWeek, d["DayOfWeek"]);
            Assert.AreEqual(u1.MapPoint, d["MapPoint"]);
        }
Ejemplo n.º 10
0
        public void ObjectMapConfig_Map_ListProperty()
        {
            var config = new ObjectMapConfig();
            config.AddConverter<String[]>(o =>
            {
                if (o is String[])
                {
                    return new ConvertResult<String[]>(true, o as String[]);
                }
                return new ConvertResult<String[]>();
            });

            var u1 = new User();
            for (int i = 0; i < 3; i++)
            {
                u1.Users.Add(new User("TestUser" + i.ToString()));
            }
            var u2 = config.Map(u1, new User());

            Assert.AreEqual(0, u2.Users.Count);
        }
Ejemplo n.º 11
0
        public void ObjectMapConfig_Map_FromDecimalToInt32()
        {
            var config = new ObjectMapConfig();
            config.PropertyMapRules.Clear();

            var rule = new PropertyNameMappingRule();
            rule.PropertyNameMaps.Add("Int32", "Decimal");
            config.PropertyMapRules.Add(rule);

            var u1 = new User();
            u1.Int32 = 23;
            var u2 = config.Map(u1, new User());

            Assert.AreEqual(23m, u2.Decimal);
        }
Ejemplo n.º 12
0
        public void ObjectMapConfig_Map_ArrayProperty()
        {
            var config = new ObjectMapConfig();
            config.AddConverter<String[]>(o =>
            {
                if (o is String[])
                {
                    return new ConvertResult<String[]>(true, o as String[]);
                }
                return new ConvertResult<String[]>();
            });

            var u1 = new User();
            u1.Tags = new String[2];
            u1.Tags[0] = "News";
            u1.Tags[1] = "Sports";
            var u2 = config.Map(u1, new User());

            Assert.AreEqual(u1.Tags[0], u2.Tags[0]);
            Assert.AreEqual(u1.Tags[1], u2.Tags[1]);
        }