예제 #1
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));
        }
예제 #2
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));
        }
예제 #3
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));
        }
예제 #4
0
        public static void ValidatePropertyThrowsIf_ValidationContext_MemberName_is_for_a_public_indexer()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "Item";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(null, validationContext));
        }
예제 #5
0
        public static void ValidatePropertyThrowsIf_value_passed_is_of_wrong_type_to_be_assigned_to_property()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "NoAttributesProperty";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(123, validationContext));
        }
예제 #6
0
        public static void ValidatePropertyThrowsIf_ValidationContext_MemberName_does_not_exist_on_object()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "NonExist";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(null, validationContext));
        }
예제 #7
0
        public static void ValidateProperty_throws_ValidationException_if_Required_attribute_test_fails()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyWithRequiredAttribute";
            var exception = Assert.Throws <ValidationException>(
                () => Validator.ValidateProperty(null, validationContext));

            AssertEx.IsType <RequiredAttribute>(exception.ValidationAttribute);
            // cannot check error message - not defined on ret builds
            Assert.Null(exception.Value);
        }
예제 #8
0
        public static void ValidateProperty_throws_ValidationException_if_errors()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyToBeTested";
            var exception = Assert.Throws <ValidationException>(
                () => Validator.ValidateProperty("Invalid Value", validationContext));

            AssertEx.IsType <ValidValueStringPropertyAttribute>(exception.ValidationAttribute);
            Assert.Equal("ValidValueStringPropertyAttribute.IsValid failed for value Invalid Value", exception.ValidationResult.ErrorMessage);
            Assert.Equal("Invalid Value", exception.Value);
        }
예제 #9
0
        public static void ValidatePropertyThrowsIf_ValidationContext_MemberName_is_null_or_empty()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = null;
            Assert.Throws <ArgumentNullException>(
                () => Validator.ValidateProperty(null, validationContext));

            validationContext.MemberName = string.Empty;
            Assert.Throws <ArgumentNullException>(
                () => Validator.ValidateProperty(null, validationContext));
        }
예제 #10
0
        public static void ValidatePropertyThrowsIf_null_passed_to_non_nullable_property()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            // cannot assign null to a non-value-type property
            validationContext.MemberName = "EnumProperty";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(null, validationContext));

            // cannot assign null to a non-nullable property
            validationContext.MemberName = "NonNullableProperty";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(null, validationContext));
        }
예제 #11
0
        public static void ValidatePropertyThrowsIf_ValidationContext_MemberName_is_not_public()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "InternalProperty";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(null, validationContext));

            validationContext.MemberName = "ProtectedProperty";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(null, validationContext));

            validationContext.MemberName = "PrivateProperty";
            Assert.Throws <ArgumentException>(
                () => Validator.ValidateProperty(null, validationContext));
        }
예제 #12
0
 public static void ValidatePropertyThrowsIf_value_is_null()
 {
     Assert.Throws <ArgumentNullException>(
         () => Validator.ValidateProperty(null, s_estValidationContext));
 }
예제 #13
0
 public static void ValidatePropertyThrowsIf_ValidationContext_is_null()
 {
     Assert.Throws <ArgumentNullException>(
         () => Validator.ValidateProperty(new object(), validationContext: null));
 }