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

Commit cb85b58

Browse files
author
N. Taylor Mullen
committed
Add tests to validate minimzed attributes.
- Added parse level rewriting tests to validate new TagHelper rewritten structures for minimized attributes. - Updated existing parser tests to understand minimized attributes. - Added codegen test to validate understanding of minimized attributes. - Added TagHelperExecutionContext tests to validate maintaining of runtime TagHelperOutput tests. #220
1 parent 2dc87ca commit cb85b58

12 files changed

+1552
-93
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using Microsoft.Internal.Web.Utils;
76

87
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
98
{
@@ -22,16 +21,13 @@ public bool Equals(IReadOnlyTagHelperAttribute attributeX, IReadOnlyTagHelperAtt
2221
attributeX == attributeY ||
2322
// Normal comparer doesn't care about the Name case, in tests we do.
2423
string.Equals(attributeX.Name, attributeY.Name, StringComparison.Ordinal) &&
25-
Equals(attributeX.Value, attributeY.Value);
24+
Equals(attributeX.Value, attributeY.Value) &&
25+
attributeX.Minimized == attributeY.Minimized;
2626
}
2727

2828
public int GetHashCode(IReadOnlyTagHelperAttribute attribute)
2929
{
30-
return HashCodeCombiner
31-
.Start()
32-
.Add(attribute.Name, StringComparer.Ordinal)
33-
.Add(attribute.Value)
34-
.CombinedHash;
30+
return attribute.GetHashCode();
3531
}
3632
}
3733
}

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void SelfClosing_ReturnsTrueOrFalseAsExpected(bool selfClosing)
2020
// Arrange & Act
2121
var executionContext = new TagHelperExecutionContext("p", selfClosing);
2222

23-
// Assert
23+
// Assert
2424
Assert.Equal(selfClosing, executionContext.SelfClosing);
2525
}
2626

@@ -181,7 +181,7 @@ public void AllAttributes_IgnoresCase(string originalName, string updatedName)
181181
}
182182

183183
[Fact]
184-
public void AddHtmlAttribute_MaintainsHTMLAttributes()
184+
public void AddHtmlAttribute_MaintainsHTMLAttributes_Unminimized()
185185
{
186186
// Arrange
187187
var executionContext = new TagHelperExecutionContext("p", selfClosing: false);
@@ -202,6 +202,54 @@ public void AddHtmlAttribute_MaintainsHTMLAttributes()
202202
CaseSensitiveTagHelperAttributeComparer.Default);
203203
}
204204

205+
[Fact]
206+
public void AddHtmlAttribute_MaintainsHTMLAttributes_Minimized()
207+
{
208+
// Arrange
209+
var executionContext = new TagHelperExecutionContext("input", selfClosing: true);
210+
var expectedAttributes = new TagHelperAttributeList
211+
{
212+
["checked"] = new TagHelperAttribute { Name = "checked", Minimized = true },
213+
["visible"] = new TagHelperAttribute { Name = "visible", Minimized = true }
214+
};
215+
216+
// Act
217+
executionContext.AddHtmlAttribute("checked");
218+
executionContext.AddHtmlAttribute("visible");
219+
220+
// Assert
221+
Assert.Equal(
222+
expectedAttributes,
223+
executionContext.HTMLAttributes,
224+
CaseSensitiveTagHelperAttributeComparer.Default);
225+
}
226+
227+
[Fact]
228+
public void AddHtmlAttribute_MaintainsHTMLAttributes_Mixed()
229+
{
230+
// Arrange
231+
var executionContext = new TagHelperExecutionContext("input", selfClosing: true);
232+
var expectedAttributes = new TagHelperAttributeList
233+
{
234+
{ "class", "btn" },
235+
{ "foo", "bar" }
236+
};
237+
expectedAttributes.Add(new TagHelperAttribute { Name = "checked", Minimized = true });
238+
expectedAttributes.Add(new TagHelperAttribute { Name = "visible", Minimized = true });
239+
240+
// Act
241+
executionContext.AddHtmlAttribute("class", "btn");
242+
executionContext.AddHtmlAttribute("foo", "bar");
243+
executionContext.AddHtmlAttribute("checked");
244+
executionContext.AddHtmlAttribute("visible");
245+
246+
// Assert
247+
Assert.Equal(
248+
expectedAttributes,
249+
executionContext.HTMLAttributes,
250+
CaseSensitiveTagHelperAttributeComparer.Default);
251+
}
252+
205253
[Fact]
206254
public void TagHelperExecutionContext_MaintainsAllAttributes()
207255
{

test/Microsoft.AspNet.Razor.Test/Framework/ParserTestBase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ private static void EvaluateSyntaxTreeNode(ErrorCollector collector, SyntaxTreeN
304304
if (actual == null)
305305
{
306306
AddNullActualError(collector, actual, expected);
307+
return;
307308
}
308309

309310
if (actual.IsBlock != expected.IsBlock)
@@ -336,7 +337,14 @@ private static void EvaluateTagHelperAttribute(ErrorCollector collector,
336337
collector.AddMessage("{0} - PASSED :: Attribute names match", expected.Key);
337338
}
338339

339-
EvaluateSyntaxTreeNode(collector, actual.Value, expected.Value);
340+
if (actual.Value == null && expected.Value == null)
341+
{
342+
collector.AddMessage("{0} - PASSED :: Attribute values match", expected.Key);
343+
}
344+
else
345+
{
346+
EvaluateSyntaxTreeNode(collector, actual.Value, expected.Value);
347+
}
340348
}
341349

342350
private static void EvaluateSpan(ErrorCollector collector, Span actual, Span expected)
@@ -412,7 +420,7 @@ private static void EvaluateTagHelperBlock(ErrorCollector collector, TagHelperBl
412420
actual.TagName,
413421
actual.SelfClosing);
414422
}
415-
423+
416424
var expectedAttributes = expected.Attributes.GetEnumerator();
417425
var actualAttributes = actual.Attributes.GetEnumerator();
418426

test/Microsoft.AspNet.Razor.Test/Generator/CSharpTagHelperRenderingTest.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,44 @@ private static IEnumerable<TagHelperDescriptor> DefaultPAndInputTagHelperDescrip
1919
private static IEnumerable<TagHelperDescriptor> PrefixedPAndInputTagHelperDescriptors
2020
=> BuildPAndInputTagHelperDescriptors("THS");
2121

22+
private static IEnumerable<TagHelperDescriptor> MinimizedTagHelperDescriptors
23+
{
24+
get
25+
{
26+
return new[]
27+
{
28+
new TagHelperDescriptor(
29+
tagName: "*",
30+
typeName: "CatchAllTagHelper",
31+
assemblyName: "SomeAssembly",
32+
attributes: new[]
33+
{
34+
new TagHelperAttributeDescriptor(
35+
"catchall-bound-string",
36+
"BoundRequiredString",
37+
typeof(string).FullName),
38+
},
39+
requiredAttributes: new[] { "catchall-unbound-required" }),
40+
new TagHelperDescriptor(
41+
tagName: "input",
42+
typeName: "InputTagHelper",
43+
assemblyName: "SomeAssembly",
44+
attributes: new[]
45+
{
46+
new TagHelperAttributeDescriptor(
47+
"input-bound-required-string",
48+
"BoundRequiredString",
49+
typeof(string).FullName),
50+
new TagHelperAttributeDescriptor(
51+
"input-bound-string",
52+
"BoundString",
53+
typeof(string).FullName)
54+
},
55+
requiredAttributes: new[] { "input-bound-required-string", "input-unbound-required" }),
56+
};
57+
}
58+
}
59+
2260
private static IEnumerable<TagHelperDescriptor> DuplicateTargetTagHelperDescriptors
2361
{
2462
get
@@ -207,6 +245,20 @@ public static TheoryData TagHelperDescriptorFlowTestData
207245
AttributeTargetingTagHelperDescriptors,
208246
AttributeTargetingTagHelperDescriptors,
209247
true
248+
},
249+
{
250+
"MinimizedTagHelpers",
251+
"MinimizedTagHelpers",
252+
MinimizedTagHelperDescriptors,
253+
MinimizedTagHelperDescriptors,
254+
false
255+
},
256+
{
257+
"MinimizedTagHelpers",
258+
"MinimizedTagHelpers.DesignTime",
259+
MinimizedTagHelperDescriptors,
260+
MinimizedTagHelperDescriptors,
261+
true
210262
}
211263
};
212264
}

test/Microsoft.AspNet.Razor.Test/Parser/Html/HtmlAttributeTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public void UnquotedLiteralAttribute()
7070
new MarkupBlock(new AttributeBlockCodeGenerator(name: "href", prefix: new LocationTagged<string>(" href=", 2, 0, 2), suffix: new LocationTagged<string>(string.Empty, 11, 0, 11)),
7171
Factory.Markup(" href=").With(SpanCodeGenerator.Null),
7272
Factory.Markup("Foo").With(new LiteralAttributeCodeGenerator(prefix: new LocationTagged<string>(string.Empty, 8, 0, 8), value: new LocationTagged<string>("Foo", 8, 0, 8)))),
73-
Factory.Markup(" Bar Baz />").Accepts(AcceptedCharacters.None))));
73+
new MarkupBlock(Factory.Markup(" Bar")),
74+
new MarkupBlock(Factory.Markup(" Baz")),
75+
Factory.Markup(" />").Accepts(AcceptedCharacters.None))));
7476
}
7577

7678
[Fact]

test/Microsoft.AspNet.Razor.Test/Parser/Html/HtmlDocumentTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public void ParseDocumentCorrectlyHandlesOddlySpacedHTMLElements()
7070
ParseDocumentTest("<div ><p class = 'bar'> Foo </p></div >",
7171
new MarkupBlock(
7272
BlockFactory.MarkupTagBlock("<div >"),
73-
BlockFactory.MarkupTagBlock("<p class = 'bar'>"),
73+
new MarkupTagBlock(
74+
Factory.Markup("<p"),
75+
new MarkupBlock(
76+
Factory.Markup(" class")),
77+
Factory.Markup(" = 'bar'>")),
7478
Factory.Markup(" Foo "),
7579
BlockFactory.MarkupTagBlock("</p>"),
7680
BlockFactory.MarkupTagBlock("</div >")));

0 commit comments

Comments
 (0)