예제 #1
0
        public void GetAttributesForParameter_NoAttributes()
        {
            // Arrange & Act
            var attributes = ModelAttributes.GetAttributesForParameter(
                typeof(MethodWithParamAttributesType)
                .GetMethod(nameof(MethodWithParamAttributesType.Method))
                .GetParameters()[0]);

            // Assert
            Assert.Empty(attributes.Attributes);
            Assert.Empty(attributes.ParameterAttributes);
            Assert.Null(attributes.TypeAttributes);
            Assert.Null(attributes.PropertyAttributes);
        }
예제 #2
0
        public void GetAttributesForParameter_NoAttributes()
        {
            // Arrange & Act
            var attributes = ModelAttributes.GetAttributesForParameter(
                typeof(MethodWithParamAttributesType)
                .GetMethod(nameof(MethodWithParamAttributesType.Method))
                .GetParameters()[0]);

            // Assert
            // Not exactly "no attributes" due to SerializableAttribute on object.
            Assert.IsType <SerializableAttribute>(Assert.Single(attributes.Attributes));
            Assert.Empty(attributes.ParameterAttributes);
            Assert.Null(attributes.PropertyAttributes);
            Assert.Equal(attributes.Attributes, attributes.TypeAttributes);
        }
예제 #3
0
        public void GetAttributesForBasePropertyFromDerivedModel_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(DerivedViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == nameof(BaseModel.BaseProperty));

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

            // Assert
            Assert.Single(attributes.Attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.Attributes.OfType <StringLengthAttribute>());

            Assert.Single(attributes.PropertyAttributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.PropertyAttributes.OfType <StringLengthAttribute>());
        }
예제 #4
0
        public void GetFromServiceAttributeFromBase_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(DerivedViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == nameof(BaseModel.RouteValue));

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

            // Assert
            Assert.Single(attributes.Attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.Attributes.OfType <FromRouteAttribute>());

            Assert.Single(attributes.PropertyAttributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.PropertyAttributes.OfType <FromRouteAttribute>());
        }
예제 #5
0
        public void GetAttributesForParameter_SomeAttributes()
        {
            // Arrange & Act
            var attributes = ModelAttributes.GetAttributesForParameter(
                typeof(MethodWithParamAttributesType)
                .GetMethod(nameof(MethodWithParamAttributesType.Method))
                .GetParameters()[1]);

            // Assert
            Assert.IsType <RequiredAttribute>(attributes.Attributes[0]);
            Assert.IsType <RangeAttribute>(attributes.Attributes[1]);
            Assert.IsType <RequiredAttribute>(attributes.ParameterAttributes[0]);
            Assert.IsType <RangeAttribute>(attributes.ParameterAttributes[1]);
            Assert.Null(attributes.TypeAttributes);
            Assert.Null(attributes.PropertyAttributes);
        }
예제 #6
0
        public void GetAttributesForParameter_IncludesTypeAttributes()
        {
            // Arrange
            var parameters = typeof(MethodWithParamAttributesType)
                             .GetMethod(nameof(MethodWithParamAttributesType.Method))
                             .GetParameters();

            // Act
            var attributes = ModelAttributes.GetAttributesForParameter(parameters[2]);

            // Assert
            Assert.Collection(attributes.Attributes,
                              attribute => Assert.IsType <BindRequiredAttribute>(attribute),
                              attribute => Assert.IsType <ClassValidator>(attribute));
            Assert.IsType <BindRequiredAttribute>(Assert.Single(attributes.ParameterAttributes));
            Assert.Null(attributes.PropertyAttributes);
            Assert.IsType <ClassValidator>(Assert.Single(attributes.TypeAttributes));
        }
예제 #7
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);
        }
예제 #8
0
        public void GetAttributesForProperty_WithModelType_IncludesTypeAttributes()
        {
            // Arrange
            var property = typeof(MergedAttributes)
                           .GetProperty(nameof(MergedAttributes.BaseModel));

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

            // Assert
            Assert.Collection(
                attributes.Attributes,
                attribute => Assert.IsType <BindRequiredAttribute>(attribute),
                attribute => Assert.IsType <ModelBinderAttribute>(attribute),
                attribute => Assert.IsType <ClassValidator>(attribute));
            Assert.IsType <BindRequiredAttribute>(Assert.Single(attributes.PropertyAttributes));
            Assert.Null(attributes.ParameterAttributes);
            Assert.Collection(
                attributes.TypeAttributes,
                attribute => Assert.IsType <ModelBinderAttribute>(attribute),
                attribute => Assert.IsType <ClassValidator>(attribute));
        }
예제 #9
0
        public void GetBindingInfo_WithAttributesAndModelMetadata_UsesPropertyPredicateProviderFromModelMetadata_WhenNotFoundViaAttributes()
        {
            // Arrange
            var attributes             = new object[] { new ModelBinderAttribute(typeof(object)), new ControllerAttribute(), new BindNeverAttribute(), };
            var modelAttributes        = new ModelAttributes(Enumerable.Empty <object>(), null, null);
            var propertyFilterProvider = Mock.Of <IPropertyFilterProvider>();
            var modelType = typeof(Guid);
            var provider  = new TestModelMetadataProvider();

            provider.ForType(modelType).BindingDetails(metadata =>
            {
                metadata.PropertyFilterProvider = propertyFilterProvider;
            });
            var modelMetadata = provider.GetMetadataForType(modelType);

            // Act
            var bindingInfo = BindingInfo.GetBindingInfo(attributes, modelMetadata);

            // Assert
            Assert.NotNull(bindingInfo);
            Assert.Same(propertyFilterProvider, bindingInfo.PropertyFilterProvider);
        }
예제 #10
0
        public void GetAttributesForParameter_SomeAttributes()
        {
            // Arrange & Act
            var attributes = ModelAttributes.GetAttributesForParameter(
                typeof(MethodWithParamAttributesType)
                .GetMethod(nameof(MethodWithParamAttributesType.Method))
                .GetParameters()[1]);

            // Assert
            Assert.Collection(
                // Take(2) to ignore ComVisibleAttribute, SerializableAttribute, ... on int.
                attributes.Attributes.Take(2),
                attribute => Assert.IsType <RequiredAttribute>(attribute),
                attribute => Assert.IsType <RangeAttribute>(attribute));
            Assert.Collection(
                attributes.ParameterAttributes,
                attribute => Assert.IsType <RequiredAttribute>(attribute),
                attribute => Assert.IsType <RangeAttribute>(attribute));
            Assert.Null(attributes.PropertyAttributes);
            Assert.Collection(
                // Take(1) because the attribute or attributes after SerializableAttribute are framework-specific.
                attributes.TypeAttributes.Take(1),
                attribute => Assert.IsType <SerializableAttribute>(attribute));
        }