public void DescriptionTests()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act & Assert
            Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").Description);
            Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "DescriptionNotSet").Description);
            Assert.Equal("Description text", provider.GetMetadataForProperty(null, typeof(DisplayModel), "DescriptionSet").Description);
        }
        public void ReadOnlyTests()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act & Assert
            Assert.False(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "NoAttributes").IsReadOnly);
            Assert.True(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "ReadOnlyAttribute").IsReadOnly);
            Assert.True(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "EditableAttribute").IsReadOnly);
            Assert.False(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "BothAttributes").IsReadOnly);
        }
        [InlineData("DisplayNameDefault", "")] // The default for DisplayName is the empty string, we don't have special handling for it, and nither does MVC.
        public void DataAnnotationsNameTests(string propertyName, string expected)
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var actual = provider.GetMetadataForProperty(null, typeof(DisplayModel), propertyName).GetDisplayName();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void ReadOnlyTests(string propertyName, bool expected)
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var actual = provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), propertyName).IsReadOnly;

            // Assert
            Assert.Equal(expected, actual);
        }
        public void GetMetadataForPropertySetsTypeAndPropertyName()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            ModelMetadata result = provider.GetMetadataForProperty(null, typeof(string), "Length");

            // Assert
            Assert.Equal(typeof(int), result.ModelType);
            Assert.Equal("Length", result.PropertyName);
        }
        public void DisplayAttribute_WithLocalizedName()
        {
            // Guard
            var expected = Resources.String1;

            Assert.NotEqual("String1", expected);

            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var actual = provider.GetMetadataForProperty(null, typeof(DisplayModel), "Localized").GetDisplayName();

            // Assert
            Assert.Equal(expected, actual);
        }
示例#7
0
        public void DataAnnotationsNameTests()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act & Assert
            Assert.Equal("NoAttribute", provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").GetDisplayName());
            Assert.Equal("NothingSet", provider.GetMetadataForProperty(null, typeof(DisplayModel), "NothingSet").GetDisplayName());
            Assert.Equal("", provider.GetMetadataForProperty(null, typeof(DisplayModel), "EmptyDisplayName").GetDisplayName());
            Assert.Equal("DescriptionSet", provider.GetMetadataForProperty(null, typeof(DisplayModel), "DescriptionSet").GetDisplayName());
            Assert.Equal("Name text1", provider.GetMetadataForProperty(null, typeof(DisplayModel), "NameSet").GetDisplayName());
            Assert.Equal("Name text2", provider.GetMetadataForProperty(null, typeof(DisplayModel), "BothSet").GetDisplayName());

            Assert.NotEqual("String1", Resources.String1);
            Assert.Equal(Resources.String1, provider.GetMetadataForProperty(null, typeof(DisplayModel), "Localized").GetDisplayName());
        }