aspnetcore/test/Microsoft.AspNetCore.Razor..../DefaultRazorIntermediateNod...

84 lines
3.7 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Testing;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Language
{
public class DefaultRazorIntermediateNodeLoweringPhaseTest
{
[Fact]
public void Execute_ThrowsForMissingDependency_SyntaxTree()
{
// Arrange
var phase = new DefaultRazorIntermediateNodeLoweringPhase();
var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase));
var codeDocument = TestRazorCodeDocument.CreateEmpty();
// Act & Assert
ExceptionAssert.Throws<InvalidOperationException>(
() => phase.Execute(codeDocument),
$"The '{nameof(DefaultRazorIntermediateNodeLoweringPhase)}' phase requires a '{nameof(RazorSyntaxTree)}' " +
$"provided by the '{nameof(RazorCodeDocument)}'.");
}
[Fact]
public void Execute_CollatesSyntaxDiagnosticsFromSourceDocument()
{
// Arrange
var phase = new DefaultRazorIntermediateNodeLoweringPhase();
var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase));
var codeDocument = TestRazorCodeDocument.Create("<p class=@(");
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source));
var options = RazorCodeGenerationOptions.CreateDefault();
// Act
phase.Execute(codeDocument);
// Assert
var documentNode = codeDocument.GetDocumentIntermediateNode();
var diagnostic = Assert.Single(documentNode.Diagnostics);
Assert.Equal(@"The explicit expression block is missing a closing "")"" character. Make sure you have a matching "")"" character for all the ""("" characters within this block, and that none of the "")"" characters are being interpreted as markup.",
diagnostic.GetMessage());
}
[Fact]
public void Execute_CollatesSyntaxDiagnosticsFromImportDocuments()
{
// Arrange
var phase = new DefaultRazorIntermediateNodeLoweringPhase();
var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase));
var codeDocument = TestRazorCodeDocument.CreateEmpty();
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source));
codeDocument.SetImportSyntaxTrees(new[]
{
RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("@ ")),
RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("<p @(")),
});
var options = RazorCodeGenerationOptions.CreateDefault();
// Act
phase.Execute(codeDocument);
// Assert
var documentNode = codeDocument.GetDocumentIntermediateNode();
Assert.Collection(documentNode.Diagnostics,
diagnostic =>
{
Assert.Equal(@"A space or line break was encountered after the ""@"" character. Only valid identifiers, keywords, comments, ""("" and ""{"" are valid at the start of a code block and they must occur immediately following ""@"" with no space in between.",
diagnostic.GetMessage());
},
diagnostic =>
{
Assert.Equal(@"The explicit expression block is missing a closing "")"" character. Make sure you have a matching "")"" character for all the ""("" characters within this block, and that none of the "")"" characters are being interpreted as markup.",
diagnostic.GetMessage());
});
}
}
}