public void GetCustomAttributesThrowsIfAttributeTypeIsNull()
        {
            // Arrange
            ParameterDescriptor pd = GetParameterDescriptor();

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { pd.GetCustomAttributes(null /* attributeType */, true); }, "attributeType");
        }
    static private void EvaluateValidationAttributes(ParameterDescriptor parameterDescriptor, object suppliedValue, ModelStateDictionary modelState)
    {
        var parameterName = parameterDescriptor.ParameterName;

        parameterDescriptor
        .GetCustomAttributes(inherit: true)
        .OfType <ValidationAttribute>()
        .Where(x => !x.IsValid(suppliedValue))
        .ForEach(x => modelState.AddModelError(parameterName, x.FormatErrorMessage(parameterName)));
    }
示例#3
0
        public void GetCustomAttributesThrowsIfAttributeTypeIsNull()
        {
            // Arrange
            ParameterDescriptor pd = GetParameterDescriptor();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                pd.GetCustomAttributes(null /* attributeType */, true);
            }, "attributeType");
        }
示例#4
0
        public void GetCustomAttributesReturnsEmptyArrayOfAttributeType()
        {
            // Arrange
            ParameterDescriptor pd = GetParameterDescriptor();

            // Act
            ObsoleteAttribute[] attrs = (ObsoleteAttribute[])pd.GetCustomAttributes(typeof(ObsoleteAttribute), true);

            // Assert
            Assert.AreEqual(0, attrs.Length);
        }
        protected override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context,
                                                                      IEnumerable <Attribute> attributes)
        {
            object descriptor;

            if (metadata.ContainerType == null && context.RouteData.DataTokens.TryGetValue(typeof(ParameterDescriptor).FullName, out descriptor))
            {
                ParameterDescriptor parameterDescriptor = (ParameterDescriptor)descriptor;
                DisplayAttribute    displayAttribute    = parameterDescriptor.GetCustomAttributes(true).OfType <DisplayAttribute>().FirstOrDefault()
                                                          ?? new DisplayAttribute {
                    Name = parameterDescriptor.ParameterName
                };
                metadata.DisplayName = displayAttribute.Name;
                var addedAttributes = parameterDescriptor.GetCustomAttributes(true).OfType <Attribute>();
                return(base.GetValidators(metadata, context, attributes.Union(addedAttributes)));
            }
            else
            {
                return(base.GetValidators(metadata, context, attributes));
            }
        }
示例#6
0
        public void GetCustomAttributesWithoutAttributeTypeCallsGetCustomAttributesWithAttributeType()
        {
            // Arrange
            object[] expected = new object[0];
            Mock <ParameterDescriptor> mockDescriptor = new Mock <ParameterDescriptor>()
            {
                CallBase = true
            };

            mockDescriptor.Setup(d => d.GetCustomAttributes(typeof(object), true)).Returns(expected);
            ParameterDescriptor pd = mockDescriptor.Object;

            // Act
            object[] returned = pd.GetCustomAttributes(true /* inherit */);

            // Assert
            Assert.AreSame(expected, returned);
        }