Пример #1
0
        public void FormatErrorMessage_NotPerformedValidation_ContainsName()
        {
            CustomValidationAttribute attribute = GetAttribute(nameof(CustomValidator.CorrectValidationMethodOneArg));
            string errorMessage = attribute.FormatErrorMessage("name");

            Assert.Contains("name", errorMessage);
            Assert.Equal(errorMessage, attribute.FormatErrorMessage("name"));
        }
Пример #2
0
        public void FormatErrorMessage_PerformedValidation_DoesNotContainName()
        {
            CustomValidationAttribute attribute = GetAttribute(nameof(CustomValidator.CorrectValidationMethodOneArg));

            Assert.False(attribute.IsValid(new TestClass("AnyString")));

            string errorMessage = attribute.FormatErrorMessage("name");

            Assert.DoesNotContain("name", errorMessage);
            Assert.Equal(errorMessage, attribute.FormatErrorMessage("name"));
        }
Пример #3
0
        /// <summary>
        /// エラーメッセージの設定
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public override string FormatErrorMessage(string name)
        {
            Attr.ErrorMessage             = this.ErrorMessage;
            Attr.ErrorMessageResourceType = this.ErrorMessageResourceType;
            Attr.ErrorMessageResourceName = this.ErrorMessageResourceName;

            return(Attr.FormatErrorMessage(name));
        }
Пример #4
0
        public void CustomValidation_Invalid_Type_Reference_Type_Fails()
        {
            CustomValidationAttribute cva     = new CustomValidationAttribute(typeof(MockValidator), "IsValid");
            ValidationContext         context = new ValidationContext(new object(), null, null);

            Assert.IsNotNull(cva.GetValidationResult(cva, context));   // cva is wrong type -- must be string
            Assert.AreEqual("wrong is not valid.", cva.FormatErrorMessage("wrong"));
        }
Пример #5
0
        public void CustomValidation_NonConvertable_Nullable_Value_Type_Fails()
        {
            CustomValidationAttribute cva     = new CustomValidationAttribute(typeof(MockValidator), "IsValidNullableValueType");
            ValidationContext         context = new ValidationContext(new object(), null, null);

            Assert.IsNotNull(cva.GetValidationResult("fred", context));
            Assert.AreEqual("type is not valid.", cva.FormatErrorMessage("type"));
        }
Пример #6
0
        public void CustomValidation_Invalid_Value_3_Param()
        {
            CustomValidationAttribute cva     = new CustomValidationAttribute(typeof(MockValidator), "IsValid");
            ValidationContext         context = new ValidationContext(new object(), null, null);

            context.MemberName = "member";
            Assert.IsNotNull(cva.GetValidationResult("Joe", context));
            Assert.AreEqual("$member", cva.FormatErrorMessage(""));
        }
Пример #7
0
        public void CustomValidation_Out_of_Range_Value_Type_Fails()
        {
            CustomValidationAttribute cva     = new CustomValidationAttribute(typeof(MockValidator), "IsValidValueType");
            ValidationContext         context = new ValidationContext(new object(), null, null);

            context.MemberName = "range";
            Assert.IsNotNull(cva.GetValidationResult(-1, context));
            Assert.AreEqual("wrong range", cva.FormatErrorMessage(""));
        }
Пример #8
0
        public void CustomValidation_Invalid_Value_1_Param()
        {
            CustomValidationAttribute cva = new CustomValidationAttribute(typeof(MockValidator), "IsValidNoMessage");

            cva.ErrorMessage = "The value '{0}' is bogus";  // to show we get message from ErrorMessage instead
            ValidationContext context = new ValidationContext(new object(), null, null);

            Assert.IsNotNull(cva.GetValidationResult("Joe", context));
            Assert.AreEqual("The value 'joe' is bogus", cva.FormatErrorMessage("joe"));
        }
Пример #9
0
        // Helper to assert the attribute fails with the expected error
        private static void AssertFailure(CustomValidationAttribute cva, string expectedError)
        {
            ValidationContext context = new ValidationContext(new object(), null, null);

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate() {
                cva.GetValidationResult(null, context);
            }, expectedError);

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate() {
                cva.FormatErrorMessage(string.Empty);
            }, expectedError);
        }
Пример #10
0
        public override AttributeDeclaration GetAttributeDeclaration(Attribute attribute)
        {
            CustomValidationAttribute cva = (CustomValidationAttribute)attribute;

            // Our convention is that parameter validation in the CVA occurs when it is
            // first used.  Simply ask the attribute to produce an error message, as this
            // will trigger an InvalidOperationException if the attribute is ill-formed
            cva.FormatErrorMessage(string.Empty);

            // Delegate to the base implementation to generate the attribute.
            // Note that the base implementation already checks that Types are
            // shared so we do not perform that check here.
            AttributeDeclaration attributeDeclaration = base.GetAttributeDeclaration(attribute);

            attributeDeclaration.RequiredTypes.Add(cva.ValidatorType);
            attributeDeclaration.RequiredMethods.Add(cva.ValidatorType.GetMethod(cva.Method));

            return(attributeDeclaration);
        }
Пример #11
0
        public void FormatErrorMessage()
        {
            var    attr = new CustomValidationAttribute(null, null);
            string msg  = null;

            Assert.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);
            Assert.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);
            Assert.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");
            Assert.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");
            Assert.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");
            Assert.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");
            Assert.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");
            Assert.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");
            Assert.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");
        }
 public void CustomValidation_Invalid_Type_Reference_Type_Fails() {
     CustomValidationAttribute cva = new CustomValidationAttribute(typeof(MockValidator), "IsValid");
     ValidationContext context = new ValidationContext(new object(), null, null);
     Assert.IsNotNull(cva.GetValidationResult(cva, context));   // cva is wrong type -- must be string
     Assert.AreEqual("wrong is not valid.", cva.FormatErrorMessage("wrong"));
 }
 public void CustomValidation_Out_of_Range_Value_Type_Fails() {
     CustomValidationAttribute cva = new CustomValidationAttribute(typeof(MockValidator), "IsValidValueType");
     ValidationContext context = new ValidationContext(new object(), null, null);
     context.MemberName = "range";
     Assert.IsNotNull(cva.GetValidationResult(-1, context));
     Assert.AreEqual("wrong range", cva.FormatErrorMessage(""));
 }
 public void CustomValidation_NonConvertable_Nullable_Value_Type_Fails() {
     CustomValidationAttribute cva = new CustomValidationAttribute(typeof(MockValidator), "IsValidNullableValueType");
     ValidationContext context = new ValidationContext(new object(), null, null);
     Assert.IsNotNull(cva.GetValidationResult("fred", context));
     Assert.AreEqual("type is not valid.", cva.FormatErrorMessage("type"));
 }
Пример #15
0
        public static void FormatErrorMessage_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
        {
            CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);

            Assert.Throws <InvalidOperationException>(() => attribute.FormatErrorMessage("name"));
        }
        // Helper to assert the attribute fails with the expected error
        private static void AssertFailure(CustomValidationAttribute cva, string expectedError) {
            ValidationContext context = new ValidationContext(new object(), null, null);

            ExceptionHelper.ExpectException<InvalidOperationException>(delegate() {
                cva.GetValidationResult(null, context);
            }, expectedError);

            ExceptionHelper.ExpectException<InvalidOperationException>(delegate() {
                cva.FormatErrorMessage(string.Empty);
            }, expectedError);
        }
        public void CustomValidation_Invalid_Value_1_Param() {
            CustomValidationAttribute cva = new CustomValidationAttribute(typeof(MockValidator), "IsValidNoMessage");
            cva.ErrorMessage = "The value '{0}' is bogus";  // to show we get message from ErrorMessage instead
            ValidationContext context = new ValidationContext(new object(), null, null);

            Assert.IsNotNull(cva.GetValidationResult("Joe", context));
            Assert.AreEqual("The value 'joe' is bogus", cva.FormatErrorMessage("joe"));
        }
 public static void FormatErrorMessage_BadlyFormed_ThrowsInvalidOperationException(Type validatorType, string method)
 {
     CustomValidationAttribute attribute = new CustomValidationAttribute(validatorType, method);
     Assert.Throws<InvalidOperationException>(() => attribute.FormatErrorMessage("name"));
 }
 public void CustomValidation_Invalid_Value_3_Param() {
     CustomValidationAttribute cva = new CustomValidationAttribute(typeof(MockValidator), "IsValid");
     ValidationContext context = new ValidationContext(new object(), null, null);
     context.MemberName = "member";
     Assert.IsNotNull(cva.GetValidationResult("Joe", context));
     Assert.AreEqual("$member", cva.FormatErrorMessage(""));
 }