// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Web.Mvc.Test;
using System.Web.Routing;
using Microsoft.Web.UnitTestUtil;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Html.Test
{
public class InputExtensionsTest
{
// CheckBox
[Fact]
public void CheckBoxDictionaryOverridesImplicitParameters()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBox("baz", new { @checked = "checked", value = "false" });
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxExplicitParametersOverrideDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString html = helper.CheckBox("foo", true /* isChecked */, new { @checked = "unchecked", value = "false" });
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxShouldNotCopyAttributesForHidden()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString html = helper.CheckBox("foo", true /* isChecked */, new { id = "myID" });
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithEmptyNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.CheckBox(String.Empty); },
"name");
}
[Fact]
public void CheckBoxWithInvalidBooleanThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act & Assert
Assert.Throws(
delegate { helper.CheckBox("bar"); },
"String was not recognized as a valid Boolean.");
}
[Fact]
public void CheckBoxCheckedWithOnlyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString html = helper.CheckBox("foo", true /* isChecked */);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxShouldRespectModelStateAttemptedValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
helper.ViewData.ModelState.SetModelValue("foo", HtmlHelperTest.GetValueProviderResult("false", "false"));
// Act
MvcHtmlString html = helper.CheckBox("foo");
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithOnlyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBox("foo");
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithOnlyName_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.CheckBox("foo");
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithNameAndObjectAttribute()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBox("foo", _attributesObjectDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithNameAndObjectAttributeWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBox("foo", _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithObjectAttribute()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString html = helper.CheckBox("foo", false /* isChecked */, _attributesObjectDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithObjectAttributeWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString html = helper.CheckBox("foo", false /* isChecked */, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithAttributeDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString html = helper.CheckBox("foo", false /* isChecked */, _attributesDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.CheckBox("foo", false /* isChecked */, _attributesDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.CheckBox("", false /* isChecked */, _attributesDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
// CheckBoxFor
[Fact]
public void CheckBoxForWitNullExpressionThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.CheckBoxFor(null),
"expression");
}
[Fact]
public void CheckBoxForWithInvalidBooleanThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act & Assert
Assert.Throws(
() => helper.CheckBoxFor(m => m.bar), // "bar" in ViewData isn't a valid boolean
"String was not recognized as a valid Boolean.");
}
[Fact]
public void CheckBoxForDictionaryOverridesImplicitParameters()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.baz, new { @checked = "checked", value = "false" });
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForShouldNotCopyAttributesForHidden()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo, new { id = "myID" });
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForCheckedWithOnlyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForCheckedWithOnlyName_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForShouldRespectModelStateAttemptedValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
helper.ViewContext.ViewData.ModelState.SetModelValue("foo", HtmlHelperTest.GetValueProviderResult("false", "false"));
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForWithObjectAttribute()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo, _attributesObjectDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForWithObjectAttributeWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForWithAttributeDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo, _attributesDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
[Fact]
public void CheckBoxForWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetCheckBoxViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.CheckBoxFor(m => m.foo, _attributesDictionary);
// Assert
Assert.Equal(@"" +
@"",
html.ToHtmlString());
}
// Culture tests
[Fact]
public void InputHelpersUseCurrentCultureToConvertValueParameter()
{
// Arrange
DateTime dt = new DateTime(1900, 1, 1, 0, 0, 0);
HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary { { "foo", dt } });
var tests = new[]
{
// Hidden(name)
new
{
Html = @"",
Action = new Func(() => helper.Hidden("foo"))
},
// Hidden(name, value)
new
{
Html = @"",
Action = new Func(() => helper.Hidden("foo", dt))
},
// Hidden(name, value, htmlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.Hidden("foo", dt, null))
},
// Hidden(name, value, htmlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.Hidden("foo", dt, new RouteValueDictionary()))
},
// RadioButton(name, value)
new
{
Html = @"",
Action = new Func(() => helper.RadioButton("foo", dt))
},
// RadioButton(name, value, isChecked)
new
{
Html = @"",
Action = new Func(() => helper.RadioButton("foo", dt, false))
},
// RadioButton(name, value, htmlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.RadioButton("foo", dt, null))
},
// RadioButton(name, value)
new
{
Html = @"",
Action = new Func(() => helper.RadioButton("foo", dt, new RouteValueDictionary()))
},
// RadioButton(name, value, isChecked, htmlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.RadioButton("foo", dt, false, null))
},
// RadioButton(name, value, isChecked, htmlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.RadioButton("foo", dt, false, new RouteValueDictionary()))
},
// TextBox(name)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("foo"))
},
// TextBox(name, value)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("foo", dt))
},
// TextBox(name, value, hmtlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("foo", dt, (object)null))
},
// TextBox(name, value, hmtlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("foo", dt, new RouteValueDictionary()))
}
};
// Act && Assert
using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US"))
{
foreach (var test in tests)
{
Assert.Equal(test.Html, test.Action().ToHtmlString());
}
}
}
// Hidden
[Fact]
public void HiddenWithByteArrayValueRendersBase64EncodedValue()
{
// Arrange
HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString result = htmlHelper.Hidden("ProductName", ByteArrayModelBinderTest.Base64TestBytes);
// Assert
Assert.Equal("", result.ToHtmlString());
}
[Fact]
public void HiddenWithBinaryArrayValueRendersBase64EncodedValue()
{
// Arrange
HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper();
// Act
MvcHtmlString result = htmlHelper.Hidden("ProductName", new Binary(new byte[] { 23, 43, 53 }));
// Assert
Assert.Equal("", result.ToHtmlString());
}
[Fact]
public void HiddenWithEmptyNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.Hidden(String.Empty); },
"name");
}
[Fact]
public void HiddenWithExplicitValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", "DefaultFoo", null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithExplicitValueAndAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", "DefaultFoo", _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithExplicitValueAndAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", "DefaultFoo", _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithExplicitValueAndAttributesObjectWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", "DefaultFoo", _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithExplicitValueNull()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", (string)null /* value */, (object)null /* htmlAttributes */);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithImplicitValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithImplicitValueAndAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", null, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("keyNotFound", null, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithImplicitValueAndAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", null, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithNameAndValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Hidden("foo", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.Hidden("foo", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.Hidden("", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithNullNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.Hidden(null /* name */); },
"name");
}
[Fact]
public void HiddenWithViewDataErrors()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewDataWithErrors());
// Act
MvcHtmlString html = helper.Hidden("foo", null, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenWithViewDataErrorsAndCustomClass()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewDataWithErrors());
// Act
MvcHtmlString html = helper.Hidden("foo", null, new { @class = "foo-class" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
// HiddenFor
[Fact]
public void HiddenForWithNullExpressionThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.HiddenFor(null),
"expression"
);
}
[Fact]
public void HiddenForWithStringValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewData.Model.foo = "DefaultFoo";
// Act
MvcHtmlString html = helper.HiddenFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenForWithByteArrayValueRendersBase64EncodedValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewData.Model.bytes = ByteArrayModelBinderTest.Base64TestBytes;
// Act
MvcHtmlString result = helper.HiddenFor(m => m.bytes);
// Assert
Assert.Equal("", result.ToHtmlString());
}
[Fact]
public void HiddenForWithBinaryValueRendersBase64EncodedValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewData.Model.binary = new Binary(new byte[] { 23, 43, 53 });
// Act
MvcHtmlString result = helper.HiddenFor(m => m.binary);
// Assert
Assert.Equal("", result.ToHtmlString());
}
[Fact]
public void HiddenForWithAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewData.Model.foo = "DefaultFoo";
// Act
MvcHtmlString html = helper.HiddenFor(m => m.foo, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenForWithAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewData.Model.foo = "DefaultFoo";
// Act
MvcHtmlString html = helper.HiddenFor(m => m.foo, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenForWithAttributesObjectWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewData.Model.foo = "DefaultFoo";
// Act
MvcHtmlString html = helper.HiddenFor(m => m.foo, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenForWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewData.Model.foo = "fooValue";
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.HiddenFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenForWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.HiddenFor(m => m);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenForWithViewDataErrors()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewDataWithErrors());
// Act
MvcHtmlString html = helper.HiddenFor(m => m.foo, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void HiddenForWithViewDataErrorsAndCustomClass()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewDataWithErrors());
// Act
MvcHtmlString html = helper.HiddenFor(m => m.foo, new { @class = "foo-class" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
// Password
[Fact]
public void PasswordWithEmptyNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.Password(String.Empty); },
"name");
}
[Fact]
public void PasswordDictionaryOverridesImplicitParameters()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", "Some Value", new { type = "fooType" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordExplicitParametersOverrideDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", "Some Value", new { value = "Another Value", name = "bar" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithExplicitValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", "DefaultFoo", (object)null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithExplicitValue_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.Password("foo", "DefaultFoo", (object)null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithExplicitValueAndAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", "DefaultFoo", _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithExplicitValueAndAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", "DefaultFoo", _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithExplicitValueNull()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", (string)null /* value */, (object)null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithImplicitValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithImplicitValueAndAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", null, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("keyNotFound", null, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithImplicitValueAndAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", null, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithImplicitValueAndAttributesObjectWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.Password("foo", null, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithNameAndValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.Password("foo", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.Password("foo", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.Password("", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithNullNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.Password(null /* name */); },
"name");
}
[Fact]
public void PasswordWithViewDataErrors()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewDataWithErrors());
// Act
MvcHtmlString html = helper.Password("foo", null, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordWithViewDataErrorsAndCustomClass()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewDataWithErrors());
// Act
MvcHtmlString html = helper.Password("foo", null, new { @class = "foo-class" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
// PasswordFor
[Fact]
public void PasswordForWithNullExpressionThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.PasswordFor(null),
"expression");
}
[Fact]
public void PasswordForDictionaryOverridesImplicitParameters()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo, new { type = "fooType" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForExpressionNameOverridesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo, new { name = "bar" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithImplicitValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithImplicitValue_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithDeepValueWithNullModel_Unobtrusive()
{ // Dev10 Bug #936192
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
// Act
MvcHtmlString html = helper.PasswordFor(m => m.contained.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithAttributesObjectWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithViewDataErrors()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewDataWithErrors());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void PasswordForWithViewDataErrorsAndCustomClass()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetPasswordViewDataWithErrors());
// Act
MvcHtmlString html = helper.PasswordFor(m => m.foo, new { @class = "foo-class" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
// RadioButton
[Fact]
public void RadioButtonDictionaryOverridesImplicitParameters()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("bar", "ViewDataBar", new { @checked = "chucked", value = "baz" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonExplicitParametersOverrideDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("bar", "ViewDataBar", false, new { @checked = "checked", value = "baz" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonShouldRespectModelStateAttemptedValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
helper.ViewData.ModelState.SetModelValue("foo", HtmlHelperTest.GetValueProviderResult("ModelStateFoo", "ModelStateFoo"));
// Act
MvcHtmlString html = helper.RadioButton("foo", "ModelStateFoo", false, new { @checked = "checked", value = "baz" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonValueParameterAlwaysRendered()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "ViewDataFoo");
MvcHtmlString html2 = helper.RadioButton("foo", "fooValue2");
// Assert
Assert.Equal(@"", html.ToHtmlString());
Assert.Equal(@"", html2.ToHtmlString());
}
[Fact]
public void RadioButtonWithEmptyNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.RadioButton(String.Empty, "value"); },
"name");
}
[Fact]
public void RadioButtonWithNullValueThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper();
// Act & Assert
Assert.ThrowsArgumentNull(
delegate { helper.RadioButton("foo", null); },
"value");
}
[Fact]
public void RadioButtonWithNameAndValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "ViewDataFoo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithNameAndValue_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.RadioButton("foo", "ViewDataFoo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.RadioButton("foo", "ViewDataFoo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.RadioButton("", "ViewDataFoo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithNameAndValueNotMatched()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithNameValueUnchecked()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("bar", "barValue", false /* isChecked */);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithNameValueChecked()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("bar", "barValue", true /* isChecked */);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithObjectAttribute()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "fooValue", _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithObjectAttributeWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "fooValue", _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithAttributeDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("bar", "barValue", _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithValueUnchecked()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "bar", false /* isChecked */);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithValueAndObjectAttributeUnchecked()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "bar", false /* isChecked */, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithValueAndObjectAttributeWithUnderscoresUnchecked()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "bar", false /* isChecked */, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonWithValueAndAttributeDictionaryUnchecked()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButton("foo", "bar", false /* isChecked */, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
// RadioButtonFor
[Fact]
public void RadioButtonForWithNullExpressionThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.RadioButtonFor(null, "value"),
"expression");
}
[Fact]
public void RadioButtonForWithNullValueThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.RadioButtonFor(m => m.foo, null),
"value");
}
[Fact]
public void RadioButtonForDictionaryOverridesImplicitParameters()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.bar, "ViewDataBar", new { @checked = "chucked", value = "baz" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForShouldRespectModelStateAttemptedValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
helper.ViewData.ModelState.SetModelValue("foo", HtmlHelperTest.GetValueProviderResult("ModelStateFoo", "ModelStateFoo"));
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.foo, "ModelStateFoo", new { @checked = "checked", value = "baz" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForValueParameterAlwaysRendered()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act & Assert
Assert.Equal(@"",
helper.RadioButtonFor(m => m.foo, "ViewDataFoo").ToHtmlString());
Assert.Equal(@"",
helper.RadioButtonFor(m => m.foo, "fooValue2").ToHtmlString());
}
[Fact]
public void RadioButtonForWithNameAndValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.foo, "ViewDataFoo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForWithNameAndValue_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.foo, "ViewDataFoo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.foo, "ViewDataFoo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForWithNameAndValueNotMatched()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.foo, "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForWithObjectAttribute()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.foo, "fooValue", _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForWithObjectAttributeWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.foo, "fooValue", _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void RadioButtonForWithAttributeDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetRadioButtonViewData());
// Act
MvcHtmlString html = helper.RadioButtonFor(m => m.bar, "barValue", _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
// TextBox
[Fact]
public void TextBoxDictionaryOverridesImplicitValues()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", "DefaultFoo", new { type = "fooType" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxExplicitParametersOverrideDictionaryValues()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", "DefaultFoo", new { value = "Some other value" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithDotReplacementForId()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo.bar.baz", null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithEmptyNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.TextBox(String.Empty); },
"name");
}
[Fact]
public void TextBoxWithExplicitValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", "DefaultFoo", (object)null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithExplicitValue_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.TextBox("foo", "DefaultFoo", (object)null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithExplicitValueAndAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", "DefaultFoo", _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithExplicitValueAndAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", "DefaultFoo", _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithExplicitValueNull()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", (string)null /* value */, (object)null);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithImplicitValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithImplicitValueAndAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", null, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("keyNotFound", null, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithImplicitValueAndAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", null, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithImplicitValueAndAttributesObjectWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", null, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithNullNameThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { helper.TextBox(null /* name */); },
"name");
}
[Fact]
public void TextBoxWithNameAndValue()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
// Act
MvcHtmlString html = helper.TextBox("foo", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.TextBox("foo", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetHiddenViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.TextBox("", "fooValue");
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithViewDataErrors()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewDataWithErrors());
// Act
MvcHtmlString html = helper.TextBox("foo", null, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxWithViewDataErrorsAndCustomClass()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewDataWithErrors());
// Act
MvcHtmlString html = helper.TextBox("foo", null, new { @class = "foo-class" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
// TextBoxFor
[Fact]
public void TextBoxForWithNullExpressionThrows()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act & Assert
Assert.ThrowsArgumentNull(
() => helper.TextBoxFor(null /* expression */),
"expression"
);
}
[Fact]
public void TextBoxForWithSimpleExpression()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithSimpleExpression_Unobtrusive()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
helper.ViewContext.ClientValidationEnabled = true;
helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
helper.ViewContext.FormContext = new FormContext();
helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithAttributesDictionary()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo, _attributesDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithAttributesObject()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithAttributesObjectWithUnderscores()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo, _attributesObjectUnderscoresDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithPrefix()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithPrefixAndEmptyName()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewData());
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
// Act
MvcHtmlString html = helper.TextBoxFor(m => m);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithErrors()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewDataWithErrors());
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo, _attributesObjectDictionary);
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxForWithErrorsAndCustomClass()
{
// Arrange
HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextBoxViewDataWithErrors());
// Act
MvcHtmlString html = helper.TextBoxFor(m => m.foo, new { @class = "foo-class" });
// Assert
Assert.Equal(@"", html.ToHtmlString());
}
[Fact]
public void TextBoxHelpersFormatValue()
{
// Arrange
DateTime dt = new DateTime(1900, 1, 1, 0, 0, 0);
HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary { { "viewDataDate", dt } });
ViewDataDictionary viewData = new ViewDataDictionary() { Model = new DateModel { date = dt } };
HtmlHelper dateModelhelper = MvcHelper.GetHtmlHelper(viewData);
var tests = new[]
{
// TextBox(name, value, format)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("viewDataDate", null, "-{0}-"))
},
// TextBox(name, value, format)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("date", dt, "-{0}-"))
},
// TextBox(name, value, format, hmtlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("date", dt, "-{0}-", (object)null))
},
// TextBox(name, value, format, hmtlAttributes)
new
{
Html = @"",
Action = new Func(() => helper.TextBox("date", dt, "-{0}-", new RouteValueDictionary()))
},
// TextBoxFor(expression, format)
new
{
Html = @"",
Action = new Func(() => dateModelhelper.TextBoxFor(m => m.date, "-{0}-"))
},
// TextBoxFor(expression, format, hmtlAttributes)
new
{
Html = @"",
Action = new Func(() => dateModelhelper.TextBoxFor(m => m.date, "-{0}-", (object)null))
},
// TextBoxFor(expression, format, hmtlAttributes)
new
{
Html = @"",
Action = new Func(() => dateModelhelper.TextBoxFor(m => m.date, "-{0}-", new RouteValueDictionary()))
}
};
// Act && Assert
using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US"))
{
foreach (var test in tests)
{
Assert.Equal(test.Html, test.Action().ToHtmlString());
}
}
}
// MODELS
private class FooModel
{
public string foo { get; set; }
public override string ToString()
{
return String.Format("{{ foo = {0} }}", foo ?? "(null)");
}
}
private class FooBarModel : FooModel
{
public string bar { get; set; }
public override string ToString()
{
return String.Format("{{ foo = {0}, bar = {1} }}", foo ?? "(null)", bar ?? "(null)");
}
}
private class FooBarBazModel
{
public bool foo { get; set; }
public bool bar { get; set; }
public bool baz { get; set; }
public override string ToString()
{
return String.Format("{{ foo = {0}, bar = {1}, baz = {2} }}", foo, bar, baz);
}
}
private class ShallowModel
{
[Required]
public string foo { get; set; }
}
private class DeepContainerModel
{
public ShallowModel contained { get; set; }
}
private class HiddenModel : FooModel
{
public byte[] bytes { get; set; }
public Binary binary { get; set; }
}
private class DateModel
{
public DateTime date { get; set; }
}
// CHECKBOX
private static ViewDataDictionary GetCheckBoxViewData()
{
ViewDataDictionary viewData = new ViewDataDictionary { { "foo", true }, { "bar", "NotTrue" }, { "baz", false } };
return viewData;
}
// HIDDEN
private static ViewDataDictionary GetHiddenViewData()
{
return new ViewDataDictionary(new HiddenModel()) { { "foo", "ViewDataFoo" } };
}
private static ViewDataDictionary GetHiddenViewDataWithErrors()
{
ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } };
viewData.Model = new HiddenModel();
ModelState modelStateFoo = new ModelState();
modelStateFoo.Errors.Add(new ModelError("foo error 1"));
modelStateFoo.Errors.Add(new ModelError("foo error 2"));
viewData.ModelState["foo"] = modelStateFoo;
modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult("AttemptedValueFoo", "AttemptedValueFoo");
return viewData;
}
// PASSWORD
private static ViewDataDictionary GetPasswordViewData()
{
return new ViewDataDictionary { { "foo", "ViewDataFoo" } };
}
private static ViewDataDictionary GetPasswordViewDataWithErrors()
{
ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } };
ModelState modelStateFoo = new ModelState();
modelStateFoo.Errors.Add(new ModelError("foo error 1"));
modelStateFoo.Errors.Add(new ModelError("foo error 2"));
viewData.ModelState["foo"] = modelStateFoo;
modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult("AttemptedValueFoo", "AttemptedValueFoo");
return viewData;
}
// RADIO
private static ViewDataDictionary GetRadioButtonViewData()
{
ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } };
viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = "ViewItemBar" };
ModelState modelState = new ModelState();
modelState.Value = HtmlHelperTest.GetValueProviderResult("ViewDataFoo", "ViewDataFoo");
viewData.ModelState["foo"] = modelState;
return viewData;
}
// TEXTBOX
private static readonly RouteValueDictionary _attributesDictionary = new RouteValueDictionary(new { baz = "BazValue" });
private static readonly object _attributesObjectDictionary = new { baz = "BazObjValue" };
private static readonly object _attributesObjectUnderscoresDictionary = new { foo_baz = "BazObjValue" };
private static ViewDataDictionary GetTextBoxViewData()
{
ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } };
viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = "ViewItemBar" };
return viewData;
}
private static ViewDataDictionary GetTextBoxViewDataWithErrors()
{
ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } };
viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = "ViewItemBar" };
ModelState modelStateFoo = new ModelState();
modelStateFoo.Errors.Add(new ModelError("foo error 1"));
modelStateFoo.Errors.Add(new ModelError("foo error 2"));
viewData.ModelState["foo"] = modelStateFoo;
modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFoo" }, "AttemptedValueFoo");
return viewData;
}
}
}