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

Commit 2d53cf3

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 903f0e9 commit 2d53cf3

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ public class TagHelperExecutionContext
1616
private readonly List<ITagHelper> _tagHelpers;
1717
private string _childContent;
1818

19+
/// <summary>
20+
/// Internal for testing purposes only.
21+
/// </summary>
22+
internal TagHelperExecutionContext(string tagName)
23+
: this(tagName,
24+
executeChildContentAsync: async () => await Task.FromResult(result: true),
25+
startWritingScope: () => { },
26+
endWritingScope: () => new StringWriter())
27+
{
28+
}
29+
1930
/// <summary>
2031
/// Instantiates a new <see cref="TagHelperExecutionContext"/>.
2132
/// </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/TagHelperOutputTest.cs

Lines changed: 6 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,7 @@ public void Content_CannotSetToNull()
4141
tagHelperOutput.Content = null;
4242

4343
// Assert
44-
Assert.Empty(tagHelperOutput.Content);
44+
Assert.Null(tagHelperOutput.Content);
4545
}
4646

4747
[Fact]

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; }

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.IO;
6+
using System.Threading.Tasks;
57
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
68
using Xunit;
79

810
namespace Microsoft.AspNet.Razor.Runtime.Test.TagHelpers
911
{
1012
public class TagHelperScopeManagerTest
1113
{
14+
private static readonly Action DefaultStartWritingScope = () => { };
15+
private static readonly Func<TextWriter> DefaultEndWritignScope = () => new StringWriter();
16+
private static readonly Func<Task> DefaultExecuteChildContentAsync =
17+
async () => await Task.FromResult(result: true);
18+
1219
[Fact]
1320
public void Begin_CreatesContextWithAppropriateTagName()
1421
{
1522
// Arrange
1623
var scopeManager = new TagHelperScopeManager();
1724

1825
// Act
19-
var executionContext = scopeManager.Begin("p");
26+
var executionContext = scopeManager.Begin("p",
27+
DefaultExecuteChildContentAsync,
28+
DefaultStartWritingScope,
29+
DefaultEndWritignScope);
2030

2131
// Assert
2232
Assert.Equal("p", executionContext.TagName);
@@ -29,8 +39,14 @@ public void Begin_CanNest()
2939
var scopeManager = new TagHelperScopeManager();
3040

3141
// Act
32-
var executionContext = scopeManager.Begin("p");
33-
executionContext = scopeManager.Begin("div");
42+
var executionContext = scopeManager.Begin("p",
43+
DefaultExecuteChildContentAsync,
44+
DefaultStartWritingScope,
45+
DefaultEndWritignScope);
46+
executionContext = scopeManager.Begin("div",
47+
DefaultExecuteChildContentAsync,
48+
DefaultStartWritingScope,
49+
DefaultEndWritignScope);
3450

3551
// Assert
3652
Assert.Equal("div", executionContext.TagName);
@@ -43,8 +59,14 @@ public void End_ReturnsParentExecutionContext()
4359
var scopeManager = new TagHelperScopeManager();
4460

4561
// Act
46-
var executionContext = scopeManager.Begin("p");
47-
executionContext = scopeManager.Begin("div");
62+
var executionContext = scopeManager.Begin("p",
63+
DefaultExecuteChildContentAsync,
64+
DefaultStartWritingScope,
65+
DefaultEndWritignScope);
66+
executionContext = scopeManager.Begin("div",
67+
DefaultExecuteChildContentAsync,
68+
DefaultStartWritingScope,
69+
DefaultEndWritignScope);
4870
executionContext = scopeManager.End();
4971

5072
// Assert
@@ -58,8 +80,14 @@ public void End_ReturnsNullIfNoNestedContext()
5880
var scopeManager = new TagHelperScopeManager();
5981

6082
// Act
61-
var executionContext = scopeManager.Begin("p");
62-
executionContext = scopeManager.Begin("div");
83+
var executionContext = scopeManager.Begin("p",
84+
DefaultExecuteChildContentAsync,
85+
DefaultStartWritingScope,
86+
DefaultEndWritignScope);
87+
executionContext = scopeManager.Begin("div",
88+
DefaultExecuteChildContentAsync,
89+
DefaultStartWritingScope,
90+
DefaultEndWritignScope);
6391
executionContext = scopeManager.End();
6492
executionContext = scopeManager.End();
6593

@@ -85,7 +113,6 @@ public void End_ThrowsIfNoScope()
85113
});
86114

87115
Assert.Equal(expectedError, ex.Message);
88-
89116
}
90117
}
91118
}

0 commit comments

Comments
 (0)