Issue #2123 - Adding AppendFormat to TagHelperContent.

This commit is contained in:
sornaks 2015-04-06 09:00:00 -07:00
parent 71b1a57617
commit 585002baf8
3 changed files with 419 additions and 1 deletions

View File

@ -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;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(string format, object arg0)
{
_buffer.Add(string.Format(format, arg0));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(string format, object arg0, object arg1)
{
_buffer.Add(string.Format(format, arg0, arg1));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2)
{
_buffer.Add(string.Format(format, arg0, arg1, arg2));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat([NotNull] string format, params object[] args)
{
_buffer.Add(string.Format(format, args));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(IFormatProvider provider, string format, object arg0)
{
_buffer.Add(string.Format(provider, format, arg0));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(
IFormatProvider provider,
string format,
object arg0,
object arg1)
{
_buffer.Add(string.Format(provider, format, arg0, arg1));
return this;
}
/// <inheritdoc />
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;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(
[NotNull] IFormatProvider provider, [NotNull] string format, params object[] args)
{
_buffer.Add(string.Format(provider, format, args));
return this;
}
/// <inheritdoc />
public override TagHelperContent Append(TagHelperContent tagHelperContent)
{

View File

@ -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
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent Append(string value);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content after
/// replacing the format item with the <see cref="string"/> representation of the
/// <paramref name="arg0"/>.
/// </summary>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="arg0">The object to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(string format, object arg0);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content after
/// replacing each format item with the <see cref="string"/> representation of the
/// <paramref name="arg0"/> and <paramref name="arg1"/>.
/// </summary>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="arg0">The object to format.</param>
/// <param name="arg1">The object to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(string format, object arg0, object arg1);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content after
/// replacing each format item with the <see cref="string"/> representation of the
/// <paramref name="arg0"/>, <paramref name="arg1"/> and <paramref name="arg2"/>.
/// </summary>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="arg0">The object to format.</param>
/// <param name="arg1">The object to format.</param>
/// <param name="arg2">The object to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content after
/// replacing each format item with the <see cref="string"/> representation of the
/// corresponding item in the <paramref name="args"/> array.
/// </summary>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="args">The object array to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(string format, params object[] args);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content with information from the
/// <paramref name="provider"/> after replacing the format item with the <see cref="string"/>
/// representation of the corresponding item in <paramref name="arg0"/>.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="arg0">The object to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(IFormatProvider provider, string format, object arg0);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content with information from the
/// <paramref name="provider"/> after replacing each format item with the <see cref="string"/>
/// representation of the corresponding item in <paramref name="arg0"/> and <paramref name="arg1"/>.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="arg0">The object to format.</param>
/// <param name="arg1">The object to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(
IFormatProvider provider,
string format,
object arg0,
object arg1);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content with information from the
/// <paramref name="provider"/> after replacing each format item with the <see cref="string"/>
/// representation of the corresponding item in <paramref name="arg0"/>, <paramref name="arg1"/>
/// and <paramref name="arg2"/>.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="arg0">The object to format.</param>
/// <param name="arg1">The object to format.</param>
/// <param name="arg2">The object to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(
IFormatProvider provider,
string format,
object arg0,
object arg1,
object arg2);
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content with information from the
/// <paramref name="provider"/> after replacing each format item with the <see cref="string"/>
/// representation of the corresponding item in the <paramref name="args"/> array.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="format">
/// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// </param>
/// <param name="args">The object array to format.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent AppendFormat(IFormatProvider provider, string format, params object[] args);
/// <summary>
/// Appends <paramref name="tagHelperContent"/> to the existing content.
/// </summary>

View File

@ -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()
{