public void ComplexObject_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter();
            objectConverter.RegisterConverter(1, new CreationDictionaryObjectConverter("xtype", new DomainTypeResolver(true)));

            Dictionary<string, object> Peter = new Dictionary<string, object>();
            Peter["Name"] = "Peter";

            List<object> pets = new List<object>();
            Dictionary<string, object> xcat = new Dictionary<string, object>();
            xcat["xtype"] = typeof(Cat).FullName;
            xcat["Name"] = "cat1";
            xcat["Age"] = 3;
            pets.Add(xcat);

            Dictionary<string, object> xdog = new Dictionary<string, object>();
            xdog["xtype"] = typeof(Dog).FullName;
            xdog["Name"] = "dog1";
            xdog["Age"] = 5;
            pets.Add(xdog);

            Peter["Pets"] = pets.ToArray();
            Peter["PetList"] = pets.ToArray();

            Person obj = new Person();
            objectConverter.MapObject(Peter, obj);

            Assert.IsNotNull(obj.Pets);
            Assert.AreEqual(2, obj.Pets.Length);

            var cat = obj.Pets[0] as Cat;
            Assert.IsNotNull(cat);
            Assert.AreEqual("cat1", cat.Name);
            Assert.AreEqual(3, cat.Age);

            var dot = obj.Pets[1] as Dog;
            Assert.IsNotNull(dot);
            Assert.AreEqual("dog1", dot.Name);
            Assert.AreEqual(5, dot.Age);

            // check list
            Assert.IsNotNull(obj.PetList);
            Assert.AreEqual(2, obj.PetList.Count);

            cat = obj.PetList[0] as Cat;
            Assert.IsNotNull(cat);
            Assert.AreEqual("cat1", cat.Name);
            Assert.AreEqual(3, cat.Age);

            dot = obj.PetList[1] as Dog;
            Assert.IsNotNull(dot);
            Assert.AreEqual("dog1", dot.Name);
            Assert.AreEqual(5, dot.Age);
        }
예제 #2
0
        public void MapUnexistPropertyException_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter();

            var target = new MapWithoutPropertyTestObject1_Copy();
            target.Name = "QQ";

            MapWithoutPropertyTestObject1 obj = new MapWithoutPropertyTestObject1();

            objectConverter.MapObject(target, obj);
            Assert.AreEqual("QQ", obj.Name);
            Assert.AreEqual(100, obj.Age);
        }
예제 #3
0
 public void MapNullStringArray_Test()
 {
     var obj1 = new StringArrayTest
         {
             StringArray = default(string[])
         };
     var obj2 = new StringArrayTest
         {
             StringArray = (string[])null,
         };
     Assert.IsNull(obj1.StringArray);
     ObjectConverter objectConverter = new ObjectConverter();
     objectConverter.MapObject(obj1, obj2);
     Assert.IsNull(obj2.StringArray);
 }
예제 #4
0
        public void MapUnexistPropertyException_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter();

            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));

            dt.Rows.Add("QQ");
            var reader = new DataTableReader(dt);
            reader.Read();
            MapWithoutPropertyTestObject1 obj = new MapWithoutPropertyTestObject1();
            objectConverter.MapObject(reader, obj);

            Assert.AreEqual("QQ", obj.Name);
            Assert.AreEqual(100, obj.Age);
        }
예제 #5
0
        public void Map_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter();

            DateTime tm = DateTime.Parse("1998-01-01");
            MapTestObject1_Copy dt = new MapTestObject1_Copy();
            dt.int16Var = 1;
            dt.int32Var = 2;
            dt.int64Var = 3;
            dt.datetimeVar = tm;
            dt.decimalVar = 1M;
            dt.floatVar = 2f;
            dt.doubleVar = 3;

            dt.stringVar = "hello";

            MapTestObject1 obj = new MapTestObject1();
            objectConverter.MapObject(dt, obj);
            Assert.AreEqual(1, obj.int16Var);
            Assert.AreEqual(2, obj.int32Var);
            Assert.AreEqual(3, obj.int64Var);
            Assert.AreEqual(tm, obj.datetimeVar);
            Assert.AreEqual(1M, obj.decimalVar);
            Assert.AreEqual(2f, obj.floatVar);
            Assert.AreEqual(3d, obj.doubleVar);

            MapTestObject2 obj2 = new MapTestObject2();
            objectConverter.MapObject(dt, obj2);
            Assert.AreEqual((short)1, obj2.int16Var);
            Assert.AreEqual(2, obj2.int32Var);
            Assert.AreEqual((long)3, obj2.int64Var);
            Assert.AreEqual(tm, obj2.datetimeVar);
            Assert.AreEqual(1M, obj2.decimalVar);
            Assert.AreEqual(2f, obj2.floatVar);
            Assert.AreEqual(3d, obj2.doubleVar);
        }
예제 #6
0
        public void MapUnexistProperty_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter()
            {
                IsIgnoreNotExistingProperty = true,
            };

            var target = new MapWithoutPropertyTestObject1_Copy();
            target.Name = "QQ";

            MapWithoutPropertyTestObject1 obj = new MapWithoutPropertyTestObject1();

            objectConverter.MapObject(target, obj);
            Assert.AreEqual("QQ", obj.Name);
            Assert.AreEqual(100, obj.Age);
        }
예제 #7
0
        public void Map_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter();

            DataTable dt = new DataTable();
            dt.Columns.Add("int16Var", typeof(Int16));
            dt.Columns.Add("int32Var", typeof(Int32));
            dt.Columns.Add("int64Var", typeof(Int64));
            dt.Columns.Add("datetimeVar", typeof(DateTime));
            dt.Columns.Add("decimalVar", typeof(decimal));
            dt.Columns.Add("floatVar", typeof(float));
            dt.Columns.Add("doubleVar", typeof(double));

            dt.Columns.Add("stringVar", typeof(string));
            dt.Columns.Add("MyEnum", typeof(int));
            DateTime tm = DateTime.Parse("1998-01-01");
            dt.Rows.Add(1, 2, 3, tm, 1M, 2f, 3d, "hello", MyEnum.A);

            DataTableReader reader = new DataTableReader(dt);
            MapTestObject1 obj = new MapTestObject1();
            reader.Read();
            objectConverter.MapObject(reader, obj);
            Assert.AreEqual(1, obj.int16Var);
            Assert.AreEqual(2, obj.int32Var);
            Assert.AreEqual(3, obj.int64Var);
            Assert.AreEqual(tm, obj.datetimeVar);
            Assert.AreEqual(1M, obj.decimalVar);
            Assert.AreEqual(2f, obj.floatVar);
            Assert.AreEqual(3d, obj.doubleVar);
            Assert.AreEqual(MyEnum.A, obj.MyEnum);

            MapTestObject2 obj2 = new MapTestObject2();
            objectConverter.MapObject(reader, obj2);
            Assert.AreEqual((short)1, obj2.int16Var);
            Assert.AreEqual(2, obj2.int32Var);
            Assert.AreEqual((long)3, obj2.int64Var);
            Assert.AreEqual(tm, obj2.datetimeVar);
            Assert.AreEqual(1M, obj2.decimalVar);
            Assert.AreEqual(2f, obj2.floatVar);
            Assert.AreEqual(3d, obj2.doubleVar);
            Assert.AreEqual(MyEnum.A, obj2.MyEnum);
        }
        public void PrivateSetter_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter()
            {
                IsIgnoreNotExistingProperty = true,
            };

            Dictionary<string, object> dt = new Dictionary<string, object>();
            dt.Add("Name", "QQ");

            var obj = new PrivateSetterObject();
            objectConverter.MapObject(dt, obj);
            Assert.IsNull(obj.Name);
        }
        public void Map_Test()
        {
            var converter = new ObjectConverter();

            DateTime tm = DateTime.Parse("1998-01-01");
            Dictionary<string, object> dt = new Dictionary<string, object>();
            dt.Add("int16Var", 1);
            dt.Add("int32Var", 2);
            dt.Add("int64Var", 3);
            dt.Add("datetimeVar", tm);
            dt.Add("decimalVar", 1m);
            dt.Add("floatVar", 2f);
            dt.Add("doubleVar", 3d);
            dt.Add("stringVar", "hello");
            dt.Add("MyEnum", MyEnum.B);

            MapTestObject1 obj = new MapTestObject1();
            converter.MapObject(dt, obj);
            Assert.AreEqual(1, obj.int16Var);
            Assert.AreEqual(2, obj.int32Var);
            Assert.AreEqual(3, obj.int64Var);
            Assert.AreEqual(tm, obj.datetimeVar);
            Assert.AreEqual(1M, obj.decimalVar);
            Assert.AreEqual(2f, obj.floatVar);
            Assert.AreEqual(3d, obj.doubleVar);
            Assert.AreEqual(MyEnum.B, obj.MyEnum);

            dt["MyEnum"] = "B";
            MapTestObject2 obj2 = new MapTestObject2();
            converter.MapObject(dt, obj2);
            Assert.AreEqual((short)1, obj2.int16Var);
            Assert.AreEqual(2, obj2.int32Var);
            Assert.AreEqual((long)3, obj2.int64Var);
            Assert.AreEqual(tm, obj2.datetimeVar);
            Assert.AreEqual(1M, obj2.decimalVar);
            Assert.AreEqual(2f, obj2.floatVar);
            Assert.AreEqual(3d, obj2.doubleVar);
            Assert.AreEqual(MyEnum.B, obj2.MyEnum);
        }
예제 #10
0
        public void MapUnexistProperty_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter()
                {
                    IsIgnoreNotExistingProperty = true,
                };

            Dictionary<string, object> dt = new Dictionary<string, object>();
            dt.Add("Name", "QQ");

            MapWithoutPropertyTestObject1 obj = new MapWithoutPropertyTestObject1();
            objectConverter.MapObject(dt, obj);
            Assert.AreEqual("QQ", obj.Name);
            Assert.AreEqual(100, obj.Age);
        }
예제 #11
0
        public void MapUnexistPropertyException_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter();
            objectConverter.RegisterConverter(1, new CreationDictionaryObjectConverter("xtype", new DomainTypeResolver(true)));

            Dictionary<string, object> dt = new Dictionary<string, object>();
            dt.Add("Name", "QQ");

            MapWithoutPropertyTestObject1 obj = new MapWithoutPropertyTestObject1();
            objectConverter.MapObject(dt, obj);
            Assert.AreEqual("QQ", obj.Name);
            Assert.AreEqual(100, obj.Age);
        }
예제 #12
0
        public void DictionObject_Test()
        {
            ObjectConverter objectConverter = new ObjectConverter();
            objectConverter.RegisterConverter(1, new CreationDictionaryObjectConverter("xtype", new DomainTypeResolver(true)));

            Dictionary<string, object> routeValue = new Dictionary<string, object>();
            routeValue["Name"] = "A";

            Dictionary<string, object> xcat = new Dictionary<string, object>();
            xcat["Name"] = "Q";
            routeValue["RouteObjects"] = xcat;

            RouteValue obj = new RouteValue();
            objectConverter.MapObject(routeValue, obj);

            Assert.IsNotNull(obj.RouteObjects);
            Assert.AreEqual("Q", obj.RouteObjects["Name"]);
        }