// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Testing; using Xunit; namespace Microsoft.AspNetCore.Mvc.Core { /// /// Test the RadioButton extensions in class. /// public class HtmlHelperRadioButtonExtensionsTest { [Fact] public void RadioButton_UsesSpecifiedExpression() { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(); // Act var radioButtonResult = helper.RadioButton("Property1", value: "myvalue"); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonResult)); } [Fact] public void RadioButtonFor_UsesSpecifiedExpression() { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(); // Act var radioButtonForResult = helper.RadioButtonFor(m => m.Property1, value: "myvalue"); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonForResult)); } [Theory] [InlineData("MyValue")] [InlineData("myvalue")] public void RadioButton_CheckedWhenValueMatchesSpecifiedExpression(string value) { // Arrange var metadataProvider = new EmptyModelMetadataProvider(); var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); helper.ViewContext.ClientValidationEnabled = false; helper.ViewData.Model = new TestModel { Property1 = value }; // Act var radioButtonResult = helper.RadioButton("Property1", value: "myvalue"); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonResult)); } [Theory] [InlineData("MyValue")] [InlineData("myvalue")] public void RadioButtonFor_CheckedWhenValueMatchesSpecifiedExpression(string value) { // Arrange var metadataProvider = new EmptyModelMetadataProvider(); var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); helper.ViewContext.ClientValidationEnabled = false; helper.ViewData.Model = new TestModel { Property1 = value }; // Act var radioButtonForResult = helper.RadioButtonFor(m => m.Property1, value: "myvalue"); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonForResult)); } [Fact] public void RadioButtonHelpers_UsesSpecifiedIsChecked() { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(); // Act var radioButtonResult = helper.RadioButton("Property1", value: "myvalue", isChecked: true); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonResult)); } [Fact] public void RadioButtonHelpers_UsesSpecifiedIsCheckedRegardlessOfValue() { // Arrange var metadataProvider = new EmptyModelMetadataProvider(); var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); helper.ViewContext.ClientValidationEnabled = false; helper.ViewData.Model = new TestModel { Property2 = true }; // Act var radioButtonResult = helper.RadioButton("Property2", value: "myvalue", isChecked: false); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonResult)); } [Fact] public void RadioButton_UsesSpecifiedHtmlAttributes() { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(); var htmlAttributes = new { attr = "value", name = "-expression-", // overridden }; // Act var radioButtonResult = helper.RadioButton("Property1", "myvalue", htmlAttributes); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonResult)); } [Fact] public void RadioButtonFor_UsesSpecifiedHtmlAttributes() { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(); var htmlAttributes = new { attr = "value", name = "-expression-", // overridden }; // Act var radioButtonForResult = helper.RadioButtonFor(m => m.Property1, "myvalue", htmlAttributes); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonForResult)); } [Fact] public void RadioButtonFor_Throws_IfFullNameEmpty() { // Arrange var expectedMessage = "The name of an HTML field cannot be null or empty. Instead use methods " + "Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNetCore.Mvc.Rendering." + "IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value."; var helper = DefaultTemplatesUtilities.GetHtmlHelper("anotherValue"); var htmlAttributes = new { attr = "value", }; // Act & Assert ExceptionAssert.ThrowsArgument( () => helper.RadioButtonFor(m => m, "myvalue", htmlAttributes), paramName: "expression", exceptionMessage: expectedMessage); } [Fact] public void RadioButtonFor_DoesNotThrow_IfFullNameEmpty_WithNameAttribute() { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper("anotherValue"); var htmlAttributes = new { attr = "value", name = "-expression-", }; // Act var radioButtonForResult = helper.RadioButtonFor(m => m, "myvalue", htmlAttributes); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(radioButtonForResult)); } private class TestModel { public string Property1 { get; set; } public bool Property2 { get; set; } } } }