Skip to content

Commit e36c4be

Browse files
RazorEngineFeatureBase: Clean up and add Initialize method
This is the same as the previous commit but results in more affected downstream code due to the changes to IRazorEngineFeature. For simplicity, each IRazorEngineFeature implementation now inherits from RazorEngineFeatureBase.
1 parent 1e065d1 commit e36c4be

27 files changed

Lines changed: 222 additions & 169 deletions

File tree

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/test/ModelDirectiveTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#nullable disable
55

6-
using System.Text;
76
using Microsoft.AspNetCore.Razor.Language;
87
using Microsoft.AspNetCore.Razor.Language.Extensions;
98
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -339,7 +338,7 @@ public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateN
339338
}
340339
}
341340

342-
private class DesignTimeOptionsFeature : IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
341+
private class DesignTimeOptionsFeature : RazorEngineFeatureBase, IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
343342
{
344343
private readonly bool _designTime;
345344

@@ -350,8 +349,6 @@ public DesignTimeOptionsFeature(bool designTime)
350349

351350
public int Order { get; }
352351

353-
public RazorEngine Engine { get; set; }
354-
355352
public void Configure(RazorParserOptionsBuilder options)
356353
{
357354
options.SetDesignTime(_designTime);

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/ModelDirectiveTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#nullable disable
55

6-
using System.Text;
76
using Microsoft.AspNetCore.Razor.Language;
87
using Microsoft.AspNetCore.Razor.Language.Extensions;
98
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -339,7 +338,7 @@ public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateN
339338
}
340339
}
341340

342-
private class DesignTimeOptionsFeature : IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
341+
private class DesignTimeOptionsFeature : RazorEngineFeatureBase, IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
343342
{
344343
private readonly bool _designTime;
345344

@@ -350,8 +349,6 @@ public DesignTimeOptionsFeature(bool designTime)
350349

351350
public int Order { get; }
352351

353-
public RazorEngine Engine { get; set; }
354-
355352
public void Configure(RazorParserOptionsBuilder options)
356353
{
357354
options.SetDesignTime(_designTime);

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/NamespaceDirectiveTest.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33

44
#nullable disable
55

6-
using System;
7-
using System.Collections.Immutable;
86
using Microsoft.AspNetCore.Razor.Language;
97
using Microsoft.AspNetCore.Razor.Language.Intermediate;
10-
using Moq;
118
using Xunit;
129

1310
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X;
@@ -136,8 +133,10 @@ public void Pass_SetsNamespace_ComputedFromImports()
136133

137134
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));
138135

139-
var pass = new NamespaceDirective.Pass();
140-
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
136+
var pass = new NamespaceDirective.Pass()
137+
{
138+
Engine = new RazorEngine(features: [], phases: [])
139+
};
141140

142141
// Act
143142
pass.Execute(codeDocument, document);
@@ -183,8 +182,10 @@ public void Pass_SetsNamespace_ComputedFromSource()
183182

184183
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));
185184

186-
var pass = new NamespaceDirective.Pass();
187-
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
185+
var pass = new NamespaceDirective.Pass()
186+
{
187+
Engine = new RazorEngine(features: [], phases: [])
188+
};
188189

189190
// Act
190191
pass.Execute(codeDocument, document);
@@ -221,8 +222,10 @@ public void Pass_SetsNamespace_SanitizesClassAndNamespace()
221222

222223
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage-Info/Add+User.cshtml"));
223224

224-
var pass = new NamespaceDirective.Pass();
225-
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
225+
var pass = new NamespaceDirective.Pass()
226+
{
227+
Engine = new RazorEngine(features: [], phases: [])
228+
};
226229

227230
// Act
228231
pass.Execute(codeDocument, document);
@@ -268,8 +271,10 @@ public void Pass_SetsNamespace_ComputedFromSource_ForView()
268271

269272
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));
270273

271-
var pass = new NamespaceDirective.Pass();
272-
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
274+
var pass = new NamespaceDirective.Pass()
275+
{
276+
Engine = new RazorEngine(features: [], phases: [])
277+
};
273278

274279
// Act
275280
pass.Execute(codeDocument, document);
@@ -306,8 +311,10 @@ public void Pass_SetsNamespace_VerbatimFromImports()
306311

307312
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));
308313

309-
var pass = new NamespaceDirective.Pass();
310-
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
314+
var pass = new NamespaceDirective.Pass()
315+
{
316+
Engine = new RazorEngine(features: [], phases: [])
317+
};
311318

312319
// Act
313320
pass.Execute(codeDocument, document);
@@ -342,8 +349,10 @@ public void Pass_DoesNothing_ForUnknownDocumentKind()
342349

343350
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));
344351

345-
var pass = new NamespaceDirective.Pass();
346-
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
352+
var pass = new NamespaceDirective.Pass()
353+
{
354+
Engine = new RazorEngine(features: [], phases: [])
355+
};
347356

348357
// Act
349358
pass.Execute(codeDocument, document);

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/ModelDirectiveTest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateN
334334
}
335335
}
336336

337-
private class DesignTimeOptionsFeature : IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
337+
private class DesignTimeOptionsFeature : RazorEngineFeatureBase, IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
338338
{
339339
private readonly bool _designTime;
340340

@@ -345,8 +345,6 @@ public DesignTimeOptionsFeature(bool designTime)
345345

346346
public int Order { get; }
347347

348-
public RazorEngine Engine { get; set; }
349-
350348
public void Configure(RazorParserOptionsBuilder options)
351349
{
352350
options.SetDesignTime(_designTime);

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentDuplicateAttributeDiagnosticPassTest.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@ public class ComponentDuplicateAttributeDiagnosticPassTest
1717
{
1818
public ComponentDuplicateAttributeDiagnosticPassTest()
1919
{
20-
Pass = new ComponentMarkupDiagnosticPass();
2120
ProjectEngine = RazorProjectEngine.Create(
2221
RazorConfiguration.Default,
2322
RazorProjectFileSystem.Create(Environment.CurrentDirectory),
2423
b =>
2524
{
26-
// Don't run the markup mutating passes.
27-
b.Features.Remove(b.Features.OfType<ComponentMarkupDiagnosticPass>().Single());
25+
// Don't run the markup mutating passes.
26+
b.Features.Remove(b.Features.OfType<ComponentMarkupDiagnosticPass>().Single());
2827
b.Features.Remove(b.Features.OfType<ComponentMarkupBlockPass>().Single());
2928
b.Features.Remove(b.Features.OfType<ComponentMarkupEncodingPass>().Single());
3029
});
3130
Engine = ProjectEngine.Engine;
3231

33-
Pass.Engine = Engine;
32+
Pass = new ComponentMarkupDiagnosticPass()
33+
{
34+
Engine = Engine
35+
};
3436
}
3537

3638
private RazorProjectEngine ProjectEngine { get; }
@@ -181,16 +183,4 @@ private DocumentIntermediateNode Lower(RazorCodeDocument codeDocument)
181183
Engine.GetFeatures<ComponentDocumentClassifierPass>().Single().Execute(codeDocument, document);
182184
return document;
183185
}
184-
185-
private class StaticTagHelperFeature : ITagHelperFeature
186-
{
187-
public RazorEngine Engine { get; set; }
188-
189-
public List<TagHelperDescriptor> TagHelpers { get; set; }
190-
191-
public IReadOnlyList<TagHelperDescriptor> GetDescriptors()
192-
{
193-
return TagHelpers;
194-
}
195-
}
196186
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupBlockPassTest.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class ComponentMarkupBlockPassTest
1616
{
1717
public ComponentMarkupBlockPassTest()
1818
{
19-
Pass = new ComponentMarkupBlockPass(RazorLanguageVersion.Latest);
2019
ProjectEngine = RazorProjectEngine.Create(
2120
RazorConfiguration.Default,
2221
RazorProjectFileSystem.Create(Environment.CurrentDirectory),
@@ -29,7 +28,10 @@ public ComponentMarkupBlockPassTest()
2928
});
3029
Engine = ProjectEngine.Engine;
3130

32-
Pass.Engine = Engine;
31+
Pass = new ComponentMarkupBlockPass(RazorLanguageVersion.Latest)
32+
{
33+
Engine = Engine
34+
};
3335
}
3436

3537
private RazorProjectEngine ProjectEngine { get; }
@@ -470,16 +472,4 @@ private DocumentIntermediateNode Lower(RazorCodeDocument codeDocument)
470472
Engine.GetFeatures<ComponentMarkupDiagnosticPass>().Single().Execute(codeDocument, document);
471473
return document;
472474
}
473-
474-
private class StaticTagHelperFeature : ITagHelperFeature
475-
{
476-
public RazorEngine Engine { get; set; }
477-
478-
public List<TagHelperDescriptor> TagHelpers { get; set; }
479-
480-
public IReadOnlyList<TagHelperDescriptor> GetDescriptors()
481-
{
482-
return TagHelpers;
483-
}
484-
}
485475
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupEncodingPassTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public ComponentMarkupEncodingPassTest()
2828
});
2929
Engine = ProjectEngine.Engine;
3030

31-
Pass.Engine = Engine;
31+
Pass = new ComponentMarkupEncodingPass(RazorLanguageVersion.Latest)
32+
{
33+
Engine = Engine
34+
};
3235
}
3336

3437
private RazorProjectEngine ProjectEngine { get; }

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentWhitespacePassTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class ComponentWhitespacePassTest
1515
{
1616
public ComponentWhitespacePassTest()
1717
{
18-
Pass = new ComponentWhitespacePass();
1918
ProjectEngine = RazorProjectEngine.Create(
2019
RazorConfiguration.Default,
2120
RazorProjectFileSystem.Create(Environment.CurrentDirectory),
@@ -28,7 +27,10 @@ public ComponentWhitespacePassTest()
2827
});
2928
Engine = ProjectEngine.Engine;
3029

31-
Pass.Engine = Engine;
30+
Pass = new ComponentWhitespacePass()
31+
{
32+
Engine = Engine
33+
};
3234
}
3335

3436
private RazorProjectEngine ProjectEngine { get; }

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
54
#nullable disable
65

76
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -26,8 +25,10 @@ public void Execute_IgnoresDocumentsWithDocumentKind()
2625
Options = RazorCodeGenerationOptions.Default,
2726
};
2827

29-
var pass = new DefaultDocumentClassifierPass();
30-
pass.Engine = CreateProjectEngine().Engine;
28+
var pass = new DefaultDocumentClassifierPass()
29+
{
30+
Engine = CreateProjectEngine().Engine
31+
};
3132

3233
// Act
3334
pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode);
@@ -46,8 +47,10 @@ public void Execute_CreatesClassStructure()
4647
Options = RazorCodeGenerationOptions.Default,
4748
};
4849

49-
var pass = new DefaultDocumentClassifierPass();
50-
pass.Engine = CreateProjectEngine().Engine;
50+
var pass = new DefaultDocumentClassifierPass()
51+
{
52+
Engine = CreateProjectEngine().Engine
53+
};
5154

5255
// Act
5356
pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode);

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,35 @@ public void Execute_ExecutesPhasesInOrder()
7171

7272
var firstPass = new Mock<IRazorDirectiveClassifierPass>(MockBehavior.Strict);
7373
firstPass.SetupGet(m => m.Order).Returns(0);
74-
firstPass.SetupProperty(m => m.Engine);
74+
75+
RazorEngine firstPassEngine = null;
76+
firstPass
77+
.SetupGet(m => m.Engine)
78+
.Returns(() => firstPassEngine);
79+
firstPass
80+
.Setup(m => m.Initialize(It.IsAny<RazorEngine>()))
81+
.Callback((RazorEngine engine) => firstPassEngine = engine);
82+
7583
firstPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() =>
7684
{
7785
originalNode.Children.Add(firstPassNode);
7886
});
7987

8088
var secondPass = new Mock<IRazorDirectiveClassifierPass>(MockBehavior.Strict);
8189
secondPass.SetupGet(m => m.Order).Returns(1);
82-
secondPass.SetupProperty(m => m.Engine);
90+
91+
RazorEngine secondPassEngine = null;
92+
secondPass
93+
.SetupGet(m => m.Engine)
94+
.Returns(() => secondPassEngine);
95+
secondPass
96+
.Setup(m => m.Initialize(It.IsAny<RazorEngine>()))
97+
.Callback((RazorEngine engine) => secondPassEngine = engine);
98+
8399
secondPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() =>
84100
{
85-
// Works only when the first pass has run before this.
86-
originalNode.Children[0].Children.Add(secondPassNode);
101+
// Works only when the first pass has run before this.
102+
originalNode.Children[0].Children.Add(secondPassNode);
87103
});
88104

89105
var phase = new DefaultRazorDirectiveClassifierPhase();

0 commit comments

Comments
 (0)