Exemple #1
0
        public void GetValidators_DoesNotCacheValidatorsWithIsReusableFalse()
        {
            // Arrange
            var cache             = new ClientValidatorCache();
            var metadata          = new TestModelMetadataProvider().GetMetadataForProperty(typeof(TypeWithProperty), "Property1");
            var validatorProvider = new ProviderWithNonReusableValidators();

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

            // Assert - 1
            var validator1 = Assert.Single(validators1.OfType <RequiredAttributeAdapter>());
            var validator2 = Assert.Single(validators1.OfType <StringLengthAttributeAdapter>());

            Assert.Contains(validator1.Attribute, metadata.ValidatorMetadata); // Copied by provider
            Assert.Contains(validator2.Attribute, metadata.ValidatorMetadata); // Copied by provider

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

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

            Assert.Same(validator1, Assert.Single(validators2.OfType <RequiredAttributeAdapter>()));        // cached
            Assert.NotSame(validator2, Assert.Single(validators2.OfType <StringLengthAttributeAdapter>())); // not cached
        }
Exemple #2
0
        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
        }
Exemple #3
0
        public void GetValidators_ReadsValidatorsFromProperty_IfRecordTypeDoesNotHaveCorrespondingParameter()
        {
            // Arrange
            var cache = new ClientValidatorCache();
            var modelMetadataProvider = new TestModelMetadataProvider();
            var metadata          = modelMetadataProvider.GetMetadataForType(typeof(TestRecordTypeWithProperty));
            var property          = metadata.Properties[nameof(TestRecordTypeWithProperty.Property2)];
            var validatorProvider = new ProviderWithNonReusableValidators();

            // Act
            var validators = cache.GetValidators(property, validatorProvider);

            // Assert
            var validator1 = Assert.Single(validators.OfType <RequiredAttributeAdapter>());
            var validator2 = Assert.Single(validators.OfType <StringLengthAttributeAdapter>());

            Assert.Contains(validator1.Attribute, property.ValidatorMetadata); // Copied by provider
            Assert.Contains(validator2.Attribute, property.ValidatorMetadata); // Copied by provider
        }
Exemple #4
0
        public void GetValidators_ReadsValidatorsFromCorrespondingRecordTypeParameter()
        {
            // Arrange
            var cache = new ClientValidatorCache();
            var modelMetadataProvider = new TestModelMetadataProvider();
            var metadata          = modelMetadataProvider.GetMetadataForType(typeof(TestRecordType));
            var property          = metadata.Properties[nameof(TestRecordType.Property1)];
            var parameter         = metadata.BoundConstructor.BoundConstructorParameters.First(f => f.Name == nameof(TestRecordType.Property1));
            var validatorProvider = new ProviderWithNonReusableValidators();

            // Act
            var validators = cache.GetValidators(property, validatorProvider);

            // Assert
            var validator1 = Assert.Single(validators.OfType <RequiredAttributeAdapter>());
            var validator2 = Assert.Single(validators.OfType <StringLengthAttributeAdapter>());

            Assert.Contains(validator1.Attribute, parameter.ValidatorMetadata); // Copied by provider
            Assert.Contains(validator2.Attribute, parameter.ValidatorMetadata); // Copied by provider
        }