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
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
Ajax
Async
ExpressionUtil
Html
Test
ChildActionExtensionsTest.cs
DefaultDisplayTemplatesTest.cs
DefaultEditorTemplatesTest.cs
DisplayNameExtensionsTest.cs
FormExtensionsTest.cs
InputExtensionsTest.cs
LabelExtensionsTest.cs
LinkExtensionsTest.cs
MvcFormTest.cs
NameExtensionsTest.cs
PartialExtensionsTest.cs
RenderPartialExtensionsTest.cs
SelectExtensionsTest.cs
TemplateHelpersTest.cs
TextAreaExtensionsTest.cs
ValidationExtensionsTest.cs
ValueExtensionsTest.cs
Properties
Razor
Test
Util
System.Web.Mvc.Test.csproj
packages.config
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
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
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
linux-packaging-mono/external/aspnetwebstack/test/System.Web.Mvc.Test/Html/Test/ValueExtensionsTest.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

167 lines
6.4 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Web.Mvc.Test;
using Microsoft.Web.UnitTestUtil;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Html.Test
{
public class ValueExtensionsTest
{
// Value
[Fact]
public void ValueWithNullNameThrows()
{
// Arrange
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetValueViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.Value(name: null),
"name"
);
}
[Fact]
public void ValueGetsValueFromViewData()
{
// Arrange
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetValueViewData());
// Act
MvcHtmlString html = helper.Value("foo");
// Assert
Assert.Equal("ViewDataFoo", html.ToHtmlString());
}
// ValueFor
[Fact]
public void ValueForWithNullExpressionThrows()
{
// Arrange
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetValueViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.ValueFor<FooBarModel, object>(expression: null),
"expression"
);
}
[Fact]
public void ValueForGetsExpressionValueFromViewDataModel()
{
// Arrange
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetValueViewData());
// Act
MvcHtmlString html = helper.ValueFor(m => m.foo);
// Assert
Assert.Equal("ViewItemFoo", html.ToHtmlString());
}
// All Value Helpers including ValueForModel
[Fact]
public void ValueHelpersWithErrorsGetValueFromModelState()
{
// Arrange
ViewDataDictionary<FooBarModel> viewDataWithErrors = new ViewDataDictionary<FooBarModel> { { "foo", "ViewDataFoo" } };
viewDataWithErrors.Model = new FooBarModel() { foo = "ViewItemFoo", bar = "ViewItemBar" };
viewDataWithErrors.TemplateInfo.HtmlFieldPrefix = "FieldPrefix";
ModelState modelStateFoo = new ModelState();
modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFoo" }, "AttemptedValueFoo");
viewDataWithErrors.ModelState["FieldPrefix.foo"] = modelStateFoo;
ModelState modelStateFooBar = new ModelState();
modelStateFooBar.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFooBar" }, "AttemptedValueFooBar");
viewDataWithErrors.ModelState["FieldPrefix"] = modelStateFooBar;
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(viewDataWithErrors);
// Act & Assert
Assert.Equal("AttemptedValueFoo", helper.Value("foo").ToHtmlString());
Assert.Equal("AttemptedValueFoo", helper.ValueFor(m => m.foo).ToHtmlString());
Assert.Equal("AttemptedValueFooBar", helper.ValueForModel().ToHtmlString());
}
[Fact]
public void ValueHelpersWithEmptyNameConvertModelValueUsingCurrentCulture()
{
// Arrange
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetValueViewData());
string expectedModelValue = "{ foo = ViewItemFoo, bar = 1900/01/01 12:00:00 AM }";
// Act & Assert
using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US"))
{
Assert.Equal(expectedModelValue, helper.Value(name: String.Empty).ToHtmlString());
Assert.Equal(expectedModelValue, helper.ValueFor(m => m).ToHtmlString());
Assert.Equal(expectedModelValue, helper.ValueForModel().ToHtmlString());
}
}
[Fact]
public void ValueHelpersFormatValue()
{
// Arrange
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetValueViewData());
string expectedModelValue = "-{ foo = ViewItemFoo, bar = 1900/01/01 12:00:00 AM }-";
string expectedBarValue = "-1900/01/01 12:00:00 AM-";
// Act & Assert
using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US"))
{
Assert.Equal(expectedModelValue, helper.ValueForModel("-{0}-").ToHtmlString());
Assert.Equal(expectedBarValue, helper.Value("bar", "-{0}-").ToHtmlString());
Assert.Equal(expectedBarValue, helper.ValueFor(m => m.bar, "-{0}-").ToHtmlString());
}
}
[Fact]
public void ValueHelpersEncodeValue()
{
// Arrange
ViewDataDictionary<FooBarModel> viewData = new ViewDataDictionary<FooBarModel> { { "foo", @"ViewDataFoo <"">" } };
viewData.Model = new FooBarModel { foo = @"ViewItemFoo <"">" };
ModelState modelStateFoo = new ModelState();
modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult(new string[] { @"AttemptedValueBar <"">" }, @"AttemptedValueBar <"">");
viewData.ModelState["bar"] = modelStateFoo;
HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(viewData);
// Act & Assert
Assert.Equal("&lt;{ foo = ViewItemFoo &lt;&quot;>, bar = (null) }", helper.ValueForModel("<{0}").ToHtmlString());
Assert.Equal("&lt;ViewDataFoo &lt;&quot;>", helper.Value("foo", "<{0}").ToHtmlString());
Assert.Equal("&lt;ViewItemFoo &lt;&quot;>", helper.ValueFor(m => m.foo, "<{0}").ToHtmlString());
Assert.Equal("AttemptedValueBar &lt;&quot;>", helper.ValueFor(m => m.bar).ToHtmlString());
}
private sealed class FooBarModel
{
public string foo { get; set; }
public object bar { get; set; }
public override string ToString()
{
return String.Format("{{ foo = {0}, bar = {1} }}", foo ?? "(null)", bar ?? "(null)");
}
}
private static ViewDataDictionary<FooBarModel> GetValueViewData()
{
ViewDataDictionary<FooBarModel> viewData = new ViewDataDictionary<FooBarModel> { { "foo", "ViewDataFoo" } };
viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = new DateTime(1900, 1, 1, 0, 0, 0) };
return viewData;
}
}
}