forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareAttributeAdapterTest.cs
More file actions
116 lines (99 loc) · 4.74 KB
/
Copy pathCompareAttributeAdapterTest.cs
File metadata and controls
116 lines (99 loc) · 4.74 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
// 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.ComponentModel;
using System.Linq;
using Microsoft.TestCommon;
using AnnotationsCompareAttribute = System.ComponentModel.DataAnnotations.CompareAttribute;
using MyResources = System.Web.Properties.Resources;
namespace System.Web.Mvc.Test
{
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")] // Uses ModelMetadataProviders.Current
public class CompareAttributeAdapterTest
{
[Fact]
[ReplaceCulture]
public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName()
{
// Arrange
var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyDisplayNameModel), "MyProperty");
var context = new ControllerContext();
var attribute = new AnnotationsCompareAttribute("OtherProperty");
var adapter = new CompareAttributeAdapter(metadata, context, attribute);
// Act
var rules = adapter.GetClientValidationRules()
.OrderBy(r => r.ValidationType)
.ToArray();
// Assert
ModelClientValidationRule rule = Assert.Single(rules);
Assert.Equal("'MyPropertyDisplayName' and 'OtherPropertyDisplayName' do not match.", rule.ErrorMessage);
}
[Fact]
[ReplaceCulture]
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName()
{
// Arrange
var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty");
var context = new ControllerContext();
var attribute = new AnnotationsCompareAttribute("OtherProperty");
var adapter = new CompareAttributeAdapter(metadata, context, attribute);
// Act
var rules = adapter.GetClientValidationRules()
.OrderBy(r => r.ValidationType)
.ToArray();
// Assert
ModelClientValidationRule rule = Assert.Single(rules);
Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage);
}
[Fact]
public void ClientRulesWithCompareAttribute_ErrorMessageUsesOverride()
{
// Arrange
ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty");
ControllerContext context = new ControllerContext();
AnnotationsCompareAttribute attribute = new AnnotationsCompareAttribute("OtherProperty")
{
ErrorMessage = "Hello '{0}', goodbye '{1}'.",
};
ModelValidator adapter = new CompareAttributeAdapter(metadata, context, attribute);
// Act
ModelClientValidationRule[] rules = adapter.GetClientValidationRules()
.OrderBy(r => r.ValidationType)
.ToArray();
// Assert
ModelClientValidationRule rule = Assert.Single(rules);
Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage);
}
[Fact]
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride()
{
// Arrange
ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty");
ControllerContext context = new ControllerContext();
AnnotationsCompareAttribute attribute = new AnnotationsCompareAttribute("OtherProperty")
{
ErrorMessageResourceName = "CompareAttributeTestResource",
ErrorMessageResourceType = typeof(MyResources),
};
ModelValidator adapter = new CompareAttributeAdapter(metadata, context, attribute);
// Act
ModelClientValidationRule[] rules = adapter.GetClientValidationRules()
.OrderBy(r => r.ValidationType)
.ToArray();
// Assert
ModelClientValidationRule rule = Assert.Single(rules);
Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage);
}
private class PropertyDisplayNameModel
{
[DisplayName("MyPropertyDisplayName")]
public string MyProperty { get; set; }
[DisplayName("OtherPropertyDisplayName")]
public string OtherProperty { get; set; }
}
private class PropertyNameModel
{
public string MyProperty { get; set; }
public string OtherProperty { get; set; }
}
}
}