From 7e166295bac6f4f2d008c9c1db7a82bfca4ade0b Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Tue, 10 Feb 2015 19:01:30 -0800 Subject: [PATCH] Simplify `", - nameof(Multiple).ToLowerInvariant(), - Multiple, - bool.FalseString.ToLowerInvariant(), - bool.TrueString.ToLowerInvariant(), - nameof(Multiple).ToLowerInvariant())); // acceptable value (as well as attribute name) - } // Ensure GenerateSelect() _never_ looks anything up in ViewData. var items = Items ?? Enumerable.Empty(); diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs index ff8036dbd0..cb14e38953 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs @@ -87,9 +87,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers } } - // Items value, Multiple value, expected items value (passed to generator), expected allowMultiple. - // Provides cross product of Items and Multiple values. These attribute values should not interact. - public static TheoryData, string, IEnumerable, bool> + // Items property value, attribute name, attribute value, expected items value (passed to generator). Provides + // cross product of Items and attributes. These values should not interact. + public static TheoryData, string, string, IEnumerable> ItemsAndMultipleDataSet { get @@ -106,24 +106,34 @@ namespace Microsoft.AspNet.Mvc.TagHelpers new[] { multiItems, multiItems }, new[] { selectItems, selectItems }, }; - var mutlipleData = new[] + var attributeData = new Dictionary(StringComparer.Ordinal) { - new Tuple(null, false), // allowMultiple determined by string datatype. - new Tuple("", false), // allowMultiple determined by string datatype. - new Tuple("true", true), - new Tuple("false", false), - new Tuple("multiple", true), - new Tuple("Multiple", true), - new Tuple("MULTIPLE", true), + // SelectTagHelper ignores all "multiple" attribute values. + { "multiple", null }, + { "mUltiple", string.Empty }, + { "muLtiple", "true" }, + { "Multiple", "false" }, + { "MUltiple", "multiple" }, + { "MuLtiple", "Multiple" }, + { "mUlTiPlE", "mUlTiPlE" }, + { "mULTiPlE", "MULTIPLE" }, + { "mUlTIPlE", "Invalid" }, + { "MULTiPLE", "0" }, + { "MUlTIPLE", "1" }, + { "MULTIPLE", "__true" }, + // SelectTagHelper also ignores non-"multiple" attributes. + { "multiple_", "multiple" }, + { "not-multiple", "multiple" }, + { "__multiple", "multiple" }, }; var theoryData = - new TheoryData, string, IEnumerable, bool>(); + new TheoryData, string, string, IEnumerable>(); foreach (var items in itemsData) { - foreach (var multiples in mutlipleData) + foreach (var attribute in attributeData) { - theoryData.Add(items[0], multiples.Item1, items[1], multiples.Item2); + theoryData.Add(items[0], attribute.Key, attribute.Value, items[1]); } } @@ -315,19 +325,22 @@ namespace Microsoft.AspNet.Mvc.TagHelpers [Theory] [MemberData(nameof(ItemsAndMultipleDataSet))] - public async Task ProcessAsync_CallsGeneratorWithExpectedValues_ItemsAndMultiple( + public async Task ProcessAsync_CallsGeneratorWithExpectedValues_ItemsAndAttribute( IEnumerable inputItems, - string multiple, - IEnumerable expectedItems, - bool expectedAllowMultiple) + string attributeName, + string attributeValue, + IEnumerable expectedItems) { // Arrange var contextAttributes = new Dictionary { - // Attribute will be restored if value matches "multiple". - { "multiple", multiple }, + // Provided for completeness. Select tag helper does not confirm AllAttributes set is consistent. + { attributeName, attributeValue }, + }; + var originalAttributes = new Dictionary + { + { attributeName, attributeValue }, }; - var originalAttributes = new Dictionary(); var propertyName = "Property1"; var expectedTagName = "select"; @@ -356,7 +369,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, // optionLabel string.Empty, // name expectedItems, - expectedAllowMultiple, + false, // allowMultiple null, // htmlAttributes out selectedValues)) .Returns((TagBuilder)null) @@ -367,7 +380,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers For = modelExpression, Items = inputItems, Generator = htmlGenerator.Object, - Multiple = multiple, ViewContext = viewContext, }; @@ -443,58 +455,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Assert.Same(selectedValues, keyValuePair.Value); } - [Theory] - [InlineData("multiple")] - [InlineData("mUlTiPlE")] - [InlineData("MULTIPLE")] - public async Task ProcessAsync_RestoresMultiple_IfForNotBound(string attributeName) - { - // Arrange - var contextAttributes = new Dictionary - { - { attributeName, "I'm more than one" }, - }; - var originalAttributes = new Dictionary - { - { "class", "form-control" }, - { "size", "2" }, - }; - var expectedAttributes = new Dictionary(originalAttributes); - expectedAttributes[attributeName] = (string)contextAttributes[attributeName]; - var expectedPreContent = "original pre-content"; - var expectedContent = "original content"; - var expectedPostContent = "original post-content"; - var expectedTagName = "select"; - - var tagHelperContext = new TagHelperContext( - contextAttributes, - uniqueId: "test", - getChildContentAsync: () => Task.FromResult("Something")); - var output = new TagHelperOutput(expectedTagName, originalAttributes) - { - PreContent = expectedPreContent, - Content = expectedContent, - PostContent = expectedPostContent, - SelfClosing = true, - }; - - var tagHelper = new SelectTagHelper - { - Multiple = "I'm more than one", - }; - - // Act - await tagHelper.ProcessAsync(tagHelperContext, output); - - // Assert - Assert.Equal(expectedAttributes, output.Attributes); - Assert.Equal(expectedPreContent, output.PreContent); - Assert.Equal(expectedContent, output.Content); - Assert.Equal(expectedPostContent, output.PostContent); - Assert.True(output.SelfClosing); - Assert.Equal(expectedTagName, output.TagName); - } - [Fact] public async Task ProcessAsync_Throws_IfForNotBoundButItemsIs() { @@ -520,46 +480,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Assert.Equal(expectedMessage, exception.Message); } - [Theory] - [InlineData("Invalid")] - [InlineData("0")] - [InlineData("1")] - [InlineData("__true")] - [InlineData("false__")] - [InlineData("__Multiple")] - [InlineData("Multiple__")] - public async Task ProcessAsync_Throws_IfMultipleInvalid(string multiple) - { - // Arrange - var contextAttributes = new Dictionary(); - var originalAttributes = new Dictionary(); - var expectedTagName = "select"; - var expectedMessage = "Cannot parse 'multiple' value '" + multiple + - "' for