From bb3ad0ede4a1ca720f626b8d477c402148d548ac Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Thu, 11 Sep 2014 11:04:18 -0700 Subject: [PATCH] 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 --- .../Framework/BlockTypes.cs | 29 ++++++++++ .../Framework/ParserTestBase.cs | 55 ++++++++++++++++++- .../Parser/BlockTest.cs | 18 ------ 3 files changed, 82 insertions(+), 20 deletions(-) diff --git a/test/Microsoft.AspNet.Razor.Test/Framework/BlockTypes.cs b/test/Microsoft.AspNet.Razor.Test/Framework/BlockTypes.cs index 139b0882a3..3ff64637dd 100644 --- a/test/Microsoft.AspNet.Razor.Test/Framework/BlockTypes.cs +++ b/test/Microsoft.AspNet.Razor.Test/Framework/BlockTypes.cs @@ -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 { @@ -174,6 +176,33 @@ namespace Microsoft.AspNet.Razor.Test.Framework } } + public class MarkupTagHelperBlock : TagHelperBlock + { + public MarkupTagHelperBlock(string tagName) + : this(tagName, new Dictionary()) + { + } + + public MarkupTagHelperBlock(string tagName, + IDictionary attributes) + : this(tagName, attributes, new SyntaxTreeNode[0]) + { + } + + public MarkupTagHelperBlock(string tagName, + params SyntaxTreeNode[] children) + : this(tagName, new Dictionary(), children) + { + } + + public MarkupTagHelperBlock(string tagName, + IDictionary attributes, + params SyntaxTreeNode[] children) + : base(new TagHelperBlockBuilder(tagName, attributes, children)) + { + } + } + public class SectionBlock : Block { private const BlockType ThisBlockType = BlockType.Section; diff --git a/test/Microsoft.AspNet.Razor.Test/Framework/ParserTestBase.cs b/test/Microsoft.AspNet.Razor.Test/Framework/ParserTestBase.cs index 238a394595..e9ff6f0d62 100644 --- a/test/Microsoft.AspNet.Razor.Test/Framework/ParserTestBase.cs +++ b/test/Microsoft.AspNet.Razor.Test/Framework/ParserTestBase.cs @@ -11,6 +11,7 @@ using System.Text; 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; @@ -291,6 +292,22 @@ namespace Microsoft.AspNet.Razor.Test.Framework } } + private static void EvaluateTagHelperAttribute(ErrorCollector collector, + KeyValuePair actual, + KeyValuePair 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)) @@ -311,11 +328,16 @@ namespace Microsoft.AspNet.Razor.Test.Framework } else { + if (actual is TagHelperBlock) + { + EvaluateTagHelperBlock(collector, actual as TagHelperBlock, expected as TagHelperBlock); + } + AddPassedMessage(collector, expected); using (collector.Indent()) { - IEnumerator expectedNodes = expected.Children.GetEnumerator(); - IEnumerator actualNodes = actual.Children.GetEnumerator(); + var expectedNodes = expected.Children.GetEnumerator(); + var actualNodes = actual.Children.GetEnumerator(); while (expectedNodes.MoveNext()) { if (!actualNodes.MoveNext()) @@ -335,6 +357,35 @@ namespace Microsoft.AspNet.Razor.Test.Framework } } + private static void EvaluateTagHelperBlock(ErrorCollector collector, TagHelperBlock actual, TagHelperBlock expected) + { + if (expected == null) + { + AddMismatchError(collector, actual, expected); + } + else + { + var expectedAttributes = expected.Attributes.GetEnumerator(); + var 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); diff --git a/test/Microsoft.AspNet.Razor.Test/Parser/BlockTest.cs b/test/Microsoft.AspNet.Razor.Test/Parser/BlockTest.cs index f203a33b43..179e7b4b22 100644 --- a/test/Microsoft.AspNet.Razor.Test/Parser/BlockTest.cs +++ b/test/Microsoft.AspNet.Razor.Test/Parser/BlockTest.cs @@ -28,24 +28,6 @@ namespace Microsoft.AspNet.Razor.Test.Parser 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() {