From 0781b5a87cb9eb8b48044b74cb8e6602650a3b63 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Mon, 10 Oct 2016 21:42:33 -0700 Subject: [PATCH] Add `HtmlContentBuilder.Count` - precursor to PR aspnet/Mvc#5378 for issue aspnet/Mvc#3918 --- .../HtmlContentBuilder.cs | 5 ++ .../HtmlContentBuilderTest.cs | 71 +++++++++++-------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/Microsoft.AspNetCore.Html.Abstractions/HtmlContentBuilder.cs b/src/Microsoft.AspNetCore.Html.Abstractions/HtmlContentBuilder.cs index 864f3c8..e61d9f7 100644 --- a/src/Microsoft.AspNetCore.Html.Abstractions/HtmlContentBuilder.cs +++ b/src/Microsoft.AspNetCore.Html.Abstractions/HtmlContentBuilder.cs @@ -30,6 +30,11 @@ namespace Microsoft.AspNetCore.Html { } + /// + /// Gets the number of elements in the . + /// + public int Count => Entries.Count; + /// /// Creates a new with the given list of entries. /// diff --git a/test/Microsoft.AspNetCore.Html.Abstractions.Test/HtmlContentBuilderTest.cs b/test/Microsoft.AspNetCore.Html.Abstractions.Test/HtmlContentBuilderTest.cs index 40219b1..c3cb7d1 100644 --- a/test/Microsoft.AspNetCore.Html.Abstractions.Test/HtmlContentBuilderTest.cs +++ b/test/Microsoft.AspNetCore.Html.Abstractions.Test/HtmlContentBuilderTest.cs @@ -22,6 +22,7 @@ namespace Microsoft.Extensions.Internal content.Append("Hello"); // Assert + Assert.Equal(1, content.Count); var result = Assert.Single(content.Entries); Assert.IsType(result); } @@ -69,6 +70,7 @@ namespace Microsoft.Extensions.Internal content.AppendHtml(new TestHtmlContent("Hello")); // Assert + Assert.Equal(1, content.Count); var result = Assert.Single(content.Entries); var testHtmlContent = Assert.IsType(result); testHtmlContent.WriteTo(writer, new HtmlTestEncoder()); @@ -86,9 +88,11 @@ namespace Microsoft.Extensions.Internal content.Append("Test"); // Assert - Assert.Equal(2, content.Entries.Count); - Assert.Equal("Written from TestHtmlContent: hello", content.Entries[0].ToString()); - Assert.Equal("Test", content.Entries[1]); + Assert.Equal(2, content.Count); + Assert.Collection( + content.Entries, + entry => Assert.Equal("Written from TestHtmlContent: hello", entry.ToString()), + entry => Assert.Equal("Test", entry)); } [Fact] @@ -103,7 +107,8 @@ namespace Microsoft.Extensions.Internal content.Clear(); // Assert - Assert.Equal(0, content.Entries.Count); + Assert.Equal(0, content.Count); + Assert.Empty(content.Entries); } [Fact] @@ -121,12 +126,13 @@ namespace Microsoft.Extensions.Internal source.CopyTo(destination); // Assert - Assert.Equal(2, source.Entries.Count); - Assert.Equal(3, destination.Entries.Count); - - Assert.Equal("some-content", Assert.IsType(destination.Entries[0])); - Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(destination.Entries[1])); - Assert.Equal("Test", Assert.IsType(destination.Entries[2])); + Assert.Equal(2, source.Count); + Assert.Equal(3, destination.Count); + Assert.Collection( + destination.Entries, + entry => Assert.Equal("some-content", Assert.IsType(entry)), + entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(entry)), + entry => Assert.Equal("Test", Assert.IsType(entry))); } [Fact] @@ -147,13 +153,14 @@ namespace Microsoft.Extensions.Internal source.CopyTo(destination); // Assert - Assert.Equal(2, source.Entries.Count); - Assert.Equal(1, nested.Entries.Count); - Assert.Equal(3, destination.Entries.Count); - - Assert.Equal("some-content", Assert.IsType(destination.Entries[0])); - Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(destination.Entries[1])); - Assert.Equal("Test", Assert.IsType(destination.Entries[2])); + Assert.Equal(2, source.Count); + Assert.Equal(1, nested.Count); + Assert.Equal(3, destination.Count); + Assert.Collection( + destination.Entries, + entry => Assert.Equal("some-content", Assert.IsType(entry)), + entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(entry)), + entry => Assert.Equal("Test", Assert.IsType(entry))); } [Fact] @@ -171,12 +178,13 @@ namespace Microsoft.Extensions.Internal source.MoveTo(destination); // Assert - Assert.Equal(0, source.Entries.Count); - Assert.Equal(3, destination.Entries.Count); - - Assert.Equal("some-content", Assert.IsType(destination.Entries[0])); - Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(destination.Entries[1])); - Assert.Equal("Test", Assert.IsType(destination.Entries[2])); + Assert.Equal(0, source.Count); + Assert.Equal(3, destination.Count); + Assert.Collection( + destination.Entries, + entry => Assert.Equal("some-content", Assert.IsType(entry)), + entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(entry)), + entry => Assert.Equal("Test", Assert.IsType(entry))); } [Fact] @@ -197,13 +205,14 @@ namespace Microsoft.Extensions.Internal source.MoveTo(destination); // Assert - Assert.Equal(0, source.Entries.Count); - Assert.Equal(0, nested.Entries.Count); - Assert.Equal(3, destination.Entries.Count); - - Assert.Equal("some-content", Assert.IsType(destination.Entries[0])); - Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(destination.Entries[1])); - Assert.Equal("Test", Assert.IsType(destination.Entries[2])); + Assert.Equal(0, source.Count); + Assert.Equal(0, nested.Count); + Assert.Equal(3, destination.Count); + Assert.Collection( + destination.Entries, + entry => Assert.Equal("some-content", Assert.IsType(entry)), + entry => Assert.Equal(new TestHtmlContent("hello"), Assert.IsType(entry)), + entry => Assert.Equal("Test", Assert.IsType(entry))); } [Fact] @@ -219,7 +228,7 @@ namespace Microsoft.Extensions.Internal content.WriteTo(writer, new HtmlTestEncoder()); // Assert - Assert.Equal(2, content.Entries.Count); + Assert.Equal(2, content.Count); Assert.Equal("Written from TestHtmlContent: HelloHtmlEncode[[Test]]", writer.ToString()); }