Fix #526 - use extension methods for AppendFormat

These changes make TagHelperContent with the IHtmlBuilder pattern, but
retain the signatures returning TagHelperContent.

Removed a bunch of tests that are no redundant with tests for the
extension methods in HttpAbstractions. Kept a few explemars in place to
ensure that the basic plumbing functions as desired.
This commit is contained in:
Ryan Nowak 2015-09-27 22:47:23 -07:00
parent e87f10e8f8
commit 9a41bafea7
3 changed files with 75 additions and 413 deletions

View File

@ -133,137 +133,6 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(string format, object arg0)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(format, arg0));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(string format, object arg0, object arg1)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(format, arg0, arg1));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(format, arg0, arg1, arg2));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(string format, params object[] args)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(format, args));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(
IFormatProvider provider,
string format,
object arg0)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(provider, format, arg0));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(
IFormatProvider provider,
string format,
object arg0,
object arg1)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(provider, format, arg0, arg1));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(
IFormatProvider provider,
string format,
object arg0,
object arg1,
object arg2)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(provider, format, arg0, arg1, arg2));
return this;
}
/// <inheritdoc />
public override TagHelperContent AppendFormat(
IFormatProvider provider,
string format,
params object[] args)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
Buffer.Append(string.Format(provider, format, args));
return this;
}
/// <inheritdoc />
public override TagHelperContent Append(IHtmlContent htmlContent)
{

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
/// <summary>
/// Abstract class used to buffer content returned by <see cref="ITagHelper"/>s.
/// </summary>
public abstract class TagHelperContent : IHtmlContent
public abstract class TagHelperContent : IHtmlContentBuilder
{
/// <summary>
/// Gets a value indicating whether the content was modifed.
@ -31,24 +31,39 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
/// <summary>
/// Sets the content.
/// </summary>
/// <param name="value">The <see cref="string"/> that replaces the content.</param>
/// <param name="htmlContent">The <see cref="IHtmlContent"/> that replaces the content.</param>
/// <returns>A reference to this instance after the set operation has completed.</returns>
public TagHelperContent SetContent(string value)
public TagHelperContent SetContent(IHtmlContent htmlContent)
{
Clear();
Append(value);
HtmlContentBuilderExtensions.SetContent(this, htmlContent);
return this;
}
/// <summary>
/// Sets the content.
/// </summary>
/// <param name="htmlContent">The <see cref="IHtmlContent"/> that replaces the content.</param>
/// <param name="unencoded">
/// The <see cref="string"/> that replaces the content. The value is assume to be unencoded
/// as-provided and will be HTML encoded before being written.
/// </param>
/// <returns>A reference to this instance after the set operation has completed.</returns>
public TagHelperContent SetContent(IHtmlContent htmlContent)
public TagHelperContent SetContent(string unencoded)
{
Clear();
Append(htmlContent);
HtmlContentBuilderExtensions.SetContent(this, unencoded);
return this;
}
/// <summary>
/// Sets the content.
/// </summary>
/// <param name="encoded">
/// The <see cref="string"/> that replaces the content. The value is assume to be HTML encoded
/// as-provided and no further encoding will be performed.
/// </param>
/// <returns>A reference to this instance after the set operation has completed.</returns>
public TagHelperContent SetContentEncoded(string encoded)
{
HtmlContentBuilderExtensions.SetContentEncoded(this, encoded);
return this;
}
@ -59,6 +74,13 @@ 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 <paramref name="htmlContent"/> to the existing content.
/// </summary>
/// <param name="htmlContent">The <see cref="IHtmlContent"/> to be appended.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent Append(IHtmlContent htmlContent);
/// <summary>
/// Appends <paramref name="value"/> to the existing content. <paramref name="value"/> is assumed
/// to be an HTML encoded <see cref="string"/> and no further encoding will be performed.
@ -69,46 +91,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
/// <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
/// replacing each format item with the HTML encoded <see cref="string"/> representation of the
/// corresponding item in the <paramref name="args"/> array.
/// </summary>
/// <param name="format">
@ -116,63 +99,15 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
/// </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);
public TagHelperContent AppendFormat(string format, params object[] args)
{
HtmlContentBuilderExtensions.AppendFormat(this, null, format, args);
return this;
}
/// <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"/>
/// <paramref name="provider"/> after replacing each format item with the HTML encoded <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>
@ -181,14 +116,11 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
/// </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="htmlContent"/> to the existing content.
/// </summary>
/// <param name="htmlContent">The <see cref="IHtmlContent"/> to be appended.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public abstract TagHelperContent Append(IHtmlContent htmlContent);
public TagHelperContent AppendFormat(IFormatProvider provider, string format, params object[] args)
{
HtmlContentBuilderExtensions.AppendFormat(this, provider, format, args);
return this;
}
/// <summary>
/// Clears the content.
@ -211,5 +143,29 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
/// <inheritdoc />
public abstract void WriteTo(TextWriter writer, IHtmlEncoder encoder);
/// <inheritdoc />
IHtmlContentBuilder IHtmlContentBuilder.Append(IHtmlContent content)
{
return Append(content);
}
/// <inheritdoc />
IHtmlContentBuilder IHtmlContentBuilder.Append(string unencoded)
{
return Append(unencoded);
}
/// <inheritdoc />
IHtmlContentBuilder IHtmlContentBuilder.AppendEncoded(string encoded)
{
return AppendEncoded(encoded);
}
/// <inheritdoc />
IHtmlContentBuilder IHtmlContentBuilder.Clear()
{
return Clear();
}
}
}

View File

@ -88,7 +88,6 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
Assert.Equal(expected, tagHelperContent.GetContent(new CommonTestEncoder()));
}
// Overload with args array is called.
[Fact]
public void CanAppendFormatContent()
{
@ -100,84 +99,10 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
// Assert
Assert.Equal(
"HtmlEncode[[First Second Third Fourth!]]",
"HtmlEncode[[First]] HtmlEncode[[Second]] HtmlEncode[[Third]] HtmlEncode[[Fourth]]!",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[0x32 - hex equivalent for 50.]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[0x32 - hex equivalent for 50.]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[0x32 - hex equivalent for 50.]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[Fact]
public void CanAppendFormat_WithAlignmentComponent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
// Act
tagHelperContent.AppendFormat("{0, -10} World!", "Hello");
// Assert
Assert.Equal(
"HtmlEncode[[Hello World!]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[Fact]
public void CanAppendFormat_WithFormatStringComponent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
// Act
tagHelperContent.AppendFormat("0x{0:X}", 50);
// Assert
Assert.Equal("HtmlEncode[[0x32]]", tagHelperContent.GetContent(new CommonTestEncoder()));
}
// Overload with args array is called.
[Fact]
public void CanAppendFormat_WithCulture()
{
@ -187,7 +112,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
// Act
tagHelperContent.AppendFormat(
CultureInfo.InvariantCulture,
"Numbers in InvariantCulture - {0, -5:N} {1} {2} {3}!",
"Numbers in InvariantCulture - {0:N} {1} {2} {3}!",
1.1,
2.98,
145.82,
@ -195,107 +120,19 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
// Assert
Assert.Equal(
"HtmlEncode[[Numbers in InvariantCulture - 1.10 2.98 145.82 32.86!]]",
"Numbers in InvariantCulture - HtmlEncode[[1.10]] HtmlEncode[[2.98]] " +
"HtmlEncode[[145.82]] HtmlEncode[[32.86]]!",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[Numbers in InvariantCulture - 1.10 !]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[Numbers in InvariantCulture - 1.10 2.98!]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[Numbers in InvariantCulture - 1.10 2.98 3.12!]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[1,21 in french!]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[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(
"HtmlEncode[[01 February 2015]]",
tagHelperContent.GetContent(new CommonTestEncoder()));
}
[Fact(Skip = "Blocked by #526")]
public void CanAppendDefaultTagHelperContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var helloWorldContent = new DefaultTagHelperContent();
helloWorldContent.SetContent("HelloWorld");
var expected = "HtmlEncode[[Content was HelloWorld]]";
var expected = "Content was HtmlEncode[[HelloWorld]]";
// Act
tagHelperContent.AppendFormat("Content was {0}", helloWorldContent);
@ -549,7 +386,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "HtmlEncode[[First ]]HtmlEncode[[Second Third]]";
var expected = "HtmlEncode[[First ]]HtmlEncode[[Second]] Third";
// Act
tagHelperContent.SetContent("First ").AppendFormat("{0} Third", "Second");
@ -563,7 +400,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "HtmlEncode[[First ]]HtmlEncode[[Second Third ]]HtmlEncode[[Fourth]]";
var expected = "HtmlEncode[[First ]]HtmlEncode[[Second]] Third HtmlEncode[[Fourth]]";
// Act
tagHelperContent