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

            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type, act);

            var context = v.ValidationEntry;

            Assert.NotNull(context);

            context.ForMember("Name",
                              c => c.NotEmpty().MinLength(4).MaxLength(15));

            var r1 = v.Verify();

            Assert.False(r1.IsValid);
            Assert.Single(r1.Errors);
            Assert.Single(r1.Errors[0].Details);

            v["Name"] = "Hulu"; //4

            var r2 = v.Verify();

            Assert.True(r2.IsValid);

            v["Name"] = "HuluWayaJavaPojoNovaLomo"; //16,greater than 15

            var r3 = v.Verify();

            Assert.False(r3.IsValid);
            Assert.Single(r3.Errors);
            Assert.Single(r3.Errors[0].Details);

            v["Name"] = ""; //0

            var r4 = v.Verify();

            Assert.False(r4.IsValid);
            Assert.Single(r4.Errors);
            Assert.Equal(2, r4.Errors[0].Details.Count);

            v["Name"] = null; //nil

            var r5 = v.Verify();

            Assert.False(r5.IsValid);
            Assert.Single(r5.Errors);
            Assert.Equal(2, r5.Errors[0].Details.Count);
        }
Example #2
0
        public void DirectInstanceWithStrategyValidTest()
        {
            var act = new NiceAct()
            {
                Name     = "Hu", //2
                Age      = 22,
                Country  = Country.China,
                Birthday = DateTime.Today
            };

            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type, act);

            var context = v.ValidationEntry;

            Assert.NotNull(context);

            context.SetStrategy <NormalNiceActValidationStrategy>();

            var r1 = v.Verify();

            Assert.False(r1.IsValid);
            Assert.Single(r1.Errors);
            Assert.Single(r1.Errors[0].Details);

            v["Name"] = "Hulu"; //4

            var r2 = v.Verify();

            Assert.True(r2.IsValid);

            v["Name"] = "HuluWayaJavaPojoNovaLomo"; //16,greater than 15

            var r3 = v.Verify();

            Assert.False(r3.IsValid);
            Assert.Single(r3.Errors);
            Assert.Single(r3.Errors[0].Details);

            v["Name"] = ""; //0

            var r4 = v.Verify();

            Assert.False(r4.IsValid);
            Assert.Single(r4.Errors);
            Assert.Equal(2, r4.Errors[0].Details.Count);

            v["Name"] = null; //nil

            var r5 = v.Verify();

            Assert.False(r5.IsValid);
            Assert.Single(r5.Errors);
            Assert.Equal(2, r5.Errors[0].Details.Count);
        }
Example #3
0
        public void DirectInstanceSelectTest()
        {
            var act = new NiceAct()
            {
                Name     = "Hu",
                Age      = 22,
                Country  = Country.China,
                Birthday = DateTime.Today
            };

            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type, act);

            var z0 = v.Select((s, o) => s);
            var z1 = v.Select((s, o, m) => s);
            var z2 = v.Select(c => c.Name);
            var z3 = v.Select <(string, int)>(c => (c.Name, c.Index));
            var z4 = v.Select(c => new { c.Name, c.Value, c.Index });

            Assert.NotNull(z0);
            Assert.NotNull(z1);
            Assert.NotNull(z2);
            Assert.NotNull(z3);
            Assert.NotNull(z4);

            var l0 = z0.FireAndReturn();
            var l1 = z1.FireAndReturn();
            var l2 = z2.FireAndReturn();
            var l3 = z3.FireAndReturn();
            var l4 = z4.FireAndReturn();

            Assert.Equal(5, l0.Count());
            Assert.Equal(5, l1.Count());
            Assert.Equal(5, l2.Count());
            Assert.Equal(5, l3.Count());
            Assert.Equal(5, l4.Count());

            var f0 = l0.First();
            var f1 = l1.First();
            var f2 = l2.First();
            var f3 = l3.First();
            var f4 = l4.First();

            Assert.Equal("Name", f0);
            Assert.Equal("Name", f1);
            Assert.Equal("Name", f2);
            Assert.Equal("Name", f3.Item1);
            Assert.Equal(0, f3.Item2);
            Assert.Equal("Name", f4.Name);
            Assert.Equal("Hu", f4.Value);
            Assert.Equal(0, f4.Index);
        }
Example #4
0
        public void DirectFutureSelectTest()
        {
            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type);

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

            var z0 = v.Select((s, o) => s);
            var z1 = v.Select((s, o, m) => s);
            var z2 = v.Select(c => c.Name);
            var z3 = v.Select <(string, int)>(c => (c.Name, c.Index));
            var z4 = v.Select(c => new { c.Name, c.Value, c.Index });

            Assert.NotNull(z0);
            Assert.NotNull(z1);
            Assert.NotNull(z2);
            Assert.NotNull(z3);
            Assert.NotNull(z4);

            var l0 = z0.FireAndReturn();
            var l1 = z1.FireAndReturn();
            var l2 = z2.FireAndReturn();
            var l3 = z3.FireAndReturn();
            var l4 = z4.FireAndReturn();

            Assert.Equal(5, l0.Count());
            Assert.Equal(5, l1.Count());
            Assert.Equal(5, l2.Count());
            Assert.Equal(5, l3.Count());
            Assert.Equal(5, l4.Count());

            var f0 = l0.First();
            var f1 = l1.First();
            var f2 = l2.First();
            var f3 = l3.First();
            var f4 = l4.First();

            Assert.Equal("Name", f0);
            Assert.Equal("Name", f1);
            Assert.Equal("Name", f2);
            Assert.Equal("Name", f3.Item1);
            Assert.Equal(0, f3.Item2);
            Assert.Equal("Name", f4.Name);
            Assert.Equal("Du", f4.Value);
            Assert.Equal(0, f4.Index);
        }
Example #5
0
        public void GenericFutureWithValueApiValidTest()
        {
            var v = LeoVisitorFactory.Create <NiceAct>();

            var context = v.ValidationEntry;

            Assert.NotNull(context);

            context.ForMember(x => x.Name,
                              c => c.NotEmpty().MinLength(4).MaxLength(15));

            var r1 = v.Verify();

            Assert.False(r1.IsValid);
            Assert.Single(r1.Errors);
            Assert.Equal(2, r1.Errors[0].Details.Count);

            v["Name"] = "Hulu"; //4

            var r2 = v.Verify();

            Assert.True(r2.IsValid);

            v["Name"] = "HuluWayaJavaPojoNovaLomo"; //16,greater than 15

            var r3 = v.Verify();

            Assert.False(r3.IsValid);
            Assert.Single(r3.Errors);
            Assert.Single(r3.Errors[0].Details);

            v["Name"] = ""; //0

            var r4 = v.Verify();

            Assert.False(r4.IsValid);
            Assert.Single(r4.Errors);
            Assert.Equal(2, r4.Errors[0].Details.Count);

            v["Name"] = null; //nil

            var r5 = v.Verify();

            Assert.False(r5.IsValid);
            Assert.Single(r5.Errors);
            Assert.Equal(2, r5.Errors[0].Details.Count);
        }
Example #6
0
        public void GenericFutureWithStrategyValidTest()
        {
            var v = LeoVisitorFactory.Create <NiceAct>();

            var context = v.ValidationEntry;

            Assert.NotNull(context);

            context.SetStrategy <GenericNiceActValidationStrategy>();

            var r1 = v.Verify();

            Assert.False(r1.IsValid);
            Assert.Single(r1.Errors);
            Assert.Equal(2, r1.Errors[0].Details.Count);

            v["Name"] = "Hulu"; //4

            var r2 = v.Verify();

            Assert.True(r2.IsValid);

            v["Name"] = "HuluWayaJavaPojoNovaLomo"; //16,greater than 15

            var r3 = v.Verify();

            Assert.False(r3.IsValid);
            Assert.Single(r3.Errors);
            Assert.Single(r3.Errors[0].Details);

            v["Name"] = ""; //0

            var r4 = v.Verify();

            Assert.False(r4.IsValid);
            Assert.Single(r4.Errors);
            Assert.Equal(2, r4.Errors[0].Details.Count);

            v["Name"] = null; //nil

            var r5 = v.Verify();

            Assert.False(r5.IsValid);
            Assert.Single(r5.Errors);
            Assert.Equal(2, r5.Errors[0].Details.Count);
        }
Example #7
0
        public void GenericInstanceWithValueApiValidTest()
        {
            var act = new NiceAct()
            {
                Name     = "Hulu",
                Age      = 22,
                Country  = Country.China,
                Birthday = DateTime.Today
            };

            var v = LeoVisitorFactory.Create <NiceAct>(act);

            var context = v.ValidationEntry;

            Assert.NotNull(context);

            context.ForMember(x => x.Name,
                              c => c.NotEmpty().MinLength(4).MaxLength(15));

            var r1 = v.Verify();

            Assert.True(r1.IsValid);

            v.ValidationEntry
            .ForMember("Name", c => c.Empty().OverwriteRule());

            var r2 = v.Verify();

            Assert.False(r2.IsValid);
            Assert.Single(r2.Errors);
            Assert.Single(r2.Errors[0].Details);

            v["Name"] = "";

            var r3 = v.Verify();

            Assert.True(r3.IsValid);

            v["Name"] = null;

            var r4 = v.Verify();

            Assert.True(r4.IsValid);
        }
Example #8
0
        public void DirectInstanceLoopTest()
        {
            var act = new NiceAct()
            {
                Name     = "Hu",
                Age      = 22,
                Country  = Country.China,
                Birthday = DateTime.Today
            };

            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type, act);

            string s0 = "", s1 = "", s2 = "";
            int    i2 = 0;
            var    ss = "NameAgeBirthdayCountryIsValid";
            var    ii = 0 + 1 + 2 + 3 + 4;

            var l0 = v.ForEach((s, o) => { s0 += s; });
            var l1 = v.ForEach((s, o, m) => { s1 += s; });
            var l2 = v.ForEach(c =>
            {
                s2 += c.Name;
                i2 += c.Index;
            });

            Assert.Equal("", s0);
            Assert.Equal("", s1);
            Assert.Equal("", s2);
            Assert.Equal(0, i2);

            l0.Fire();
            l1.Fire();
            l2.Fire();

            Assert.Equal(ss, s0);
            Assert.Equal(ss, s1);
            Assert.Equal(ss, s2);
            Assert.Equal(ii, i2);

            l2.Fire();
            Assert.Equal($"{ss}{ss}", s2);
            Assert.Equal(ii + ii, i2);
        }
Example #9
0
        public void DirectFutureLoopTest()
        {
            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type);

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

            string s0 = "", s1 = "", s2 = "";
            int    i2 = 0;
            var    ss = "NameAgeBirthdayCountryIsValid";
            var    ii = 0 + 1 + 2 + 3 + 4;

            var l0 = v.ForEach((s, o) => { s0 += s; });
            var l1 = v.ForEach((s, o, m) => { s1 += s; });
            var l2 = v.ForEach(c =>
            {
                s2 += c.Name;
                i2 += c.Index;
            });

            Assert.Equal("", s0);
            Assert.Equal("", s1);
            Assert.Equal("", s2);
            Assert.Equal(0, i2);

            l0.Fire();
            l1.Fire();
            l2.Fire();

            Assert.Equal(ss, s0);
            Assert.Equal(ss, s1);
            Assert.Equal(ss, s2);
            Assert.Equal(ii, i2);

            l2.Fire();
            Assert.Equal($"{ss}{ss}", s2);
            Assert.Equal(ii + ii, i2);
        }
Example #10
0
        public void GenericFutureWithFluentApiAndValTest()
        {
            var v = LeoVisitorFactory.Create <NiceAct>();

            v["Name"] = "Balala";

            v.ValidationEntry.ForMember(x => x.Name, c => c.Must(NameMustBeBalala2).WithMessage("MustBala!"));

            var r = v.Verify();

            Assert.True(r.IsValid);

            v["Name"] = "Bong";

            r = v.Verify();

            Assert.False(r.IsValid);
            Assert.Single(r.Errors);
            Assert.Single(r.Errors[0].Details);
            Assert.Equal("MustBala!", r.Errors[0].Details[0].ErrorMessage);
        }
Example #11
0
        public void DirectFutureWithFluentApiTest()
        {
            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type);

            v["Name"] = "Balala";

            v.ValidationEntry.ForMember("Name", c => c.Must(NameMustBeBalala).WithMessage("MustBala!"));

            var r = v.Verify();

            Assert.True(r.IsValid);

            v["Name"] = "Bong";

            r = v.Verify();

            Assert.False(r.IsValid);
            Assert.Single(r.Errors);
            Assert.Single(r.Errors[0].Details);
            Assert.Equal("MustBala!", r.Errors[0].Details[0].ErrorMessage);
        }
Example #12
0
        public void DirectFutureWithValueApiValidTest()
        {
            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type);

            v["Name"] = "Hulu";

            var context = v.ValidationEntry;

            Assert.NotNull(context);

            context.ForMember("Name",
                              c => c.NotEmpty().MinLength(4).MaxLength(15));

            var r1 = v.Verify();

            Assert.True(r1.IsValid);

            v.ValidationEntry
            .ForMember("Name", c => c.Empty().OverwriteRule());

            var r2 = v.Verify();

            Assert.False(r2.IsValid);
            Assert.Single(r2.Errors);
            Assert.Single(r2.Errors[0].Details);

            v["Name"] = "";

            var r3 = v.Verify();

            Assert.True(r3.IsValid);

            v["Name"] = null;

            var r4 = v.Verify();

            Assert.True(r4.IsValid);
        }
Example #13
0
        public void FackStaticTypeTest()
        {
            var act = new StaticNiceAct2();

            StaticNiceAct2.Name     = "Hu";
            StaticNiceAct2.Age      = 22;
            StaticNiceAct2.Country  = Country.China;
            StaticNiceAct2.Birthday = DateTime.Today;

            var type = typeof(StaticNiceAct2);

            var v = LeoVisitorFactory.Create(type, act);

            Assert.False(v.IsStatic);
            Assert.Equal(typeof(StaticNiceAct2), 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 <NiceAct, string>(x => x.Name));
            Assert.Equal(199, v.GetValue <NiceAct, int>(x => x.Age));
            Assert.Equal(Country.China, v.GetValue <NiceAct, Country>(x => x.Country));
            Assert.Equal(DateTime.Today.AddDays(-2), v.GetValue <NiceAct, DateTime>(x => x.Birthday));
            Assert.False(v.GetValue <NiceAct, bool>(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"]);
        }
Example #14
0
        public void GenericFutureTest()
        {
            var v = LeoVisitorFactory.Create <NiceAct>();

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

            var n1  = new NiceAct();
            var b1  = v.TryRepeat(n1, out var o1);
            var to1 = (NiceAct)o1;

            Assert.True(b1);
            Assert.Equal("Du", to1.Name);
            Assert.Equal(55, to1.Age);
            Assert.Equal(Country.USA, to1.Country);
            Assert.Equal(DateTime.Today.AddDays(-1), to1.Birthday);
            Assert.True(to1.IsValid);

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

            var n2 = new NiceAct();
            var b2 = v.TryRepeat(n2, out var o2);

            var b3 = v.TryRepeat(n1, out var o3);

            var b4 = v.TryRepeat(out var o4);

            var b5 = v.TryRepeatAs <NiceAct>(out var o5);

            var n6 = new NiceAct();
            var b6 = v.TryRepeatAs <NiceAct>(n6, out var o6);

            Assert.True(b2);
            Assert.Equal("Au", o2.Name);
            Assert.Equal(77, o2.Age);
            Assert.Equal(Country.China, o2.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o2.Birthday);
            Assert.False(o2.IsValid);

            Assert.True(b3);
            Assert.Equal("Au", o3.Name);
            Assert.Equal(77, o3.Age);
            Assert.Equal(Country.China, o3.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o3.Birthday);
            Assert.False(o3.IsValid);

            Assert.True(b4);
            Assert.Equal("Au", o4.Name);
            Assert.Equal(77, o4.Age);
            Assert.Equal(Country.China, o4.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o4.Birthday);
            Assert.False(o4.IsValid);

            Assert.True(b5);
            Assert.Equal("Au", o5.Name);
            Assert.Equal(77, o5.Age);
            Assert.Equal(Country.China, o5.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o5.Birthday);
            Assert.False(o5.IsValid);

            Assert.True(b6);
            Assert.Equal("Au", o6.Name);
            Assert.Equal(77, o6.Age);
            Assert.Equal(Country.China, o6.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o6.Birthday);
            Assert.False(o6.IsValid);

            var repeater = v.ForRepeat();
            var n7       = new NiceAct();
            var o7       = repeater.Play(n7);
            var o8       = repeater.NewAndPlay();

            Assert.Equal("Au", o7.Name);
            Assert.Equal(77, o7.Age);
            Assert.Equal(Country.China, o7.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o7.Birthday);
            Assert.False(o7.IsValid);

            Assert.Equal("Au", o8.Name);
            Assert.Equal(77, o8.Age);
            Assert.Equal(Country.China, o8.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o8.Birthday);
            Assert.False(o8.IsValid);

            var d = new Dictionary <string, object>();

            d["Name"]     = "AuX";
            d["Age"]      = 707;
            d["Country"]  = Country.USA;
            d["Birthday"] = DateTime.Today.AddDays(10);
            d["IsValid"]  = true;

            var b10 = v.TryRepeat(d, out var to10);

            var b11 = v.TryRepeatAs <NiceAct>(d, out var o11);

            var o12 = repeater.Play(d);

            Assert.True(b10);
            Assert.Equal("Au", to10.Name);
            Assert.Equal(77, to10.Age);
            Assert.Equal(Country.China, to10.Country);
            Assert.Equal(DateTime.Today.AddDays(1), to10.Birthday);
            Assert.False(to10.IsValid);

            Assert.True(b11);
            Assert.Equal("Au", o11.Name);
            Assert.Equal(77, o11.Age);
            Assert.Equal(Country.China, o11.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o11.Birthday);
            Assert.False(o11.IsValid);

            Assert.Equal("Au", o12.Name);
            Assert.Equal(77, o12.Age);
            Assert.Equal(Country.China, o12.Country);
            Assert.Equal(DateTime.Today.AddDays(1), o12.Birthday);
            Assert.False(o12.IsValid);
        }
        public void GenericFutureTest()
        {
            var d = new Dictionary <string, object>();

            d["Name"]     = "Haha";
            d["Age"]      = 505;
            d["Country"]  = Country.China;
            d["Birthday"] = DateTime.Today.AddDays(9);
            d["IsValid"]  = true;

            var v = LeoVisitorFactory.Create <NiceAct>(d);

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

            Assert.Equal("Haha", v.GetValue <string>("Name"));
            Assert.Equal(505, v.GetValue <int>("Age"));
            Assert.Equal(Country.China, v.GetValue <Country>("Country"));
            Assert.Equal(DateTime.Today.AddDays(9), v.GetValue <DateTime>("Birthday"));
            Assert.True(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 <NiceAct, string>(x => x.Name));
            Assert.Equal(199, v.GetValue <NiceAct, int>(x => x.Age));
            Assert.Equal(Country.China, v.GetValue <NiceAct, Country>(x => x.Country));
            Assert.Equal(DateTime.Today.AddDays(-2), v.GetValue <NiceAct, DateTime>(x => x.Birthday));
            Assert.False(v.GetValue <NiceAct, bool>(x => x.IsValid));

            v.SetValue <string>(x => x.Name, "Lu");
            v.SetValue <int>(x => x.Age, 11);
            v.SetValue <Country>(x => x.Country, Country.USA);
            v.SetValue <DateTime>(x => x.Birthday, DateTime.Today);
            v.SetValue <bool>(x => x.IsValid, true);

            Assert.Equal("Lu", v.GetValue(x => x.Name));
            Assert.Equal(11, v.GetValue(x => x.Age));
            Assert.Equal(Country.USA, v.GetValue(x => x.Country));
            Assert.Equal(DateTime.Today, v.GetValue(x => x.Birthday));
            Assert.True(v.GetValue(x => x.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"]);
        }
Example #16
0
        public void DirectInstanceWithValueApiValidTest()
        {
            var act = new NiceAct()
            {
                Name     = "Hulu",
                Age      = 22,
                Country  = Country.China,
                Birthday = DateTime.Today
            };

            var type = typeof(NiceAct);
            var v    = LeoVisitorFactory.Create(type, act);

            v.ValidationEntry
            .ForMember("Name", c => c.NotEmpty().MinLength(4).MaxLength(15));

            var r1 = v.Verify();

            Assert.True(r1.IsValid);

            v.ValidationEntry
            .ForMember("Name", c => c.Empty().OverwriteRule());

            var r2 = v.Verify();

            Assert.False(r2.IsValid);
            Assert.Single(r2.Errors);
            Assert.Single(r2.Errors[0].Details);

            v["Name"] = "";

            var r3 = v.Verify();

            Assert.True(r3.IsValid);

            v["Name"] = null;

            var r4 = v.Verify();

            Assert.True(r4.IsValid);

            // NotEqual is not mutually exclusive, so these three tokens can be added. The default is AppendRule mode.
            v.ValidationEntry.ForMember("Name", c => c.Length(4, 15).NotEqual("Huhuhu").NotEqual("Lululu").NotEqual("Hu"));

            r1 = v.Verify();
            Assert.True(r1.IsValid);

            // NotEqual is not mutually exclusive, so these three tokens can be added.
            v.ValidationEntry.ForMember("Name", c => c.Length(4, 15).NotEqual("Huhuhu").NotEqual("Lululu").NotEqual("Hu").OverwriteRule());

            v["Name"] = "Hululu";
            r1        = v.Verify();
            Assert.True(r1.IsValid);

            v["Name"] = "Huhuhu";
            r1        = v.Verify();
            Assert.False(r1.IsValid);
            Assert.Single(r1.Errors);
            Assert.Single(r1.Errors[0].Details);

            v["Name"] = "Lululu";
            r1        = v.Verify();
            Assert.False(r1.IsValid);
            Assert.Single(r1.Errors);
            Assert.Single(r1.Errors[0].Details);

            v["Name"] = "Hu";
            r1        = v.Verify();
            Assert.False(r1.IsValid);
            Assert.Single(r1.Errors);
            Assert.Equal(2, r1.Errors[0].Details.Count);

            // Revert to RequiredNull token to test the mutual exclusion under AppendRule
            v.ValidationEntry
            .ForMember("Name", c => c.Empty().OverwriteRule());

            // Due to mutual exclusion, these conditions will all fail to take effect (because of the AppendRule mode).
            v.ValidationEntry
            .ForMember("Name", c => c.NotEmpty().MinLength(4).MaxLength(15).AppendRule());

            v["Name"] = "";

            r1 = v.Verify();
            Assert.True(r1.IsValid);

            v["Name"] = null;

            r1 = v.Verify();
            Assert.True(r1.IsValid);
        }