forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAnnotationsModelMetadataProviderTest.cs
More file actions
194 lines (153 loc) · 6.45 KB
/
Copy pathDataAnnotationsModelMetadataProviderTest.cs
File metadata and controls
194 lines (153 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.TestCommon;
namespace System.Web.Http.Metadata.Providers
{
public class DataAnnotationsModelMetadataProviderTest
{
[Fact]
public void GetMetadataForPropertiesSetTypesAndPropertyNames()
{
// Arrange
var provider = new DataAnnotationsModelMetadataProvider();
// Act
IEnumerable<ModelMetadata> result = provider.GetMetadataForProperties("foo", typeof(string));
// Assert
Assert.Contains(result, m => m.ModelType == typeof(int)
&& m.PropertyName == "Length"
&& (int)m.Model == 3);
}
[Fact]
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);
}
[Fact]
public void GetMetadataForTypeSetsTypeWithNullPropertyName()
{
// Arrange
var provider = new DataAnnotationsModelMetadataProvider();
// Act
ModelMetadata result = provider.GetMetadataForType(null, typeof(string));
// Assert
Assert.Equal(typeof(string), result.ModelType);
Assert.Null(result.PropertyName);
}
[Theory]
[InlineData("NoAttributes", false)]
[InlineData("ReadOnlyAttribute", true)]
[InlineData("EditableAttribute", true)]
[InlineData("BothAttributes", false)]
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);
}
[Theory]
[InlineData("NoAttribute", "NoAttribute")]
[InlineData("NothingSet", "NothingSet")]
[InlineData("EmptyDisplayName", "")]
[InlineData("DescriptionSet", "DescriptionSet")]
[InlineData("NameSet", "Name text1")]
[InlineData("DisplayNameSet", "Just DisplayName")]
[InlineData("BothSet", "Name text2")]
[InlineData("FallbackToDisplayName", "Fallback")]
[InlineData("FallbackToProperty", "FallbackToProperty")]
[InlineData("FallbackToPropertyFromDisplayName", "FallbackToPropertyFromDisplayName")]
[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);
}
[Fact]
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);
}
[Theory]
[InlineData("NoAttribute", null)]
[InlineData("NothingSet", null)]
[InlineData("NameSet", null)]
[InlineData("DescriptionSet", "Description text1")]
[InlineData("BothSet", "Description text2")]
public void DataAnnotationsDescriptionTests(string propertyName, string expected)
{
// Arrange
var provider = new DataAnnotationsModelMetadataProvider();
// Act
var actual = provider.GetMetadataForProperty(null, typeof(DisplayModel), propertyName).Description;
// Assert
Assert.Equal(expected, actual);
}
// [Display] & [DisplayName] tests
private class DisplayModel
{
public int NoAttribute { get; set; }
// Description + Name combination.
[Display]
public int NothingSet { get; set; }
[Display(Name = "")]
public int EmptyDisplayName { get; set; }
[Display(Description = "Description text1")]
public int DescriptionSet { get; set; }
[Display(Name = "Name text1")]
public int NameSet { get; set; }
[DisplayName("Just DisplayName")]
public int DisplayNameSet { get; set; }
[Display(Description = "Description text2", Name = "Name text2")]
[DisplayName("This won't be used")]
public int BothSet { get; set; }
[Display(Name = "String1", ResourceType = typeof(Resources))]
public int Localized { get; set; }
[Display]
[DisplayName("Fallback")]
public int FallbackToDisplayName { get; set; }
[Display]
public int FallbackToProperty { get; set; }
[DisplayName(null)]
public int FallbackToPropertyFromDisplayName { get; set; }
[DisplayName]
public int DisplayNameDefault { get; set; }
}
// [ReadOnly] & [Editable] tests
private class ReadOnlyModel
{
public int NoAttributes { get; set; }
[ReadOnly(true)]
public int ReadOnlyAttribute { get; set; }
[Editable(false)]
public int EditableAttribute { get; set; }
[ReadOnly(true)]
[Editable(true)]
public int BothAttributes { get; set; }
// Editable trumps ReadOnly
}
}
}