Excluding a test case in Mono + Fixing a test case. Fixing line lengths. Fixing error messages for tests to pass in Mono.

This commit is contained in:
sornaks 2014-10-06 17:14:05 -07:00
parent a88f59fc49
commit 5b2948dd73
4 changed files with 200 additions and 96 deletions

View File

@ -15,7 +15,7 @@ using Microsoft.Framework.OptionsModel;
using Moq; using Moq;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.Core.Test namespace Microsoft.AspNet.Mvc
{ {
public class InputObjectBindingTests public class InputObjectBindingTests
{ {
@ -58,7 +58,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
Assert.False(modelStateDictionary.IsValid); Assert.False(modelStateDictionary.IsValid);
Assert.Equal(1, modelStateDictionary.ErrorCount); Assert.Equal(1, modelStateDictionary.ErrorCount);
Assert.Equal( Assert.Equal(
"The field UserName must be a string or array type with a minimum length of '5'.", ValidationAttributeUtil.GetMinLengthErrorMessage(5, "UserName"),
Assert.Single(Assert.Single(modelStateDictionary.Values).Errors).ErrorMessage); Assert.Single(Assert.Single(modelStateDictionary.Values).Errors).ErrorMessage);
var model = result["foo"] as User; var model = result["foo"] as User;
Assert.Equal(sampleName, model.Name); Assert.Equal(sampleName, model.Name);
@ -113,10 +113,10 @@ namespace Microsoft.AspNet.Mvc.Core.Test
Assert.Equal(2, modelStateDictionary.ErrorCount); Assert.Equal(2, modelStateDictionary.ErrorCount);
var model = result["foo"] as Customers; var model = result["foo"] as Customers;
Assert.Equal( Assert.Equal(
"The field UserName must be a string or array type with a minimum length of '5'.", ValidationAttributeUtil.GetMinLengthErrorMessage(5, "UserName"),
modelStateDictionary["foo.Users[0].UserName"].Errors[0].ErrorMessage); modelStateDictionary["foo.Users[0].UserName"].Errors[0].ErrorMessage);
Assert.Equal( Assert.Equal(
"The field UserName must be a string or array type with a minimum length of '5'.", ValidationAttributeUtil.GetMinLengthErrorMessage(5, "UserName"),
modelStateDictionary["foo.Users[1].UserName"].Errors[0].ErrorMessage); modelStateDictionary["foo.Users[1].UserName"].Errors[0].ErrorMessage);
Assert.Equal(2, model.Users.Count); Assert.Equal(2, model.Users.Count);
Assert.Equal(sampleFirstUser, model.Users[0].Name); Assert.Equal(sampleFirstUser, model.Users[0].Name);

View File

@ -0,0 +1,16 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.ComponentModel.DataAnnotations;
namespace Microsoft.AspNet.Mvc
{
public static class ValidationAttributeUtil
{
public static string GetMinLengthErrorMessage(int length, string field)
{
var attr = new MinLengthAttribute(length);
return attr.FormatErrorMessage(field);
}
}
}

View File

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
namespace Microsoft.AspNet.Mvc.ModelBinding namespace Microsoft.AspNet.Mvc.ModelBinding
@ -13,5 +12,34 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var attr = new RequiredAttribute(); var attr = new RequiredAttribute();
return attr.FormatErrorMessage(field); return attr.FormatErrorMessage(field);
} }
public static string GetStringLengthErrorMessage(int? minimumLength, int maximumLength, string field)
{
var attr = new StringLengthAttribute(maximumLength);
if (minimumLength != null)
{
attr.MinimumLength = (int)minimumLength;
}
return attr.FormatErrorMessage(field);
}
public static string GetMaxLengthErrorMessage(int maximumLength, string field)
{
var attr = new MaxLengthAttribute(maximumLength);
return attr.FormatErrorMessage(field);
}
public static string GetRegExErrorMessage(string pattern, string field)
{
var attr = new RegularExpressionAttribute(pattern);
return attr.FormatErrorMessage(field);
}
public static string GetRangeErrorMessage(int min, int max, string field)
{
var attr = new RangeAttribute(min, max);
return attr.FormatErrorMessage(field);
}
} }
} }

View File

@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNet.Mvc.OptionDescriptors; using Microsoft.AspNet.Mvc.OptionDescriptors;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
@ -35,111 +36,173 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
yield return new object[] { "foo", typeof(string), new Dictionary<string, string>() }; yield return new object[] { "foo", typeof(string), new Dictionary<string, string>() };
// Object Traversal : make sure we can traverse the object graph without throwing // Object Traversal : make sure we can traverse the object graph without throwing
yield return new object[] { new ValueType() { Reference = "ref", Value = 256 }, typeof(ValueType), new Dictionary<string, string>() }; yield return new object[]
yield return new object[] { new ReferenceType() { Reference = "ref", Value = 256 }, typeof(ReferenceType), new Dictionary<string, string>() }; {
new ValueType() { Reference = "ref", Value = 256 },
typeof(ValueType),
new Dictionary<string, string>()
};
yield return new object[]
{
new ReferenceType() { Reference = "ref", Value = 256 },
typeof(ReferenceType),
new Dictionary<string, string>()
};
// Classes // Classes
yield return new object[] { new Person() { Name = "Rick", Profession = "Astronaut" }, typeof(Person), new Dictionary<string, string>() }; yield return new object[]
yield return new object[] { new Person(), typeof(Person), new Dictionary<string, string>() {
{ new Person() { Name = "Rick", Profession = "Astronaut" },
{ "Name", "The Name field is required." }, typeof(Person),
{ "Profession", "The Profession field is required." } new Dictionary<string, string>()
} };
}; yield return new object[]
{
new Person(),
typeof(Person),
new Dictionary<string, string>()
{
{ "Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
{ "Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }
}
};
yield return new object[] { new Person() { Name = "Rick", Friend = new Person() }, typeof(Person), new Dictionary<string, string>() yield return new object[]
{ {
{ "Profession", "The Profession field is required." }, new Person() { Name = "Rick", Friend = new Person() },
{ "Friend.Name", "The Name field is required." }, typeof(Person),
{ "Friend.Profession", "The Profession field is required." } new Dictionary<string, string>()
} {
}; { "Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") },
{ "Friend.Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
{ "Friend.Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }
}
};
// Collections // Collections
yield return new object[] { new Person[] { new Person(), new Person() }, typeof(Person[]), new Dictionary<string, string>() yield return new object[]
{ {
{ "[0].Name", "The Name field is required." }, new Person[] { new Person(), new Person() },
{ "[0].Profession", "The Profession field is required." }, typeof(Person[]),
{ "[1].Name", "The Name field is required." }, new Dictionary<string, string>()
{ "[1].Profession", "The Profession field is required." } {
} { "[0].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
}; { "[0].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") },
{ "[1].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
{ "[1].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }
}
};
yield return new object[] { new List<Person> { new Person(), new Person() }, typeof(Person[]), new Dictionary<string, string>() yield return new object[]
{ {
{ "[0].Name", "The Name field is required." }, new List<Person> { new Person(), new Person() },
{ "[0].Profession", "The Profession field is required." }, typeof(Person[]),
{ "[1].Name", "The Name field is required." }, new Dictionary<string, string>()
{ "[1].Profession", "The Profession field is required." } {
} { "[0].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
}; { "[0].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") },
{ "[1].Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
{ "[1].Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }
}
};
yield return new object[] { new Dictionary<string, Person> { { "Joe", new Person() } , { "Mark", new Person() } }, typeof(Dictionary<string, Person>), new Dictionary<string, string>() if (!TestPlatformHelper.IsMono)
{
// In Mono this throws a NullRef Exception.
// Should be investigated - https://github.com/aspnet/Mvc/issues/1261
yield return new object[]
{
new Dictionary<string, Person> { { "Joe", new Person() } , { "Mark", new Person() } },
typeof(Dictionary<string, Person>),
new Dictionary<string, string>()
{ {
{ "[0].Value.Name", "The Name field is required." }, { "[0].Value.Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
{ "[0].Value.Profession", "The Profession field is required." }, { "[0].Value.Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") },
{ "[1].Value.Name", "The Name field is required." }, { "[1].Value.Name", ValidationAttributeUtil.GetRequiredErrorMessage("Name") },
{ "[1].Value.Profession", "The Profession field is required." } { "[1].Value.Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }
} }
}; };
}
// IValidatableObject's // IValidatableObject's
yield return new object[] { new ValidatableModel(), typeof(ValidatableModel), new Dictionary<string, string>() yield return new object[]
{ {
{ "", "Error1" }, new ValidatableModel(),
{ "Property1", "Error2" }, typeof(ValidatableModel),
{ "Property2", "Error3" }, new Dictionary<string, string>()
{ "Property3", "Error3" } {
} { "", "Error1" },
}; { "Property1", "Error2" },
{ "Property2", "Error3" },
{ "Property3", "Error3" }
}
};
yield return new object[]{ new[] { new ValidatableModel() }, typeof(ValidatableModel[]), new Dictionary<string, string>() yield return new object[]
{ {
{ "[0]", "Error1" }, new[] { new ValidatableModel() },
{ "[0].Property1", "Error2" }, typeof(ValidatableModel[]),
{ "[0].Property2", "Error3" }, new Dictionary<string, string>()
{ "[0].Property3", "Error3" } {
} { "[0]", "Error1" },
}; { "[0].Property1", "Error2" },
{ "[0].Property2", "Error3" },
{ "[0].Property3", "Error3" }
}
};
// Nested Objects // Nested Objects
yield return new object[] { new Org() { yield return new object[]
Id = 1, {
OrgName = "Org", new Org()
Dev = new Team {
{ Id = 1,
Id = 10, OrgName = "Org",
TeamName = "HelloWorldTeam", Dev = new Team
Lead = "SampleLeadDev", {
TeamSize = 2 Id = 10,
}, TeamName = "HelloWorldTeam",
Test = new Team Lead = "SampleLeadDev",
{ TeamSize = 2
Id = 11, },
TeamName = "HWT", Test = new Team
Lead = "SampleTestLead", {
TeamSize = 12 Id = 11,
} TeamName = "HWT",
}, typeof(Org), new Dictionary<string, string>() Lead = "SampleTestLead",
{ TeamSize = 12
{ "OrgName", "The field OrgName must be a string with a minimum length of 4 and a maximum length of 20." }, }
{ "Dev.Lead", "The field Lead must be a string or array type with a maximum length of '10'." }, },
{ "Dev.TeamSize", "The field TeamSize must be between 3 and 100." }, typeof(Org),
{ "Test.TeamName", "The field TeamName must be a string with a minimum length of 4 and a maximum length of 20." }, new Dictionary<string, string>()
{ "Test.Lead", "The field Lead must be a string or array type with a maximum length of '10'." } {
} { "OrgName", ValidationAttributeUtil.GetStringLengthErrorMessage(4, 20, "OrgName") },
{ "Dev.Lead", ValidationAttributeUtil.GetMaxLengthErrorMessage(10, "Lead") },
{ "Dev.TeamSize", ValidationAttributeUtil.GetRangeErrorMessage(3, 100, "TeamSize") },
{ "Test.TeamName", ValidationAttributeUtil.GetStringLengthErrorMessage(4, 20, "TeamName") },
{ "Test.Lead", ValidationAttributeUtil.GetMaxLengthErrorMessage(10, "Lead") }
}
}; };
// Testing we don't validate fields // Testing we don't validate fields
yield return new object[] { new VariableTest() { test = 5 }, typeof(VariableTest), new Dictionary<string, string>() }; yield return new object[]
{
new VariableTest() { test = 5 },
typeof(VariableTest),
new Dictionary<string, string>()
};
// Testing we don't blow up on cycles // Testing we don't blow up on cycles
yield return new object[] { LonelyPerson, typeof(Person), new Dictionary<string, string>() yield return new object[]
{ {
{ "Name", "The field Name must be a string with a maximum length of 10." }, LonelyPerson,
{ "Profession", "The Profession field is required." } typeof(Person),
} new Dictionary<string, string>()
}; {
{ "Name", ValidationAttributeUtil.GetStringLengthErrorMessage(null, 10, "Name") },
{ "Profession", ValidationAttributeUtil.GetRequiredErrorMessage("Profession") }
}
};
} }
} }
@ -209,12 +272,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Contains("Street", validationContext.ModelState.Keys); Assert.Contains("Street", validationContext.ModelState.Keys);
var streetState = validationContext.ModelState["Street"]; var streetState = validationContext.ModelState["Street"];
Assert.Equal(2, streetState.Errors.Count); Assert.Equal(2, streetState.Errors.Count);
Assert.Equal( var errorCollection = streetState.Errors.Select(e => e.ErrorMessage);
"The field Street must be a string with a maximum length of 5.", Assert.Contains(ValidationAttributeUtil.GetStringLengthErrorMessage(null, 5, "Street"), errorCollection);
streetState.Errors[0].ErrorMessage); Assert.Contains(ValidationAttributeUtil.GetRegExErrorMessage("hehehe", "Street"), errorCollection);
Assert.Equal(
"The field Street must match the regular expression 'hehehe'.",
streetState.Errors[1].ErrorMessage);
} }
[Fact] [Fact]