forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAnnotationsModelMetadataProviderTestBase.cs
More file actions
718 lines (563 loc) · 27.6 KB
/
Copy pathDataAnnotationsModelMetadataProviderTestBase.cs
File metadata and controls
718 lines (563 loc) · 27.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// 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.Mvc.Test
{
public abstract class DataAnnotationsModelMetadataProviderTestBase
{
protected abstract AssociatedMetadataProvider MakeProvider();
[Fact]
public void GetMetadataForPropertiesSetTypesAndPropertyNames()
{
// Arrange
var provider = MakeProvider();
// 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 = MakeProvider();
// 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 = MakeProvider();
// Act
ModelMetadata result = provider.GetMetadataForType(null, typeof(string));
// Assert
Assert.Equal(typeof(string), result.ModelType);
Assert.Null(result.PropertyName);
}
// [HiddenInput] tests
class HiddenModel
{
public int NoAttribute { get; set; }
[HiddenInput]
public int DefaultHidden { get; set; }
[HiddenInput(DisplayValue = false)]
public int HiddenWithDisplayValueFalse { get; set; }
[HiddenInput]
[UIHint("CustomUIHint")]
public int HiddenAndUIHint { get; set; }
}
[Fact]
public void HiddenAttributeSetsTemplateHintAndHideSurroundingHtml()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
ModelMetadata noAttributeMetadata = provider.GetMetadataForProperty(null, typeof(HiddenModel), "NoAttribute");
Assert.Null(noAttributeMetadata.TemplateHint);
Assert.False(noAttributeMetadata.HideSurroundingHtml);
ModelMetadata defaultHiddenMetadata = provider.GetMetadataForProperty(null, typeof(HiddenModel), "DefaultHidden");
Assert.Equal("HiddenInput", defaultHiddenMetadata.TemplateHint);
Assert.False(defaultHiddenMetadata.HideSurroundingHtml);
ModelMetadata hiddenWithDisplayValueFalseMetadata = provider.GetMetadataForProperty(null, typeof(HiddenModel), "HiddenWithDisplayValueFalse");
Assert.Equal("HiddenInput", hiddenWithDisplayValueFalseMetadata.TemplateHint);
Assert.True(hiddenWithDisplayValueFalseMetadata.HideSurroundingHtml);
// [UIHint] overrides the template hint from [Hidden]
Assert.Equal("CustomUIHint", provider.GetMetadataForProperty(null, typeof(HiddenModel), "HiddenAndUIHint").TemplateHint);
}
// [UIHint] tests
class UIHintModel
{
public int NoAttribute { get; set; }
[UIHint("MyCustomTemplate")]
public int DefaultUIHint { get; set; }
[UIHint("MyMvcTemplate", "MVC")]
public int MvcUIHint { get; set; }
[UIHint("MyWebFormsTemplate", "WebForms")]
public int NoMvcUIHint { get; set; }
[UIHint("MyDefaultTemplate")]
[UIHint("MyWebFormsTemplate", "WebForms")]
[UIHint("MyMvcTemplate", "MVC")]
public int MultipleUIHint { get; set; }
}
[Fact]
public void UIHintAttributeSetsTemplateHint()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(UIHintModel), "NoAttribute").TemplateHint);
Assert.Equal("MyCustomTemplate", provider.GetMetadataForProperty(null, typeof(UIHintModel), "DefaultUIHint").TemplateHint);
Assert.Equal("MyMvcTemplate", provider.GetMetadataForProperty(null, typeof(UIHintModel), "MvcUIHint").TemplateHint);
Assert.Null(provider.GetMetadataForProperty(null, typeof(UIHintModel), "NoMvcUIHint").TemplateHint);
Assert.Equal("MyMvcTemplate", provider.GetMetadataForProperty(null, typeof(UIHintModel), "MultipleUIHint").TemplateHint);
}
// [DataType] tests
const string DerivedDataTypeAttributeFormatString = "Time is {0:HH:mm}";
class DerivedDataTypeAttribute : DataTypeAttribute
{
public DerivedDataTypeAttribute()
: base(DataType.Time)
{
DisplayFormat = new DisplayFormatAttribute
{
ApplyFormatInEditMode = true,
DataFormatString = DerivedDataTypeAttributeFormatString,
};
}
}
class DataTypeModel
{
public int NoAttribute { get; set; }
[DataType(DataType.EmailAddress)]
public int EmailAddressProperty { get; set; }
[DataType("CustomDataType")]
public int CustomDataTypeProperty { get; set; }
[DataType(DataType.Date)]
public DateTime DateProperty { get; set; }
[DerivedDataType]
public DateTime TimeProperty { get; set; }
}
[Fact]
public void DataTypeAttributeSetsDataTypeName()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DataTypeModel), "NoAttribute").DataTypeName);
Assert.Equal("EmailAddress", provider.GetMetadataForProperty(null, typeof(DataTypeModel), "EmailAddressProperty").DataTypeName);
Assert.Equal("CustomDataType", provider.GetMetadataForProperty(null, typeof(DataTypeModel), "CustomDataTypeProperty").DataTypeName);
Assert.Equal("Date", provider.GetMetadataForProperty(null, typeof(DataTypeModel), "DateProperty").DataTypeName);
Assert.Equal("Time", provider.GetMetadataForProperty(null, typeof(DataTypeModel), "TimeProperty").DataTypeName);
}
[Theory]
[InlineData("NoAttribute", null)]
[InlineData("EmailAddressProperty", null)]
[InlineData("CustomDataTypeProperty", null)]
[InlineData("DateProperty", "{0:d}")] // DataType.Date default format
[InlineData("TimeProperty", DerivedDataTypeAttributeFormatString)]
public void DataTypeAttributeSetsDisplayFormats(string propertyName, string formatString)
{
// Arrange
var provider = MakeProvider();
// Act
string displayFormat = provider.GetMetadataForProperty(null, typeof(DataTypeModel), propertyName).DisplayFormatString;
string editFormat = provider.GetMetadataForProperty(null, typeof(DataTypeModel), propertyName).EditFormatString;
// Assert
Assert.Equal(formatString, displayFormat);
Assert.Equal(formatString, editFormat);
}
[Theory]
[InlineData("NoAttribute", false)]
[InlineData("EmailAddressProperty", false)]
[InlineData("CustomDataTypeProperty", false)]
[InlineData("DateProperty", false)] // Uses DataType.Date default format
[InlineData("TimeProperty", true)]
public void DataTypeAttributeSetsHasNonDefaultEditFormat(string propertyName, bool expectedNonDefaultEditFormat)
{
// Arrange
var provider = MakeProvider();
// Act
bool hasNonDefaultEditFormat = provider.GetMetadataForProperty(null, typeof(DataTypeModel), propertyName).HasNonDefaultEditFormat;
// Assert
Assert.Equal(expectedNonDefaultEditFormat, hasNonDefaultEditFormat);
}
// [ReadOnly] & [Editable] tests
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
}
[Fact]
public void ReadOnlyTests()
{
// Arrange
var provider = MakeProvider();
// 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);
}
// [DisplayFormat] tests
class DisplayFormatModel
{
public int NoAttribute { get; set; }
[DisplayFormat(NullDisplayText = "(null value)")]
public int NullDisplayText { get; set; }
[DisplayFormat(DataFormatString = "Data {0} format")]
public int DisplayFormatString { get; set; }
[DisplayFormat(DataFormatString = "Data {0} format", ApplyFormatInEditMode = true)]
public int DisplayAndEditFormatString { get; set; }
[DisplayFormat(ConvertEmptyStringToNull = true)]
public int ConvertEmptyStringToNullTrue { get; set; }
[DisplayFormat(ConvertEmptyStringToNull = false)]
public int ConvertEmptyStringToNullFalse { get; set; }
[DataType(DataType.Currency)]
public int DataTypeWithoutDisplayFormatOverride { get; set; }
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "format override", ApplyFormatInEditMode = true)]
public int DataTypeWithDisplayFormatOverride { get; set; }
[DisplayFormat(HtmlEncode = true)]
public int HtmlEncodeTrue { get; set; }
[DisplayFormat(HtmlEncode = false)]
public int HtmlEncodeFalse { get; set; }
[DataType(DataType.Currency)]
[DisplayFormat(HtmlEncode = false)]
public int HtmlEncodeFalseWithDataType { get; set; }
// DataType trumps DisplayFormat.HtmlEncode
}
[Fact]
public void DisplayFormatAttributetSetsNullDisplayText()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "NoAttribute").NullDisplayText);
Assert.Equal("(null value)", provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "NullDisplayText").NullDisplayText);
}
[Fact]
public void DisplayFormatAttributeSetsDisplayFormatString()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "NoAttribute").DisplayFormatString);
Assert.Equal("Data {0} format", provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "DisplayFormatString").DisplayFormatString);
Assert.Equal("Data {0} format", provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "DisplayAndEditFormatString").DisplayFormatString);
}
[Fact]
public void DisplayFormatAttributeSetEditFormatString()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "NoAttribute").EditFormatString);
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "DisplayFormatString").EditFormatString);
Assert.Equal("Data {0} format", provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "DisplayAndEditFormatString").EditFormatString);
}
[Fact]
public void DisplayFormatAttributeSetsConvertEmptyStringToNull()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.True(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "NoAttribute").ConvertEmptyStringToNull);
Assert.True(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "ConvertEmptyStringToNullTrue").ConvertEmptyStringToNull);
Assert.False(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "ConvertEmptyStringToNullFalse").ConvertEmptyStringToNull);
}
[Fact]
public void DataTypeWithoutDisplayFormatOverrideUsesDataTypesDisplayFormat()
{
// Arrange
var provider = MakeProvider();
// Act
string result = provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "DataTypeWithoutDisplayFormatOverride").DisplayFormatString;
// Assert
Assert.Equal("{0:C}", result); // Currency's default format string
}
[Fact]
public void DataTypeWithDisplayFormatOverrideUsesDisplayFormatOverride()
{
// Arrange
var provider = MakeProvider();
// Act
string result = provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "DataTypeWithDisplayFormatOverride").DisplayFormatString;
// Assert
Assert.Equal("format override", result);
}
[Fact]
public void DataTypeInfluencedByDisplayFormatAttributeHtmlEncode()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "NoAttribute").DataTypeName);
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "HtmlEncodeTrue").DataTypeName);
Assert.Equal("Html", provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "HtmlEncodeFalse").DataTypeName);
Assert.Equal("Currency", provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), "HtmlEncodeFalseWithDataType").DataTypeName);
}
[Theory]
[InlineData("NoAttribute", false)]
[InlineData("NullDisplayText", false)]
[InlineData("DisplayFormatString", false)]
[InlineData("DisplayAndEditFormatString", true)]
[InlineData("ConvertEmptyStringToNullTrue", false)]
[InlineData("ConvertEmptyStringToNullFalse", false)]
[InlineData("DataTypeWithoutDisplayFormatOverride", false)]
[InlineData("DataTypeWithDisplayFormatOverride", true)]
[InlineData("HtmlEncodeTrue", false)]
[InlineData("HtmlEncodeFalse", false)]
[InlineData("HtmlEncodeFalseWithDataType", false)]
public void DisplayFormatSetsHasNonDefaultEditFormat(string propertyName, bool expectedHasNonDefaultEditFormat)
{
// Arrange
AssociatedMetadataProvider provider = MakeProvider();
// Act
bool hasNonDefaultEditFormat = provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), propertyName).HasNonDefaultEditFormat;
// Assert
Assert.Equal(expectedHasNonDefaultEditFormat, hasNonDefaultEditFormat);
}
[Theory]
[InlineData("NoAttribute", true)]
[InlineData("HtmlEncodeTrue", true)]
[InlineData("HtmlEncodeFalse", false)]
[InlineData("HtmlEncodeFalseWithDataType", false)]
public void DisplayFormatSetsHtmlEncode(string propertyName, bool expectedHtmlEncode)
{
// Arrange
AssociatedMetadataProvider provider = MakeProvider();
// Act
bool htmlEncode = provider.GetMetadataForProperty(null, typeof(DisplayFormatModel), propertyName).HtmlEncode;
// Assert
Assert.Equal(expectedHtmlEncode, htmlEncode);
}
// [ScaffoldColumn] tests
class ScaffoldColumnModel
{
public int NoAttribute { get; set; }
[ScaffoldColumn(true)]
public int ScaffoldColumnTrue { get; set; }
[ScaffoldColumn(false)]
public int ScaffoldColumnFalse { get; set; }
}
[Fact]
public void ScaffoldColumnAttributeSetsShowForDisplay()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.True(provider.GetMetadataForProperty(null, typeof(ScaffoldColumnModel), "NoAttribute").ShowForDisplay);
Assert.True(provider.GetMetadataForProperty(null, typeof(ScaffoldColumnModel), "ScaffoldColumnTrue").ShowForDisplay);
Assert.False(provider.GetMetadataForProperty(null, typeof(ScaffoldColumnModel), "ScaffoldColumnFalse").ShowForDisplay);
}
[Fact]
public void ScaffoldColumnAttributeSetsShowForEdit()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.True(provider.GetMetadataForProperty(null, typeof(ScaffoldColumnModel), "NoAttribute").ShowForEdit);
Assert.True(provider.GetMetadataForProperty(null, typeof(ScaffoldColumnModel), "ScaffoldColumnTrue").ShowForEdit);
Assert.False(provider.GetMetadataForProperty(null, typeof(ScaffoldColumnModel), "ScaffoldColumnFalse").ShowForEdit);
}
// [DisplayColumn] tests
[DisplayColumn("NoPropertyWithThisName")]
class UnknownDisplayColumnModel
{
}
[Fact]
public void SimpleDisplayNameWithUnknownDisplayColumnThrows()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Throws<InvalidOperationException>(
() => provider.GetMetadataForType(() => new UnknownDisplayColumnModel(), typeof(UnknownDisplayColumnModel)).SimpleDisplayText,
typeof(UnknownDisplayColumnModel).FullName + " has a DisplayColumn attribute for NoPropertyWithThisName, but property NoPropertyWithThisName does not exist.");
}
[DisplayColumn("WriteOnlyProperty")]
class WriteOnlyDisplayColumnModel
{
public int WriteOnlyProperty
{
set { }
}
}
[DisplayColumn("PrivateReadPublicWriteProperty")]
class PrivateReadPublicWriteDisplayColumnModel
{
public int PrivateReadPublicWriteProperty { private get; set; }
}
[Fact]
public void SimpleDisplayTextForTypeWithWriteOnlyDisplayColumnThrows()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Throws<InvalidOperationException>(
() => provider.GetMetadataForType(() => new WriteOnlyDisplayColumnModel(), typeof(WriteOnlyDisplayColumnModel)).SimpleDisplayText,
typeof(WriteOnlyDisplayColumnModel).FullName + " has a DisplayColumn attribute for WriteOnlyProperty, but property WriteOnlyProperty does not have a public getter.");
Assert.Throws<InvalidOperationException>(
() => provider.GetMetadataForType(() => new PrivateReadPublicWriteDisplayColumnModel(), typeof(PrivateReadPublicWriteDisplayColumnModel)).SimpleDisplayText,
typeof(PrivateReadPublicWriteDisplayColumnModel).FullName + " has a DisplayColumn attribute for PrivateReadPublicWriteProperty, but property PrivateReadPublicWriteProperty does not have a public getter.");
}
[DisplayColumn("DisplayColumnProperty")]
class SimpleDisplayTextAttributeModel
{
public int FirstProperty
{
get { return 42; }
}
[ScaffoldColumn(false)]
public string DisplayColumnProperty { get; set; }
}
class SimpleDisplayTextAttributeModelContainer
{
[DisplayFormat(NullDisplayText = "This is the null display text")]
public SimpleDisplayTextAttributeModel Inner { get; set; }
}
[Fact]
public void SimpleDisplayTextForNonNullClassWithNonNullDisplayColumnValue()
{
// Arrange
string expected = "Custom property display value";
var provider = MakeProvider();
var model = new SimpleDisplayTextAttributeModel { DisplayColumnProperty = expected };
var metadata = provider.GetMetadataForType(() => model, typeof(SimpleDisplayTextAttributeModel));
// Act
string result = metadata.SimpleDisplayText;
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void SimpleDisplayTextForNullClassRevertsToDefaultBehavior()
{
// Arrange
var provider = MakeProvider();
var metadata = provider.GetMetadataForProperty(null, typeof(SimpleDisplayTextAttributeModelContainer), "Inner");
// Act
string result = metadata.SimpleDisplayText;
// Assert
Assert.Equal("This is the null display text", result);
}
[Fact]
public void SimpleDisplayTextForNonNullClassWithNullDisplayColumnValueRevertsToDefaultBehavior()
{
// Arrange
var provider = MakeProvider();
var model = new SimpleDisplayTextAttributeModel();
var metadata = provider.GetMetadataForType(() => model, typeof(SimpleDisplayTextAttributeModel));
// Act
string result = metadata.SimpleDisplayText;
// Assert
Assert.Equal("42", result); // Falls back to the default logic of first property value
}
// [Required] tests
class IsRequiredModel
{
public int NonNullableWithout { get; set; }
public string NullableWithout { get; set; }
[Required]
public string NullableWith { get; set; }
}
[Fact]
public void IsRequiredTests()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.True(provider.GetMetadataForProperty(null, typeof(IsRequiredModel), "NonNullableWithout").IsRequired);
Assert.False(provider.GetMetadataForProperty(null, typeof(IsRequiredModel), "NullableWithout").IsRequired);
Assert.True(provider.GetMetadataForProperty(null, typeof(IsRequiredModel), "NullableWith").IsRequired);
}
// [Display] & [DisplayName] tests
class DisplayModel
{
public int NoAttribute { get; set; }
// Description
[Display]
public int DescriptionNotSet { get; set; }
[Display(Description = "Description text")]
public int DescriptionSet { get; set; }
// DisplayName
[DisplayName("Value from DisplayName")]
public int DisplayNameAttributeNoDisplayAttribute { get; set; }
[Display]
public int DisplayAttributeNameNotSet { get; set; }
[Display(Name = "Non empty name")]
public int DisplayAttributeNonEmptyName { get; set; }
[Display]
[DisplayName("Value from DisplayName")]
public int BothAttributesNameNotSet { get; set; }
[Display(Name = "Value from Display")]
[DisplayName("Value from DisplayName")]
public int BothAttributes { get; set; }
// Display trumps DisplayName
// Order
[Display]
public int OrderNotSet { get; set; }
[Display(Order = 2112)]
public int OrderSet { get; set; }
// ShortDisplayName
[Display]
public int ShortNameNotSet { get; set; }
[Display(ShortName = "Short name")]
public int ShortNameSet { get; set; }
// Watermark
[Display]
public int PromptNotSet { get; set; }
[Display(Prompt = "Enter stuff here")]
public int PromptSet { get; set; }
}
[Fact]
public void DescriptionTests()
{
// Arrange
var provider = MakeProvider();
// 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);
}
[Fact]
public void DisplayNameTests()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").DisplayName);
Assert.Equal("Value from DisplayName", provider.GetMetadataForProperty(null, typeof(DisplayModel), "DisplayNameAttributeNoDisplayAttribute").DisplayName);
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "DisplayAttributeNameNotSet").DisplayName);
Assert.Equal("Non empty name", provider.GetMetadataForProperty(null, typeof(DisplayModel), "DisplayAttributeNonEmptyName").DisplayName);
Assert.Equal("Value from DisplayName", provider.GetMetadataForProperty(null, typeof(DisplayModel), "BothAttributesNameNotSet").DisplayName);
Assert.Equal("Value from Display", provider.GetMetadataForProperty(null, typeof(DisplayModel), "BothAttributes").DisplayName);
}
[Fact]
public void OrderTests()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Equal(10000, provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").Order);
Assert.Equal(10000, provider.GetMetadataForProperty(null, typeof(DisplayModel), "OrderNotSet").Order);
Assert.Equal(2112, provider.GetMetadataForProperty(null, typeof(DisplayModel), "OrderSet").Order);
}
[Fact]
public void ShortDisplayNameTests()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").ShortDisplayName);
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "ShortNameNotSet").ShortDisplayName);
Assert.Equal("Short name", provider.GetMetadataForProperty(null, typeof(DisplayModel), "ShortNameSet").ShortDisplayName);
}
[Fact]
public void WatermarkTests()
{
// Arrange
var provider = MakeProvider();
// Act & Assert
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").Watermark);
Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "PromptNotSet").Watermark);
Assert.Equal("Enter stuff here", provider.GetMetadataForProperty(null, typeof(DisplayModel), "PromptSet").Watermark);
}
}
}