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

Commit 0e04ec5

Browse files
author
N. Taylor Mullen
committed
Fix test to work with TH rewriters.
- Added understanding to the ParserTestBase for TagHelperBlock's - Added a helper class MarkupTagHelperBlock to make building test TagHelpers easier. - Fixed some existing tests that "new"d up the RazorParser and didn't obide by the new contract (to provide optimizers). #71
1 parent 2a9b522 commit 0e04ec5

File tree

3 files changed

+82
-20
lines changed

3 files changed

+82
-20
lines changed

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

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

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Microsoft.AspNet.Razor.Generator;
67
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
8+
using Microsoft.AspNet.Razor.Parser.TagHelpers;
79

810
namespace Microsoft.AspNet.Razor.Test.Framework
911
{
@@ -174,6 +176,33 @@ public MarkupBlock(IEnumerable<SyntaxTreeNode> children)
174176
}
175177
}
176178

179+
public class MarkupTagHelperBlock : TagHelperBlock
180+
{
181+
public MarkupTagHelperBlock(string tagName)
182+
: this(tagName, new Dictionary<string, SyntaxTreeNode>())
183+
{
184+
}
185+
186+
public MarkupTagHelperBlock(string tagName,
187+
IDictionary<string, SyntaxTreeNode> attributes)
188+
: this(tagName, attributes, new SyntaxTreeNode[0])
189+
{
190+
}
191+
192+
public MarkupTagHelperBlock(string tagName,
193+
params SyntaxTreeNode[] children)
194+
: this(tagName, new Dictionary<string, SyntaxTreeNode>(), children)
195+
{
196+
}
197+
198+
public MarkupTagHelperBlock(string tagName,
199+
IDictionary<string, SyntaxTreeNode> attributes,
200+
params SyntaxTreeNode[] children)
201+
: base(new TagHelperBlockBuilder(tagName, attributes, children))
202+
{
203+
}
204+
}
205+
177206
public class SectionBlock : Block
178207
{
179208
private const BlockType ThisBlockType = BlockType.Section;

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNet.Razor.Generator;
1212
using Microsoft.AspNet.Razor.Parser;
1313
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
14+
using Microsoft.AspNet.Razor.Parser.TagHelpers;
1415
using Microsoft.AspNet.Razor.Text;
1516
using Xunit;
1617

@@ -291,6 +292,22 @@ private static void EvaluateSyntaxTreeNode(ErrorCollector collector, SyntaxTreeN
291292
}
292293
}
293294

295+
private static void EvaluateTagHelperAttribute(ErrorCollector collector,
296+
KeyValuePair<string, SyntaxTreeNode> actual,
297+
KeyValuePair<string, SyntaxTreeNode> expected)
298+
{
299+
if (actual.Key != expected.Key)
300+
{
301+
collector.AddError("{0} - FAILED :: Attribute names do not match", expected.Key);
302+
}
303+
else
304+
{
305+
collector.AddMessage("{0} - PASSED :: Attribute names match", expected.Key);
306+
}
307+
308+
EvaluateSyntaxTreeNode(collector, actual.Value, expected.Value);
309+
}
310+
294311
private static void EvaluateSpan(ErrorCollector collector, Span actual, Span expected)
295312
{
296313
if (!Equals(expected, actual))
@@ -311,11 +328,16 @@ private static void EvaluateBlock(ErrorCollector collector, Block actual, Block
311328
}
312329
else
313330
{
331+
if (actual is TagHelperBlock)
332+
{
333+
EvaluateTagHelperBlock(collector, actual as TagHelperBlock, expected as TagHelperBlock);
334+
}
335+
314336
AddPassedMessage(collector, expected);
315337
using (collector.Indent())
316338
{
317-
IEnumerator<SyntaxTreeNode> expectedNodes = expected.Children.GetEnumerator();
318-
IEnumerator<SyntaxTreeNode> actualNodes = actual.Children.GetEnumerator();
339+
var expectedNodes = expected.Children.GetEnumerator();
340+
var actualNodes = actual.Children.GetEnumerator();
319341
while (expectedNodes.MoveNext())
320342
{
321343
if (!actualNodes.MoveNext())
@@ -335,6 +357,35 @@ private static void EvaluateBlock(ErrorCollector collector, Block actual, Block
335357
}
336358
}
337359

360+
private static void EvaluateTagHelperBlock(ErrorCollector collector, TagHelperBlock actual, TagHelperBlock expected)
361+
{
362+
if (expected == null)
363+
{
364+
AddMismatchError(collector, actual, expected);
365+
}
366+
else
367+
{
368+
var expectedAttributes = expected.Attributes.GetEnumerator();
369+
var actualAttributes = actual.Attributes.GetEnumerator();
370+
371+
while (expectedAttributes.MoveNext())
372+
{
373+
if (!actualAttributes.MoveNext())
374+
{
375+
collector.AddError("{0} - FAILED :: No more attributes on this node", expectedAttributes.Current);
376+
}
377+
else
378+
{
379+
EvaluateTagHelperAttribute(collector, actualAttributes.Current, expectedAttributes.Current);
380+
}
381+
}
382+
while (actualAttributes.MoveNext())
383+
{
384+
collector.AddError("End of Attributes - FAILED :: Found Attribute: {0}", actualAttributes.Current.Key);
385+
}
386+
}
387+
}
388+
338389
private static void AddPassedMessage(ErrorCollector collector, SyntaxTreeNode expected)
339390
{
340391
collector.AddMessage("{0} - PASSED", expected);

test/Microsoft.AspNet.Razor.Test/Parser/BlockTest.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,6 @@ public void ConstructorWithBlockBuilderSetsParent()
2828
Assert.Same(block, span.Parent);
2929
}
3030

31-
[Fact]
32-
public void ConstructorCopiesBasicValuesFromBlockBuilder()
33-
{
34-
// Arrange
35-
BlockBuilder builder = new BlockBuilder()
36-
{
37-
Name = "Foo",
38-
Type = BlockType.Helper
39-
};
40-
41-
// Act
42-
Block actual = builder.Build();
43-
44-
// Assert
45-
Assert.Equal("Foo", actual.Name);
46-
Assert.Equal(BlockType.Helper, actual.Type);
47-
}
48-
4931
[Fact]
5032
public void ConstructorTransfersInstanceOfCodeGeneratorFromBlockBuilder()
5133
{

0 commit comments

Comments
 (0)