Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
using static Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeAssert;
Expand Down Expand Up @@ -76,4 +77,47 @@ public void Execute_WrapsStatementInSectionNode()
Assert.Equal("Header", section.SectionName);
Children(section, c => Html(" <p>Hello World</p> ", c));
}

[Fact]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding WorkItem attributes

public void SectionDirective_IsRecognizedInLegacyFiles()
{
// Arrange
var content = "@section Header { <p>Hello World</p> }";
var source = TestRazorSourceDocument.Create(content, filePath: "test.cshtml", relativePath: "test.cshtml");

// Act
var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Legacy);
ProjectEngine.Engine.Process(codeDocument);

// Assert
var syntaxTree = codeDocument.GetSyntaxTree();
Assert.NotNull(syntaxTree);

// The section directive should be recognized without errors
var diagnostics = syntaxTree.Diagnostics;
Assert.Empty(diagnostics);
}

[Fact]
public void SectionDirective_IsNotRecognizedInComponentFiles()
{
// Arrange
var content = "@section Header { <p>Hello World</p> }";
var source = TestRazorSourceDocument.Create(content, filePath: "test.razor", relativePath: "test.razor");

// Act
var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
ProjectEngine.Engine.Process(codeDocument);

// Assert
var syntaxTree = codeDocument.GetSyntaxTree();
Assert.NotNull(syntaxTree);

// The section directive should NOT be recognized in component files.
// Verify that no RazorDirective node for 'section' exists in the syntax tree
var directiveNodes = syntaxTree.Root.DescendantNodes()
.OfType<Microsoft.AspNetCore.Razor.Language.Syntax.RazorDirectiveSyntax>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we similarly verify that such node exists in the test above?


Assert.DoesNotContain(directiveNodes, d => d.DirectiveDescriptor?.Directive == "section");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,4 @@ public void _WithDoubleTransition2()
{
ParseDocumentTest("@section s {<span foo='@DateTime.Now @@' />}", [SectionDirective.Directive]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void Register(RazorProjectEngineBuilder builder)
throw new ArgumentNullException(nameof(builder));
}

builder.AddDirective(Directive, RazorFileKind.Legacy, RazorFileKind.Component);
builder.AddDirective(Directive, RazorFileKind.Legacy);
builder.Features.Add(new SectionDirectivePass());
builder.AddTargetExtension(new SectionTargetExtension());
}
Expand Down