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

Commit 3aa9323

Browse files
author
N. Taylor Mullen
committed
Addressed code review comments.
1 parent 99f8720 commit 3aa9323

File tree

4 files changed

+50
-56
lines changed

4 files changed

+50
-56
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("Microsoft.AspNet.Mvc.TagHelpers.Test")]

src/Microsoft.AspNet.Mvc.TagHelpers/ValidationSummary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public enum ValidationSummary
1414
None,
1515

1616
/// <summary>
17-
/// Validation summary with model-level errors only.
17+
/// Validation summary with model-level errors only (excludes all property errors).
1818
/// </summary>
1919
ModelOnly,
2020

src/Microsoft.AspNet.Mvc.TagHelpers/ValidationSummaryTagHelper.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,31 @@
99
namespace Microsoft.AspNet.Mvc.TagHelpers
1010
{
1111
/// <summary>
12-
/// <see cref="ITagHelper"/> implementation targeting &lt;div&gt; elements with <c>validation-summary</c>
13-
/// attributes.
14-
/// </summary>S
12+
/// <see cref="ITagHelper"/> implementation targeting &lt;div&gt; elements with a <c>validation-summary</c>
13+
/// attribute.
14+
/// </summary>
1515
[TagName("div")]
1616
[ContentBehavior(ContentBehavior.Append)]
1717
public class ValidationSummaryTagHelper : TagHelper
1818
{
19+
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing.
1920
[Activate]
20-
private ViewContext ViewContext { get; set; }
21+
protected internal ViewContext ViewContext { get; set; }
2122

23+
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing.
2224
[Activate]
23-
private IHtmlGenerator Generator { get; set; }
25+
protected internal IHtmlGenerator Generator { get; set; }
2426

2527
// TODO: Change to ValidationSummary enum once https://github.com/aspnet/Razor/issues/196 has been completed.
2628
/// <summary>
27-
/// Acceptable values are defined by the <see cref="ValidationSummary"/> enum.
29+
/// If <c>All</c> or <c>ModelOnly</c>, appends a validation summary. Acceptable values are defined by the
30+
/// <see cref="ValidationSummary"/> enum.
2831
/// </summary>
2932
[HtmlAttributeName("validation-summary")]
3033
public string ValidationSummaryValue { get; set; }
3134

3235
/// <inheritdoc />
36+
/// Does nothing if <see cref="ValidationSummaryValue"/> is <c>null</c>, empty or "None".
3337
public override void Process(TagHelperContext context, TagHelperOutput output)
3438
{
3539
if (!string.IsNullOrEmpty(ValidationSummaryValue))
@@ -41,9 +45,9 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
4145
Resources.FormatValidationSummaryTagHelper_InvalidValidationSummaryValue(
4246
"validation-summary",
4347
ValidationSummaryValue,
44-
ValidationSummary.All.ToString(),
45-
ValidationSummary.ModelOnly.ToString(),
46-
ValidationSummary.None.ToString()));
48+
ValidationSummary.All,
49+
ValidationSummary.ModelOnly,
50+
ValidationSummary.None));
4751
}
4852
else if (validationSummaryValue == ValidationSummary.None)
4953
{

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

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNet.Mvc.ModelBinding;
1111
using Microsoft.AspNet.Mvc.Razor;
1212
using Microsoft.AspNet.Mvc.Rendering;
13+
using Microsoft.AspNet.PipelineCore;
1314
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
1415
using Microsoft.AspNet.Routing;
1516
using Moq;
@@ -41,8 +42,8 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
4142
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
4243
Model model = null;
4344
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
44-
var activator = new DefaultTagHelperActivator();
45-
activator.Activate(validationSummaryTagHelper, viewContext);
45+
validationSummaryTagHelper.ViewContext = viewContext;
46+
validationSummaryTagHelper.Generator = htmlGenerator;
4647

4748
// Act
4849
await validationSummaryTagHelper.ProcessAsync(tagHelperContext, output);
@@ -76,8 +77,8 @@ public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedPar
7677
.Setup(mock => mock.GenerateValidationSummary(expectedViewContext, true, null, null, null))
7778
.Returns(new TagBuilder("div"))
7879
.Verifiable();
79-
80-
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
80+
validationSummaryTagHelper.ViewContext = expectedViewContext;
81+
validationSummaryTagHelper.Generator = generator.Object;
8182

8283
// Act & Assert
8384
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
@@ -107,29 +108,33 @@ public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
107108

108109
tagBuilder.Attributes.Add("data-foo", "bar");
109110
tagBuilder.Attributes.Add("data-hello", "world");
111+
tagBuilder.Attributes.Add("anything", "something");
110112

111-
var expectedViewContext = CreateViewContext();
112113
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
113-
var setup = generator
114-
.Setup(mock => mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
114+
generator
115+
.Setup(mock => mock.GenerateValidationSummary(
116+
It.IsAny<ViewContext>(),
115117
It.IsAny<bool>(),
116118
It.IsAny<string>(),
117119
It.IsAny<string>(),
118120
It.IsAny<object>()))
119121
.Returns(tagBuilder);
120-
121-
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
122+
var viewContext = CreateViewContext();
123+
validationSummaryTagHelper.ViewContext = viewContext;
124+
validationSummaryTagHelper.Generator = generator.Object;
122125

123126
// Act
124127
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
125128

126129
// Assert
127130
Assert.Equal(output.TagName, "div");
128-
Assert.Equal(2, output.Attributes.Count);
131+
Assert.Equal(3, output.Attributes.Count);
129132
var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-foo"));
130133
Assert.Equal("bar", attribute.Value);
131134
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-hello"));
132135
Assert.Equal("world", attribute.Value);
136+
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("anything"));
137+
Assert.Equal("something", attribute.Value);
133138
Assert.Equal("Content of validation summaryNew HTML", output.Content);
134139
}
135140

@@ -148,10 +153,10 @@ public async Task ProcessAsync_DoesNothingIfNullOrEmptyValidationSummaryValue(st
148153
attributes: new Dictionary<string, string>(),
149154
content: "Content of validation message");
150155

151-
var expectedViewContext = CreateViewContext();
152156
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
153-
154-
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
157+
var viewContext = CreateViewContext();
158+
validationSummaryTagHelper.ViewContext = viewContext;
159+
validationSummaryTagHelper.Generator = generator.Object;
155160

156161
// Act
157162
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
@@ -183,18 +188,19 @@ public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone_IgnoresCase
183188
InnerHtml = "New HTML"
184189
};
185190

186-
var expectedViewContext = CreateViewContext();
187-
var generator = new Mock<IHtmlGenerator>(MockBehavior.Loose);
191+
var generator = new Mock<IHtmlGenerator>();
188192
generator
189-
.Setup(mock => mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
193+
.Setup(mock => mock.GenerateValidationSummary(
194+
It.IsAny<ViewContext>(),
190195
It.IsAny<bool>(),
191196
It.IsAny<string>(),
192197
It.IsAny<string>(),
193198
It.IsAny<object>()))
194199
.Returns(tagBuilder)
195200
.Verifiable();
196-
197-
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
201+
var viewContext = CreateViewContext();
202+
validationSummaryTagHelper.ViewContext = viewContext;
203+
validationSummaryTagHelper.Generator = generator.Object;
198204

199205
// Act
200206
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
@@ -225,14 +231,7 @@ public async Task ProcessAsync_DoesNotGenerateValidationSummaryWhenNone_IgnoresC
225231
InnerHtml = "New HTML"
226232
};
227233

228-
var generator = new Mock<IHtmlGenerator>(MockBehavior.Loose);
229-
generator
230-
.Setup(mock => mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
231-
It.IsAny<bool>(),
232-
It.IsAny<string>(),
233-
It.IsAny<string>(),
234-
It.IsAny<object>()))
235-
.Throws(new Exception("Shouldn't be called."));
234+
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
236235

237236
// Act
238237
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
@@ -260,10 +259,8 @@ public async Task ProcessAsync_ThrowsWhenInvalidValidationSummaryValue()
260259
"'All', 'ModelOnly' and 'None'.";
261260

262261
// Act
263-
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
264-
{
265-
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
266-
});
262+
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() =>
263+
validationSummaryTagHelper.ProcessAsync(context: null, output: output));
267264

268265
// Assert
269266
Assert.Equal(expectedMessage, ex.Message);
@@ -272,31 +269,18 @@ public async Task ProcessAsync_ThrowsWhenInvalidValidationSummaryValue()
272269
private static ViewContext CreateViewContext()
273270
{
274271
var actionContext = new ActionContext(
275-
new Mock<HttpContext>().Object,
272+
new DefaultHttpContext(),
276273
new RouteData(),
277274
new ActionDescriptor());
278275

279276
return new ViewContext(
280277
actionContext,
281278
Mock.Of<IView>(),
282279
new ViewDataDictionary(
283-
new DataAnnotationsModelMetadataProvider()),
284-
new StringWriter());
280+
new EmptyModelMetadataProvider()),
281+
TextWriter.Null);
285282
}
286283

287-
private static void SetViewContextAndGenerator(ITagHelper tagHelper,
288-
ViewContext viewContext,
289-
IHtmlGenerator generator)
290-
{
291-
var tagHelperType = tagHelper.GetType();
292-
293-
tagHelperType.GetProperty("ViewContext", BindingFlags.NonPublic | BindingFlags.Instance)
294-
.SetValue(tagHelper, viewContext);
295-
tagHelperType.GetProperty("Generator", BindingFlags.NonPublic | BindingFlags.Instance)
296-
.SetValue(tagHelper, generator);
297-
}
298-
299-
300284
private class Model
301285
{
302286
public string Text { get; set; }

0 commit comments

Comments
 (0)