You've already forked linux-packaging-mono
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
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
ApplicationParts
Extensions
Helpers
Html
CheckBoxTest.cs
HtmlHelperFactory.cs
HtmlHelperTest.cs
InputHelperTest.cs
RadioButtonTest.cs
SelectHelperTest.cs
TextAreaHelperTest.cs
ValidationHelperTest.cs
Instrumentation
Mvc
Properties
ScopeStorage
TestFiles
Utils
Validation
WebPage
App.config
PreApplicationStartCodeTest.cs
System.Web.WebPages.Test.csproj
packages.config
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
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
212 lines
8.2 KiB
C#
212 lines
8.2 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.Web.WebPages.Html;
|
|||
|
using Moq;
|
|||
|
using Xunit;
|
|||
|
using Assert = Microsoft.TestCommon.AssertEx;
|
|||
|
|
|||
|
namespace System.Web.WebPages.Test
|
|||
|
{
|
|||
|
public class TextAreaExtensionsTest
|
|||
|
{
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithEmptyNameThrows()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create();
|
|||
|
|
|||
|
// Act and assert
|
|||
|
Assert.ThrowsArgument(() => helper.TextArea(null), "name", "Value cannot be null or an empty string.");
|
|||
|
|
|||
|
// Act and assert
|
|||
|
Assert.ThrowsArgument(() => helper.TextArea(String.Empty), "name", "Value cannot be null or an empty string.");
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithDefaultRowsAndCols()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create();
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo");
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>", html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithZeroRowsAndColumns()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create();
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", null, 0, 0, null);
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea id=""foo"" name=""foo""></textarea>", html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithNonZeroRowsAndColumns()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create();
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", null, 4, 10, null);
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea cols=""10"" id=""foo"" name=""foo"" rows=""4""></textarea>", html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithObjectAttributes()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
ModelStateDictionary modelState = new ModelStateDictionary();
|
|||
|
modelState.SetModelValue("foo", "foo-value");
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", new { attr = "value", cols = 6 });
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea attr=""value"" cols=""6"" id=""foo"" name=""foo"" rows=""2"">foo-value</textarea>", html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithExplicitValue()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
ModelStateDictionary modelState = new ModelStateDictionary();
|
|||
|
modelState.SetModelValue("foo", "explicit-foo-value");
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", "explicit-foo-value", new { attr = "attr-value", cols = 6 });
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea attr=""attr-value"" cols=""6"" id=""foo"" name=""foo"" rows=""2"">explicit-foo-value</textarea>",
|
|||
|
html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithDictionaryAttributes()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
ModelStateDictionary modelState = new ModelStateDictionary();
|
|||
|
modelState.SetModelValue("foo", "explicit-foo-value");
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
|
|||
|
var attributes = new Dictionary<string, object>() { { "attr", "attr-val" }, { "rows", 15 }, { "cols", 12 } };
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", attributes);
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea attr=""attr-val"" cols=""12"" id=""foo"" name=""foo"" rows=""15"">explicit-foo-value</textarea>",
|
|||
|
html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithNoValueAndObjectAttributes()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create();
|
|||
|
var attributes = new Dictionary<string, object>() { { "attr", "attr-val" }, { "rows", 15 }, { "cols", 12 } };
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", attributes);
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea attr=""attr-val"" cols=""12"" id=""foo"" name=""foo"" rows=""15""></textarea>",
|
|||
|
html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithNullValue()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
ModelStateDictionary modelState = new ModelStateDictionary();
|
|||
|
modelState.SetModelValue("foo", "explicit-foo-value");
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
|
|||
|
var attributes = new Dictionary<string, object>() { { "attr", "attr-val" }, { "rows", 15 }, { "cols", 12 } };
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", null, attributes);
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea attr=""attr-val"" cols=""12"" id=""foo"" name=""foo"" rows=""15"">explicit-foo-value</textarea>",
|
|||
|
html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithError()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
ModelStateDictionary modelState = new ModelStateDictionary();
|
|||
|
modelState.AddError("foo", "some error");
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", String.Empty);
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea class=""input-validation-error"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
|
|||
|
html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaWithErrorAndCustomCssClass()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
ModelStateDictionary modelState = new ModelStateDictionary();
|
|||
|
modelState.AddError("foo", "some error");
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", String.Empty, new { @class = "my-css" });
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea class=""input-validation-error my-css"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
|
|||
|
html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
// [Fact]
|
|||
|
// Cant test this in multi-threaded
|
|||
|
public void TextAreaWithCustomErrorClass()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
ModelStateDictionary modelState = new ModelStateDictionary();
|
|||
|
modelState.AddError("foo", "some error");
|
|||
|
HtmlHelper.ValidationInputCssClassName = "custom-input-validation-error";
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
|
|||
|
|
|||
|
// Act
|
|||
|
var html = helper.TextArea("foo", String.Empty, new { @class = "my-css" });
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea class=""custom-input-validation-error my-css"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
|
|||
|
html.ToHtmlString());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void TextAreaAddsUnobtrusiveValidationAttributes()
|
|||
|
{
|
|||
|
// Arrange
|
|||
|
const string fieldName = "name";
|
|||
|
var modelStateDictionary = new ModelStateDictionary();
|
|||
|
var validationHelper = new ValidationHelper(new Mock<HttpContextBase>().Object, modelStateDictionary);
|
|||
|
HtmlHelper helper = HtmlHelperFactory.Create(modelStateDictionary, validationHelper);
|
|||
|
|
|||
|
// Act
|
|||
|
validationHelper.RequireField(fieldName, "Please specify a valid Name.");
|
|||
|
validationHelper.Add(fieldName, Validator.StringLength(30, errorMessage: "Name cannot exceed {0} characters"));
|
|||
|
var html = helper.TextArea(fieldName, htmlAttributes: new Dictionary<string, object> { { "data-some-val", "5" } });
|
|||
|
|
|||
|
// Assert
|
|||
|
Assert.Equal(@"<textarea cols=""20"" data-some-val=""5"" data-val=""true"" data-val-length=""Name cannot exceed 30 characters"" data-val-length-max=""30"" data-val-required=""Please specify a valid Name."" id=""name"" name=""name"" rows=""2""></textarea>",
|
|||
|
html.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|