public static void RequiresValidationContext_return_false_for_valid_validation_type_and_one_arg_method() {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArg");
            Assert.False(attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArgStronglyTyped");
            Assert.False(attribute.RequiresValidationContext);
        }
        public static void RequiresValidationContext_throws_InvalidOperationException_if_attribute_not_well_formed() {
            var attribute = new CustomValidationAttribute(null, "Does not matter");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(NonPublicCustomValidator), "Does not matter");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), null);
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), string.Empty);
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonExistentMethod");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonPublicValidationMethod");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonStaticValidationMethod");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodDoesNotReturnValidationResult");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithNoArgs");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithByRefArg");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodTwoArgsButSecondIsNotValidationContext");
            Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);
        }
        public static void RequiresValidationContext_return_true_for_valid_validation_type_and_two_arg_method() {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgs");
            Assert.True(attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsStronglyTyped");
            Assert.True(attribute.RequiresValidationContext);
        }
        public static void Validation_works_for_null_and_non_null_values_and_validation_method_taking_nullable_value_type()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArgNullable");

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));
            AssertEx.DoesNotThrow(
                () => attribute.Validate(new TestStruct()
            {
                Value = "Valid Value"
            }, s_testValidationContext));
            Assert.Throws <ValidationException>(
                () => attribute.Validate(new TestStruct()
            {
                Value = "Some non-valid value"
            }, s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsWithFirstNullable");
            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));
            AssertEx.DoesNotThrow(
                () => attribute.Validate(new TestStruct()
            {
                Value = "Valid Value"
            }, s_testValidationContext));
            Assert.Throws <ValidationException>(
                () => attribute.Validate(new TestStruct()
            {
                Value = "Some non-valid value"
            }, s_testValidationContext));
        }
        public static void Can_construct_attribute_and_get_values()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "SomeMethod");

            Assert.Equal(typeof(CustomValidator), attribute.ValidatorType);
            Assert.Equal("SomeMethod", attribute.Method);
        }
        public static void Constructor(Type validatorType, string method)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);

            Assert.Equal(validatorType, attribute.ValidatorType);
            Assert.Equal(method, attribute.Method);
        }
        public static void Validate_successful_for_validation_method_with_strongly_typed_first_arg_and_value_type_assignable_from_expected_type()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsStronglyTyped");

            AssertEx.DoesNotThrow(
                () => attribute.Validate(new DerivedTestClass("Validation returns success for DerivedTestClass too"), s_testValidationContext));
        }
        public static void Validate_throws_InvalidOperationException_for_invalid_validation_method()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), null);

            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), string.Empty);
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonExistentMethod");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonPublicValidationMethod");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonStaticValidationMethod");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodDoesNotReturnValidationResult");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithNoArgs");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithByRefArg");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodTwoArgsButSecondIsNotValidationContext");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));
        }
        public static void Validate_throws_InvalidOperationException_for_invalid_validation_method()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), null);
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), string.Empty);
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonExistentMethod");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonPublicValidationMethod");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonStaticValidationMethod");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodDoesNotReturnValidationResult");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithNoArgs");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithByRefArg");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodTwoArgsButSecondIsNotValidationContext");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));
        }
        public static void Validate_InvalidArguments_ThrowsValidationException(string method, object value, Type exceptionType)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(typeof(CustomValidator), method);

            Assert.Throws(exceptionType, () => attribute.Validate(value, s_testValidationContext));

            Assert.NotEmpty(attribute.FormatErrorMessage("name"));
        }
        public static void Validate_successful_for_valid_validation_type_and_method()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArg");
            AssertEx.DoesNotThrow(() => attribute.Validate("Validation returns success for any string", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgs");
            AssertEx.DoesNotThrow(() => attribute.Validate(new TestClass("Validation returns success for any TestClass"), s_testValidationContext));
        }
        public static void Validate_throws_InvalidOperationException_for_invalid_ValidatorType()
        {
            var attribute = new CustomValidationAttribute(null, "Does not matter");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(NonPublicCustomValidator), "Does not matter");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));
        }
Exemplo n.º 13
0
        public static void RequiresValidationContext_return_true_for_valid_validation_type_and_two_arg_method()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgs");

            Assert.True(attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsStronglyTyped");
            Assert.True(attribute.RequiresValidationContext);
        }
Exemplo n.º 14
0
        public static void RequiresValidationContext_return_false_for_valid_validation_type_and_one_arg_method()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArg");

            Assert.False(attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArgStronglyTyped");
            Assert.False(attribute.RequiresValidationContext);
        }
Exemplo n.º 15
0
        public static void Validate_successful_for_valid_validation_type_and_method_with_strongly_typed_first_arg()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArgStronglyTyped");

            AssertEx.DoesNotThrow(() => attribute.Validate("Validation returns success for any string", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsStronglyTyped");
            AssertEx.DoesNotThrow(() => attribute.Validate(new TestClass("Validation returns success for any TestClass"), s_testValidationContext));
        }
Exemplo n.º 16
0
        public static void Validate_throws_InvalidOperationException_for_invalid_ValidatorType()
        {
            var attribute = new CustomValidationAttribute(null, "Does not matter");

            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(NonPublicCustomValidator), "Does not matter");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));
        }
Exemplo n.º 17
0
        public static void Can_construct_attribute_and_get_invalid_values()
        {
            var attribute = new CustomValidationAttribute(null, null);
            Assert.Equal(null, attribute.ValidatorType);
            Assert.Equal(null, attribute.Method);

            attribute = new CustomValidationAttribute(typeof(string), string.Empty);
            Assert.Equal(typeof(string), attribute.ValidatorType);
            Assert.Equal(string.Empty, attribute.Method);

            attribute = new CustomValidationAttribute(typeof(int), " \t\r\n");
            Assert.Equal(typeof(int), attribute.ValidatorType);
            Assert.Equal(" \t\r\n", attribute.Method);
        }
Exemplo n.º 18
0
        public static void Can_construct_attribute_and_get_invalid_values()
        {
            var attribute = new CustomValidationAttribute(null, null);

            Assert.Equal(null, attribute.ValidatorType);
            Assert.Equal(null, attribute.Method);

            attribute = new CustomValidationAttribute(typeof(string), string.Empty);
            Assert.Equal(typeof(string), attribute.ValidatorType);
            Assert.Equal(string.Empty, attribute.Method);

            attribute = new CustomValidationAttribute(typeof(int), " \t\r\n");
            Assert.Equal(typeof(int), attribute.ValidatorType);
            Assert.Equal(" \t\r\n", attribute.Method);
        }
Exemplo n.º 19
0
        public static void Validate_successful_for_validation_method_with_strongly_typed_first_arg_and_value_type_convertible_to_expected_type()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodIntegerArg");

            // validation works for integer value as it is declared with integer arg
            AssertEx.DoesNotThrow(() => attribute.Validate(123, s_testValidationContext));

            // also works with bool, long, float & double as can convert them to int
            AssertEx.DoesNotThrow(() => attribute.Validate(false, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(123456L, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(123.456F, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(123.456D, s_testValidationContext));

            // does not work with TestClass or DateTime as cannot convert them
            Assert.Throws <ValidationException>(() => attribute.Validate(new TestClass("Does not convert to int"), s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate(new DateTime(2014, 3, 19), s_testValidationContext));
        }
Exemplo n.º 20
0
		public void Constructor ()
		{
			var attr = new CustomValidationAttribute (null, "MyMethod");
			Assert.IsNull (attr.ValidatorType, "#A1-1");
			Assert.AreEqual ("MyMethod", attr.Method, "#A1-2");

			attr = new CustomValidationAttribute (typeof (string), null);
			Assert.AreEqual (typeof (string), attr.ValidatorType, "#A2-1");
			Assert.IsNull (attr.Method, "#A2-2");

			attr = new CustomValidationAttribute (null, null);
			Assert.IsNull (attr.ValidatorType, "#A3-1");
			Assert.IsNull (attr.Method, "#A3-2");

			attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
			Assert.AreEqual (typeof (string), attr.ValidatorType, "#A5-1");
			Assert.AreEqual ("NoSuchMethod", attr.Method, "#A5-2");
		}
Exemplo n.º 21
0
		public void TypeId ()
		{
			var attr = new CustomValidationAttribute (null, "MyMethod");
			Assert.IsNotNull (attr.TypeId, "#A1-1");
			Assert.AreEqual (typeof (Tuple<string, Type>), attr.TypeId.GetType (), "#A1-2");

			var typeid = attr.TypeId as Tuple <string, Type>;
			Assert.IsNotNull (typeid.Item1, "#A2-1");
			Assert.AreEqual ("MyMethod", typeid.Item1, "#A2-2");
			Assert.IsNull (typeid.Item2, "#A2-3");

			attr = new CustomValidationAttribute (typeof (CustomValidationAttributeTest), "MyMethod");
			typeid = attr.TypeId as Tuple<string, Type>;
			Assert.IsNotNull (typeid.Item1, "#A3-1");
			Assert.AreEqual ("MyMethod", typeid.Item1, "#A3-2");
			Assert.IsNotNull (typeid.Item2, "#A3-3");
			Assert.AreEqual (typeof (CustomValidationAttributeTest), typeid.Item2, "#A3-4");

			var typeid2 = attr.TypeId as Tuple<string, Type>;
			Assert.IsTrue (Object.ReferenceEquals (typeid, typeid2), "#A4");
		}
Exemplo n.º 22
0
        public static void Validate_throws_ValidationException_for_invalid_values()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArg");

            Assert.Throws <ValidationException>(
                () => attribute.Validate(new TestClass("Value is not a string - so validation fails"), s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgs");
            Assert.Throws <ValidationException>(
                () => attribute.Validate("Value is not a TestClass - so validation fails", s_testValidationContext));

            // This Assert produces different results on Core CLR versus .Net Native. In CustomValidationAttribute.TryConvertValue()
            // we call Convert.ChangeType(instanceOfAClass, typeof(string), ...). On K this throws InvalidCastException because
            // the class does not implement IConvertible. On N this just returns the result of ToString() on the class and does not throw.
            // As of 7/9/14 no plans to change this.
            //attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArgStronglyTyped");
            //Assert.Throws<ValidationException>(
            //    () => attribute.Validate(new TestClass("Validation method expects a string but is given a TestClass and so fails"), TestValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsStronglyTyped");
            Assert.Throws <ValidationException>(
                () => attribute.Validate("Validation method expects a TestClass but is given a string and so fails", s_testValidationContext));
        }
Exemplo n.º 23
0
        public static void RequiresValidationContext_throws_InvalidOperationException_if_attribute_not_well_formed()
        {
            var attribute = new CustomValidationAttribute(null, "Does not matter");

            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(NonPublicCustomValidator), "Does not matter");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), null);
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), string.Empty);
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonExistentMethod");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonPublicValidationMethod");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "NonStaticValidationMethod");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodDoesNotReturnValidationResult");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithNoArgs");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodWithByRefArg");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "ValidationMethodTwoArgsButSecondIsNotValidationContext");
            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);
        }
        public static void RequiresValidationContext_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);

            Assert.Throws <InvalidOperationException>(() => attribute.RequiresValidationContext);
        }
        public static void Validate_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);

            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));
        }
        public static void FormatErrorMessage_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);

            Assert.Throws <InvalidOperationException>(() => attribute.FormatErrorMessage("name"));
        }
 public static void FormatErrorMessage_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
 {
     CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);
     Assert.Throws<InvalidOperationException>(() => attribute.FormatErrorMessage("name"));
 }
        public static void Validate_ValidArguments_DoesNotThrow(string method, object value)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(typeof(CustomValidator), method);

            attribute.Validate(value, s_testValidationContext);
        }
 public static void Constructor(Type validatorType, string method)
 {
     CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);
     Assert.Equal(validatorType, attribute.ValidatorType);
     Assert.Equal(method, attribute.Method);
 }
Exemplo n.º 30
0
		public void FormatErrorMessage ()
		{
			var attr = new CustomValidationAttribute (null, null);
			string msg = null;

			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute.ValidatorType was not specified.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 88

				msg = attr.FormatErrorMessage (null);
			}, "#A1");

			attr = new CustomValidationAttribute (typeof (string), null);
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 102

				msg = attr.FormatErrorMessage (null);
			}, "#A2");

			attr = new CustomValidationAttribute (typeof (string), String.Empty);
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 117

				msg = attr.FormatErrorMessage (null);
			}, "#A3");

			attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute method 'NoSuchMethod' does not exist in type 'String' or is not public and static.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 126

				msg = attr.FormatErrorMessage (null);
			}, "#A4");

			attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The custom validation type 'PrivateValidatorMethodContainer' must be public.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 138

				msg = attr.FormatErrorMessage (null);
			}, "#A5");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute method 'MethodOne' in type 'PublicValidatorMethodContainer' 
				//        must return System.ComponentModel.DataAnnotations.ValidationResult.  Use System.ComponentModel.DataAnnotations.ValidationResult.Success 
				//        to represent success.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 150
				msg = attr.FormatErrorMessage (null);
			}, "#A6");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute method 'MethodTwo' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodTwo(object value, ValidationContext context).  The value can be strongly typed.  The ValidationContext parameter is optional.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 163
				msg = attr.FormatErrorMessage (null);
			}, "#A7");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
			msg = attr.FormatErrorMessage (null);
			Assert.IsNotNull (msg, "#A8-1");
			Assert.IsTrue (msg.Length > 0, "#A8-2");
			Assert.AreEqual (" is not valid.", msg, "#A8-3");
			
			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
			msg = attr.FormatErrorMessage ("test");
			Assert.IsNotNull (msg, "#A9-1");
			Assert.IsTrue (msg.Length > 0, "#A9-2");
			Assert.AreEqual ("test is not valid.", msg, "#A9-3");
			
			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute method 'MethodFive' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodFive(object value, ValidationContext context).  The value can be strongly typed.  The ValidationContext parameter is optional.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 180
				msg = attr.FormatErrorMessage (null);
			}, "#A10");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
				// System.InvalidOperationException : The CustomValidationAttribute method 'MethodSix' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodSix(object value, ValidationContext context).  The value can be strongly typed.  The ValidationContext parameter is optional.
				//
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
				// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
				// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 191
				msg = attr.FormatErrorMessage (null);
			}, "#A11");
		}
Exemplo n.º 31
0
 public static void Validate_successful_for_validation_method_with_strongly_typed_first_arg_and_value_type_assignable_from_expected_type()
 {
     var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsStronglyTyped");
     AssertEx.DoesNotThrow(
         () => attribute.Validate(new DerivedTestClass("Validation returns success for DerivedTestClass too"), s_testValidationContext));
 }
Exemplo n.º 32
0
        public static void Validation_works_for_null_and_non_null_values_and_validation_method_taking_nullable_value_type()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArgNullable");
            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));
            AssertEx.DoesNotThrow(
                () => attribute.Validate(new TestStruct() { Value = "Valid Value" }, s_testValidationContext));
            Assert.Throws<ValidationException>(
                () => attribute.Validate(new TestStruct() { Value = "Some non-valid value" }, s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsWithFirstNullable");
            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));
            AssertEx.DoesNotThrow(
                () => attribute.Validate(new TestStruct() { Value = "Valid Value" }, s_testValidationContext));
            Assert.Throws<ValidationException>(
                () => attribute.Validate(new TestStruct() { Value = "Some non-valid value" }, s_testValidationContext));
        }
Exemplo n.º 33
0
        public static void Validate_throws_ValidationException_for_invalid_values()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArg");
            Assert.Throws<ValidationException>(
                () => attribute.Validate(new TestClass("Value is not a string - so validation fails"), s_testValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgs");
            Assert.Throws<ValidationException>(
                () => attribute.Validate("Value is not a TestClass - so validation fails", s_testValidationContext));

            // This Assert produces different results on Core CLR versus .Net Native. In CustomValidationAttribute.TryConvertValue()
            // we call Convert.ChangeType(instanceOfAClass, typeof(string), ...). On K this throws InvalidCastException because
            // the class does not implement IConvertible. On N this just returns the result of ToString() on the class and does not throw.
            // As of 7/9/14 no plans to change this.
            //attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodOneArgStronglyTyped");
            //Assert.Throws<ValidationException>(
            //    () => attribute.Validate(new TestClass("Validation method expects a string but is given a TestClass and so fails"), TestValidationContext));

            attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodTwoArgsStronglyTyped");
            Assert.Throws<ValidationException>(
                () => attribute.Validate("Validation method expects a TestClass but is given a string and so fails", s_testValidationContext));
        }
Exemplo n.º 34
0
 public static void Can_construct_attribute_and_get_values()
 {
     var attribute = new CustomValidationAttribute(typeof(CustomValidator), "SomeMethod");
     Assert.Equal(typeof(CustomValidator), attribute.ValidatorType);
     Assert.Equal("SomeMethod", attribute.Method);
 }
 public static void Validate_ValidArguments_DoesNotThrow(string method, object value)
 {
     CustomValidationAttribute attribute = new CustomValidationAttribute(typeof(CustomValidator), method);
     attribute.Validate(value, s_testValidationContext);
 }
        public static void RequiresValidationContext_Get_ReturnsExpected(Type validatorType, string method, bool expected)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);

            Assert.Equal(expected, attribute.RequiresValidationContext);
        }
 public static void RequiresValidationContext_Get_ReturnsExpected(Type validatorType, string method, bool expected)
 {
     CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);
     Assert.Equal(expected, attribute.RequiresValidationContext);
 }
Exemplo n.º 38
0
        public static void Validate_successful_for_validation_method_with_strongly_typed_first_arg_and_value_type_convertible_to_expected_type()
        {
            var attribute = new CustomValidationAttribute(typeof(CustomValidator), "CorrectValidationMethodIntegerArg");

            // validation works for integer value as it is declared with integer arg
            AssertEx.DoesNotThrow(() => attribute.Validate(123, s_testValidationContext));

            // also works with bool, long, float & double as can convert them to int
            AssertEx.DoesNotThrow(() => attribute.Validate(false, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(123456L, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(123.456F, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(123.456D, s_testValidationContext));

            // does not work with TestClass or DateTime as cannot convert them
            Assert.Throws<ValidationException>(() => attribute.Validate(new TestClass("Does not convert to int"), s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate(new DateTime(2014, 3, 19), s_testValidationContext));
        }
        public static void Validate_InvalidArguments_ThrowsValidationException(string method, object value, Type exceptionType)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(typeof(CustomValidator), method);
            Assert.Throws(exceptionType, () => attribute.Validate(value, s_testValidationContext));

            Assert.NotEmpty(attribute.FormatErrorMessage("name"));
        }
Exemplo n.º 40
0
		public void IsValid ()
		{
			var attr = new CustomValidationAttribute (null, null);

			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A1");

			attr = new CustomValidationAttribute (typeof (string), null);
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A2");

			attr = new CustomValidationAttribute (typeof (string), String.Empty);
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A3");

			attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A4");

			attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A5");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A6");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A7");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
			bool valid = attr.IsValid ("test");
			Assert.IsTrue (valid, "#A8-1");
			valid = attr.IsValid (null);
			Assert.IsFalse (valid, "#A8-2");
			valid = attr.IsValid ("failTest");
			Assert.IsFalse (valid, "#A8-3");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
			valid = attr.IsValid ("test");
			Assert.IsTrue (valid, "#A9-1");
			valid = attr.IsValid (null);
			Assert.IsFalse (valid, "#A9-2");
			valid = attr.IsValid ("failTest");
			Assert.IsFalse (valid, "#A9-3");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A10");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
			AssertExtensions.Throws<InvalidOperationException> (() => {
				attr.IsValid ("test");
			}, "#A11");

			attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSeven");
			AssertExtensions.Throws<ApplicationException> (() => {
				attr.IsValid ("test");
			}, "#A12");
		}
 public static void Validate_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
 {
     CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("Does not matter", s_testValidationContext));
 }
 public static void RequiresValidationContext_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
 {
     CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);
     Assert.Throws<InvalidOperationException>(() => attribute.RequiresValidationContext);
 }