Example #1
0
        public void GetDisplayDetails_DisplayAttribute_NameFromResources()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var display = new DisplayAttribute()
            {
#if USE_REAL_RESOURCES
                Name         = nameof(Test.Resources.DisplayAttribute_Name),
                ResourceType = typeof(Test.Resources),
#else
                Name         = nameof(Test.Resources.DisplayAttribute_Name),
                ResourceType = typeof(Test.TestResources),
#endif
            };

            var attributes = new Attribute[] { display };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new DisplayMetadataProviderContext(key, attributes);

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal("name from resources", context.DisplayMetadata.DisplayName);
        }
Example #2
0
        public void GetDisplayMetadata_DisplayAttribute_DescriptionFromResources()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var display = new DisplayAttribute()
            {
#if USE_REAL_RESOURCES
                Description  = nameof(Test.Resources.DisplayAttribute_Description),
                ResourceType = typeof(Test.Resources),
#else
                Description  = nameof(DataAnnotations.Test.Resources.DisplayAttribute_Description),
                ResourceType = typeof(TestResources),
#endif
            };

            var attributes = new Attribute[] { display };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal("description from resources", context.DisplayMetadata.Description());
        }
Example #3
0
        public void GetDisplayMetadata_IsFlagsEnum_ReflectsModelType(Type type, bool expectedIsFlagsEnum)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key        = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context    = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal(expectedIsFlagsEnum, context.DisplayMetadata.IsFlagsEnum);
        }
Example #4
0
        public void GetValidationMetadata_RequiredAttribute_SetsIsRequiredToTrue()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var required = new RequiredAttribute();

            var attributes = new Attribute[] { required };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            // Act
            provider.GetValidationMetadata(context);

            // Assert
            Assert.True(context.ValidationMetadata.IsRequired);
        }
Example #5
0
        public void GetBindingDetails_NoEditableAttribute_IsReadOnlyLeftAlone(bool?initialValue)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var attributes = new Attribute[] { };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new BindingMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            context.BindingMetadata.IsReadOnly = initialValue;

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.Equal(initialValue, context.BindingMetadata.IsReadOnly);
        }
Example #6
0
        public void GetBindingMetadata_EditableAttributeTrue_SetsReadOnlyFalse()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var editable = new EditableAttribute(allowEdit: true);

            var attributes = new Attribute[] { editable };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new BindingMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.False(context.BindingMetadata.IsReadOnly);
        }
Example #7
0
        public void GetDisplayMetadata_EnumDisplayNamesAndValues_ReflectsModelType(
            Type type,
            IEnumerable <KeyValuePair <string, string> > expectedKeyValuePairs)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key        = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context    = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal(expectedKeyValuePairs, context.DisplayMetadata.EnumDisplayNamesAndValues);
        }
Example #8
0
        public void GetBindingDetails_RequiredAttribute_SetsIsRequiredToTrue()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var required = new RequiredAttribute();

            var attributes = new Attribute[] { required };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new BindingMetadataProviderContext(key, attributes);

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.True(context.BindingMetadata.IsRequired);
        }
Example #9
0
        public void GetBindingMetadata_RequiredAttribute_IsBindingRequiredLeftAlone(bool initialValue)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var attributes = new Attribute[] { new RequiredAttribute() };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new BindingMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            context.BindingMetadata.IsBindingRequired = initialValue;

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.Equal(initialValue, context.BindingMetadata.IsBindingRequired);
        }
Example #10
0
        public void GetDisplayDetails_RequiredAttribute_SetsRequired()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var required = new RequiredAttribute();

            var attributes = new Attribute[] { required };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new BindingMetadataProviderContext(key, attributes);

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.Equal(true, context.BindingMetadata.IsRequired);
        }
Example #11
0
        public void GetDisplayMetadata_EnumNamesAndValues_ReflectsModelType(
            Type type,
            IReadOnlyDictionary <string, string> expectedDictionary)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key        = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context    = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            // This assertion does *not* require entry orders to match.
            Assert.Equal(expectedDictionary, context.DisplayMetadata.EnumNamesAndValues);
        }
Example #12
0
        public void GetDisplayMetadata_FindsDisplayFormat_FromDataType()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var dataType      = new DataTypeAttribute(DataType.Currency);
            var displayFormat = dataType.DisplayFormat; // Non-null for DataType.Currency.

            var attributes = new[] { dataType, };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
Example #13
0
        public void GetValidationDetails_ValidatableObject_ReturnsObject()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var attribute  = new TestValidationAttribute();
            var attributes = new Attribute[] { attribute };
            var key        = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context    = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            // Act
            provider.GetValidationMetadata(context);

            // Assert
            var validatorMetadata = Assert.Single(context.ValidationMetadata.ValidatorMetadata);

            Assert.Same(attribute, validatorMetadata);
        }
        public void GetDisplayMetadata_SimpleAttributes(
            object attribute,
            Func<DisplayMetadata, object> accessor,
            object expected)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(new object[] { attribute }));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            var value = accessor(context.DisplayMetadata);
            Assert.Equal(expected, value);
        }
        public void GetDisplayMetadata_FindsDisplayFormat_FromDataType()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var dataType = new DataTypeAttribute(DataType.Currency);
            var displayFormat = dataType.DisplayFormat; // Non-null for DataType.Currency.

            var attributes = new[] { dataType, };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
Example #16
0
        public void GetDisplayMetadata_SimpleAttributes(
            object attribute,
            Func <DisplayMetadata, object> accessor,
            object expected)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key     = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(new object[] { attribute }));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            var value = accessor(context.DisplayMetadata);

            Assert.Equal(expected, value);
        }
Example #17
0
        public void GetDisplayMetadata_EnumDisplayNamesAndValues_ReflectsModelType(
            Type type,
            IEnumerable <KeyValuePair <string, string> > expectedKeyValuePairs)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key        = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context    = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            // OrderBy is used because the order of the results may very depending on the platform / client.
            Assert.Equal(
                expectedKeyValuePairs?.OrderBy(item => item.Key, StringComparer.Ordinal),
                context.DisplayMetadata.EnumDisplayNamesAndValues?.OrderBy(item => item.Key, StringComparer.Ordinal));
        }
Example #18
0
        public void GetDisplayMetadata_FindsDisplayFormat_OverridingDataType()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var dataType      = new DataTypeAttribute(DataType.Time); // Has a non-null DisplayFormat.
            var displayFormat = new DisplayFormatAttribute()          // But these values override the values from DataType
            {
                DataFormatString = "Cool {0}",
            };

            var attributes = new Attribute[] { dataType, displayFormat, };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
        public void GetBindingMetadata_RequiredAttribute_IsBindingRequiredLeftAlone(bool initialValue)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var attributes = new Attribute[] { new RequiredAttribute() };
            var key = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context = new BindingMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));
            context.BindingMetadata.IsBindingRequired = initialValue;

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.Equal(initialValue, context.BindingMetadata.IsBindingRequired);
        }
        public void GetValidationMetadata_RequiredAttribute_SetsIsRequiredToTrue()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var required = new RequiredAttribute();

            var attributes = new Attribute[] { required };
            var key = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            // Act
            provider.GetValidationMetadata(context);

            // Assert
            Assert.True(context.ValidationMetadata.IsRequired);
        }
        public void GetDisplayMetadata_EnumDisplayNamesAndValues_ReflectsModelType(
            Type type,
            IEnumerable<KeyValuePair<string, string>> expectedKeyValuePairs)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            // OrderBy is used because the order of the results may very depending on the platform / client.
            Assert.Equal(
                expectedKeyValuePairs?.OrderBy(item => item.Key, StringComparer.Ordinal),
                context.DisplayMetadata.EnumDisplayNamesAndValues?.OrderBy(item => item.Key, StringComparer.Ordinal));
        }
        public void GetDisplayMetadata_EnumNamesAndValues_ReflectsModelType(
            Type type,
            IReadOnlyDictionary<string, string> expectedDictionary)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            // This assertion does *not* require entry orders to match.
            Assert.Equal(expectedDictionary, context.DisplayMetadata.EnumNamesAndValues);
        }
        public void GetDisplayMetadata_IsFlagsEnum_ReflectsModelType(Type type, bool expectedIsFlagsEnum)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal(expectedIsFlagsEnum, context.DisplayMetadata.IsFlagsEnum);
        }
        public void GetBindingMetadata_EditableAttributeTrue_SetsReadOnlyFalse()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var editable = new EditableAttribute(allowEdit: true);

            var attributes = new Attribute[] { editable };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new BindingMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.False(context.BindingMetadata.IsReadOnly);
        }
        public void GetBindingDetails_RequiredAttribute_SetsIsRequiredToTrue()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var required = new RequiredAttribute();

            var attributes = new Attribute[] { required };
            var key = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context = new BindingMetadataProviderContext(key, attributes);

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.True(context.BindingMetadata.IsRequired);
        }
        public void GetDisplayDetails_EnumDisplayNamesAndValues_ReflectsModelType(
            Type type,
            IEnumerable<KeyValuePair<string, string>> expectedKeyValuePairs)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var key = ModelMetadataIdentity.ForType(type);
            var attributes = new object[0];
            var context = new DisplayMetadataProviderContext(key, attributes);

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal(expectedKeyValuePairs, context.DisplayMetadata.EnumDisplayNamesAndValues);
        }
        public void GetDisplayDetails_DisplayAttribute_NameFromResources()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var display = new DisplayAttribute()
            {
#if USE_REAL_RESOURCES
                Name = nameof(Test.Resources.DisplayAttribute_Name),
                ResourceType = typeof(Test.Resources),
#else
                Name = nameof(Test.Resources.DisplayAttribute_Name),
                ResourceType = typeof(Test.TestResources),
#endif
            };

            var attributes = new Attribute[] { display };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, attributes);

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal("name from resources", context.DisplayMetadata.DisplayName);
        }
        public void GetBindingDetails_NoEditableAttribute_IsReadOnlyLeftAlone(bool? initialValue)
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var attributes = new Attribute[] { };
            var key = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context = new BindingMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));
            context.BindingMetadata.IsReadOnly = initialValue;

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.Equal(initialValue, context.BindingMetadata.IsReadOnly);
        }
        public void GetValidationDetails_ValidatableObject_AlreadyInContext_Ignores()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var attribute = new TestValidationAttribute();
            var attributes = new Attribute[] { attribute };
            var key = ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string));
            var context = new ValidationMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));
            context.ValidationMetadata.ValidatorMetadata.Add(attribute);

            // Act
            provider.GetValidationMetadata(context);

            // Assert
            var validatorMetadata = Assert.Single(context.ValidationMetadata.ValidatorMetadata);
            Assert.Same(attribute, validatorMetadata);
        }
        public void GetDisplayMetadata_DisplayAttribute_DescriptionFromResources()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var display = new DisplayAttribute()
            {
#if USE_REAL_RESOURCES
                Description = nameof(Test.Resources.DisplayAttribute_Description),
                ResourceType = typeof(Test.Resources),
#else
                Description = nameof(DataAnnotations.Test.Resources.DisplayAttribute_Description),
                ResourceType = typeof(TestResources),
#endif
            };

            var attributes = new Attribute[] { display };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal("description from resources", context.DisplayMetadata.Description());
        }
        public void GetDisplayMetadata_FindsDisplayFormat_OverridingDataType()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var dataType = new DataTypeAttribute(DataType.Time); // Has a non-null DisplayFormat.
            var displayFormat = new DisplayFormatAttribute() // But these values override the values from DataType
            {
                DataFormatString = "Cool {0}",
            };

            var attributes = new Attribute[] { dataType, displayFormat, };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
        public void GetDisplayDetails_RequiredAttribute_SetsRequired()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var required = new RequiredAttribute();

            var attributes = new Attribute[] { required };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new BindingMetadataProviderContext(key, attributes);

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.Equal(true, context.BindingMetadata.IsRequired);
        }