public void GetFromServiceAttributeFromBase_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(DerivedViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == "Calculator");

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            Assert.Single(attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.OfType <FromServicesAttribute>());
        }
        public void GetAttributesForBaseProperty_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(BaseViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == "BaseProperty");

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            Assert.Single(attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.OfType <StringLengthAttribute>());
        }
        public void GetAttributesForVirtualPropertyFromDerived_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(DerivedViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == "VirtualProperty");

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            Assert.Single(attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.OfType <RangeAttribute>());
        }
        private ModelMetadata GetMetadataForParameterCore(Func <object> modelAccessor,
                                                          string parameterName,
                                                          ParameterInfo parameter)
        {
            var parameterInfo =
                CreateParameterInfo(parameter.ParameterType,
                                    ModelAttributes.GetAttributesForParameter(parameter),
                                    parameterName);

            var metadata = CreateMetadataFromPrototype(parameterInfo.Prototype, modelAccessor);

            return(metadata);
        }
        public void GetAttributesForTestPropertyFromDerived_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(DerivedViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == "TestProperty");

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            Assert.Single(attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.OfType <StringLengthAttribute>());
            Assert.DoesNotContain(typeof(RangeAttribute), attributes);
        }
        private PropertyInformation CreatePropertyInformation(Type containerType, PropertyHelper helper)
        {
            var property = helper.Property;

            return(new PropertyInformation
            {
                PropertyHelper = helper,
                Prototype = CreateMetadataPrototype(ModelAttributes.GetAttributesForProperty(property),
                                                    containerType,
                                                    property.PropertyType,
                                                    property.Name),
                IsReadOnly = !property.CanWrite || property.SetMethod.IsPrivate
            });
        }
        public void GetAttributesForTestProperty_ModelOverridesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(BaseViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == "TestProperty");

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            var rangeAttribute = attributes.OfType <RangeAttribute>().FirstOrDefault();

            Assert.NotNull(rangeAttribute);
            Assert.Equal(0, (int)rangeAttribute.Minimum);
            Assert.Equal(10, (int)rangeAttribute.Maximum);
            Assert.Single(attributes.OfType <FromHeaderAttribute>());
        }
        private IEnumerable <IModelValidator> GetValidatorsForProperty(ModelMetadata metadata)
        {
            var propertyName = metadata.PropertyName;
            var bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase;
            var property     = metadata.ContainerType
                               .GetProperty(propertyName, bindingFlags);

            if (property == null)
            {
                throw new ArgumentException(
                          Resources.FormatCommon_PropertyNotFound(
                              metadata.ContainerType.FullName,
                              metadata.PropertyName),
                          "metadata");
            }

            var attributes = ModelAttributes.GetAttributesForProperty(metadata.ContainerType, property);

            return(GetValidators(metadata, attributes));
        }
Example #9
0
        public void GetAttributesForProperty_MergedAttributes()
        {
            // Arrange
            var property = typeof(MergedAttributes).GetRuntimeProperty(nameof(MergedAttributes.Property));

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(typeof(MergedAttributes), property);

            // Assert
            Assert.Equal(3, attributes.Attributes.Count);
            Assert.IsType <RequiredAttribute>(attributes.Attributes[0]);
            Assert.IsType <RangeAttribute>(attributes.Attributes[1]);
            Assert.IsType <ClassValidator>(attributes.Attributes[2]);

            Assert.Equal(2, attributes.PropertyAttributes.Count);
            Assert.IsType <RequiredAttribute>(attributes.PropertyAttributes[0]);
            Assert.IsType <RangeAttribute>(attributes.PropertyAttributes[1]);

            var attribute = Assert.Single(attributes.TypeAttributes);

            Assert.IsType <ClassValidator>(attribute);
        }
        private IEnumerable <IModelValidator> GetValidatorsForType(ModelMetadata metadata)
        {
            var attributes = ModelAttributes.GetAttributesForType(metadata.ModelType);

            return(GetValidators(metadata, attributes));
        }