Beispiel #1
0
        public static void Validate_successful_for_empty_string_if_AllowEmptyStrings_is_true()
        {
            var attribute = new RequiredAttribute();

            attribute.AllowEmptyStrings = true;
            AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext));
        }
        public static void Validate_successful_for_null_or_empty_value()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext));
        }
        public static void TestNoThrowIfOverrideIsValid05()
        {
            var attribute = new ValidationAttributeOverrideBothIsValids();

            Assert.Throws <ValidationException>(() => attribute.Validate("Valid 1-Arg Value", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("Valid 2-Args Value", s_testValidationContext));
        }
        public static void TestNoThrowIfOverrideIsValid01()
        {
            var attribute = new ValidationAttributeOverrideTwoArgsIsValid();

            AssertEx.DoesNotThrow(
                () => attribute.Validate("Valid Value", "Name to put in error message does not matter - no error"));
        }
Beispiel #5
0
        public static void ValidateProperty_succeeds_if_null_passed_to_nullable_property()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "NullableProperty";
            AssertEx.DoesNotThrow(() => Validator.ValidateProperty(null, validationContext));
        }
        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 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));
        }
Beispiel #8
0
        public static void GetDataTypeName_and_IsValid_on_non_null_custom_DataTypeAttribute_is_successful()
        {
            var attribute = new DataTypeAttribute("TestCustomDataType");

            Assert.Equal("TestCustomDataType", attribute.GetDataTypeName());
            AssertEx.DoesNotThrow(() => attribute.Validate(new object(), s_testValidationContext));
        }
        public static void Validate_successful_for_null_or_empty_value()
        {
            var attribute = new RegularExpressionAttribute("SomePattern");

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));         // Null is valid
            AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext)); // Empty string is valid
        }
        public static void Validate_successful_for_matching_non_flags_enums_and_matching_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            AssertEx.DoesNotThrow(() => attribute.Validate(NonFlagsEnumType.A, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(10, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(100, s_testValidationContext));
        }
Beispiel #11
0
        public static void ValidateValue_succeeds_if_no_attributes_to_validate_regardless_of_value()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "NoAttributesProperty";
            AssertEx.DoesNotThrow(() => Validator.ValidateValue(null, validationContext, Enumerable.Empty <ValidationAttribute>()));
            AssertEx.DoesNotThrow(() => Validator.ValidateValue(new object(), validationContext, Enumerable.Empty <ValidationAttribute>()));
        }
Beispiel #12
0
        public static void ValidateProperty_succeeds_if_all_attributes_are_valid()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyWithRequiredAttribute";
            AssertEx.DoesNotThrow(
                () => Validator.ValidateProperty("Valid Value", validationContext));
        }
Beispiel #13
0
        public static void ValidateProperty_succeeds_if_no_attributes_to_validate()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "NoAttributesProperty";
            AssertEx.DoesNotThrow(
                () => Validator.ValidateProperty("Any Value", validationContext));
        }
Beispiel #14
0
        public static void Validate_successful_for_value_matching_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");

            AssertEx.DoesNotThrow(() => attribute.Validate("defghi", s_testValidationContext));

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            AssertEx.DoesNotThrow(() => attribute.Validate("bcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy", s_testValidationContext));
        }
Beispiel #15
0
        public static void ValidateValue_succeeds_if_Property_has_no_RequiredAttribute_and_value_is_valid()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyToBeTested";
            var attributesToValidate = new ValidationAttribute[] { new ValidValueStringPropertyAttribute() };

            AssertEx.DoesNotThrow(() => Validator.ValidateValue("Valid Value", validationContext, attributesToValidate));
        }
        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));
        }
Beispiel #17
0
        public static void Constructor_creates_new_instance_for_two_arg_constructor()
        {
            var testDataAnnotationsDerived = new TestClass();

            AssertEx.DoesNotThrow(() => new ValidationContext(testDataAnnotationsDerived, null));
            var items = new Dictionary <object, object>();

            AssertEx.DoesNotThrow(() => new ValidationContext(testDataAnnotationsDerived, items));
        }
Beispiel #18
0
        public static void GetValidationResult_returns_DefaultErrorMessage_if_ErrorMessage_is_not_set()
        {
            var attribute         = new PhoneAttribute();
            var toBeTested        = new PhoneClassToBeTested();
            var validationContext = new ValidationContext(toBeTested);

            validationContext.MemberName = "PhonePropertyToBeTested";
            AssertEx.DoesNotThrow(() => attribute.GetValidationResult(toBeTested, validationContext));
        }
        public static void Validate_successful_for_valid_values()
        {
            var attribute = new UrlAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext)); // Null is valid
            AssertEx.DoesNotThrow(() => attribute.Validate("http://foo.bar", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("https://foo.bar", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("ftp://foo.bar", s_testValidationContext));
        }
        public static void TestNoThrowIfOverrideIsValid04()
        {
            var attribute = new ValidationAttributeOverrideBothIsValids();

            AssertEx.DoesNotThrow(
                () => attribute.Validate("Valid 1-Arg Value", "Name to put in error message does not matter - no error"));
            Assert.Throws <ValidationException>(
                () => attribute.Validate("Valid 2-Args Value", "Name to put in error message does not matter - no error"));
        }
Beispiel #21
0
        public static void CompareAttribute_can_be_derived_from_and_override_is_valid()
        {
            var otherObject   = new CompareObject("a");
            var currentObject = new CompareObject("b");
            var testContext   = new ValidationContext(otherObject, null, null);

            var attribute = new DerivedCompareAttribute("CompareProperty");

            AssertEx.DoesNotThrow(() => attribute.Validate(currentObject.CompareProperty, testContext));
        }
Beispiel #22
0
        public static void ValidateObject_succeeds_if_no_errors()
        {
            var objectToBeValidated = "ToBeValidated";
            var validationContext   = new ValidationContext(objectToBeValidated);

            AssertEx.DoesNotThrow(
                () => Validator.ValidateObject(objectToBeValidated, validationContext));
            AssertEx.DoesNotThrow(
                () => Validator.ValidateObject(objectToBeValidated, validationContext, true));
        }
Beispiel #23
0
        public static void Validate_does_not_throw_when_compared_objects_are_equal()
        {
            var otherObject   = new CompareObject("test");
            var currentObject = new CompareObject("test");
            var testContext   = new ValidationContext(otherObject, null, null);

            var attribute = new CompareAttribute("CompareProperty");

            AssertEx.DoesNotThrow(() => attribute.Validate(currentObject.CompareProperty, testContext));
        }
Beispiel #24
0
        public static void Can_validate_valid_values_for_double_constructor()
        {
            var attribute = new RangeAttribute(1.0, 3.0);

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));         // null is valid
            AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext)); // empty string is valid
            AssertEx.DoesNotThrow(() => attribute.Validate(1.0, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(2.0, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(3.0, s_testValidationContext));
        }
        public static void Validate_successful_for_valid_values()
        {
            var attribute = new CreditCardAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));                  // Null is valid
            AssertEx.DoesNotThrow(() => attribute.Validate("0000000000000000", s_testValidationContext));    // Simplest valid value
            AssertEx.DoesNotThrow(() => attribute.Validate("1234567890123452", s_testValidationContext));    // Good checksum
            AssertEx.DoesNotThrow(() => attribute.Validate("1234-5678-9012-3452", s_testValidationContext)); // Good checksum, with dashes
            AssertEx.DoesNotThrow(() => attribute.Validate("1234 5678 9012 3452", s_testValidationContext)); // Good checksum, with spaces
        }
        public static void Validate_successful_for_valid_domain_part()
        {
            var attribute = new EmailAddressAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate("*****@*****.**", s_testValidationContext));       // Simple valid value
            AssertEx.DoesNotThrow(() => attribute.Validate("someName@some~domain.com", s_testValidationContext));      // With tilde
            AssertEx.DoesNotThrow(() => attribute.Validate("someName@some_domain.com", s_testValidationContext));      // With underscore
            AssertEx.DoesNotThrow(() => attribute.Validate("*****@*****.**", s_testValidationContext));             // numbers are valid
            AssertEx.DoesNotThrow(() => attribute.Validate("someName@someDomain\uFFEF.com", s_testValidationContext)); // With valid \u character
        }
Beispiel #27
0
        public static void TestValidateObjectNotThrowIfvalidateAllPropertiesFalse()
        {
            var objectToBeValidated = new ToBeValidated()
            {
                PropertyWithRequiredAttribute = "Invalid Value"
            };
            var validationContext = new ValidationContext(objectToBeValidated);

            AssertEx.DoesNotThrow(
                () => Validator.ValidateObject(objectToBeValidated, validationContext, false));
        }
Beispiel #28
0
        public static void ValidateObject_succeeds_if_validateAllProperties_is_true_and_all_attributes_are_valid()
        {
            var objectToBeValidated = new ToBeValidated()
            {
                PropertyWithRequiredAttribute = "Valid Value"
            };
            var validationContext = new ValidationContext(objectToBeValidated);

            AssertEx.DoesNotThrow(
                () => Validator.ValidateObject(objectToBeValidated, validationContext, true));
        }
        public static void Validate_successful_for_value_matching_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");

            attribute.MatchTimeoutInMilliseconds = 5000; // note: timeout is just a number much larger than we expect the test to take
            AssertEx.DoesNotThrow(() => attribute.Validate("defghi", s_testValidationContext));

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            attribute.MatchTimeoutInMilliseconds = 10000; // note: timeout is just a number much larger than we expect the test to take
            AssertEx.DoesNotThrow(() => attribute.Validate("bcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy", s_testValidationContext));
        }
        public static void Validate_successful_for_valid_local_part()
        {
            var attribute = new EmailAddressAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate("*****@*****.**", s_testValidationContext));               // Simple valid value
            AssertEx.DoesNotThrow(() => attribute.Validate("*****@*****.**", s_testValidationContext));                   // numbers are valid
            AssertEx.DoesNotThrow(() => attribute.Validate("*****@*****.**", s_testValidationContext));     // With dot in name
            AssertEx.DoesNotThrow(() => attribute.Validate("\[email protected]", s_testValidationContext));                 // With valid \u character
            AssertEx.DoesNotThrow(() => attribute.Validate("!#$%&'*+-/=?^_`|[email protected]", s_testValidationContext));      // With valid (but unusual) characters
            AssertEx.DoesNotThrow(() => attribute.Validate("\"firstName.lastName\"@someDomain.com", s_testValidationContext)); // quotes around whole local part
        }