public void GetValidators_CachesAllValidators()
    {
        // Arrange
        var cache             = new ClientValidatorCache();
        var metadata          = new TestModelMetadataProvider().GetMetadataForProperty(typeof(TypeWithProperty), "Property1");
        var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

        // Act - 1
        var validators1 = cache.GetValidators(metadata, validatorProvider);

        // Assert - 1
        var attribute1 = Assert.Single(validators1.OfType <RequiredAttributeAdapter>()).Attribute;
        var attribute2 = Assert.Single(validators1.OfType <StringLengthAttributeAdapter>()).Attribute;

        Assert.Contains(attribute1, metadata.ValidatorMetadata); // Copied by provider
        Assert.Contains(attribute2, metadata.ValidatorMetadata); // Copied by provider

        // Act - 2
        var validators2 = cache.GetValidators(metadata, validatorProvider);

        // Assert - 2
        Assert.Same(validators1, validators2);

        Assert.Contains(validators1[0], validators2); // Cached
        Assert.Contains(validators1[1], validators2); // Cached
    }
        public void CreateValidators_ClientValidatorAttribute_DefaultAdapter()
        {
            // Arrange
            var metadataProvider  = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(CustomValidationAttributeOnProperty),
                nameof(CustomValidationAttributeOnProperty.Property));
            var context = new ClientValidatorProviderContext(metadata, GetClientValidatorItems(metadata));

            // Act
            validatorProvider.CreateValidators(context);

            // Assert
            var validatorItems = context.Results;

            Assert.IsType <CustomValidationAttribute>(Assert.Single(validatorItems).Validator);
        }
        public void CreateValidators_FromModelMetadataType_MergedValidators()
        {
            // Arrange
            var metadataProvider  = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(ProductViewModel),
                nameof(ProductViewModel.Name));
            var context = new ClientValidatorProviderContext(metadata, GetClientValidatorItems(metadata));

            // Act
            validatorProvider.CreateValidators(context);

            // Assert
            var validatorItems = context.Results;

            Assert.Equal(2, validatorItems.Count);
            Assert.Single(validatorItems, v => v.Validator is RegularExpressionAttributeAdapter);
            Assert.Single(validatorItems, v => v.Validator is StringLengthAttributeAdapter);
        }
        public void CreateValidators_ClientValidatorAttribute_SpecificAdapter()
        {
            // Arrange
            var metadataProvider  = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(RangeAttributeOnProperty),
                nameof(RangeAttributeOnProperty.Property));
            var context = new ClientValidatorProviderContext(metadata, GetClientValidatorItems(metadata));

            // Act
            validatorProvider.CreateValidators(context);

            // Assert
            var validatorItems = context.Results;

            Assert.Equal(2, validatorItems.Count);
            Assert.Single(validatorItems, v => v.Validator is RangeAttributeAdapter);
            Assert.Single(validatorItems, v => v.Validator is RequiredAttributeAdapter);
        }