From 585002baf8b6ef5239f66257ba565cdf71ed3b4d Mon Sep 17 00:00:00 2001 From: sornaks Date: Mon, 6 Apr 2015 09:00:00 -0700 Subject: [PATCH] Issue #2123 - Adding AppendFormat to TagHelperContent. --- .../TagHelpers/DefaultTagHelperContent.cs | 67 +++++ .../TagHelpers/TagHelperContent.cs | 117 +++++++++ .../TagHelpers/DefaultTagHelperContentTest.cs | 236 +++++++++++++++++- 3 files changed, 419 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs index 5d776a4474..5883f38039 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.IO; using Microsoft.Framework.Internal; @@ -89,6 +90,72 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers return this; } + /// + public override TagHelperContent AppendFormat(string format, object arg0) + { + _buffer.Add(string.Format(format, arg0)); + return this; + } + + /// + public override TagHelperContent AppendFormat(string format, object arg0, object arg1) + { + _buffer.Add(string.Format(format, arg0, arg1)); + return this; + } + + /// + public override TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2) + { + _buffer.Add(string.Format(format, arg0, arg1, arg2)); + return this; + } + + /// + public override TagHelperContent AppendFormat([NotNull] string format, params object[] args) + { + _buffer.Add(string.Format(format, args)); + return this; + } + + /// + public override TagHelperContent AppendFormat(IFormatProvider provider, string format, object arg0) + { + _buffer.Add(string.Format(provider, format, arg0)); + return this; + } + + /// + public override TagHelperContent AppendFormat( + IFormatProvider provider, + string format, + object arg0, + object arg1) + { + _buffer.Add(string.Format(provider, format, arg0, arg1)); + return this; + } + + /// + public override TagHelperContent AppendFormat( + IFormatProvider provider, + string format, + object arg0, + object arg1, + object arg2) + { + _buffer.Add(string.Format(provider, format, arg0, arg1, arg2)); + return this; + } + + /// + public override TagHelperContent AppendFormat( + [NotNull] IFormatProvider provider, [NotNull] string format, params object[] args) + { + _buffer.Add(string.Format(provider, format, args)); + return this; + } + /// public override TagHelperContent Append(TagHelperContent tagHelperContent) { diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs index 6cd36c3306..740e991ffb 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections; using System.Collections.Generic; @@ -47,6 +48,122 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// A reference to this instance after the append operation has completed. public abstract TagHelperContent Append(string value); + /// + /// Appends the specified to the existing content after + /// replacing the format item with the representation of the + /// . + /// + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat(string format, object arg0); + + /// + /// Appends the specified to the existing content after + /// replacing each format item with the representation of the + /// and . + /// + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object to format. + /// The object to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat(string format, object arg0, object arg1); + + /// + /// Appends the specified to the existing content after + /// replacing each format item with the representation of the + /// , and . + /// + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object to format. + /// The object to format. + /// The object to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2); + + /// + /// Appends the specified to the existing content after + /// replacing each format item with the representation of the + /// corresponding item in the array. + /// + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object array to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat(string format, params object[] args); + + /// + /// Appends the specified to the existing content with information from the + /// after replacing the format item with the + /// representation of the corresponding item in . + /// + /// An object that supplies culture-specific formatting information. + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat(IFormatProvider provider, string format, object arg0); + + /// + /// Appends the specified to the existing content with information from the + /// after replacing each format item with the + /// representation of the corresponding item in and . + /// + /// An object that supplies culture-specific formatting information. + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object to format. + /// The object to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat( + IFormatProvider provider, + string format, + object arg0, + object arg1); + + /// + /// Appends the specified to the existing content with information from the + /// after replacing each format item with the + /// representation of the corresponding item in , + /// and . + /// + /// An object that supplies culture-specific formatting information. + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object to format. + /// The object to format. + /// The object to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat( + IFormatProvider provider, + string format, + object arg0, + object arg1, + object arg2); + + /// + /// Appends the specified to the existing content with information from the + /// after replacing each format item with the + /// representation of the corresponding item in the array. + /// + /// An object that supplies culture-specific formatting information. + /// + /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). + /// + /// The object array to format. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent AppendFormat(IFormatProvider provider, string format, params object[] args); + /// /// Appends to the existing content. /// diff --git a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs index 0b0d09bc85..8cce5976a3 100644 --- a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs +++ b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs @@ -2,8 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.IO; +using System.Globalization; using System.Linq; +using Microsoft.AspNet.Testing; using Xunit; namespace Microsoft.AspNet.Razor.Runtime.TagHelpers @@ -86,6 +87,200 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers Assert.Equal(expected, tagHelperContent.GetContent()); } + // Overload with args array is called. + [Fact] + public void CanAppendFormatContent() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat("{0} {1} {2} {3}!", "First", "Second", "Third", "Fourth"); + + // Assert + Assert.Equal("First Second Third Fourth!", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormatContent_With1Argument() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat("0x{0, -5:X} - hex equivalent for 50.", 50); + + // Assert + Assert.Equal("0x32 - hex equivalent for 50.", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormatContent_With2Arguments() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat("0x{0, -5:X} - hex equivalent for {1}.", 50, 50); + + // Assert + Assert.Equal("0x32 - hex equivalent for 50.", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormatContent_With3Arguments() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat("0x{0, -5:X} - {1} equivalent for {2}.", 50, "hex", 50); + + // Assert + Assert.Equal("0x32 - hex equivalent for 50.", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormat_WithAlignmentComponent() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat("{0, -10} World!", "Hello"); + + // Assert + Assert.Equal("Hello World!", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormat_WithFormatStringComponent() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat("0x{0:X}", 50); + + // Assert + Assert.Equal("0x32", tagHelperContent.GetContent()); + } + + // Overload with args array is called. + [Fact] + public void CanAppendFormat_WithCulture() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat( + CultureInfo.InvariantCulture, + "Numbers in InvariantCulture - {0, -5:N} {1} {2} {3}!", + 1.1, + 2.98, + 145.82, + 32.86); + + // Assert + Assert.Equal("Numbers in InvariantCulture - 1.10 2.98 145.82 32.86!", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormat_WithCulture_1Argument() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat( + CultureInfo.InvariantCulture, + "Numbers in InvariantCulture - {0, -5:N}!", + 1.1); + + // Assert + Assert.Equal("Numbers in InvariantCulture - 1.10 !", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormat_WithCulture_2Arguments() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat( + CultureInfo.InvariantCulture, + "Numbers in InvariantCulture - {0, -5:N} {1}!", + 1.1, + 2.98); + + // Assert + Assert.Equal("Numbers in InvariantCulture - 1.10 2.98!", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormat_WithCulture_3Arguments() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat( + CultureInfo.InvariantCulture, + "Numbers in InvariantCulture - {0, -5:N} {1} {2}!", + 1.1, + 2.98, + 3.12); + + // Assert + Assert.Equal("Numbers in InvariantCulture - 1.10 2.98 3.12!", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendFormat_WithDifferentCulture() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + var culture = new CultureInfo("fr-FR"); + + // Act + tagHelperContent.AppendFormat(culture, "{0} in french!", 1.21); + + // Assert + Assert.Equal("1,21 in french!", tagHelperContent.GetContent()); + } + + [Fact] + [ReplaceCulture] + public void CanAppendFormat_WithDifferentCurrentCulture() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + + // Act + tagHelperContent.AppendFormat(CultureInfo.CurrentCulture, "{0:D}", DateTime.Parse("01/02/2015")); + + // Assert + Assert.Equal("01 February 2015", tagHelperContent.GetContent()); + } + + [Fact] + public void CanAppendDefaultTagHelperContent() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + var helloWorldContent = new DefaultTagHelperContent(); + helloWorldContent.SetContent("HelloWorld"); + var expected = "Content was HelloWorld"; + + // Act + tagHelperContent.AppendFormat("Content was {0}", helloWorldContent); + + // Assert + Assert.Equal(expected, tagHelperContent.GetContent()); + } + [Fact] public void Append_WithTagHelperContent_MultipleAppends() { @@ -359,6 +554,45 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers Assert.Equal(expected, tagHelperContent.GetContent()); } + [Fact] + public void Fluent_SetContent_AppendFormat_WritesExpectedContent() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + var expected = new[] { "First ", "Second Third" }; + var i = 0; + + // Act + tagHelperContent.SetContent("First ").AppendFormat("{0} Third", "Second"); + + // Assert + foreach (var value in tagHelperContent) + { + Assert.Equal(expected[i++], value); + } + } + + [Fact] + public void Fluent_SetContent_AppendFormat_Append_WritesExpectedContent() + { + // Arrange + var tagHelperContent = new DefaultTagHelperContent(); + var expected = new[] { "First ", "Second Third ", "Fourth" }; + var i = 0; + + // Act + tagHelperContent + .SetContent("First ") + .AppendFormat("{0} Third ", "Second") + .Append("Fourth"); + + // Assert + foreach (var value in tagHelperContent) + { + Assert.Equal(expected[i++], value); + } + } + [Fact] public void Fluent_Clear_SetContent_WritesExpectedContent() {