Files
data
debian
docs
eglib
external
Lucene.Net.Light
Newtonsoft.Json
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
cecil
debian-snapshot
ikdasm
ikvm
referencesource
rx
ikvm-native
libgc
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
ChangeLog.REMOVED.git-id
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
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
linux-packaging-mono/external/aspnetwebstack/test/Microsoft.Web.Mvc.Test/ModelBinding/Test/CollectionModelBinderTest.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

248 lines
11 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Web.UnitTestUtil;
using Moq;
using Xunit;
namespace Microsoft.Web.Mvc.ModelBinding.Test
{
public class CollectionModelBinderTest
{
[Fact]
public void BindComplexCollectionFromIndexes_FiniteIndexes()
{
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider
{
{ "someName[foo]", "42" },
{ "someName[baz]", "200" }
}
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, new[] { "foo", "bar", "baz" });
// Assert
Assert.Equal(new[] { 42, 0, 200 }, boundCollection.ToArray());
Assert.Equal(new[] { "someName[foo]", "someName[baz]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
}
[Fact]
public void BindComplexCollectionFromIndexes_InfiniteIndexes()
{
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider
{
{ "someName[0]", "42" },
{ "someName[1]", "100" },
{ "someName[3]", "400" }
}
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, null /* indexNames */);
// Assert
Assert.Equal(new[] { 42, 100 }, boundCollection.ToArray());
Assert.Equal(new[] { "someName[0]", "someName[1]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
}
[Fact]
public void BindModel_ComplexCollection()
{
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider
{
{ "someName.index", new[] { "foo", "bar", "baz" } },
{ "someName[foo]", "42" },
{ "someName[bar]", "100" },
{ "someName[baz]", "200" }
}
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
CollectionModelBinder<int> modelBinder = new CollectionModelBinder<int>();
// Act
bool retVal = modelBinder.BindModel(controllerContext, bindingContext);
// Assert
Assert.Equal(new[] { 42, 100, 200 }, ((List<int>)bindingContext.Model).ToArray());
}
[Fact]
public void BindModel_SimpleCollection()
{
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider
{
{ "someName", new[] { "42", "100", "200" } }
}
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
CollectionModelBinder<int> modelBinder = new CollectionModelBinder<int>();
// Act
bool retVal = modelBinder.BindModel(controllerContext, bindingContext);
// Assert
Assert.True(retVal);
Assert.Equal(new[] { 42, 100, 200 }, ((List<int>)bindingContext.Model).ToArray());
}
[Fact]
public void BindSimpleCollection_RawValueIsEmptyCollection_ReturnsEmptyList()
{
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(null, null, new object[0], null);
// Assert
Assert.NotNull(boundCollection);
Assert.Empty(boundCollection);
}
[Fact]
public void BindSimpleCollection_RawValueIsNull_ReturnsNull()
{
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(null, null, null, null);
// Assert
Assert.Null(boundCollection);
}
[Fact]
public void BindSimpleCollection_SubBinderDoesNotExist()
{
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider()
};
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(controllerContext, bindingContext, new int[1], culture);
// Assert
Assert.Equal(new[] { 0 }, boundCollection.ToArray());
Assert.Empty(bindingContext.ValidationNode.ChildNodes);
}
[Fact]
public void BindSimpleCollection_SubBindingSucceeds()
{
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider()
};
ModelValidationNode childValidationNode = null;
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
{
Assert.Equal("someName", mbc.ModelName);
childValidationNode = mbc.ValidationNode;
mbc.Model = 42;
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(controllerContext, bindingContext, new int[1], culture);
// Assert
Assert.Equal(new[] { 42 }, boundCollection.ToArray());
Assert.Equal(new[] { childValidationNode }, bindingContext.ValidationNode.ChildNodes.ToArray());
}
}
}