Inheritance: ValidateFooAttribute
        public void GetValidationResult()
        {
            var attr = new ValidateBarAttribute();

            try
            {
                attr.GetValidationResult("stuff", null);
                Assert.Fail("#A1");
            }
            catch (ArgumentNullException)
            {
                // success
            }

            var vc = new ValidationContext("stuff", null, null);

            vc.DisplayName = "MyStuff";
            var vr = attr.GetValidationResult("stuff", vc);

            Assert.IsNull(vr, "#A2");

            vr = attr.GetValidationResult(null, vc);
            Assert.IsNotNull(vr, "#A3-1");
            Assert.IsNotNull(vr.ErrorMessage, "#A3-2");
            Assert.AreEqual("The field MyStuff is invalid.", vr.ErrorMessage, "#A3-3");

            attr.ErrorMessage = "My Error Message: {0}";
            vr = attr.GetValidationResult(null, vc);
            Assert.IsNotNull(vr, "#A4-1");
            Assert.IsNotNull(vr.ErrorMessage, "#A4-2");
            Assert.AreEqual("My Error Message: MyStuff", vr.ErrorMessage, "#A4-3");

            attr.ErrorMessage             = null;
            attr.ErrorMessageResourceName = "ErrorProperty1";
            attr.ErrorMessageResourceType = typeof(FooErrorMessageProvider);
            vr = attr.GetValidationResult(null, vc);
            Assert.IsNotNull(vr, "#A5-1");
            Assert.IsNotNull(vr.ErrorMessage, "#A5-2");
            Assert.AreEqual("Error Message 1", vr.ErrorMessage, "#A5-3");

            attr.ErrorMessage             = "My Error Message: {0}";
            attr.ErrorMessageResourceName = null;
            attr.ErrorMessageResourceType = null;
            vr = attr.GetValidationResult(null, vc);
            Assert.IsNotNull(vr, "#A6-1");
            Assert.IsNotNull(vr.MemberNames, "#A6-2");
            int count = 0;

            foreach (string s in vr.MemberNames)
            {
                count++;
            }
            Assert.AreEqual(0, count, "#A6-3");
            Assert.AreEqual("My Error Message: MyStuff", vr.ErrorMessage, "#A6-4");

            attr.ValidationResultErrorMessage = "My VR message";
            vr = attr.GetValidationResult(null, vc);
            Assert.IsNotNull(vr, "#A7-1");
            Assert.AreEqual("My VR message", vr.ErrorMessage, "#A7-2");
        }
Beispiel #2
0
        public void IsValid_Object_ValidationContext()
        {
            var attr = new ValidateBarAttribute();

            try {
                // ...
                attr.CallIsValid(null, null);
                Assert.Fail("#A1");
            } catch (NullReferenceException) {
                // success
            }

            var vc = new ValidationContext("stuff", null, null);
            var vr = attr.CallIsValid(null, vc);

            Assert.IsNotNull(vr, "#A2-1");
            Assert.IsNotNull(vr.ErrorMessage, "#A2-2");
            Assert.AreEqual("The field String is invalid.", vr.ErrorMessage, "#A2-3");
            Assert.IsNotNull(vr.MemberNames, "#A2-4");

            int count = 0;

            foreach (string s in vr.MemberNames)
            {
                count++;
            }
            Assert.AreEqual(0, count, "#A2-5");

            vc.MemberName = "SomeMember";
            vr            = attr.CallIsValid(null, vc);
            Assert.IsNotNull(vr, "#A3-1");
            Assert.IsNotNull(vr.ErrorMessage, "#A3-2");
            Assert.AreEqual("The field String is invalid.", vr.ErrorMessage, "#A3-3");
            Assert.IsNotNull(vr.MemberNames, "#A3-4");

            var list = new List <string> ();

            foreach (string s in vr.MemberNames)
            {
                list.Add(s);
            }
            Assert.AreEqual(1, list.Count, "#A3-5");
            Assert.AreEqual("SomeMember", list [0], "#A3-6");
        }
		public void IsValid_Object_ValidationContext ()
		{
			var attr = new ValidateBarAttribute ();

			try {
				// ...
				attr.CallIsValid (null, null);
				Assert.Fail ("#A1");
			} catch (NullReferenceException) {
				// success
			}

			var vc = new ValidationContext ("stuff", null, null);
			var vr = attr.CallIsValid (null, vc);
			Assert.IsNotNull (vr, "#A2-1");
			Assert.IsNotNull (vr.ErrorMessage, "#A2-2");
			Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A2-3");
			Assert.IsNotNull (vr.MemberNames, "#A2-4"); 
			
			int count = 0;
			foreach (string s in vr.MemberNames)
				count++;
			Assert.AreEqual (0, count, "#A2-5");

			vc.MemberName = "SomeMember";
			vr = attr.CallIsValid (null, vc);
			Assert.IsNotNull (vr, "#A3-1");
			Assert.IsNotNull (vr.ErrorMessage, "#A3-2");
			Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A3-3");
			Assert.IsNotNull (vr.MemberNames, "#A3-4");

			var list = new List <string> ();
			foreach (string s in vr.MemberNames)
				list.Add (s);
			Assert.AreEqual (1, list.Count, "#A3-5");
			Assert.AreEqual ("SomeMember", list [0], "#A3-6");
		}
		public void GetValidationResult ()
		{
			var attr = new ValidateBarAttribute ();

			try {
				attr.GetValidationResult ("stuff", null);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException) {
				// success
			}

			var vc = new ValidationContext ("stuff", null, null);
			vc.DisplayName = "MyStuff";
			var vr = attr.GetValidationResult ("stuff", vc);
			Assert.IsNull (vr, "#A2");

			vr = attr.GetValidationResult (null, vc);
			Assert.IsNotNull(vr, "#A3-1");
			Assert.IsNotNull (vr.ErrorMessage, "#A3-2");
			Assert.AreEqual ("The field MyStuff is invalid.", vr.ErrorMessage, "#A3-3");

			attr.ErrorMessage = "My Error Message: {0}";
			vr = attr.GetValidationResult (null, vc);
			Assert.IsNotNull (vr, "#A4-1");
			Assert.IsNotNull (vr.ErrorMessage, "#A4-2");
			Assert.AreEqual ("My Error Message: MyStuff", vr.ErrorMessage, "#A4-3");

			attr.ErrorMessage = null;
			attr.ErrorMessageResourceName = "ErrorProperty1";
			attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
			vr = attr.GetValidationResult (null, vc);
			Assert.IsNotNull (vr, "#A5-1");
			Assert.IsNotNull (vr.ErrorMessage, "#A5-2");
			Assert.AreEqual ("Error Message 1", vr.ErrorMessage, "#A5-3");

			attr.ErrorMessage = "My Error Message: {0}";
			attr.ErrorMessageResourceName = null;
			attr.ErrorMessageResourceType = null;
			vr = attr.GetValidationResult (null, vc);
			Assert.IsNotNull (vr, "#A6-1");
			Assert.IsNotNull (vr.MemberNames, "#A6-2");
			int count = 0;
			foreach (string s in vr.MemberNames)
				count++;
			Assert.AreEqual (0, count, "#A6-3");
			Assert.AreEqual ("My Error Message: MyStuff", vr.ErrorMessage, "#A6-4");

			attr.ValidationResultErrorMessage = "My VR message";
			vr = attr.GetValidationResult (null, vc);
			Assert.IsNotNull (vr, "#A7-1");
			Assert.AreEqual ("My VR message", vr.ErrorMessage, "#A7-2");
		}