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

Commit 6acf294

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 9102ed2 commit 6acf294

File tree

6 files changed

+284
-57
lines changed

6 files changed

+284
-57
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,76 @@
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.Same(content1, content2);
44+
Assert.Equal(expectedContent, content1);
45+
Assert.Equal(expectedContent, content2);
46+
}
47+
48+
[Fact]
49+
public async Task ExecuteChildContentAsync_IsNotMemoized()
50+
{
51+
// Arrange
52+
var childContentExecutionCount = 0;
53+
var executionContext = new TagHelperExecutionContext(
54+
"p",
55+
uniqueId: string.Empty,
56+
executeChildContentAsync: () =>
57+
{
58+
childContentExecutionCount++;
59+
60+
return Task.FromResult(result: true);
61+
},
62+
startWritingScope: () => { },
63+
endWritingScope: () => new StringWriter());
64+
65+
// Act
66+
await executionContext.ExecuteChildContentAsync();
67+
await executionContext.ExecuteChildContentAsync();
68+
await executionContext.ExecuteChildContentAsync();
69+
70+
// Assert
71+
Assert.Equal(3, childContentExecutionCount);
72+
}
73+
1274
public static TheoryData<string, string> DictionaryCaseTestingData
1375
{
1476
get

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

Lines changed: 168 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,65 @@ 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
{
24-
// Arrange
25-
var tagHelperOutput = new TagHelperOutput("p");
24+
// Arrange & Act
25+
var tagHelperOutput = new TagHelperOutput("p")
26+
{
27+
TagName = null
28+
};
2629

27-
// Act
28-
tagHelperOutput.TagName = null;
30+
// Assert
31+
Assert.Null(tagHelperOutput.TagName);
32+
}
33+
34+
[Fact]
35+
public void Content_CanSetToNull()
36+
{
37+
// Arrange & Act
38+
var tagHelperOutput = new TagHelperOutput("p")
39+
{
40+
Content = null
41+
};
2942

3043
// Assert
31-
Assert.Empty(tagHelperOutput.TagName);
44+
Assert.Null(tagHelperOutput.Content);
3245
}
3346

3447
[Fact]
35-
public void Content_CannotSetToNull()
48+
public void PreContent_CanSetToNull()
3649
{
37-
// Arrange
38-
var tagHelperOutput = new TagHelperOutput("p");
50+
// Arrange & Act
51+
var tagHelperOutput = new TagHelperOutput("p")
52+
{
53+
PreContent = null
54+
};
3955

40-
// Act
41-
tagHelperOutput.Content = null;
56+
// Assert
57+
Assert.Null(tagHelperOutput.PreContent);
58+
}
59+
60+
[Fact]
61+
public void PostContent_CanSetToNull()
62+
{
63+
// Arrange & Act
64+
var tagHelperOutput = new TagHelperOutput("p")
65+
{
66+
PostContent = null
67+
};
4268

4369
// Assert
44-
Assert.Empty(tagHelperOutput.Content);
70+
Assert.Null(tagHelperOutput.PostContent);
4571
}
4672

4773
[Fact]
@@ -119,9 +145,10 @@ public void GenerateStartTag_ReturnsNothingIfWhitespaceTagName()
119145
{
120146
{ "class", "btn" },
121147
{ "something", " spaced " }
122-
});
123-
124-
tagHelperOutput.SelfClosing = true;
148+
})
149+
{
150+
SelfClosing = true
151+
};
125152

126153
// Act
127154
var output = tagHelperOutput.GenerateStartTag();
@@ -135,9 +162,10 @@ public void GenerateStartTag_ReturnsNothingIfWhitespaceTagName()
135162
public void GenerateEndTag_ReturnsNothingIfWhitespaceTagName()
136163
{
137164
// Arrange
138-
var tagHelperOutput = new TagHelperOutput(" "); ;
139-
140-
tagHelperOutput.Content = "Hello World";
165+
var tagHelperOutput = new TagHelperOutput(" ")
166+
{
167+
Content = "Hello World"
168+
};
141169

142170
// Act
143171
var output = tagHelperOutput.GenerateEndTag();
@@ -147,12 +175,46 @@ public void GenerateEndTag_ReturnsNothingIfWhitespaceTagName()
147175
}
148176

149177
[Fact]
150-
public void GenerateContent_ReturnsContent()
178+
public void GeneratePreContent_ReturnsPreContent()
151179
{
152180
// Arrange
153-
var tagHelperOutput = new TagHelperOutput("p");
181+
var tagHelperOutput = new TagHelperOutput("p")
182+
{
183+
PreContent = "Hello World"
184+
};
185+
186+
// Act
187+
var output = tagHelperOutput.GeneratePreContent();
188+
189+
// Assert
190+
Assert.Equal("Hello World", output);
191+
}
192+
193+
[Fact]
194+
public void GeneratePreContent_ReturnsNothingIfSelfClosing()
195+
{
196+
// Arrange
197+
var tagHelperOutput = new TagHelperOutput("p")
198+
{
199+
SelfClosing = true,
200+
PreContent = "Hello World"
201+
};
202+
203+
// Act
204+
var output = tagHelperOutput.GeneratePreContent();
205+
206+
// Assert
207+
Assert.Empty(output);
208+
}
154209

155-
tagHelperOutput.Content = "Hello World";
210+
[Fact]
211+
public void GenerateContent_ReturnsContent()
212+
{
213+
// Arrange
214+
var tagHelperOutput = new TagHelperOutput("p")
215+
{
216+
Content = "Hello World"
217+
};
156218

157219
// Act
158220
var output = tagHelperOutput.GenerateContent();
@@ -168,18 +230,50 @@ public void GenerateContent_ReturnsNothingIfSelfClosing()
168230
// Arrange
169231
var tagHelperOutput = new TagHelperOutput("p")
170232
{
171-
SelfClosing = true
233+
SelfClosing = true,
234+
Content = "Hello World"
172235
};
173236

174-
tagHelperOutput.Content = "Hello World";
175-
176237
// Act
177238
var output = tagHelperOutput.GenerateContent();
178239

179240
// Assert
180241
Assert.Empty(output);
181242
}
182243

244+
[Fact]
245+
public void GeneratePostContent_ReturnsPostContent()
246+
{
247+
// Arrange
248+
var tagHelperOutput = new TagHelperOutput("p")
249+
{
250+
PostContent = "Hello World"
251+
};
252+
253+
// Act
254+
var output = tagHelperOutput.GeneratePostContent();
255+
256+
// Assert
257+
Assert.Equal("Hello World", output);
258+
}
259+
260+
[Fact]
261+
public void GeneratePostContent_ReturnsNothingIfSelfClosing()
262+
{
263+
// Arrange
264+
var tagHelperOutput = new TagHelperOutput("p")
265+
{
266+
SelfClosing = true,
267+
PostContent = "Hello World"
268+
};
269+
270+
// Act
271+
var output = tagHelperOutput.GeneratePostContent();
272+
273+
// Assert
274+
Assert.Empty(output);
275+
}
276+
183277
[Fact]
184278
public void GenerateEndTag_ReturnsEndTag()
185279
{
@@ -209,6 +303,54 @@ public void GenerateEndTag_ReturnsNothingIfSelfClosing()
209303
Assert.Empty(output);
210304
}
211305

306+
[Fact]
307+
public void SuppressOutput_Sets_TagName_Content_PreContent_PostContent_ToNull()
308+
{
309+
// Arrange
310+
var tagHelperOutput = new TagHelperOutput("p")
311+
{
312+
PreContent = "Pre Content",
313+
Content = "Content",
314+
PostContent = "Post Content"
315+
};
316+
317+
// Act
318+
tagHelperOutput.SuppressOutput();
319+
320+
// Assert
321+
Assert.Null(tagHelperOutput.TagName);
322+
Assert.Null(tagHelperOutput.PreContent);
323+
Assert.Null(tagHelperOutput.Content);
324+
Assert.Null(tagHelperOutput.PostContent);
325+
}
326+
327+
[Fact]
328+
public void SuppressOutput_PreventsTagOutput()
329+
{
330+
// Arrange
331+
var tagHelperOutput = new TagHelperOutput("p",
332+
attributes: new Dictionary<string, string>
333+
{
334+
{ "class", "btn" },
335+
{ "something", " spaced " }
336+
})
337+
{
338+
PreContent = "Pre Content",
339+
Content = "Content",
340+
PostContent = "Post Content"
341+
};
342+
343+
// Act
344+
tagHelperOutput.SuppressOutput();
345+
346+
// Assert
347+
Assert.Empty(tagHelperOutput.GenerateStartTag());
348+
Assert.Null(tagHelperOutput.GeneratePreContent());
349+
Assert.Null(tagHelperOutput.GenerateContent());
350+
Assert.Null(tagHelperOutput.GeneratePostContent());
351+
Assert.Empty(tagHelperOutput.GenerateEndTag());
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)