Prereq for converting tag helper tests

This commit is contained in:
Ajay Bhargav Baaskaran 2018-06-27 15:56:20 -07:00
parent 0d7e716e6d
commit b232a1103a
3 changed files with 87 additions and 7 deletions

View File

@ -68,6 +68,12 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
.OrderBy(error => error.Span.AbsoluteIndex)
.ToList();
if (UseBaselineTests && !IsTheory)
{
BaselineTest(actualTree, verifySyntaxTree: false, actualErrors.ToArray());
return;
}
EvaluateRazorErrors(actualErrors, expectedErrors.ToList());
EvaluateParseTree(actualTree, expectedOutput);
}

View File

@ -162,16 +162,24 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
return Regex.Replace(content, "(?<!\r)\n", "\r\n", RegexOptions.None, TimeSpan.FromSeconds(10));
}
internal virtual void BaselineTest(RazorSyntaxTree syntaxTree)
internal virtual void BaselineTest(RazorSyntaxTree syntaxTree, bool verifySyntaxTree = true)
{
SyntaxTreeVerifier.Verify(syntaxTree);
if (verifySyntaxTree)
{
SyntaxTreeVerifier.Verify(syntaxTree);
}
AssertSyntaxTreeNodeMatchesBaseline(syntaxTree);
}
internal virtual void BaselineTest(Block root)
internal virtual void BaselineTest(Block root, bool verifySyntaxTree = true, params RazorDiagnostic[] diagnostics)
{
SyntaxTreeVerifier.Verify(root);
AssertSyntaxTreeNodeMatchesBaseline(root);
if (verifySyntaxTree)
{
SyntaxTreeVerifier.Verify(root);
}
AssertSyntaxTreeNodeMatchesBaseline(root, diagnostics);
}
internal RazorSyntaxTree ParseBlock(string document, bool designTime)

View File

@ -35,11 +35,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
if (block is TagHelperBlock tagHelperBlock)
{
// Write tag name
WriteSeparator();
Write(tagHelperBlock.TagName);
WriteSeparator();
Write(tagHelperBlock.TagMode);
// Write descriptors
foreach (var descriptor in tagHelperBlock.Binding.Descriptors)
{
WriteSeparator();
@ -48,6 +48,36 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
var typeName = descriptor.Name.Substring(descriptor.Name.LastIndexOf('.') + 1);
Write(typeName);
}
// Write tag mode, start tag and end tag
Depth++;
WriteNewLine();
WriteIndent();
Write(tagHelperBlock.TagMode);
WriteSeparator();
Write(GetNodeContent(tagHelperBlock.SourceStartTag));
if (tagHelperBlock.SourceEndTag != null)
{
Write(" ... ");
Write(GetNodeContent(tagHelperBlock.SourceEndTag));
}
// Write attributes
foreach (var attribute in tagHelperBlock.Attributes)
{
WriteNewLine();
WriteIndent();
Write(attribute.Name);
WriteSeparator();
Write(attribute.AttributeStructure);
Depth++;
WriteNewLine();
// Recursively render attribute value
VisitNode(attribute.Value);
Depth--;
}
Depth--;
}
}
@ -226,5 +256,41 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
_writer.Write(value);
}
private string GetNodeContent(SyntaxTreeNode node)
{
if (node is Span span)
{
return span.Content;
}
else if (node is Block block)
{
var content = string.Empty;
foreach (var child in block.Children)
{
content += GetNodeContent(child);
}
return content;
}
return string.Empty;
}
private void VisitNode(SyntaxTreeNode node)
{
Visit(node);
if (node is Block block)
{
Depth++;
foreach (var child in block.Children)
{
WriteNewLine();
VisitNode(child);
}
Depth--;
}
}
}
}