Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit fd67fbb

Browse files
author
N. Taylor Mullen
committed
Modify tests to allow new content mode design for runtime components.
- Added a new internal ctor for TagHelperExecutionContext since it's used in multiple tests to allow for less friction testing. #221
1 parent 9cca015 commit fd67fbb

File tree

6 files changed

+263
-37
lines changed

6 files changed

+263
-37
lines changed

src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperExecutionContext.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ public class TagHelperExecutionContext
1919
private readonly Func<TextWriter> _endWritingScope;
2020
private string _childContent;
2121

22+
/// <summary>
23+
/// Internal for testing purposes only.
24+
/// </summary>
25+
internal TagHelperExecutionContext(string tagName)
26+
: this(tagName,
27+
uniqueId: string.Empty,
28+
executeChildContentAsync: async () => await Task.FromResult(result: true),
29+
startWritingScope: () => { },
30+
endWritingScope: () => new StringWriter())
31+
{
32+
}
33+
2234
/// <summary>
2335
/// Instantiates a new <see cref="TagHelperExecutionContext"/>.
2436
/// </summary>

src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperOutput.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ internal TagHelperOutput(string tagName)
2424
Attributes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2525
}
2626

27-
// Internal for testing
28-
internal TagHelperOutput(string tagName, [NotNull] IDictionary<string, string> attributes)
29-
: this(tagName, attributes, string.Empty)
30-
{
31-
}
32-
3327
/// <summary>
3428
/// Instantiates a new instance of <see cref="TagHelperOutput"/>.
3529
/// </summary>

test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperExecutionContextTest.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,75 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
6+
using System.IO;
57
using System.Linq;
8+
using System.Threading.Tasks;
69
using Xunit;
710

811
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
912
{
1013
public class TagHelperExecutionContextTest
1114
{
15+
[Fact]
16+
public async Task GetChildContentAsync_CachesValue()
17+
{
18+
// Arrange
19+
var writer = new StringWriter();
20+
var expectedContent = string.Empty;
21+
var executionContext = new TagHelperExecutionContext(
22+
"p",
23+
uniqueId: string.Empty,
24+
executeChildContentAsync: () =>
25+
{
26+
if (string.IsNullOrEmpty(expectedContent))
27+
{
28+
expectedContent = "Hello from child content: " + Guid.NewGuid().ToString();
29+
}
30+
31+
writer.Write(expectedContent);
32+
33+
return Task.FromResult(result: true);
34+
},
35+
startWritingScope: () => { },
36+
endWritingScope: () => writer);
37+
38+
// Act
39+
var content1 = await executionContext.GetChildContentAsync();
40+
var content2 = await executionContext.GetChildContentAsync();
41+
42+
// Assert
43+
Assert.Equal(expectedContent, content1);
44+
Assert.Equal(expectedContent, content2);
45+
}
46+
47+
[Fact]
48+
public async Task ExecuteChildContentAsync_IsNotMemoized()
49+
{
50+
// Arrange
51+
var childContentExecutionCount = 0;
52+
var executionContext = new TagHelperExecutionContext(
53+
"p",
54+
uniqueId: string.Empty,
55+
executeChildContentAsync: () =>
56+
{
57+
childContentExecutionCount++;
58+
59+
return Task.FromResult(result: true);
60+
},
61+
startWritingScope: () => { },
62+
endWritingScope: () => new StringWriter());
63+
64+
// Act
65+
await executionContext.ExecuteChildContentAsync();
66+
await executionContext.ExecuteChildContentAsync();
67+
await executionContext.ExecuteChildContentAsync();
68+
69+
// Assert
70+
Assert.Equal(3, childContentExecutionCount);
71+
}
72+
1273
public static TheoryData<string, string> DictionaryCaseTestingData
1374
{
1475
get

test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperOutputTest.cs

Lines changed: 148 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
99
public class TagHelperOutputTest
1010
{
1111
[Fact]
12-
public void TagName_CannotSetToNullInCtor()
12+
public void TagName_CanSetToNullInCtor()
1313
{
1414
// Arrange & Act
1515
var tagHelperOutput = new TagHelperOutput(null);
1616

1717
// Assert
18-
Assert.Empty(tagHelperOutput.TagName);
18+
Assert.Null(tagHelperOutput.TagName);
1919
}
2020

2121
[Fact]
22-
public void TagName_CannotSetToNull()
22+
public void TagName_CanSetToNull()
2323
{
2424
// Arrange
2525
var tagHelperOutput = new TagHelperOutput("p");
@@ -28,11 +28,11 @@ public void TagName_CannotSetToNull()
2828
tagHelperOutput.TagName = null;
2929

3030
// Assert
31-
Assert.Empty(tagHelperOutput.TagName);
31+
Assert.Null(tagHelperOutput.TagName);
3232
}
3333

3434
[Fact]
35-
public void Content_CannotSetToNull()
35+
public void Content_CanSetToNull()
3636
{
3737
// Arrange
3838
var tagHelperOutput = new TagHelperOutput("p");
@@ -41,7 +41,33 @@ public void Content_CannotSetToNull()
4141
tagHelperOutput.Content = null;
4242

4343
// Assert
44-
Assert.Empty(tagHelperOutput.Content);
44+
Assert.Null(tagHelperOutput.Content);
45+
}
46+
47+
[Fact]
48+
public void PreContent_CanSetToNull()
49+
{
50+
// Arrange
51+
var tagHelperOutput = new TagHelperOutput("p");
52+
53+
// Act
54+
tagHelperOutput.PreContent = null;
55+
56+
// Assert
57+
Assert.Null(tagHelperOutput.PreContent);
58+
}
59+
60+
[Fact]
61+
public void PostContent_CanSetToNull()
62+
{
63+
// Arrange
64+
var tagHelperOutput = new TagHelperOutput("p");
65+
66+
// Act
67+
tagHelperOutput.PostContent = null;
68+
69+
// Assert
70+
Assert.Null(tagHelperOutput.PostContent);
4571
}
4672

4773
[Fact]
@@ -146,6 +172,39 @@ public void GenerateEndTag_ReturnsNothingIfWhitespaceTagName()
146172
Assert.Empty(output);
147173
}
148174

175+
[Fact]
176+
public void GeneratePreContent_ReturnsPreContent()
177+
{
178+
// Arrange
179+
var tagHelperOutput = new TagHelperOutput("p");
180+
181+
tagHelperOutput.PreContent = "Hello World";
182+
183+
// Act
184+
var output = tagHelperOutput.GeneratePreContent();
185+
186+
// Assert
187+
Assert.Equal("Hello World", output);
188+
}
189+
190+
[Fact]
191+
public void GeneratePreContent_ReturnsNothingIfSelfClosing()
192+
{
193+
// Arrange
194+
var tagHelperOutput = new TagHelperOutput("p")
195+
{
196+
SelfClosing = true
197+
};
198+
199+
tagHelperOutput.PreContent = "Hello World";
200+
201+
// Act
202+
var output = tagHelperOutput.GeneratePreContent();
203+
204+
// Assert
205+
Assert.Empty(output);
206+
}
207+
149208
[Fact]
150209
public void GenerateContent_ReturnsContent()
151210
{
@@ -180,6 +239,39 @@ public void GenerateContent_ReturnsNothingIfSelfClosing()
180239
Assert.Empty(output);
181240
}
182241

242+
[Fact]
243+
public void GeneratePostContent_ReturnsPreContent()
244+
{
245+
// Arrange
246+
var tagHelperOutput = new TagHelperOutput("p");
247+
248+
tagHelperOutput.PostContent = "Hello World";
249+
250+
// Act
251+
var output = tagHelperOutput.GeneratePostContent();
252+
253+
// Assert
254+
Assert.Equal("Hello World", output);
255+
}
256+
257+
[Fact]
258+
public void GeneratePostContent_ReturnsNothingIfSelfClosing()
259+
{
260+
// Arrange
261+
var tagHelperOutput = new TagHelperOutput("p")
262+
{
263+
SelfClosing = true
264+
};
265+
266+
tagHelperOutput.PostContent = "Hello World";
267+
268+
// Act
269+
var output = tagHelperOutput.GeneratePostContent();
270+
271+
// Assert
272+
Assert.Empty(output);
273+
}
274+
183275
[Fact]
184276
public void GenerateEndTag_ReturnsEndTag()
185277
{
@@ -209,6 +301,56 @@ public void GenerateEndTag_ReturnsNothingIfSelfClosing()
209301
Assert.Empty(output);
210302
}
211303

304+
[Fact]
305+
public void SupressOutput_Sets_TagName_Content_PreContent_PostContent_ToNull()
306+
{
307+
// Arrange
308+
var tagHelperOutput = new TagHelperOutput("p")
309+
{
310+
PreContent = "Pre Content",
311+
Content = "Content",
312+
PostContent = "Post Content"
313+
};
314+
315+
// Act
316+
tagHelperOutput.SupressOutput();
317+
318+
// Assert
319+
Assert.Null(tagHelperOutput.TagName);
320+
Assert.Null(tagHelperOutput.PreContent);
321+
Assert.Null(tagHelperOutput.Content);
322+
Assert.Null(tagHelperOutput.PostContent);
323+
}
324+
325+
[Fact]
326+
public void SupressOutput_PreventsTagOutput()
327+
{
328+
// Arrange
329+
var tagHelperOutput = new TagHelperOutput("p",
330+
attributes: new Dictionary<string, string>
331+
{
332+
{ "class", "btn" },
333+
{ "something", " spaced " }
334+
})
335+
{
336+
PreContent = "Pre Content",
337+
Content = "Content",
338+
PostContent = "Post Content"
339+
};
340+
341+
// Act
342+
tagHelperOutput.SupressOutput();
343+
344+
var output = tagHelperOutput.GenerateStartTag() +
345+
tagHelperOutput.GeneratePreContent() +
346+
tagHelperOutput.GenerateContent() +
347+
tagHelperOutput.GeneratePostContent() +
348+
tagHelperOutput.GenerateEndTag();
349+
350+
// Assert
351+
Assert.Empty(output);
352+
}
353+
212354
[Theory]
213355
[InlineData("class", "ClASs")]
214356
[InlineData("CLaSs", "class")]

test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperRunnerTest.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,6 @@ public async Task RunAsync_AllowsDataRetrievalFromTagHelperContext()
6666
Assert.Equal("True", output.Attributes["foo"]);
6767
}
6868

69-
[Fact]
70-
public async Task RunAsync_WithContentSetsOutputsContent()
71-
{
72-
// Arrange
73-
var runner = new TagHelperRunner();
74-
var executionContext = new TagHelperExecutionContext("p");
75-
var tagHelper = new ExecutableTagHelper();
76-
var contentWriter = new StringWriter(new StringBuilder("Hello World"));
77-
78-
// Act
79-
executionContext.Add(tagHelper);
80-
var output = await runner.RunAsync(executionContext, contentWriter);
81-
82-
// Assert
83-
Assert.Equal(output.Content, "Hello World");
84-
}
85-
8669
private class ExecutableTagHelper : TagHelper
8770
{
8871
public bool Processed { get; set; }

0 commit comments

Comments
 (0)