示例#1
0
        public void CreateDisplayMetadata_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.CreateDisplayMetadata(context);

            // Assert
            Assert.Equal("description from resources", context.DisplayMetadata.Description());
        }
示例#2
0
        public void CreateDisplayMetadata_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.CreateDisplayMetadata(context);

            // Assert
            Assert.Equal(expectedIsFlagsEnum, context.DisplayMetadata.IsFlagsEnum);
        }
示例#3
0
        public void CreateDisplayMetadata_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.CreateDisplayMetadata(context);

            // Assert
            // This assertion does *not* require entry orders to match.
            Assert.Equal(expectedDictionary, context.DisplayMetadata.EnumNamesAndValues);
        }
示例#4
0
        public void CreateDisplayMetadata_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.CreateDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
示例#5
0
        public void CreateDisplayMetadata_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.CreateDisplayMetadata(context);

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

            Assert.Equal(expected, value);
        }
示例#6
0
        public void CreateDisplayMetadata_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.CreateDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
示例#7
0
        public void CreateDisplayMetadata_EnumGroupedDisplayNamesAndValues_ReflectsModelType(
            Type type,
            IEnumerable <KeyValuePair <EnumGroupAndName, 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.CreateDisplayMetadata(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.Group, StringComparer.Ordinal)
                .ThenBy(item => item.Key.Name, StringComparer.Ordinal),
                context.DisplayMetadata.EnumGroupedDisplayNamesAndValues?.OrderBy(item => item.Key.Group, StringComparer.Ordinal)
                .ThenBy(item => item.Key.Name, StringComparer.Ordinal));
        }