Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 5b2948d

Browse files
author
sornaks
committed
Excluding a test case in Mono + Fixing a test case. Fixing line lengths. Fixing error messages for tests to pass in Mono.
1 parent a88f59f commit 5b2948d

File tree

4 files changed

+200
-96
lines changed

4 files changed

+200
-96
lines changed

test/Microsoft.AspNet.Mvc.Core.Test/InputObjectBindingTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using Moq;
1616
using Xunit;
1717

18-
namespace Microsoft.AspNet.Mvc.Core.Test
18+
namespace Microsoft.AspNet.Mvc
1919
{
2020
public class InputObjectBindingTests
2121
{
@@ -58,7 +58,7 @@ public async Task GetArguments_UsingInputFormatter_DeserializesWithValidationErr
5858
Assert.False(modelStateDictionary.IsValid);
5959
Assert.Equal(1, modelStateDictionary.ErrorCount);
6060
Assert.Equal(
61-
"The field UserName must be a string or array type with a minimum length of '5'.",
61+
ValidationAttributeUtil.GetMinLengthErrorMessage(5, "UserName"),
6262
Assert.Single(Assert.Single(modelStateDictionary.Values).Errors).ErrorMessage);
6363
var model = result["foo"] as User;
6464
Assert.Equal(sampleName, model.Name);
@@ -113,10 +113,10 @@ public async Task GetArguments_UsingInputFormatter_DeserializesArrays_WithErrors
113113
Assert.Equal(2, modelStateDictionary.ErrorCount);
114114
var model = result["foo"] as Customers;
115115
Assert.Equal(
116-
"The field UserName must be a string or array type with a minimum length of '5'.",
116+
ValidationAttributeUtil.GetMinLengthErrorMessage(5, "UserName"),
117117
modelStateDictionary["foo.Users[0].UserName"].Errors[0].ErrorMessage);
118118
Assert.Equal(
119-
"The field UserName must be a string or array type with a minimum length of '5'.",
119+
ValidationAttributeUtil.GetMinLengthErrorMessage(5, "UserName"),
120120
modelStateDictionary["foo.Users[1].UserName"].Errors[0].ErrorMessage);
121121
Assert.Equal(2, model.Users.Count);
122122
Assert.Equal(sampleFirstUser, model.Users[0].Name);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.ComponentModel.DataAnnotations;
5+
6+
namespace Microsoft.AspNet.Mvc
7+
{
8+
public static class ValidationAttributeUtil
9+
{
10+
public static string GetMinLengthErrorMessage(int length, string field)
11+
{
12+
var attr = new MinLengthAttribute(length);
13+
return attr.FormatErrorMessage(field);
14+
}
15+
}
16+
}

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Utils/ValidationAttributeUtil.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.ComponentModel.DataAnnotations;
65

76
namespace Microsoft.AspNet.Mvc.ModelBinding
@@ -13,5 +12,34 @@ public static string GetRequiredErrorMessage(string field)
1312
var attr = new RequiredAttribute();
1413
return attr.FormatErrorMessage(field);
1514
}
15+
16+
public static string GetStringLengthErrorMessage(int? minimumLength, int maximumLength, string field)
17+
{
18+
var attr = new StringLengthAttribute(maximumLength);
19+
if (minimumLength != null)
20+
{
21+
attr.MinimumLength = (int)minimumLength;
22+
}
23+
24+
return attr.FormatErrorMessage(field);
25+
}
26+
27+
public static string GetMaxLengthErrorMessage(int maximumLength, string field)
28+
{
29+
var attr = new MaxLengthAttribute(maximumLength);
30+
return attr.FormatErrorMessage(field);
31+
}
32+
33+
public static string GetRegExErrorMessage(string pattern, string field)
34+
{
35+
var attr = new RegularExpressionAttribute(pattern);
36+
return attr.FormatErrorMessage(field);
37+
}
38+
39+
public static string GetRangeErrorMessage(int min, int max, string field)
40+
{
41+
var attr = new RangeAttribute(min, max);
42+
return attr.FormatErrorMessage(field);
43+
}
1644
}
1745
}

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/DefaultBodyModelValidatorTests.cs

Lines changed: 151 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.ComponentModel.DataAnnotations;
8+
using System.Linq;
89
using Microsoft.AspNet.Mvc.OptionDescriptors;
910
using Microsoft.AspNet.Testing;
1011
using Microsoft.Framework.DependencyInjection;
@@ -35,111 +36,173 @@ public static IEnumerable<object[]> ValidationErrors
3536
yield return new object[] { "foo", typeof(string), new Dictionary<string, string>() };
3637

3738
// 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+
};
4051

4152
// 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+
};
4969

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+
};
5781

5882
// 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+
};
6795

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+
};
76108

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>()
78118
{
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") }
83123
}
84124
};
125+
}
85126

86127
// 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+
};
95140

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+
};
104153

105154
// 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+
}
131185
};
132186

133187
// 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+
};
135194

136195
// 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+
};
143206
}
144207
}
145208

@@ -209,12 +272,9 @@ public void MultipleValidationErrorsOnSameMemberReported()
209272
Assert.Contains("Street", validationContext.ModelState.Keys);
210273
var streetState = validationContext.ModelState["Street"];
211274
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);
218278
}
219279

220280
[Fact]

0 commit comments

Comments
 (0)