Fix 4 unit test failures

- NRE in TypeExtensions.IsCompatibleWith()
- ComplexModelDtoResultTest.Constructor_ThrowsIfValidationNodeIsNull() won't work w/o validation
- BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() looking for wrong message
- BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState_ErrorNotAddedIfCallbackReturnsNull() a dupe
- Also add [NotNull] attributes in TypeExtensions
This commit is contained in:
dougbu 2014-03-09 15:07:19 -07:00
parent 123089d5c7
commit 59eaa8f642
3 changed files with 17 additions and 35 deletions

View File

@ -5,23 +5,23 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Internal
{
public static class TypeExtensions
{
public static bool IsCompatibleWith(this Type type, object value)
public static bool IsCompatibleWith([NotNull] this Type type, object value)
{
return (value == null && AllowsNullValue(type)) ||
type.GetTypeInfo().IsAssignableFrom(value.GetType().GetTypeInfo());
(value != null && type.GetTypeInfo().IsAssignableFrom(value.GetType().GetTypeInfo()));
}
public static bool IsNullableValueType(this Type type)
public static bool IsNullableValueType([NotNull] this Type type)
{
return Nullable.GetUnderlyingType(type) != null;
}
public static bool AllowsNullValue(this Type type)
public static bool AllowsNullValue([NotNull] this Type type)
{
return (!type.GetTypeInfo().IsValueType || IsNullableValueType(type));
}
public static bool HasStringConverter(this Type type)
public static bool HasStringConverter([NotNull] this Type type)
{
// TODO: This depends on TypeConverter which does not exist in the CoreCLR.
// return TypeDescriptor.GetConverter(type).CanConvertFrom(typeof(string));
@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Internal
return false;
}
public static Type[] GetTypeArgumentsIfMatch(Type closedType, Type matchingOpenType)
public static Type[] GetTypeArgumentsIfMatch([NotNull] Type closedType, Type matchingOpenType)
{
TypeInfo closedTypeInfo = closedType.GetTypeInfo();
if (!closedTypeInfo.IsGenericType)

View File

@ -4,14 +4,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
{
public class ComplexModelDtoResultTest
{
[Fact]
public void Constructor_ThrowsIfValidationNodeIsNull()
{
// Act & assert
ExceptionAssert.ThrowsArgumentNull(
() => new ComplexModelDtoResult("some string"),
"validationNode");
}
// TODO: Validation
////[Fact]
////public void Constructor_ThrowsIfValidationNodeIsNull()
////{
//// // Act & assert
//// ExceptionAssert.ThrowsArgumentNull(
//// () => new ComplexModelDtoResult("some string"),
//// "validationNode");
////}
// TODO: Validation
//[Fact]

View File

@ -24,30 +24,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
// Act
bool retVal = binder.BindModel(bindingContext);
// Assert
Assert.False(retVal);
Assert.Equal("The value 'not an integer' is not valid for Int32.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage);
}
[Fact]
public void BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState_ErrorNotAddedIfCallbackReturnsNull()
{
// Arrange
ModelBindingContext bindingContext = GetBindingContext(typeof(int));
bindingContext.ValueProvider = new SimpleHttpValueProvider
{
{ "theModelName", "not an integer" }
};
TypeConverterModelBinder binder = new TypeConverterModelBinder();
// Act
bool retVal = binder.BindModel(bindingContext);
// Assert
Assert.False(retVal);
Assert.Null(bindingContext.Model);
Assert.True(bindingContext.ModelState.IsValid);
Assert.False(bindingContext.ModelState.IsValid);
Assert.Equal("Input string was not in a correct format.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage);
}
// TODO: TypeConverter