public void UpdateValueOutOfVisitorTest()
        {
            var act = new NiceAct()
            {
                Name     = "Hu",
                Age      = 22,
                Country  = Country.China,
                Birthday = DateTime.Today
            };

            var v = act.ToVisitor();

            Assert.Equal("Hu", v.GetValue <string>("Name"));
            Assert.Equal(22, v.GetValue <int>("Age"));
            Assert.Equal(Country.China, v.GetValue <Country>("Country"));
            Assert.Equal(DateTime.Today, v.GetValue <DateTime>("Birthday"));
            Assert.False(v.GetValue <bool>("IsValid"));

            act.Name     = "Du";
            act.Age      = 55;
            act.Country  = Country.USA;
            act.Birthday = DateTime.Today.AddDays(-1);
            act.IsValid  = true;

            Assert.Equal("Du", v.GetValue <string>("Name"));
            Assert.Equal(55, v.GetValue <int>("Age"));
            Assert.Equal(Country.USA, v.GetValue <Country>("Country"));
            Assert.Equal(DateTime.Today.AddDays(-1), v.GetValue <DateTime>("Birthday"));
            Assert.True(v.GetValue <bool>("IsValid"));
        }
Exemplo n.º 2
0
        public void DirectInstanceExtensionsTest()
        {
            var act = new NiceAct()
            {
                Name     = "Hu",
                Age      = 22,
                Country  = Country.China,
                Birthday = DateTime.Today
            };

            var v = act.ToVisitor();

            Assert.False(v.IsStatic);
            Assert.Equal(typeof(NiceAct), v.SourceType);
            Assert.Equal(AlgorithmKind.Precision, v.AlgorithmKind);

            Assert.Equal("Hu", v.GetValue <string>("Name"));
            Assert.Equal(22, v.GetValue <int>("Age"));
            Assert.Equal(Country.China, v.GetValue <Country>("Country"));
            Assert.Equal(DateTime.Today, v.GetValue <DateTime>("Birthday"));
            Assert.False(v.GetValue <bool>("IsValid"));

            v.SetValue("Name", "Du");
            v.SetValue("Age", 55);
            v.SetValue("Country", Country.USA);
            v.SetValue("Birthday", DateTime.Today.AddDays(-1));
            v.SetValue("IsValid", true);

            Assert.Equal("Du", v.GetValue <string>("Name"));
            Assert.Equal(55, v.GetValue <int>("Age"));
            Assert.Equal(Country.USA, v.GetValue <Country>("Country"));
            Assert.Equal(DateTime.Today.AddDays(-1), v.GetValue <DateTime>("Birthday"));
            Assert.True(v.GetValue <bool>("IsValid"));

            v["Name"]     = "Au";
            v["Age"]      = 77;
            v["Country"]  = Country.China;
            v["Birthday"] = DateTime.Today.AddDays(1);
            v["IsValid"]  = false;

            Assert.Equal("Au", v.GetValue <string>("Name"));
            Assert.Equal(77, v.GetValue <int>("Age"));
            Assert.Equal(Country.China, v.GetValue <Country>("Country"));
            Assert.Equal(DateTime.Today.AddDays(1), v.GetValue <DateTime>("Birthday"));
            Assert.False(v.GetValue <bool>("IsValid"));

            v.SetValue <NiceAct>(x => x.Name, "Zu");
            v.SetValue <NiceAct>(x => x.Age, 99);
            v.SetValue <NiceAct>(x => x.Country, Country.USA);
            v.SetValue <NiceAct>(x => x.Birthday, DateTime.Today.AddDays(2));
            v.SetValue <NiceAct>(x => x.IsValid, true);

            Assert.Equal("Zu", v.GetValue <NiceAct>(x => x.Name));
            Assert.Equal(99, v.GetValue <NiceAct>(x => x.Age));
            Assert.Equal(Country.USA, v.GetValue <NiceAct>(x => x.Country));
            Assert.Equal(DateTime.Today.AddDays(2), v.GetValue <NiceAct>(x => x.Birthday));
            Assert.True((bool)v.GetValue <NiceAct>(x => x.IsValid));

            v.SetValue <NiceAct, string>(x => x.Name, "Xu");
            v.SetValue <NiceAct, int>(x => x.Age, 199);
            v.SetValue <NiceAct, Country>(x => x.Country, Country.China);
            v.SetValue <NiceAct, DateTime>(x => x.Birthday, DateTime.Today.AddDays(-2));
            v.SetValue <NiceAct, bool>(x => x.IsValid, false);

            Assert.Equal("Xu", v.GetValue(x => x.Name));
            Assert.Equal(199, v.GetValue(x => x.Age));
            Assert.Equal(Country.China, v.GetValue(x => x.Country));
            Assert.Equal(DateTime.Today.AddDays(-2), v.GetValue(x => x.Birthday));
            Assert.False(v.GetValue(x => x.IsValid));

            var d = v.ToDictionary();

            Assert.Equal("Xu", d["Name"]);
            Assert.Equal(199, d["Age"]);
            Assert.Equal(Country.China, d["Country"]);
            Assert.Equal(DateTime.Today.AddDays(-2), d["Birthday"]);
            Assert.False((bool)d["IsValid"]);

            d["Name"]     = "Ax";
            d["Age"]      = 10086;
            d["Country"]  = Country.China;
            d["Birthday"] = DateTime.Today.AddDays(10);
            d["IsValid"]  = true;

            v.SetValue(d);

            Assert.Equal("Ax", d["Name"]);
            Assert.Equal(10086, d["Age"]);
            Assert.Equal(Country.China, d["Country"]);
            Assert.Equal(DateTime.Today.AddDays(10), d["Birthday"]);
            Assert.True((bool)d["IsValid"]);
        }