|
5 | 5 | using System;
|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.ComponentModel.DataAnnotations;
|
| 8 | +using System.Linq; |
8 | 9 | using Microsoft.AspNet.Mvc.OptionDescriptors;
|
9 | 10 | using Microsoft.AspNet.Testing;
|
10 | 11 | using Microsoft.Framework.DependencyInjection;
|
@@ -35,111 +36,173 @@ public static IEnumerable<object[]> ValidationErrors
|
35 | 36 | yield return new object[] { "foo", typeof(string), new Dictionary<string, string>() };
|
36 | 37 |
|
37 | 38 | // Object Traversal : make sure we can traverse the object graph without throwing
|
38 |
| - yield return new object[] { new ValueType() { Reference = "ref", Value = 256 }, typeof(ValueType), new Dictionary<string, string>() }; |
39 |
| - yield return new object[] { new ReferenceType() { Reference = "ref", Value = 256 }, typeof(ReferenceType), new Dictionary<string, string>() }; |
| 39 | + yield return new object[] |
| 40 | + { |
| 41 | + new ValueType() { Reference = "ref", Value = 256 }, |
| 42 | + typeof(ValueType), |
| 43 | + new Dictionary<string, string>() |
| 44 | + }; |
| 45 | + yield return new object[] |
| 46 | + { |
| 47 | + new ReferenceType() { Reference = "ref", Value = 256 }, |
| 48 | + typeof(ReferenceType), |
| 49 | + new Dictionary<string, string>() |
| 50 | + }; |
40 | 51 |
|
41 | 52 | // Classes
|
42 |
| - yield return new object[] { new Person() { Name = "Rick", Profession = "Astronaut" }, typeof(Person), new Dictionary<string, string>() }; |
43 |
| - yield return new object[] { new Person(), typeof(Person), new Dictionary<string, string>() |
44 |
| - { |
45 |
| - { "Name", "The Name field is required." }, |
46 |
| - { "Profession", "The Profession field is required." } |
47 |
| - } |
48 |
| - }; |
| 53 | + yield return new object[] |
| 54 | + { |
| 55 | + new Person() { Name = "Rick", Profession = "Astronaut" }, |
| 56 | + typeof(Person), |
| 57 | + new Dictionary<string, string>() |
| 58 | + }; |
| 59 | + yield return new object[] |
| 60 | + { |
| 61 | + new Person(), |
| 62 | + typeof(Person), |
| 63 | + new Dictionary<string, string>() |
| 64 | + { |
| 65 | + { "Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 66 | + { "Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") } |
| 67 | + } |
| 68 | + }; |
49 | 69 |
|
50 |
| - yield return new object[] { new Person() { Name = "Rick", Friend = new Person() }, typeof(Person), new Dictionary<string, string>() |
51 |
| - { |
52 |
| - { "Profession", "The Profession field is required." }, |
53 |
| - { "Friend.Name", "The Name field is required." }, |
54 |
| - { "Friend.Profession", "The Profession field is required." } |
55 |
| - } |
56 |
| - }; |
| 70 | + yield return new object[] |
| 71 | + { |
| 72 | + new Person() { Name = "Rick", Friend = new Person() }, |
| 73 | + typeof(Person), |
| 74 | + new Dictionary<string, string>() |
| 75 | + { |
| 76 | + { "Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }, |
| 77 | + { "Friend.Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 78 | + { "Friend.Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") } |
| 79 | + } |
| 80 | + }; |
57 | 81 |
|
58 | 82 | // Collections
|
59 |
| - yield return new object[] { new Person[] { new Person(), new Person() }, typeof(Person[]), new Dictionary<string, string>() |
60 |
| - { |
61 |
| - { "[0].Name", "The Name field is required." }, |
62 |
| - { "[0].Profession", "The Profession field is required." }, |
63 |
| - { "[1].Name", "The Name field is required." }, |
64 |
| - { "[1].Profession", "The Profession field is required." } |
65 |
| - } |
66 |
| - }; |
| 83 | + yield return new object[] |
| 84 | + { |
| 85 | + new Person[] { new Person(), new Person() }, |
| 86 | + typeof(Person[]), |
| 87 | + new Dictionary<string, string>() |
| 88 | + { |
| 89 | + { "[0].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 90 | + { "[0].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }, |
| 91 | + { "[1].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 92 | + { "[1].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") } |
| 93 | + } |
| 94 | + }; |
67 | 95 |
|
68 |
| - yield return new object[] { new List<Person> { new Person(), new Person() }, typeof(Person[]), new Dictionary<string, string>() |
69 |
| - { |
70 |
| - { "[0].Name", "The Name field is required." }, |
71 |
| - { "[0].Profession", "The Profession field is required." }, |
72 |
| - { "[1].Name", "The Name field is required." }, |
73 |
| - { "[1].Profession", "The Profession field is required." } |
74 |
| - } |
75 |
| - }; |
| 96 | + yield return new object[] |
| 97 | + { |
| 98 | + new List<Person> { new Person(), new Person() }, |
| 99 | + typeof(Person[]), |
| 100 | + new Dictionary<string, string>() |
| 101 | + { |
| 102 | + { "[0].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 103 | + { "[0].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }, |
| 104 | + { "[1].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 105 | + { "[1].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") } |
| 106 | + } |
| 107 | + }; |
76 | 108 |
|
77 |
| - yield return new object[] { new Dictionary<string, Person> { { "Joe", new Person() } , { "Mark", new Person() } }, typeof(Dictionary<string, Person>), new Dictionary<string, string>() |
| 109 | + if (!TestPlatformHelper.IsMono) |
| 110 | + { |
| 111 | + // In Mono this throws a NullRef Exception. |
| 112 | + // Should be investigated - https://github.com/aspnet/Mvc/issues/1261 |
| 113 | + yield return new object[] |
| 114 | + { |
| 115 | + new Dictionary<string, Person> { { "Joe", new Person() } , { "Mark", new Person() } }, |
| 116 | + typeof(Dictionary<string, Person>), |
| 117 | + new Dictionary<string, string>() |
78 | 118 | {
|
79 |
| - { "[0].Value.Name", "The Name field is required." }, |
80 |
| - { "[0].Value.Profession", "The Profession field is required." }, |
81 |
| - { "[1].Value.Name", "The Name field is required." }, |
82 |
| - { "[1].Value.Profession", "The Profession field is required." } |
| 119 | + { "[0].Value.Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 120 | + { "[0].Value.Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }, |
| 121 | + { "[1].Value.Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") }, |
| 122 | + { "[1].Value.Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") } |
83 | 123 | }
|
84 | 124 | };
|
| 125 | + } |
85 | 126 |
|
86 | 127 | // IValidatableObject's
|
87 |
| - yield return new object[] { new ValidatableModel(), typeof(ValidatableModel), new Dictionary<string, string>() |
88 |
| - { |
89 |
| - { "", "Error1" }, |
90 |
| - { "Property1", "Error2" }, |
91 |
| - { "Property2", "Error3" }, |
92 |
| - { "Property3", "Error3" } |
93 |
| - } |
94 |
| - }; |
| 128 | + yield return new object[] |
| 129 | + { |
| 130 | + new ValidatableModel(), |
| 131 | + typeof(ValidatableModel), |
| 132 | + new Dictionary<string, string>() |
| 133 | + { |
| 134 | + { "", "Error1" }, |
| 135 | + { "Property1", "Error2" }, |
| 136 | + { "Property2", "Error3" }, |
| 137 | + { "Property3", "Error3" } |
| 138 | + } |
| 139 | + }; |
95 | 140 |
|
96 |
| - yield return new object[]{ new[] { new ValidatableModel() }, typeof(ValidatableModel[]), new Dictionary<string, string>() |
97 |
| - { |
98 |
| - { "[0]", "Error1" }, |
99 |
| - { "[0].Property1", "Error2" }, |
100 |
| - { "[0].Property2", "Error3" }, |
101 |
| - { "[0].Property3", "Error3" } |
102 |
| - } |
103 |
| - }; |
| 141 | + yield return new object[] |
| 142 | + { |
| 143 | + new[] { new ValidatableModel() }, |
| 144 | + typeof(ValidatableModel[]), |
| 145 | + new Dictionary<string, string>() |
| 146 | + { |
| 147 | + { "[0]", "Error1" }, |
| 148 | + { "[0].Property1", "Error2" }, |
| 149 | + { "[0].Property2", "Error3" }, |
| 150 | + { "[0].Property3", "Error3" } |
| 151 | + } |
| 152 | + }; |
104 | 153 |
|
105 | 154 | // Nested Objects
|
106 |
| - yield return new object[] { new Org() { |
107 |
| - Id = 1, |
108 |
| - OrgName = "Org", |
109 |
| - Dev = new Team |
110 |
| - { |
111 |
| - Id = 10, |
112 |
| - TeamName = "HelloWorldTeam", |
113 |
| - Lead = "SampleLeadDev", |
114 |
| - TeamSize = 2 |
115 |
| - }, |
116 |
| - Test = new Team |
117 |
| - { |
118 |
| - Id = 11, |
119 |
| - TeamName = "HWT", |
120 |
| - Lead = "SampleTestLead", |
121 |
| - TeamSize = 12 |
122 |
| - } |
123 |
| - }, typeof(Org), new Dictionary<string, string>() |
124 |
| - { |
125 |
| - { "OrgName", "The field OrgName must be a string with a minimum length of 4 and a maximum length of 20." }, |
126 |
| - { "Dev.Lead", "The field Lead must be a string or array type with a maximum length of '10'." }, |
127 |
| - { "Dev.TeamSize", "The field TeamSize must be between 3 and 100." }, |
128 |
| - { "Test.TeamName", "The field TeamName must be a string with a minimum length of 4 and a maximum length of 20." }, |
129 |
| - { "Test.Lead", "The field Lead must be a string or array type with a maximum length of '10'." } |
130 |
| - } |
| 155 | + yield return new object[] |
| 156 | + { |
| 157 | + new Org() |
| 158 | + { |
| 159 | + Id = 1, |
| 160 | + OrgName = "Org", |
| 161 | + Dev = new Team |
| 162 | + { |
| 163 | + Id = 10, |
| 164 | + TeamName = "HelloWorldTeam", |
| 165 | + Lead = "SampleLeadDev", |
| 166 | + TeamSize = 2 |
| 167 | + }, |
| 168 | + Test = new Team |
| 169 | + { |
| 170 | + Id = 11, |
| 171 | + TeamName = "HWT", |
| 172 | + Lead = "SampleTestLead", |
| 173 | + TeamSize = 12 |
| 174 | + } |
| 175 | + }, |
| 176 | + typeof(Org), |
| 177 | + new Dictionary<string, string>() |
| 178 | + { |
| 179 | + { "OrgName", ValidationAttributeUtil.GetStringLengthErrorMessage(4, 20, "OrgName") }, |
| 180 | + { "Dev.Lead", ValidationAttributeUtil.GetMaxLengthErrorMessage(10, "Lead") }, |
| 181 | + { "Dev.TeamSize", ValidationAttributeUtil.GetRangeErrorMessage(3, 100, "TeamSize") }, |
| 182 | + { "Test.TeamName", ValidationAttributeUtil.GetStringLengthErrorMessage(4, 20, "TeamName") }, |
| 183 | + { "Test.Lead", ValidationAttributeUtil.GetMaxLengthErrorMessage(10, "Lead") } |
| 184 | + } |
131 | 185 | };
|
132 | 186 |
|
133 | 187 | // Testing we don't validate fields
|
134 |
| - yield return new object[] { new VariableTest() { test = 5 }, typeof(VariableTest), new Dictionary<string, string>() }; |
| 188 | + yield return new object[] |
| 189 | + { |
| 190 | + new VariableTest() { test = 5 }, |
| 191 | + typeof(VariableTest), |
| 192 | + new Dictionary<string, string>() |
| 193 | + }; |
135 | 194 |
|
136 | 195 | // Testing we don't blow up on cycles
|
137 |
| - yield return new object[] { LonelyPerson, typeof(Person), new Dictionary<string, string>() |
138 |
| - { |
139 |
| - { "Name", "The field Name must be a string with a maximum length of 10." }, |
140 |
| - { "Profession", "The Profession field is required." } |
141 |
| - } |
142 |
| - }; |
| 196 | + yield return new object[] |
| 197 | + { |
| 198 | + LonelyPerson, |
| 199 | + typeof(Person), |
| 200 | + new Dictionary<string, string>() |
| 201 | + { |
| 202 | + { "Name", ValidationAttributeUtil.GetStringLengthErrorMessage(null, 10, "Name") }, |
| 203 | + { "Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") } |
| 204 | + } |
| 205 | + }; |
143 | 206 | }
|
144 | 207 | }
|
145 | 208 |
|
@@ -209,12 +272,9 @@ public void MultipleValidationErrorsOnSameMemberReported()
|
209 | 272 | Assert.Contains("Street", validationContext.ModelState.Keys);
|
210 | 273 | var streetState = validationContext.ModelState["Street"];
|
211 | 274 | Assert.Equal(2, streetState.Errors.Count);
|
212 |
| - Assert.Equal( |
213 |
| - "The field Street must be a string with a maximum length of 5.", |
214 |
| - streetState.Errors[0].ErrorMessage); |
215 |
| - Assert.Equal( |
216 |
| - "The field Street must match the regular expression 'hehehe'.", |
217 |
| - streetState.Errors[1].ErrorMessage); |
| 275 | + var errorCollection = streetState.Errors.Select(e => e.ErrorMessage); |
| 276 | + Assert.Contains(ValidationAttributeUtil.GetStringLengthErrorMessage(null, 5, "Street"), errorCollection); |
| 277 | + Assert.Contains(ValidationAttributeUtil.GetRegExErrorMessage("hehehe", "Street"), errorCollection); |
218 | 278 | }
|
219 | 279 |
|
220 | 280 | [Fact]
|
|
0 commit comments