| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155 |
- commit 771a7e35a41bc5d884fa5a71dfbffe13f8adf36b
- Author: N. Taylor Mullen <[email protected]>
- Date: Mon Jan 22 18:10:21 2018 -0800
- Add MVC support for RazorProjectEngine.
-
- - Make `RazorProjectEngine` call paths for all feature registrations.
- - Add `DefaultMvcImportFeature` for latest and 1.X MVC.
- - Ported `AddTargetExtension` and `AddDirective` to `RazorProjectEngineBuilderExtensions`.
- - Added tests and a test file system project type.
- - Moved obsolete `IRazorEngineBuilder` methods to the bottom of each file. Will actually obsolete the methods once `RazorProjectEngine` is working end-to-end.
-
- #1828
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/DefaultMvcImportFeature.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/DefaultMvcImportFeature.cs
- new file mode 100644
- index 00000000000..d630a70c72f
- --- /dev/null
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/DefaultMvcImportFeature.cs
- @@ -0,0 +1,84 @@
- +// 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 System.Collections.Generic;
- +using System.IO;
- +using System.Linq;
- +using System.Text;
- +using Microsoft.AspNetCore.Razor.Language;
- +
- +namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
- +{
- + internal class DefaultMvcImportFeature : RazorProjectEngineFeatureBase, IRazorImportFeature
- + {
- + private const string ImportsFileName = "_ViewImports.cshtml";
- +
- + public IReadOnlyList<RazorSourceDocument> GetImports(string sourceFilePath)
- + {
- + if (string.IsNullOrEmpty(sourceFilePath))
- + {
- + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpy, nameof(sourceFilePath));
- + }
- +
- + var imports = new List<RazorSourceDocument>();
- + AddDefaultDirectivesImport(imports);
- +
- + // We add hierarchical imports second so any default directive imports can be overridden.
- + AddHierarchicalImports(sourceFilePath, imports);
- +
- + return imports;
- + }
- +
- + // Internal for testing
- + internal static void AddDefaultDirectivesImport(List<RazorSourceDocument> imports)
- + {
- + using (var stream = new MemoryStream())
- + using (var writer = new StreamWriter(stream, Encoding.UTF8))
- + {
- + writer.WriteLine("@using System");
- + writer.WriteLine("@using System.Collections.Generic");
- + writer.WriteLine("@using System.Linq");
- + writer.WriteLine("@using System.Threading.Tasks");
- + writer.WriteLine("@using Microsoft.AspNetCore.Mvc");
- + writer.WriteLine("@using Microsoft.AspNetCore.Mvc.Rendering");
- + writer.WriteLine("@using Microsoft.AspNetCore.Mvc.ViewFeatures");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider");
- + writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor");
- + writer.Flush();
- +
- + stream.Position = 0;
- + var defaultMvcImports = RazorSourceDocument.ReadFrom(stream, fileName: null, encoding: Encoding.UTF8);
- + imports.Add(defaultMvcImports);
- + }
- + }
- +
- + // Internal for testing
- + internal void AddHierarchicalImports(string sourceFilePath, List<RazorSourceDocument> imports)
- + {
- + // We want items in descending order. FindHierarchicalItems returns items in ascending order.
- + var importProjectItems = ProjectEngine.FileSystem.FindHierarchicalItems(sourceFilePath, ImportsFileName).Reverse();
- + foreach (var importProjectItem in importProjectItems)
- + {
- + RazorSourceDocument importSourceDocument;
- +
- + if (importProjectItem.Exists)
- + {
- + importSourceDocument = RazorSourceDocument.ReadFrom(importProjectItem);
- + }
- + else
- + {
- + // File doesn't exist on disk so just add a marker source document as an identifier for "there could be something here".
- + var sourceDocumentProperties = new RazorSourceDocumentProperties(importProjectItem.FilePath, importProjectItem.RelativePhysicalPath);
- + importSourceDocument = RazorSourceDocument.Create(string.Empty, sourceDocumentProperties);
- + }
- +
- + imports.Add(importSourceDocument);
- + }
- + }
- + }
- +}
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/InjectDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/InjectDirective.cs
- index c5e84d3ede1..c1c348f8a96 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/InjectDirective.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/InjectDirective.cs
- @@ -24,8 +24,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
- builder.Description = Resources.InjectDirective_Description;
- });
-
- - public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- builder.Features.Add(new Pass());
- builder.AddTargetExtension(new InjectTargetExtension());
- @@ -99,5 +104,20 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
- }
- }
- }
- +
- + #region Obsolete
- + public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new Pass());
- + builder.AddTargetExtension(new InjectTargetExtension());
- + return builder;
- + }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ModelDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ModelDirective.cs
- index 572d1b9ccb9..2b67228fae2 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ModelDirective.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ModelDirective.cs
- @@ -21,8 +21,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
- builder.Description = Resources.ModelDirective_Description;
- });
-
- - public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- builder.Features.Add(new Pass(builder.DesignTime));
- return builder;
- @@ -128,5 +133,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
- }
- }
- }
- +
- + #region Obsolete
- + public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new Pass(builder.DesignTime));
- + return builder;
- + }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/RazorExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/RazorExtensions.cs
- index 341bb5d47a6..c9b0642ad50 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/RazorExtensions.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/RazorExtensions.cs
- @@ -9,8 +9,68 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
- {
- public static class RazorExtensions
- {
- + public static void Register(RazorProjectEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + EnsureDesignTime(builder);
- +
- + InjectDirective.Register(builder);
- + ModelDirective.Register(builder);
- +
- + FunctionsDirective.Register(builder);
- + InheritsDirective.Register(builder);
- +
- + // Register section directive with the 1.x compatible target extension.
- + builder.AddDirective(SectionDirective.Directive);
- + builder.Features.Add(new SectionDirectivePass());
- + builder.AddTargetExtension(new LegacySectionTargetExtension());
- +
- + builder.AddTargetExtension(new TemplateTargetExtension()
- + {
- + TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
- + });
- +
- + builder.Features.Add(new ModelExpressionPass());
- + builder.Features.Add(new MvcViewDocumentClassifierPass());
- +
- + builder.SetImportFeature(new DefaultMvcImportFeature());
- + }
- +
- + public static void RegisterViewComponentTagHelpers(RazorProjectEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + EnsureDesignTime(builder);
- +
- + builder.Features.Add(new ViewComponentTagHelperPass());
- + builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());
- + }
- +
- + private static void EnsureDesignTime(RazorProjectEngineBuilder builder)
- + {
- + if (builder.DesignTime)
- + {
- + return;
- + }
- +
- + throw new NotSupportedException(Resources.RuntimeCodeGenerationNotSupported);
- + }
- +
- + #region Obsolete
- public static void Register(IRazorEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- EnsureDesignTime(builder);
-
- InjectDirective.Register(builder);
- @@ -35,6 +95,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
-
- public static void RegisterViewComponentTagHelpers(IRazorEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- EnsureDesignTime(builder);
-
- builder.Features.Add(new ViewComponentTagHelperPass());
- @@ -50,5 +115,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
-
- throw new NotSupportedException(Resources.RuntimeCodeGenerationNotSupported);
- }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/DefaultMvcImportFeature.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/DefaultMvcImportFeature.cs
- new file mode 100644
- index 00000000000..8cbf0f11a15
- --- /dev/null
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/DefaultMvcImportFeature.cs
- @@ -0,0 +1,86 @@
- +// 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 System.Collections.Generic;
- +using System.IO;
- +using System.Linq;
- +using System.Text;
- +using Microsoft.AspNetCore.Razor.Language;
- +
- +namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- +{
- + internal class DefaultMvcImportFeature : RazorProjectEngineFeatureBase, IRazorImportFeature
- + {
- + private const string ImportsFileName = "_ViewImports.cshtml";
- +
- + public IReadOnlyList<RazorSourceDocument> GetImports(string sourceFilePath)
- + {
- + if (string.IsNullOrEmpty(sourceFilePath))
- + {
- + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpy, nameof(sourceFilePath));
- + }
- +
- + var imports = new List<RazorSourceDocument>();
- + AddDefaultDirectivesImport(imports);
- +
- + // We add hierarchical imports second so any default directive imports can be overridden.
- + AddHierarchicalImports(sourceFilePath, imports);
- +
- + return imports;
- + }
- +
- + // Internal for testing
- + internal static void AddDefaultDirectivesImport(List<RazorSourceDocument> imports)
- + {
- + using (var stream = new MemoryStream())
- + using (var writer = new StreamWriter(stream, Encoding.UTF8))
- + {
- + writer.WriteLine("@using System");
- + writer.WriteLine("@using System.Collections.Generic");
- + writer.WriteLine("@using System.Linq");
- + writer.WriteLine("@using System.Threading.Tasks");
- + writer.WriteLine("@using Microsoft.AspNetCore.Mvc");
- + writer.WriteLine("@using Microsoft.AspNetCore.Mvc.Rendering");
- + writer.WriteLine("@using Microsoft.AspNetCore.Mvc.ViewFeatures");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url");
- + writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider");
- + writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor");
- + writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor");
- + writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor");
- + writer.Flush();
- +
- + stream.Position = 0;
- + var defaultMvcImports = RazorSourceDocument.ReadFrom(stream, fileName: null, encoding: Encoding.UTF8);
- + imports.Add(defaultMvcImports);
- + }
- + }
- +
- + // Internal for testing
- + internal void AddHierarchicalImports(string sourceFilePath, List<RazorSourceDocument> imports)
- + {
- + // We want items in descending order. FindHierarchicalItems returns items in ascending order.
- + var importProjectItems = ProjectEngine.FileSystem.FindHierarchicalItems(sourceFilePath, ImportsFileName).Reverse();
- + foreach (var importProjectItem in importProjectItems)
- + {
- + RazorSourceDocument importSourceDocument;
- +
- + if (importProjectItem.Exists)
- + {
- + importSourceDocument = RazorSourceDocument.ReadFrom(importProjectItem);
- + }
- + else
- + {
- + // File doesn't exist on disk so just add a marker source document as an identifier for "there could be something here".
- + var sourceDocumentProperties = new RazorSourceDocumentProperties(importProjectItem.FilePath, importProjectItem.RelativePhysicalPath);
- + importSourceDocument = RazorSourceDocument.Create(string.Empty, sourceDocumentProperties);
- + }
- +
- + imports.Add(importSourceDocument);
- + }
- + }
- + }
- +}
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectDirective.cs
- index 81f1d5b8341..41bb56b57d8 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectDirective.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectDirective.cs
- @@ -24,8 +24,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- builder.Description = Resources.InjectDirective_Description;
- });
-
- - public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- builder.Features.Add(new Pass());
- builder.AddTargetExtension(new InjectTargetExtension());
- @@ -99,5 +104,20 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- }
- }
- }
- +
- + #region Obsolete
- + public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new Pass());
- + builder.AddTargetExtension(new InjectTargetExtension());
- + return builder;
- + }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs
- index 17d98d72b44..a5d77b401e8 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs
- @@ -21,8 +21,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- builder.Description = Resources.ModelDirective_Description;
- });
-
- - public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- builder.Features.Add(new Pass(builder.DesignTime));
- return builder;
- @@ -135,5 +140,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- }
- }
- }
- +
- + #region Obsolete
- + public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new Pass(builder.DesignTime));
- + return builder;
- + }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs
- index dec8c7894cd..b0c197f42b5 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs
- @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- builder.Description = Resources.NamespaceDirective_Description;
- });
-
- - public static void Register(IRazorEngineBuilder builder)
- + public static void Register(RazorProjectEngineBuilder builder)
- {
- if (builder == null)
- {
- @@ -186,5 +186,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- base.VisitDirective(node);
- }
- }
- +
- + #region Obsolete
- + public static void Register(IRazorEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException();
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new Pass());
- + }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs
- index 5f617a1ebd7..cd3624f4350 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs
- @@ -32,8 +32,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
-
- public IntermediateNode DirectiveNode { get; }
-
- - public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- return builder;
- }
- @@ -98,5 +103,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- }
- }
- }
- +
- + #region Obsolete
- + public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + return builder;
- + }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs
- index fc1ad0c1624..19aa2ced3c4 100644
- --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs
- +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs
- @@ -1,6 +1,7 @@
- // 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.Razor.Language;
- using Microsoft.AspNetCore.Razor.Language.Extensions;
-
- @@ -8,8 +9,51 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- {
- public static class RazorExtensions
- {
- + public static void Register(RazorProjectEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + InjectDirective.Register(builder);
- + ModelDirective.Register(builder);
- + NamespaceDirective.Register(builder);
- + PageDirective.Register(builder);
- +
- + FunctionsDirective.Register(builder);
- + InheritsDirective.Register(builder);
- + SectionDirective.Register(builder);
- +
- + builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());
- + builder.AddTargetExtension(new TemplateTargetExtension()
- + {
- + TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
- + });
- +
- + builder.Features.Add(new ModelExpressionPass());
- + builder.Features.Add(new PagesPropertyInjectionPass());
- + builder.Features.Add(new ViewComponentTagHelperPass());
- + builder.Features.Add(new RazorPageDocumentClassifierPass());
- + builder.Features.Add(new MvcViewDocumentClassifierPass());
- +
- + if (!builder.DesignTime)
- + {
- + builder.Features.Add(new AssemblyAttributeInjectionPass());
- + builder.Features.Add(new InstrumentationPass());
- + }
- +
- + builder.SetImportFeature(new DefaultMvcImportFeature());
- + }
- +
- + #region Obsolete
- public static void Register(IRazorEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- InjectDirective.Register(builder);
- ModelDirective.Register(builder);
- NamespaceDirective.Register(builder);
- @@ -37,5 +81,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- builder.Features.Add(new InstrumentationPass());
- }
- }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorImportFeature.cs b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorImportFeature.cs
- index ed25827c629..3d5c015a263 100644
- --- a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorImportFeature.cs
- +++ b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorImportFeature.cs
- @@ -6,10 +6,8 @@ using System.Collections.Generic;
-
- namespace Microsoft.AspNetCore.Razor.Language
- {
- - internal class DefaultRazorImportFeature : IRazorImportFeature
- + internal class DefaultRazorImportFeature : RazorProjectEngineFeatureBase, IRazorImportFeature
- {
- - public RazorProjectEngine ProjectEngine { get; set; }
- -
- public IReadOnlyList<RazorSourceDocument> GetImports(string sourceFilePath) => Array.Empty<RazorSourceDocument>();
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirective.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirective.cs
- index f9033b8a83c..0adb20aefba 100644
- --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirective.cs
- +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirective.cs
- @@ -1,6 +1,7 @@
- // 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.Razor.Language.Legacy;
-
- namespace Microsoft.AspNetCore.Razor.Language.Extensions
- @@ -15,10 +16,28 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
- builder.Description = Resources.FunctionsDirective_Description;
- });
-
- + public static void Register(RazorProjectEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new FunctionsDirectivePass());
- + }
- +
- + #region Obsolete
- public static void Register(IRazorEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- builder.Features.Add(new FunctionsDirectivePass());
- }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirective.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirective.cs
- index 289b5d853c2..6cef321c73c 100644
- --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirective.cs
- +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirective.cs
- @@ -1,6 +1,7 @@
- // 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.Razor.Language.Legacy;
-
- namespace Microsoft.AspNetCore.Razor.Language.Extensions
- @@ -17,10 +18,28 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
- builder.Description = Resources.InheritsDirective_Description;
- });
-
- + public static void Register(RazorProjectEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new InheritsDirectivePass());
- + }
- +
- + #region Obsolete
- public static void Register(IRazorEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- builder.Features.Add(new InheritsDirectivePass());
- }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirective.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirective.cs
- index 1542525a700..14c461d08ff 100644
- --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirective.cs
- +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirective.cs
- @@ -1,6 +1,7 @@
- // 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.Razor.Language.Legacy;
-
- namespace Microsoft.AspNetCore.Razor.Language.Extensions
- @@ -16,11 +17,30 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
- builder.Description = Resources.SectionDirective_Description;
- });
-
- + public static void Register(RazorProjectEngineBuilder builder)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + builder.AddDirective(Directive);
- + builder.Features.Add(new SectionDirectivePass());
- + builder.AddTargetExtension(new SectionTargetExtension());
- + }
- +
- + #region Obsolete
- public static void Register(IRazorEngineBuilder builder)
- {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- builder.AddDirective(Directive);
- builder.Features.Add(new SectionDirectivePass());
- builder.AddTargetExtension(new SectionTargetExtension());
- }
- + #endregion
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs
- index 7001e288a5f..8bd17e0dd67 100644
- --- a/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs
- +++ b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineBuilderExtensions.cs
- @@ -3,6 +3,7 @@
-
- using System;
- using System.Linq;
- +using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
-
- namespace Microsoft.AspNetCore.Razor.Language
- {
- @@ -29,5 +30,77 @@ namespace Microsoft.AspNetCore.Razor.Language
-
- builder.Features.Add(feature);
- }
- +
- + /// <summary>
- + /// Adds the specified <see cref="ICodeTargetExtension"/>.
- + /// </summary>
- + /// <param name="builder">The <see cref="RazorProjectEngineBuilder"/>.</param>
- + /// <param name="extension">The <see cref="ICodeTargetExtension"/> to add.</param>
- + /// <returns>The <see cref="RazorProjectEngineBuilder"/>.</returns>
- + public static RazorProjectEngineBuilder AddTargetExtension(this RazorProjectEngineBuilder builder, ICodeTargetExtension extension)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + if (extension == null)
- + {
- + throw new ArgumentNullException(nameof(extension));
- + }
- +
- + var targetExtensionFeature = GetTargetExtensionFeature(builder);
- + targetExtensionFeature.TargetExtensions.Add(extension);
- +
- + return builder;
- + }
- +
- + /// <summary>
- + /// Adds the specified <see cref="DirectiveDescriptor"/>.
- + /// </summary>
- + /// <param name="builder">The <see cref="RazorProjectEngineBuilder"/>.</param>
- + /// <param name="directive">The <see cref="DirectiveDescriptor"/> to add.</param>
- + /// <returns>The <see cref="RazorProjectEngineBuilder"/>.</returns>
- + public static RazorProjectEngineBuilder AddDirective(this RazorProjectEngineBuilder builder, DirectiveDescriptor directive)
- + {
- + if (builder == null)
- + {
- + throw new ArgumentNullException(nameof(builder));
- + }
- +
- + if (directive == null)
- + {
- + throw new ArgumentNullException(nameof(directive));
- + }
- +
- + var directiveFeature = GetDirectiveFeature(builder);
- + directiveFeature.Directives.Add(directive);
- +
- + return builder;
- + }
- +
- + private static IRazorDirectiveFeature GetDirectiveFeature(RazorProjectEngineBuilder builder)
- + {
- + var directiveFeature = builder.Features.OfType<IRazorDirectiveFeature>().FirstOrDefault();
- + if (directiveFeature == null)
- + {
- + directiveFeature = new DefaultRazorDirectiveFeature();
- + builder.Features.Add(directiveFeature);
- + }
- +
- + return directiveFeature;
- + }
- +
- + private static IRazorTargetExtensionFeature GetTargetExtensionFeature(RazorProjectEngineBuilder builder)
- + {
- + var targetExtensionFeature = builder.Features.OfType<IRazorTargetExtensionFeature>().FirstOrDefault();
- + if (targetExtensionFeature == null)
- + {
- + targetExtensionFeature = new DefaultRazorTargetExtensionFeature();
- + builder.Features.Add(targetExtensionFeature);
- + }
- +
- + return targetExtensionFeature;
- + }
- }
- }
- diff --git a/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineFeatureBase.cs b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineFeatureBase.cs
- new file mode 100644
- index 00000000000..f5944a83ffc
- --- /dev/null
- +++ b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngineFeatureBase.cs
- @@ -0,0 +1,31 @@
- +// 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;
- +
- +namespace Microsoft.AspNetCore.Razor.Language
- +{
- + public abstract class RazorProjectEngineFeatureBase : IRazorProjectEngineFeature
- + {
- + private RazorProjectEngine _projectEngine;
- +
- + public RazorProjectEngine ProjectEngine
- + {
- + get => _projectEngine;
- + set
- + {
- + if (value == null)
- + {
- + throw new ArgumentNullException(nameof(value));
- + }
- +
- + _projectEngine = value;
- + OnInitialized();
- + }
- + }
- +
- + protected virtual void OnInitialized()
- + {
- + }
- + }
- +}
- diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/DefaultMvcImportFeatureTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/DefaultMvcImportFeatureTest.cs
- new file mode 100644
- index 00000000000..32e254a45bd
- --- /dev/null
- +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/DefaultMvcImportFeatureTest.cs
- @@ -0,0 +1,77 @@
- +// 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.Collections.Generic;
- +using Microsoft.AspNetCore.Razor.Language;
- +using Moq;
- +using Xunit;
- +
- +namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
- +{
- + public class DefaultMvcImportFeatureTest
- + {
- + [Fact]
- + public void AddDefaultDirectivesImport_AddsSingleDynamicImport()
- + {
- + // Arrange
- + var imports = new List<RazorSourceDocument>();
- +
- + // Act
- + DefaultMvcImportFeature.AddDefaultDirectivesImport(imports);
- +
- + // Assert
- + var import = Assert.Single(imports);
- + Assert.Null(import.FilePath);
- + }
- +
- + [Fact]
- + public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
- + {
- + // Arrange
- + var imports = new List<RazorSourceDocument>();
- + var testFileSystem = new TestRazorProjectFileSystem(new[]
- + {
- + new TestRazorProjectItem("/Index.cshtml"),
- + new TestRazorProjectItem("/_ViewImports.cshtml"),
- + new TestRazorProjectItem("/Contact/_ViewImports.cshtml"),
- + new TestRazorProjectItem("/Contact/Index.cshtml"),
- + });
- + var mvcImportFeature = new DefaultMvcImportFeature()
- + {
- + ProjectEngine = Mock.Of<RazorProjectEngine>(projectEngine => projectEngine.FileSystem == testFileSystem)
- + };
- +
- + // Act
- + mvcImportFeature.AddHierarchicalImports("/Contact/Index.cshtml", imports);
- +
- + // Assert
- + Assert.Collection(imports,
- + import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
- + import => Assert.Equal("/Contact/_ViewImports.cshtml", import.FilePath));
- + }
- +
- + [Fact]
- + public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
- + {
- + // Arrange
- + var imports = new List<RazorSourceDocument>();
- + var testFileSystem = new TestRazorProjectFileSystem(new[]
- + {
- + new TestRazorProjectItem("/Pages/Contact/Index.cshtml"),
- + });
- + var mvcImportFeature = new DefaultMvcImportFeature()
- + {
- + ProjectEngine = Mock.Of<RazorProjectEngine>(projectEngine => projectEngine.FileSystem == testFileSystem)
- + };
- +
- + // Act
- + mvcImportFeature.AddHierarchicalImports("/Pages/Contact/Index.cshtml", imports);
- +
- + // Assert
- + Assert.Collection(imports,
- + import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
- + import => Assert.Equal("/Pages/_ViewImports.cshtml", import.FilePath),
- + import => Assert.Equal("/Pages/Contact/_ViewImports.cshtml", import.FilePath));
- + }
- + }
- +}
- diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/DefaultMvcImportFeatureTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/DefaultMvcImportFeatureTest.cs
- new file mode 100644
- index 00000000000..31d66517a1a
- --- /dev/null
- +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/DefaultMvcImportFeatureTest.cs
- @@ -0,0 +1,77 @@
- +// 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.Collections.Generic;
- +using Microsoft.AspNetCore.Razor.Language;
- +using Moq;
- +using Xunit;
- +
- +namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
- +{
- + public class DefaultMvcImportFeatureTest
- + {
- + [Fact]
- + public void AddDefaultDirectivesImport_AddsSingleDynamicImport()
- + {
- + // Arrange
- + var imports = new List<RazorSourceDocument>();
- +
- + // Act
- + DefaultMvcImportFeature.AddDefaultDirectivesImport(imports);
- +
- + // Assert
- + var import = Assert.Single(imports);
- + Assert.Null(import.FilePath);
- + }
- +
- + [Fact]
- + public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
- + {
- + // Arrange
- + var imports = new List<RazorSourceDocument>();
- + var testFileSystem = new TestRazorProjectFileSystem(new[]
- + {
- + new TestRazorProjectItem("/Index.cshtml"),
- + new TestRazorProjectItem("/_ViewImports.cshtml"),
- + new TestRazorProjectItem("/Contact/_ViewImports.cshtml"),
- + new TestRazorProjectItem("/Contact/Index.cshtml"),
- + });
- + var mvcImportFeature = new DefaultMvcImportFeature()
- + {
- + ProjectEngine = Mock.Of<RazorProjectEngine>(projectEngine => projectEngine.FileSystem == testFileSystem)
- + };
- +
- + // Act
- + mvcImportFeature.AddHierarchicalImports("/Contact/Index.cshtml", imports);
- +
- + // Assert
- + Assert.Collection(imports,
- + import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
- + import => Assert.Equal("/Contact/_ViewImports.cshtml", import.FilePath));
- + }
- +
- + [Fact]
- + public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
- + {
- + // Arrange
- + var imports = new List<RazorSourceDocument>();
- + var testFileSystem = new TestRazorProjectFileSystem(new[]
- + {
- + new TestRazorProjectItem("/Pages/Contact/Index.cshtml"),
- + });
- + var mvcImportFeature = new DefaultMvcImportFeature()
- + {
- + ProjectEngine = Mock.Of<RazorProjectEngine>(projectEngine => projectEngine.FileSystem == testFileSystem)
- + };
- +
- + // Act
- + mvcImportFeature.AddHierarchicalImports("/Pages/Contact/Index.cshtml", imports);
- +
- + // Assert
- + Assert.Collection(imports,
- + import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
- + import => Assert.Equal("/Pages/_ViewImports.cshtml", import.FilePath),
- + import => Assert.Equal("/Pages/Contact/_ViewImports.cshtml", import.FilePath));
- + }
- + }
- +}
- diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorProjectEngineBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorProjectEngineBuilderExtensionsTest.cs
- index 44f93a970a1..d9c0630005d 100644
- --- a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorProjectEngineBuilderExtensionsTest.cs
- +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorProjectEngineBuilderExtensionsTest.cs
- @@ -1,6 +1,7 @@
- // 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 Microsoft.AspNetCore.Razor.Language.CodeGeneration;
- using Moq;
- using Xunit;
-
- @@ -26,5 +27,77 @@ namespace Microsoft.AspNetCore.Razor.Language
- var feature = Assert.Single(builder.Features);
- Assert.Same(newFeature, feature);
- }
- +
- + [Fact]
- + public void AddTargetExtension_CreatesAndAddsToTargetExtensionFeatureIfItDoesNotExist()
- + {
- + // Arrange
- + var builder = new DefaultRazorProjectEngineBuilder(false, Mock.Of<RazorProjectFileSystem>());
- + var expectedExtension = Mock.Of<ICodeTargetExtension>();
- +
- + // Act
- + builder.AddTargetExtension(expectedExtension);
- +
- + // Assert
- + var feature = Assert.Single(builder.Features);
- + var codeTargetExtensionFeature = Assert.IsAssignableFrom<IRazorTargetExtensionFeature>(feature);
- + var extensions = Assert.Single(codeTargetExtensionFeature.TargetExtensions);
- + Assert.Same(expectedExtension, extensions);
- + }
- +
- + [Fact]
- + public void AddTargetExtension_UsesExistingFeatureIfExistsAndAddsTo()
- + {
- + // Arrange
- + var builder = new DefaultRazorProjectEngineBuilder(false, Mock.Of<RazorProjectFileSystem>());
- + var codeTargetExtensionFeature = new DefaultRazorTargetExtensionFeature();
- + builder.Features.Add(codeTargetExtensionFeature);
- + var expectedExtension = Mock.Of<ICodeTargetExtension>();
- +
- + // Act
- + builder.AddTargetExtension(expectedExtension);
- +
- + // Assert
- + var feature = Assert.Single(builder.Features);
- + Assert.Same(codeTargetExtensionFeature, feature);
- + var extensions = Assert.Single(codeTargetExtensionFeature.TargetExtensions);
- + Assert.Same(expectedExtension, extensions);
- + }
- +
- + [Fact]
- + public void AddDirective_CreatesAndAddsToDirectiveFeatureIfItDoesNotExist()
- + {
- + // Arrange
- + var builder = new DefaultRazorProjectEngineBuilder(false, Mock.Of<RazorProjectFileSystem>());
- + var expectedDirective = Mock.Of<DirectiveDescriptor>();
- +
- + // Act
- + builder.AddDirective(expectedDirective);
- +
- + // Assert
- + var feature = Assert.Single(builder.Features);
- + var directiveFeature = Assert.IsAssignableFrom<IRazorDirectiveFeature>(feature);
- + var directive = Assert.Single(directiveFeature.Directives);
- + Assert.Same(expectedDirective, directive);
- + }
- +
- + [Fact]
- + public void AddDirective_UsesExistingFeatureIfExistsAndAddsTo()
- + {
- + // Arrange
- + var builder = new DefaultRazorProjectEngineBuilder(false, Mock.Of<RazorProjectFileSystem>());
- + var directiveFeature = new DefaultRazorDirectiveFeature();
- + builder.Features.Add(directiveFeature);
- + var expecteDirective = Mock.Of<DirectiveDescriptor>();
- +
- + // Act
- + builder.AddDirective(expecteDirective);
- +
- + // Assert
- + var feature = Assert.Single(builder.Features);
- + Assert.Same(directiveFeature, feature);
- + var directive = Assert.Single(directiveFeature.Directives);
- + Assert.Same(expecteDirective, directive);
- + }
- }
- }
- diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorProjectEngineFeatureBaseTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorProjectEngineFeatureBaseTest.cs
- new file mode 100644
- index 00000000000..8344e787c7d
- --- /dev/null
- +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorProjectEngineFeatureBaseTest.cs
- @@ -0,0 +1,34 @@
- +// 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 Moq;
- +using Xunit;
- +
- +namespace Microsoft.AspNetCore.Razor.Language
- +{
- + public class RazorProjectEngineFeatureBaseTest
- + {
- + [Fact]
- + public void ProjectEngineSetter_CallsOnInitialized()
- + {
- + // Arrange
- + var testFeature = new TestFeature();
- +
- + // Act
- + testFeature.ProjectEngine = Mock.Of<RazorProjectEngine>();
- +
- + // Assert
- + Assert.Equal(1, testFeature.InitializationCount);
- + }
- +
- + private class TestFeature : RazorProjectEngineFeatureBase
- + {
- + public int InitializationCount { get; private set; }
- +
- + protected override void OnInitialized()
- + {
- + InitializationCount++;
- + }
- + }
- + }
- +}
- diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/TestRazorProjectFileSystem.cs b/test/Microsoft.AspNetCore.Razor.Test.Common/Language/TestRazorProjectFileSystem.cs
- similarity index 100%
- rename from test/Microsoft.AspNetCore.Razor.Language.Test/TestRazorProjectFileSystem.cs
- rename to test/Microsoft.AspNetCore.Razor.Test.Common/Language/TestRazorProjectFileSystem.cs
|