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

Commit 6735c30

Browse files
author
N. Taylor Mullen
committed
Modify TagHelper tests to abide by new content mode design.
- React to aspnet/Razor#221 - Modified existing TagHelper tests to no longer rely on ContentBehavior. - Updated signatures of TagHelperExecutionContext and TagHelperContext pieces.
1 parent b7aea79 commit 6735c30

20 files changed

+548
-217
lines changed

test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,29 @@ public override async Task ExecuteAsync()
4545
BeginContext(120, 2, true);
4646
WriteLiteral("\r\n");
4747
EndContext();
48-
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("input-test", "test");
48+
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("input-test", "test", async() => {
49+
}
50+
, StartWritingScope, EndWritingScope);
4951
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<Microsoft.AspNet.Mvc.Razor.InputTestTagHelper>();
5052
__tagHelperExecutionContext.Add(__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper);
5153
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For = CreateModelExpression(__model => __model.Now);
5254
__tagHelperExecutionContext.AddTagHelperAttribute("for", __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For);
5355
__tagHelperExecutionContext.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result;
5456
WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag());
57+
WriteLiteral(__tagHelperExecutionContext.Output.GeneratePreContent());
58+
if (__tagHelperExecutionContext.Output.ContentSet)
59+
{
60+
WriteLiteral(__tagHelperExecutionContext.Output.GenerateContent());
61+
}
62+
else if (__tagHelperExecutionContext.ChildContentRetrieved)
63+
{
64+
WriteLiteral(__tagHelperExecutionContext.GetChildContentAsync().Result);
65+
}
66+
else
67+
{
68+
__tagHelperExecutionContext.ExecuteChildContentAsync().Wait();
69+
}
70+
WriteLiteral(__tagHelperExecutionContext.Output.GeneratePostContent());
5571
WriteLiteral(__tagHelperExecutionContext.Output.GenerateEndTag());
5672
__tagHelperExecutionContext = __tagHelperScopeManager.End();
5773
}

test/Microsoft.AspNet.Mvc.TagHelpers.Test/AnchorTagHelperTest.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
3232
{ "asp-host", "contoso.com" },
3333
{ "asp-protocol", "http" }
3434
},
35-
uniqueId: "test");
35+
uniqueId: "test",
36+
getChildContentAsync: () => Task.FromResult("Something Else"));
3637
var output = new TagHelperOutput(
3738
expectedTagName,
3839
attributes: new Dictionary<string, string>
3940
{
4041
{ "id", "myanchor" },
4142
{ "asp-route-foo", "bar" },
42-
},
43-
content: "Something");
43+
})
44+
{
45+
Content = "Something"
46+
};
4447

4548
var urlHelper = new Mock<IUrlHelper>();
4649
urlHelper
@@ -85,11 +88,15 @@ public async Task ProcessAsync_CallsIntoRouteLinkWithExpectedParameters()
8588
{
8689
// Arrange
8790
var context = new TagHelperContext(
88-
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
91+
allAttributes: new Dictionary<string, object>(),
92+
uniqueId: "test",
93+
getChildContentAsync: () => Task.FromResult("Something"));
8994
var output = new TagHelperOutput(
9095
"a",
91-
attributes: new Dictionary<string, string>(),
92-
content: string.Empty);
96+
attributes: new Dictionary<string, string>())
97+
{
98+
Content = string.Empty
99+
};
93100

94101
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
95102
generator
@@ -119,11 +126,15 @@ public async Task ProcessAsync_CallsIntoActionLinkWithExpectedParameters()
119126
{
120127
// Arrange
121128
var context = new TagHelperContext(
122-
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
129+
allAttributes: new Dictionary<string, object>(),
130+
uniqueId: "test",
131+
getChildContentAsync: () => Task.FromResult("Something"));
123132
var output = new TagHelperOutput(
124133
"a",
125-
attributes: new Dictionary<string, string>(),
126-
content: string.Empty);
134+
attributes: new Dictionary<string, string>())
135+
{
136+
Content = string.Empty
137+
};
127138

128139
var generator = new Mock<IHtmlGenerator>();
129140
generator
@@ -166,8 +177,7 @@ public async Task ProcessAsync_ThrowsIfHrefConflictsWithBoundAttributes(string p
166177
attributes: new Dictionary<string, string>()
167178
{
168179
{ "href", "http://www.contoso.com" }
169-
},
170-
content: string.Empty);
180+
});
171181
if (propertyName == "asp-route-")
172182
{
173183
output.Attributes.Add("asp-route-foo", "bar");
@@ -202,8 +212,7 @@ public async Task ProcessAsync_ThrowsIfRouteAndActionOrControllerProvided(string
202212
typeof(AnchorTagHelper).GetProperty(propertyName).SetValue(anchorTagHelper, "Home");
203213
var output = new TagHelperOutput(
204214
"a",
205-
attributes: new Dictionary<string, string>(),
206-
content: string.Empty);
215+
attributes: new Dictionary<string, string>());
207216
var expectedErrorMessage = "Cannot determine an 'href' attribute for <a>. An <a> with a specified " +
208217
"'asp-route' must not have an 'asp-action' or 'asp-controller' attribute.";
209218

test/Microsoft.AspNet.Mvc.TagHelpers.Test/FormTagHelperTest.cs

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
3333
{ "method", "post" },
3434
{ "asp-anti-forgery", true }
3535
},
36-
uniqueId: "test");
36+
uniqueId: "test",
37+
getChildContentAsync: () => Task.FromResult("Something Else"));
3738
var output = new TagHelperOutput(
3839
expectedTagName,
3940
attributes: new Dictionary<string, string>
4041
{
4142
{ "id", "myform" },
4243
{ "asp-route-foo", "bar" },
43-
},
44-
content: "Something");
44+
})
45+
{
46+
PostContent = "Something"
47+
};
4548
var urlHelper = new Mock<IUrlHelper>();
4649
urlHelper
4750
.Setup(mock => mock.Action(It.IsAny<string>(),
@@ -56,8 +59,8 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
5659
var viewContext = TestableHtmlGenerator.GetViewContext(model: null,
5760
htmlGenerator: htmlGenerator,
5861
metadataProvider: metadataProvider);
59-
var expectedContent = "Something" + htmlGenerator.GenerateAntiForgery(viewContext)
60-
.ToString(TagRenderMode.SelfClosing);
62+
var expectedPostContent = "Something" + htmlGenerator.GenerateAntiForgery(viewContext)
63+
.ToString(TagRenderMode.SelfClosing);
6164
var formTagHelper = new FormTagHelper
6265
{
6366
Action = "index",
@@ -79,24 +82,27 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
7982
Assert.Equal("post", attribute.Value);
8083
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("action"));
8184
Assert.Equal("home/index", attribute.Value);
82-
Assert.Equal(expectedContent, output.Content);
85+
Assert.Empty(output.PreContent);
86+
Assert.Empty(output.Content);
87+
Assert.Equal(expectedPostContent, output.PostContent);
8388
Assert.Equal(expectedTagName, output.TagName);
8489
}
8590

8691
[Theory]
8792
[InlineData(true, "<input />")]
8893
[InlineData(false, "")]
8994
[InlineData(null, "<input />")]
90-
public async Task ProcessAsync_GeneratesAntiForgeryCorrectly(bool? antiForgery, string expectedContent)
95+
public async Task ProcessAsync_GeneratesAntiForgeryCorrectly(bool? antiForgery, string expectedPostContent)
9196
{
9297
// Arrange
9398
var viewContext = CreateViewContext();
9499
var context = new TagHelperContext(
95-
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
100+
allAttributes: new Dictionary<string, object>(),
101+
uniqueId: "test",
102+
getChildContentAsync: () => Task.FromResult("Something"));
96103
var output = new TagHelperOutput(
97104
"form",
98-
attributes: new Dictionary<string, string>(),
99-
content: string.Empty);
105+
attributes: new Dictionary<string, string>());
100106
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
101107
generator
102108
.Setup(mock => mock.GenerateForm(
@@ -124,7 +130,9 @@ public async Task ProcessAsync_GeneratesAntiForgeryCorrectly(bool? antiForgery,
124130
// Assert
125131
Assert.Equal("form", output.TagName);
126132
Assert.Empty(output.Attributes);
127-
Assert.Equal(expectedContent, output.Content);
133+
Assert.Empty(output.PreContent);
134+
Assert.Empty(output.Content);
135+
Assert.Equal(expectedPostContent, output.PostContent);
128136
}
129137

130138
[Fact]
@@ -133,16 +141,17 @@ public async Task ProcessAsync_BindsRouteValuesFromTagHelperOutput()
133141
// Arrange
134142
var testViewContext = CreateViewContext();
135143
var context = new TagHelperContext(
136-
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
144+
allAttributes: new Dictionary<string, object>(),
145+
uniqueId: "test",
146+
getChildContentAsync: () => Task.FromResult("Something"));
137147
var expectedAttribute = new KeyValuePair<string, string>("asp-ROUTEE-NotRoute", "something");
138148
var output = new TagHelperOutput(
139149
"form",
140150
attributes: new Dictionary<string, string>()
141151
{
142152
{ "asp-route-val", "hello" },
143153
{ "asp-roUte--Foo", "bar" }
144-
},
145-
content: string.Empty);
154+
});
146155
output.Attributes.Add(expectedAttribute);
147156

148157
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
@@ -184,7 +193,9 @@ public async Task ProcessAsync_BindsRouteValuesFromTagHelperOutput()
184193
Assert.Equal("form", output.TagName);
185194
var attribute = Assert.Single(output.Attributes);
186195
Assert.Equal(expectedAttribute, attribute);
196+
Assert.Empty(output.PreContent);
187197
Assert.Empty(output.Content);
198+
Assert.Empty(output.PostContent);
188199
generator.Verify();
189200
}
190201

@@ -194,11 +205,12 @@ public async Task ProcessAsync_CallsIntoGenerateFormWithExpectedParameters()
194205
// Arrange
195206
var viewContext = CreateViewContext();
196207
var context = new TagHelperContext(
197-
allAttributes: new Dictionary<string, object>(), uniqueId: "test");
208+
allAttributes: new Dictionary<string, object>(),
209+
uniqueId: "test",
210+
getChildContentAsync: () => Task.FromResult("Something"));
198211
var output = new TagHelperOutput(
199212
"form",
200-
attributes: new Dictionary<string, string>(),
201-
content: string.Empty);
213+
attributes: new Dictionary<string, string>());
202214
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
203215
generator
204216
.Setup(mock => mock.GenerateForm(viewContext, "Index", "Home", null, "POST", null))
@@ -220,7 +232,9 @@ public async Task ProcessAsync_CallsIntoGenerateFormWithExpectedParameters()
220232

221233
Assert.Equal("form", output.TagName);
222234
Assert.Empty(output.Attributes);
235+
Assert.Empty(output.PreContent);
223236
Assert.Empty(output.Content);
237+
Assert.Empty(output.PostContent);
224238
}
225239

226240
[Theory]
@@ -238,14 +252,15 @@ public async Task ProcessAsync_RestoresBoundAttributesIfActionIsSpecified(string
238252
attributes: new Dictionary<string, string>
239253
{
240254
{ "aCTiON", htmlAction },
241-
},
242-
content: string.Empty);
255+
});
256+
243257
var context = new TagHelperContext(
244258
allAttributes: new Dictionary<string, object>()
245259
{
246260
{ "METhod", "POST" }
247261
},
248-
uniqueId: "test");
262+
uniqueId: "test",
263+
getChildContentAsync: () => Task.FromResult("Something"));
249264

250265
// Act
251266
await formTagHelper.ProcessAsync(context, output);
@@ -257,7 +272,9 @@ public async Task ProcessAsync_RestoresBoundAttributesIfActionIsSpecified(string
257272
Assert.Equal(htmlAction, attribute.Value);
258273
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("METhod"));
259274
Assert.Equal("POST", attribute.Value);
275+
Assert.Empty(output.PreContent);
260276
Assert.Empty(output.Content);
277+
Assert.Empty(output.PostContent);
261278
}
262279

263280
[Theory]
@@ -266,11 +283,12 @@ public async Task ProcessAsync_RestoresBoundAttributesIfActionIsSpecified(string
266283
[InlineData(null, "")]
267284
public async Task ProcessAsync_SupportsAntiForgeryIfActionIsSpecified(
268285
bool? antiForgery,
269-
string expectedContent)
286+
string expectedPostContent)
270287
{
271288
// Arrange
272289
var viewContext = CreateViewContext();
273290
var generator = new Mock<IHtmlGenerator>();
291+
274292
generator.Setup(mock => mock.GenerateAntiForgery(It.IsAny<ViewContext>()))
275293
.Returns(new TagBuilder("input"));
276294
var formTagHelper = new FormTagHelper
@@ -284,9 +302,15 @@ public async Task ProcessAsync_SupportsAntiForgeryIfActionIsSpecified(
284302
attributes: new Dictionary<string, string>
285303
{
286304
{ "aCTiON", "my-action" },
287-
},
288-
content: string.Empty);
289-
var context = new TagHelperContext(allAttributes: new Dictionary<string, object>(), uniqueId: "test");
305+
});
306+
var context = new TagHelperContext(
307+
allAttributes: new Dictionary<string, object>()
308+
{
309+
{ "aCTiON", "http://www.contoso.com" }
310+
},
311+
uniqueId: "test",
312+
getChildContentAsync: () => Task.FromResult("Something"));
313+
290314

291315
// Act
292316
await formTagHelper.ProcessAsync(context, output);
@@ -295,7 +319,9 @@ public async Task ProcessAsync_SupportsAntiForgeryIfActionIsSpecified(
295319
Assert.Equal("form", output.TagName);
296320
var attribute = Assert.Single(output.Attributes);
297321
Assert.Equal(new KeyValuePair<string, string>("aCTiON", "my-action"), attribute);
298-
Assert.Equal(expectedContent, output.Content);
322+
Assert.Empty(output.PreContent);
323+
Assert.Empty(output.Content);
324+
Assert.Equal(expectedPostContent, output.PostContent);
299325
}
300326

301327
[Theory]
@@ -314,8 +340,7 @@ public async Task ProcessAsync_ThrowsIfActionConflictsWithBoundAttributes(string
314340
attributes: new Dictionary<string, string>
315341
{
316342
{ "action", "my-action" },
317-
},
318-
content: string.Empty);
343+
});
319344
if (propertyName == "asp-route-")
320345
{
321346
tagHelperOutput.Attributes.Add("asp-route-foo", "bar");

0 commit comments

Comments
 (0)