Skip to content

Commit 45e62cb

Browse files
Fix DefaultRazorParsingPhaseTest
Because RazorCodeDocument now owns its ParserOptions and CodeGenerationOptions, it is generally important to create a RazorCodeDocument by calling RazorProjectEngine.CreateCodeDocument(...) in order to configure the options correctly.
1 parent cd5a228 commit 45e62cb

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/DefaultRazorParsingPhaseTest.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ public void Execute_AddsSyntaxTree()
1313
{
1414
// Arrange
1515
var phase = new DefaultRazorParsingPhase();
16-
var engine = RazorProjectEngine.CreateEmpty(builder =>
16+
17+
var projectEngine = RazorProjectEngine.CreateEmpty(builder =>
1718
{
1819
builder.Phases.Add(phase);
1920
builder.Features.Add(new DefaultRazorParserOptionsFeature());
2021
});
2122

22-
var codeDocument = TestRazorCodeDocument.CreateEmpty();
23+
var codeDocument = projectEngine.CreateEmptyCodeDocument();
2324

2425
// Act
2526
phase.Execute(codeDocument);
@@ -33,14 +34,15 @@ public void Execute_UsesConfigureParserFeatures()
3334
{
3435
// Arrange
3536
var phase = new DefaultRazorParsingPhase();
36-
var engine = RazorProjectEngine.CreateEmpty((builder) =>
37+
38+
var projectEngine = RazorProjectEngine.CreateEmpty((builder) =>
3739
{
3840
builder.Phases.Add(phase);
3941
builder.Features.Add(new DefaultRazorParserOptionsFeature());
40-
builder.ConfigureParserOptions(ConfigureDirective);
42+
builder.AddDirective(CreateDirective());
4143
});
4244

43-
var codeDocument = TestRazorCodeDocument.CreateEmpty();
45+
var codeDocument = projectEngine.CreateEmptyCodeDocument();
4446

4547
// Act
4648
phase.Execute(codeDocument);
@@ -56,20 +58,20 @@ public void Execute_ParsesImports()
5658
{
5759
// Arrange
5860
var phase = new DefaultRazorParsingPhase();
59-
var engine = RazorProjectEngine.CreateEmpty((builder) =>
61+
62+
var projectEngine = RazorProjectEngine.CreateEmpty(builder =>
6063
{
6164
builder.Phases.Add(phase);
6265
builder.Features.Add(new DefaultRazorParserOptionsFeature());
63-
builder.ConfigureParserOptions(ConfigureDirective);
66+
builder.AddDirective(CreateDirective());
6467
});
6568

66-
var sourceDocument = TestRazorSourceDocument.Create();
67-
68-
var imports = ImmutableArray.Create(
69+
var source = TestRazorSourceDocument.Create();
70+
var importSources = ImmutableArray.Create(
6971
TestRazorSourceDocument.Create(),
7072
TestRazorSourceDocument.Create());
7173

72-
var codeDocument = RazorCodeDocument.Create(sourceDocument, imports: default);
74+
var codeDocument = projectEngine.CreateCodeDocument(source, importSources);
7375

7476
// Act
7577
phase.Execute(codeDocument);
@@ -79,12 +81,10 @@ public void Execute_ParsesImports()
7981
Assert.False(importSyntaxTrees.IsDefault);
8082
Assert.Collection(
8183
importSyntaxTrees,
82-
t => { Assert.Same(t.Source, imports[0]); Assert.Equal("test", Assert.Single(t.Options.Directives).Directive); },
83-
t => { Assert.Same(t.Source, imports[1]); Assert.Equal("test", Assert.Single(t.Options.Directives).Directive); });
84+
t => { Assert.Same(t.Source, importSources[0]); Assert.Equal("test", Assert.Single(t.Options.Directives).Directive); },
85+
t => { Assert.Same(t.Source, importSources[1]); Assert.Equal("test", Assert.Single(t.Options.Directives).Directive); });
8486
}
8587

86-
private static void ConfigureDirective(RazorParserOptions.Builder builder)
87-
{
88-
builder.Directives = [DirectiveDescriptor.CreateDirective("test", DirectiveKind.SingleLine)];
89-
}
88+
private static DirectiveDescriptor CreateDirective()
89+
=> DirectiveDescriptor.CreateDirective("test", DirectiveKind.SingleLine);
9090
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectEngine.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,16 @@ internal RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem, bool
146146
: CreateCodeDocumentCore(projectItem);
147147
}
148148

149-
internal RazorCodeDocument CreateCodeDocument(RazorSourceDocument source, string fileKind)
149+
internal RazorCodeDocument CreateCodeDocument(
150+
RazorSourceDocument source,
151+
string fileKind,
152+
ImmutableArray<RazorSourceDocument> importSources,
153+
IReadOnlyList<TagHelperDescriptor>? tagHelpers,
154+
string? cssScope)
150155
{
151156
ArgHelper.ThrowIfNull(source);
152157

153-
return CreateCodeDocumentCore(source, fileKind, importSources: default, tagHelpers: null, cssScope: null, configureParser: null, configureCodeGeneration: null);
158+
return CreateCodeDocumentCore(source, fileKind, importSources, tagHelpers, cssScope, configureParser: null, configureCodeGeneration: null);
154159
}
155160

156161
private RazorCodeDocument CreateCodeDocumentCore(
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Immutable;
5+
6+
namespace Microsoft.AspNetCore.Razor.Language;
7+
8+
public static class RazorProjectEngineExtensions
9+
{
10+
private static string DefaultFileKind => FileKinds.Legacy;
11+
12+
public static RazorCodeDocument CreateEmptyCodeDocument(this RazorProjectEngine projectEngine)
13+
{
14+
var source = TestRazorSourceDocument.Create(content: string.Empty);
15+
16+
return projectEngine.CreateCodeDocument(source, DefaultFileKind, importSources: default);
17+
}
18+
19+
public static RazorCodeDocument CreateCodeDocument(
20+
this RazorProjectEngine projectEngine,
21+
RazorSourceDocument source,
22+
string fileKind)
23+
{
24+
return projectEngine.CreateCodeDocument(source, fileKind, importSources: default);
25+
}
26+
27+
public static RazorCodeDocument CreateCodeDocument(
28+
this RazorProjectEngine projectEngine,
29+
RazorSourceDocument source,
30+
ImmutableArray<RazorSourceDocument> importSources)
31+
{
32+
return projectEngine.CreateCodeDocument(source, fileKind: DefaultFileKind, importSources);
33+
}
34+
35+
public static RazorCodeDocument CreateCodeDocument(
36+
this RazorProjectEngine projectEngine,
37+
RazorSourceDocument source,
38+
string fileKind,
39+
ImmutableArray<RazorSourceDocument> importSources)
40+
{
41+
return projectEngine.CreateCodeDocument(source, fileKind, importSources, tagHelpers: null, cssScope: null);
42+
}
43+
}

0 commit comments

Comments
 (0)