Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Controls
ModelBinding
Test
ArrayModelBinderProviderTest.cs
ArrayModelBinderTest.cs
BinaryDataModelBinderProviderTest.cs
BindingBehaviorAttributeTest.cs
CollectionModelBinderProviderTest.cs
CollectionModelBinderTest.cs
CollectionModelBinderUtilTest.cs
ComplexModelDtoModelBinderProviderTest.cs
ComplexModelDtoModelBinderTest.cs
ComplexModelDtoResultTest.cs
ComplexModelDtoTest.cs
DictionaryModelBinderProviderTest.cs
DictionaryModelBinderTest.cs
ExtensibleModelBinderAdapterTest.cs
ExtensibleModelBindingContextTest.cs
GenericModelBinderProviderTest.cs
KeyValuePairModelBinderProviderTest.cs
KeyValuePairModelBinderTest.cs
KeyValuePairModelBinderUtilTest.cs
ModelBinderConfigTest.cs
ModelBinderProviderCollectionTest.cs
ModelBinderProvidersTest.cs
ModelBinderUtilTest.cs
ModelValidationNodeTest.cs
MutableObjectModelBinderProviderTest.cs
MutableObjectModelBinderTest.cs
SimpleModelBinderProviderTest.cs
TypeConverterModelBinderProviderTest.cs
TypeConverterModelBinderTest.cs
TypeMatchModelBinderProviderTest.cs
TypeMatchModelBinderTest.cs
Properties
Test
Microsoft.Web.Mvc.Test.csproj
packages.config
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
libgc
llvm
m4
man
mcs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

327 lines
11 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace Microsoft.Web.Mvc.ModelBinding.Test
{
public class ModelBinderUtilTest
{
[Fact]
public void CastOrDefault_CorrectType_ReturnsInput()
{
// Act
int retVal = ModelBinderUtil.CastOrDefault<int>(42);
// Assert
Assert.Equal(42, retVal);
}
[Fact]
public void CastOrDefault_IncorrectType_ReturnsDefaultTModel()
{
// Act
DateTime retVal = ModelBinderUtil.CastOrDefault<DateTime>(42);
// Assert
Assert.Equal(default(DateTime), retVal);
}
[Fact]
public void CreateIndexModelName_EmptyParentName()
{
// Act
string fullChildName = ModelBinderUtil.CreateIndexModelName("", 42);
// Assert
Assert.Equal("[42]", fullChildName);
}
[Fact]
public void CreateIndexModelName_IntIndex()
{
// Act
string fullChildName = ModelBinderUtil.CreateIndexModelName("parentName", 42);
// Assert
Assert.Equal("parentName[42]", fullChildName);
}
[Fact]
public void CreateIndexModelName_StringIndex()
{
// Act
string fullChildName = ModelBinderUtil.CreateIndexModelName("parentName", "index");
// Assert
Assert.Equal("parentName[index]", fullChildName);
}
[Fact]
public void CreatePropertyModelName()
{
// Act
string fullChildName = ModelBinderUtil.CreatePropertyModelName("parentName", "childName");
// Assert
Assert.Equal("parentName.childName", fullChildName);
}
[Fact]
public void CreatePropertyModelName_EmptyParentName()
{
// Act
string fullChildName = ModelBinderUtil.CreatePropertyModelName("", "childName");
// Assert
Assert.Equal("childName", fullChildName);
}
[Fact]
public void GetPossibleBinderInstance_Match_ReturnsBinder()
{
// Act
IExtensibleModelBinder binder = ModelBinderUtil.GetPossibleBinderInstance(typeof(List<int>), typeof(List<>), typeof(SampleGenericBinder<>));
// Assert
Assert.IsType<SampleGenericBinder<int>>(binder);
}
[Fact]
public void GetPossibleBinderInstance_NoMatch_ReturnsNull()
{
// Act
IExtensibleModelBinder binder = ModelBinderUtil.GetPossibleBinderInstance(typeof(ArraySegment<int>), typeof(List<>), typeof(SampleGenericBinder<>));
// Assert
Assert.Null(binder);
}
[Fact]
public void RawValueToObjectArray_RawValueIsEnumerable_ReturnsInputAsArray()
{
// Assert
List<int> original = new List<int> { 1, 2, 3, 4 };
// Act
object[] retVal = ModelBinderUtil.RawValueToObjectArray(original);
// Assert
Assert.Equal(new object[] { 1, 2, 3, 4 }, retVal);
}
[Fact]
public void RawValueToObjectArray_RawValueIsObject_WrapsObjectInSingleElementArray()
{
// Act
object[] retVal = ModelBinderUtil.RawValueToObjectArray(42);
// Assert
Assert.Equal(new object[] { 42 }, retVal);
}
[Fact]
public void RawValueToObjectArray_RawValueIsObjectArray_ReturnsInputInstance()
{
// Assert
object[] original = new object[2];
// Act
object[] retVal = ModelBinderUtil.RawValueToObjectArray(original);
// Assert
Assert.Same(original, retVal);
}
[Fact]
public void RawValueToObjectArray_RawValueIsString_WrapsStringInSingleElementArray()
{
// Act
object[] retVal = ModelBinderUtil.RawValueToObjectArray("hello");
// Assert
Assert.Equal(new object[] { "hello" }, retVal);
}
[Fact]
public void ReplaceEmptyStringWithNull_ConvertEmptyStringToNullDisabled_ModelIsEmptyString_LeavesModelAlone()
{
// Arrange
ModelMetadata modelMetadata = GetMetadata(typeof(string));
modelMetadata.ConvertEmptyStringToNull = false;
// Act
object model = "";
ModelBinderUtil.ReplaceEmptyStringWithNull(modelMetadata, ref model);
// Assert
Assert.Equal("", model);
}
[Fact]
public void ReplaceEmptyStringWithNull_ConvertEmptyStringToNullEnabled_ModelIsEmptyString_ReplacesModelWithNull()
{
// Arrange
ModelMetadata modelMetadata = GetMetadata(typeof(string));
modelMetadata.ConvertEmptyStringToNull = true;
// Act
object model = "";
ModelBinderUtil.ReplaceEmptyStringWithNull(modelMetadata, ref model);
// Assert
Assert.Null(model);
}
[Fact]
public void ReplaceEmptyStringWithNull_ConvertEmptyStringToNullEnabled_ModelIsWhitespaceString_ReplacesModelWithNull()
{
// Arrange
ModelMetadata modelMetadata = GetMetadata(typeof(string));
modelMetadata.ConvertEmptyStringToNull = true;
// Act
object model = " "; // whitespace
ModelBinderUtil.ReplaceEmptyStringWithNull(modelMetadata, ref model);
// Assert
Assert.Null(model);
}
[Fact]
public void ReplaceEmptyStringWithNull_ConvertEmptyStringToNullDisabled_ModelIsNotEmptyString_LeavesModelAlone()
{
// Arrange
ModelMetadata modelMetadata = GetMetadata(typeof(string));
modelMetadata.ConvertEmptyStringToNull = true;
// Act
object model = 42;
ModelBinderUtil.ReplaceEmptyStringWithNull(modelMetadata, ref model);
// Assert
Assert.Equal(42, model);
}
[Fact]
public void ValidateBindingContext_SuccessWithNonNullModel()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadata(typeof(string))
};
bindingContext.ModelMetadata.Model = "hello!";
// Act
ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), false);
// Assert
// Nothing to do - if we got this far without throwing, the test succeeded
}
[Fact]
public void ValidateBindingContext_SuccessWithNullModel()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadata(typeof(string))
};
// Act
ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true);
// Assert
// Nothing to do - if we got this far without throwing, the test succeeded
}
[Fact]
public void ValidateBindingContextThrowsIfBindingContextIsNull()
{
// Act & assert
Assert.ThrowsArgumentNull(
delegate { ModelBinderUtil.ValidateBindingContext(null, typeof(string), true); }, "bindingContext");
}
[Fact]
public void ValidateBindingContextThrowsIfModelInstanceIsWrongType()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadata(typeof(string))
};
bindingContext.ModelMetadata.Model = 42;
// Act & assert
Assert.Throws<ArgumentException>(
delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); },
@"The binding context has a Model of type 'System.Int32', but this binder can only operate on models of type 'System.String'.
Parameter name: bindingContext");
}
[Fact]
public void ValidateBindingContextThrowsIfModelIsNullButCannotBe()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadata(typeof(string))
};
// Act & assert
Assert.Throws<ArgumentException>(
delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), false); },
@"The binding context has a null Model, but this binder requires a non-null model of type 'System.String'.
Parameter name: bindingContext");
}
[Fact]
public void ValidateBindingContextThrowsIfModelMetadataIsNull()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
// Act & assert
Assert.Throws<ArgumentException>(
delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); },
@"The binding context cannot have a null ModelMetadata.
Parameter name: bindingContext");
}
[Fact]
public void ValidateBindingContextThrowsIfModelTypeIsWrong()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadata(typeof(object))
};
// Act & assert
Assert.Throws<ArgumentException>(
delegate { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(string), true); },
@"The binding context has a ModelType of 'System.Object', but this binder can only operate on models of type 'System.String'.
Parameter name: bindingContext");
}
private static ModelMetadata GetMetadata(Type modelType)
{
EmptyModelMetadataProvider provider = new EmptyModelMetadataProvider();
return provider.GetMetadataForType(null, modelType);
}
private class SampleGenericBinder<T> : IExtensibleModelBinder
{
public bool BindModel(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext)
{
throw new NotImplementedException();
}
}
}
}