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

Commit

Permalink
Fix test to work with TH rewriters.
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
NTaylorMullen committed Sep 9, 2014
1 parent fbc8df5 commit 2aae557
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
29 changes: 29 additions & 0 deletions test/Microsoft.AspNet.Razor.Test/Framework/BlockTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Microsoft.AspNet.Razor.Parser.TagHelpers;

namespace Microsoft.AspNet.Razor.Test.Framework
{
Expand Down Expand Up @@ -174,6 +176,33 @@ public MarkupBlock(IEnumerable<SyntaxTreeNode> children)
}
}

public class MarkupTagHelperBlock : TagHelperBlock
{
public MarkupTagHelperBlock(string tagName)
: this(tagName, new Dictionary<string, SyntaxTreeNode>())
{
}

public MarkupTagHelperBlock(string tagName,
IDictionary<string, SyntaxTreeNode> attributes)
: this(tagName, attributes, new SyntaxTreeNode[0])
{
}

public MarkupTagHelperBlock(string tagName,
params SyntaxTreeNode[] children)
: this(tagName, new Dictionary<string, SyntaxTreeNode>(), children)
{
}

public MarkupTagHelperBlock(string tagName,
IDictionary<string, SyntaxTreeNode> attributes,
params SyntaxTreeNode[] children)
: base(new TagHelperBlockBuilder(tagName, attributes, children))
{
}
}

public class SectionBlock : Block
{
private const BlockType ThisBlockType = BlockType.Section;
Expand Down
55 changes: 53 additions & 2 deletions test/Microsoft.AspNet.Razor.Test/Framework/ParserTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Microsoft.AspNet.Razor.Parser.TagHelpers;
using Microsoft.AspNet.Razor.Text;
using Xunit;

Expand Down Expand Up @@ -291,6 +292,22 @@ private static void EvaluateSyntaxTreeNode(ErrorCollector collector, SyntaxTreeN
}
}

private static void EvaluateTagHelperAttribute(ErrorCollector collector,
KeyValuePair<string, SyntaxTreeNode> actual,
KeyValuePair<string, SyntaxTreeNode> expected)
{
if (actual.Key != expected.Key)
{
collector.AddError("{0} - FAILED :: Attribute names do not match", expected.Key);
}
else
{
collector.AddMessage("{0} - PASSED :: Attribute names match", expected.Key);
}

EvaluateSyntaxTreeNode(collector, actual.Value, expected.Value);
}

private static void EvaluateSpan(ErrorCollector collector, Span actual, Span expected)
{
if (!Equals(expected, actual))
Expand All @@ -311,11 +328,16 @@ private static void EvaluateBlock(ErrorCollector collector, Block actual, Block
}
else
{
if (actual is TagHelperBlock)
{
EvaluateTagHelperBlock(collector, actual as TagHelperBlock, expected as TagHelperBlock);
}

AddPassedMessage(collector, expected);
using (collector.Indent())
{
IEnumerator<SyntaxTreeNode> expectedNodes = expected.Children.GetEnumerator();
IEnumerator<SyntaxTreeNode> actualNodes = actual.Children.GetEnumerator();
var expectedNodes = expected.Children.GetEnumerator();
var actualNodes = actual.Children.GetEnumerator();
while (expectedNodes.MoveNext())
{
if (!actualNodes.MoveNext())
Expand All @@ -335,6 +357,35 @@ private static void EvaluateBlock(ErrorCollector collector, Block actual, Block
}
}

private static void EvaluateTagHelperBlock(ErrorCollector collector, TagHelperBlock actual, TagHelperBlock expected)
{
if (expected == null)
{
AddMismatchError(collector, actual, expected);
}
else
{
IEnumerator<KeyValuePair<string, SyntaxTreeNode>> expectedAttributes = expected.Attributes.GetEnumerator();
IEnumerator<KeyValuePair<string, SyntaxTreeNode>> actualAttributes = actual.Attributes.GetEnumerator();

while (expectedAttributes.MoveNext())
{
if (!actualAttributes.MoveNext())
{
collector.AddError("{0} - FAILED :: No more attributes on this node", expectedAttributes.Current);
}
else
{
EvaluateTagHelperAttribute(collector, actualAttributes.Current, expectedAttributes.Current);
}
}
while (actualAttributes.MoveNext())
{
collector.AddError("End of Attributes - FAILED :: Found Attribute: {0}", actualAttributes.Current.Key);
}
}
}

private static void AddPassedMessage(ErrorCollector collector, SyntaxTreeNode expected)
{
collector.AddMessage("{0} - PASSED", expected);
Expand Down
18 changes: 0 additions & 18 deletions test/Microsoft.AspNet.Razor.Test/Parser/BlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@ public void ConstructorWithBlockBuilderSetsParent()
Assert.Same(block, span.Parent);
}

[Fact]
public void ConstructorCopiesBasicValuesFromBlockBuilder()
{
// Arrange
BlockBuilder builder = new BlockBuilder()
{
Name = "Foo",
Type = BlockType.Helper
};

// Act
Block actual = builder.Build();

// Assert
Assert.Equal("Foo", actual.Name);
Assert.Equal(BlockType.Helper, actual.Type);
}

[Fact]
public void ConstructorTransfersInstanceOfCodeGeneratorFromBlockBuilder()
{
Expand Down

0 comments on commit 2aae557

Please sign in to comment.