Add `HtmlContentBuilder.Count`

- precursor to PR aspnet/Mvc#5378 for issue aspnet/Mvc#3918
This commit is contained in:
Doug Bunting 2016-10-10 21:42:33 -07:00
Родитель f99c930354
Коммит 0781b5a87c
2 изменённых файлов: 45 добавлений и 31 удалений

Просмотреть файл

@ -30,6 +30,11 @@ namespace Microsoft.AspNetCore.Html
{ {
} }
/// <summary>
/// Gets the number of elements in the <see cref="HtmlContentBuilder"/>.
/// </summary>
public int Count => Entries.Count;
/// <summary> /// <summary>
/// Creates a new <see cref="HtmlContentBuilder"/> with the given list of entries. /// Creates a new <see cref="HtmlContentBuilder"/> with the given list of entries.
/// </summary> /// </summary>

Просмотреть файл

@ -22,6 +22,7 @@ namespace Microsoft.Extensions.Internal
content.Append("Hello"); content.Append("Hello");
// Assert // Assert
Assert.Equal(1, content.Count);
var result = Assert.Single(content.Entries); var result = Assert.Single(content.Entries);
Assert.IsType<string>(result); Assert.IsType<string>(result);
} }
@ -69,6 +70,7 @@ namespace Microsoft.Extensions.Internal
content.AppendHtml(new TestHtmlContent("Hello")); content.AppendHtml(new TestHtmlContent("Hello"));
// Assert // Assert
Assert.Equal(1, content.Count);
var result = Assert.Single(content.Entries); var result = Assert.Single(content.Entries);
var testHtmlContent = Assert.IsType<TestHtmlContent>(result); var testHtmlContent = Assert.IsType<TestHtmlContent>(result);
testHtmlContent.WriteTo(writer, new HtmlTestEncoder()); testHtmlContent.WriteTo(writer, new HtmlTestEncoder());
@ -86,9 +88,11 @@ namespace Microsoft.Extensions.Internal
content.Append("Test"); content.Append("Test");
// Assert // Assert
Assert.Equal(2, content.Entries.Count); Assert.Equal(2, content.Count);
Assert.Equal("Written from TestHtmlContent: hello", content.Entries[0].ToString()); Assert.Collection(
Assert.Equal("Test", content.Entries[1]); content.Entries,
entry => Assert.Equal("Written from TestHtmlContent: hello", entry.ToString()),
entry => Assert.Equal("Test", entry));
} }
[Fact] [Fact]
@ -103,7 +107,8 @@ namespace Microsoft.Extensions.Internal
content.Clear(); content.Clear();
// Assert // Assert
Assert.Equal(0, content.Entries.Count); Assert.Equal(0, content.Count);
Assert.Empty(content.Entries);
} }
[Fact] [Fact]
@ -121,12 +126,13 @@ namespace Microsoft.Extensions.Internal
source.CopyTo(destination); source.CopyTo(destination);
// Assert // Assert
Assert.Equal(2, source.Entries.Count); Assert.Equal(2, source.Count);
Assert.Equal(3, destination.Entries.Count); Assert.Equal(3, destination.Count);
Assert.Collection(
Assert.Equal("some-content", Assert.IsType<string>(destination.Entries[0])); destination.Entries,
Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(destination.Entries[1])); entry => Assert.Equal("some-content", Assert.IsType<string>(entry)),
Assert.Equal("Test", Assert.IsType<string>(destination.Entries[2])); entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(entry)),
entry => Assert.Equal("Test", Assert.IsType<string>(entry)));
} }
[Fact] [Fact]
@ -147,13 +153,14 @@ namespace Microsoft.Extensions.Internal
source.CopyTo(destination); source.CopyTo(destination);
// Assert // Assert
Assert.Equal(2, source.Entries.Count); Assert.Equal(2, source.Count);
Assert.Equal(1, nested.Entries.Count); Assert.Equal(1, nested.Count);
Assert.Equal(3, destination.Entries.Count); Assert.Equal(3, destination.Count);
Assert.Collection(
Assert.Equal("some-content", Assert.IsType<string>(destination.Entries[0])); destination.Entries,
Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(destination.Entries[1])); entry => Assert.Equal("some-content", Assert.IsType<string>(entry)),
Assert.Equal("Test", Assert.IsType<string>(destination.Entries[2])); entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(entry)),
entry => Assert.Equal("Test", Assert.IsType<string>(entry)));
} }
[Fact] [Fact]
@ -171,12 +178,13 @@ namespace Microsoft.Extensions.Internal
source.MoveTo(destination); source.MoveTo(destination);
// Assert // Assert
Assert.Equal(0, source.Entries.Count); Assert.Equal(0, source.Count);
Assert.Equal(3, destination.Entries.Count); Assert.Equal(3, destination.Count);
Assert.Collection(
Assert.Equal("some-content", Assert.IsType<string>(destination.Entries[0])); destination.Entries,
Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(destination.Entries[1])); entry => Assert.Equal("some-content", Assert.IsType<string>(entry)),
Assert.Equal("Test", Assert.IsType<string>(destination.Entries[2])); entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(entry)),
entry => Assert.Equal("Test", Assert.IsType<string>(entry)));
} }
[Fact] [Fact]
@ -197,13 +205,14 @@ namespace Microsoft.Extensions.Internal
source.MoveTo(destination); source.MoveTo(destination);
// Assert // Assert
Assert.Equal(0, source.Entries.Count); Assert.Equal(0, source.Count);
Assert.Equal(0, nested.Entries.Count); Assert.Equal(0, nested.Count);
Assert.Equal(3, destination.Entries.Count); Assert.Equal(3, destination.Count);
Assert.Collection(
Assert.Equal("some-content", Assert.IsType<string>(destination.Entries[0])); destination.Entries,
Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(destination.Entries[1])); entry => Assert.Equal("some-content", Assert.IsType<string>(entry)),
Assert.Equal("Test", Assert.IsType<string>(destination.Entries[2])); entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType<TestHtmlContent>(entry)),
entry => Assert.Equal("Test", Assert.IsType<string>(entry)));
} }
[Fact] [Fact]
@ -219,7 +228,7 @@ namespace Microsoft.Extensions.Internal
content.WriteTo(writer, new HtmlTestEncoder()); content.WriteTo(writer, new HtmlTestEncoder());
// Assert // Assert
Assert.Equal(2, content.Entries.Count); Assert.Equal(2, content.Count);
Assert.Equal("Written from TestHtmlContent: HelloHtmlEncode[[Test]]", writer.ToString()); Assert.Equal("Written from TestHtmlContent: HelloHtmlEncode[[Test]]", writer.ToString());
} }