aspnetcore/test/Microsoft.AspNet.Mvc.ModelB.../Metadata/CachedDataAnnotationsModelM...

93 lines
3.4 KiB
C#

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class CachedDataAnnotationsModelMetadataProviderTest
{
[Fact]
public void DataAnnotationsModelMetadataProvider_ReadsScaffoldColumnAttribute_ForShowForDisplay()
{
// Arrange
var type = typeof(ScaffoldColumnModel);
var provider = new DataAnnotationsModelMetadataProvider();
// Act & Assert
Assert.True(provider.GetMetadataForProperty(null, type, "NoAttribute").ShowForDisplay);
Assert.True(provider.GetMetadataForProperty(null, type, "ScaffoldColumnTrue").ShowForDisplay);
Assert.False(provider.GetMetadataForProperty(null, type, "ScaffoldColumnFalse").ShowForDisplay);
}
[Fact]
public void DataAnnotationsModelMetadataProvider_ReadsScaffoldColumnAttribute_ForShowForEdit()
{
// Arrange
var type = typeof(ScaffoldColumnModel);
var provider = new DataAnnotationsModelMetadataProvider();
// Act & Assert
Assert.True(provider.GetMetadataForProperty(null, type, "NoAttribute").ShowForEdit);
Assert.True(provider.GetMetadataForProperty(null, type, "ScaffoldColumnTrue").ShowForEdit);
Assert.False(provider.GetMetadataForProperty(null, type, "ScaffoldColumnFalse").ShowForEdit);
}
[Fact]
public void HiddenInputWorksOnProperty()
{
// Arrange
var provider = new DataAnnotationsModelMetadataProvider();
var metadata = provider.GetMetadataForType(modelAccessor: null, modelType: typeof(ClassWithHiddenProperties));
var property = metadata.Properties.First(m => string.Equals("DirectlyHidden", m.PropertyName));
// Act
var result = property.HideSurroundingHtml;
// Assert
Assert.True(result);
}
// TODO #1000; enable test once we detect attributes on the property's type
public void HiddenInputWorksOnPropertyType()
{
// Arrange
var provider = new DataAnnotationsModelMetadataProvider();
var metadata = provider.GetMetadataForType(modelAccessor: null, modelType: typeof(ClassWithHiddenProperties));
var property = metadata.Properties.First(m => string.Equals("OfHiddenType", m.PropertyName));
// Act
var result = property.HideSurroundingHtml;
// Assert
Assert.True(result);
}
private class ScaffoldColumnModel
{
public int NoAttribute { get; set; }
[ScaffoldColumn(scaffold: true)]
public int ScaffoldColumnTrue { get; set; }
[ScaffoldColumn(scaffold: false)]
public int ScaffoldColumnFalse { get; set; }
}
[HiddenInput(DisplayValue = false)]
private class HiddenClass
{
public string Property { get; set; }
}
private class ClassWithHiddenProperties
{
[HiddenInput(DisplayValue = false)]
public string DirectlyHidden { get; set; }
public HiddenClass OfHiddenType { get; set; }
}
}
}