public void PropertyValidationFilter_Null_ForType()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attributes = new Attribute[] { new ValidateNeverAttribute() };
            var key        = ModelMetadataIdentity.ForType(typeof(ValidateNeverClass));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, null, null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.Null(context.ValidationMetadata.PropertyValidationFilter);
        }
        public void PropertyValidationFilter_Null_IfPropertyHasValidateNeverOnItsType()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attributes = new Attribute[] { new ValidateNeverAttribute() };
            var key        = ModelMetadataIdentity.ForProperty(typeof(string).GetProperty(nameof(string.Length)), typeof(int), typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0], null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.Null(context.ValidationMetadata.PropertyValidationFilter);
        }
        public void GetValidationDetails_MarkedWithModelValidator_ReturnsValidator()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attribute  = new TestModelValidationAttribute();
            var attributes = new Attribute[] { attribute };
            var key        = ModelMetadataIdentity.ForProperty(typeof(string).GetProperty(nameof(string.Length)), typeof(int), typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(new object[0], attributes, null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            var validatorMetadata = Assert.Single(context.ValidationMetadata.ValidatorMetadata);

            Assert.Same(attribute, validatorMetadata);
        }
        public void PropertyValidationFilter_ShouldValidateEntry_False_IfPropertyHasValidateNever()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attributes = new Attribute[] { new ValidateNeverAttribute() };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(new object[0], attributes, null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.NotNull(context.ValidationMetadata.PropertyValidationFilter);
            Assert.False(context.ValidationMetadata.PropertyValidationFilter.ShouldValidateEntry(
                             new ValidationEntry(),
                             new ValidationEntry()));
        }
        public void PropertyValidationFilter_ShouldValidateEntry_False_IfContainingTypeInheritsValidateNever()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var key = ModelMetadataIdentity.ForProperty(
                typeof(ValidateNeverSubclass).GetProperty(nameof(ValidateNeverSubclass.SubclassName)),
                typeof(string),
                typeof(ValidateNeverSubclass));
            var context = new ValidationMetadataProviderContext(key, new ModelAttributes(new object[0], new object[0], null));

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            Assert.NotNull(context.ValidationMetadata.PropertyValidationFilter);
            Assert.False(context.ValidationMetadata.PropertyValidationFilter.ShouldValidateEntry(
                             new ValidationEntry(),
                             new ValidationEntry()));
        }
        public void GetValidationDetails_Validator_AlreadyInContext_Ignores()
        {
            // Arrange
            var provider = new DefaultValidationMetadataProvider();

            var attribute  = new TestValidationAttribute();
            var attributes = new Attribute[] { attribute };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            context.ValidationMetadata.ValidatorMetadata.Add(attribute);

            // Act
            provider.CreateValidationMetadata(context);

            // Assert
            var validatorMetadata = Assert.Single(context.ValidationMetadata.ValidatorMetadata);

            Assert.Same(attribute, validatorMetadata);
        }