Esempio n. 1
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"));
        }
Esempio n. 2
0
        public void IsValid()
        {
            var attr = new CustomValidationAttribute(null, null);

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

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

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

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

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

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

            attr = new CustomValidationAttribute(typeof(PublicValidatorMethodContainer), "MethodTwo");
            Assert.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");
            Assert.Throws <InvalidOperationException> (() => {
                attr.IsValid("test");
            }, "#A10");

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

            attr = new CustomValidationAttribute(typeof(PublicValidatorMethodContainer), "MethodSeven");
            Assert.Throws <ApplicationException> (() => {
                attr.IsValid("test");
            }, "#A12");
        }
Esempio n. 3
0
 /// <summary>
 /// データチェック
 /// </summary>
 /// <param name="value">値</param>
 /// <returns>検証成功時はtrue</returns>
 public override bool IsValid(object value)
 {
     return(Attr.IsValid(value));
 }