예제 #1
0
        public void Adds_And_Formats_Message_Using_Lambda_Expression_Index()
        {
            var list = new List <int> {
                1, 2, 3
            };

            // The for loop and i variable (instead of constant) are important
            // because the compiler generates dependant "DisplayClass" classes,
            // which results in an additional member-access expression in the tree,
            // e.g. cDisplayClass31.CS$<>8__locals1.list[cDisplayClass31.i]
            // instead of cDisplayClass31.list[cDisplayClass31.i]

            for (int i = 0; i < 1; i++)
            {
                var errors = new ErrorBuilder();
                errors.Add("{1} = {0}", () => list[i]);

                var err = errors.GetErrors();

                Assert.AreEqual("[0] = " + list[i].ToString(), err[0].ErrorMessage);
                Assert.AreEqual("[0]", err[0].MemberNames.First());

                errors.Clear();
                errors.IncludeValueSelectorFirstKeySegment = true;

                errors.Add("{1} = {0}", () => list[i]);

                err = errors.GetErrors();

                Assert.AreEqual("list[0] = " + list[i].ToString(), err[0].ErrorMessage);
                Assert.AreEqual("list[0]", err[0].MemberNames.First());
            }
        }
예제 #2
0
        public void Adds_And_Formats_Message()
        {
            var errors = new ErrorBuilder();

            errors.Add("a {0}", "b");
            errors.Add("a {0} {1}", "b", "c");

            var err = errors.GetErrors();

            Assert.AreEqual("a b", err[0].ErrorMessage);
            Assert.AreEqual("a b c", err[1].ErrorMessage);
        }
예제 #3
0
        public void Adds_Message()
        {
            var errors = new ErrorBuilder();

            errors.Add("a");

            Assert.AreEqual("a", errors.GetErrors()[0].ErrorMessage);
        }
예제 #4
0
        public void Null_Key_Is_Not_Added_To_Member_Names()
        {
            var errors = new ErrorBuilder();

            errors.Add("a", null, null);

            var err = errors.GetErrors();

            Assert.AreEqual(0, err[0].MemberNames.Count());
        }
예제 #5
0
        public void Formats_Message_With_DisplayName()
        {
            var obj = new TestModel();

            var errors = new ErrorBuilder();

            errors.Add("Bad {1}", () => obj.SomeProperty);

            var err = errors.GetErrors();

            Assert.AreEqual("Bad Some Property", err[0].ErrorMessage);
        }
예제 #6
0
        public void Adds_And_Formats_Message_Using_Lambda_Expression_Simple()
        {
            string a = "ddf";

            var errors = new ErrorBuilder();

            errors.Add("{1} = {0}", () => a.Length);

            var err = errors.GetErrors();

            Assert.AreEqual("Length = " + a.Length.ToString(), err[0].ErrorMessage);
            Assert.AreEqual("Length", err[0].MemberNames.First());

            errors.Clear();
            errors.IncludeValueSelectorFirstKeySegment = true;

            errors.Add("{1} = {0}", () => a.Length);

            err = errors.GetErrors();

            Assert.AreEqual("Length = " + a.Length.ToString(), err[0].ErrorMessage);
            Assert.AreEqual("a.Length", err[0].MemberNames.First());
        }