From 989a6c699fd89aa7789f4863d2d797589cf1d373 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 28 Feb 2018 18:09:13 -0800 Subject: [PATCH] Don't add the tag helper provider by default Since the default tag helper provider is used by MVC then MVC should include it. Now that Blazor is in the mix we shouldn't include it for all configurations. --- .../RazorExtensions.cs | 5 + ...iewComponentTagHelperDescriptorProvider.cs | 8 - .../ViewComponentTypeVisitor.cs | 18 +- .../RazorExtensions.cs | 5 +- ...iewComponentTagHelperDescriptorProvider.cs | 8 - .../ViewComponentTypeVisitor.cs | 7 +- .../TagHelperDescriptorProviderContext.cs | 4 + .../TagHelperResolver.cs | 2 + .../DefaultTagHelperDescriptorFactory.cs | 20 +- .../DefaultTagHelperDescriptorProvider.cs | 6 +- .../RemoteTagHelperResolver.cs | 8 +- .../DefaultTagHelperResolver.cs | 5 +- .../ProjectSystem/Rules/RazorConfiguration.cs | 2 +- .../ProjectSystem/Rules/RazorGeneral.cs | 2 +- .../CodeGenerationIntegrationTest.cs | 291 +++++++++--------- .../MvcShim.cs | 45 +++ ...omponentTagHelperDescriptorProviderTest.cs | 11 +- .../CodeGenerationIntegrationTest.cs | 99 +++--- .../MvcShim.cs | 45 +++ ...omponentTagHelperDescriptorProviderTest.cs | 10 +- .../TestCompilation.cs | 4 +- .../ViewComponentAttribute.cs | 11 + .../ActionResult.cs | 0 .../RedirectResult.cs | 0 .../ViewComponentAttribute.cs | 11 + .../DefaultTagHelperDescriptorFactoryTest.cs | 96 +++--- .../DefaultTagHelperDescriptorProviderTest.cs | 7 +- .../DefaultProjectEngineFactoryServiceTest.cs | 1 + 28 files changed, 405 insertions(+), 326 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcShim.cs create mode 100644 test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/MvcShim.cs create mode 100644 test/Microsoft.AspNetCore.Razor.Test.MvcShim.Version1_X/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs rename test/Microsoft.AspNetCore.Razor.Test.MvcShim/{Microsoft.AspNetCore.Mvc.Core => Microsoft.AspNetCore.Mvc}/ActionResult.cs (100%) rename test/Microsoft.AspNetCore.Razor.Test.MvcShim/{Microsoft.AspNetCore.Mvc.Core => Microsoft.AspNetCore.Mvc}/RedirectResult.cs (100%) create mode 100644 test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs 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 18bfe99a61..50fce1da36 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/RazorExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/RazorExtensions.cs @@ -4,6 +4,7 @@ using System; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language.Extensions; +using Microsoft.CodeAnalysis.Razor; namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X { @@ -22,6 +23,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X FunctionsDirective.Register(builder); InheritsDirective.Register(builder); + builder.Features.Add(new DefaultTagHelperDescriptorProvider()); + // Register section directive with the 1.x compatible target extension. builder.AddDirective(SectionDirective.Directive); builder.Features.Add(new SectionDirectivePass()); @@ -67,6 +70,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X FunctionsDirective.Register(builder); InheritsDirective.Register(builder); + builder.Features.Add(new DefaultTagHelperDescriptorProvider()); + // Register section directive with the 1.x compatible target extension. builder.AddDirective(SectionDirective.Directive); builder.Features.Add(new SectionDirectivePass()); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs index 0b85fdaab3..af79139c69 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs @@ -11,10 +11,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X { public sealed class ViewComponentTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider { - // Hack for testability. The visitor will normally just no op if we're not referencing - // an appropriate version of MVC. - internal bool ForceEnabled { get; set; } - public int Order { get; set; } public void Execute(TagHelperDescriptorProviderContext context) @@ -33,10 +29,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X var types = new List(); var visitor = ViewComponentTypeVisitor.Create(compilation, types); - if (ForceEnabled) - { - visitor.Enabled = true; - } // We always visit the global namespace. visitor.Visit(compilation.Assembly.GlobalNamespace); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs index 90cb1eedca..be987cbdd5 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs @@ -18,22 +18,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X public static ViewComponentTypeVisitor Create(Compilation compilation, List results) { - var enabled = false; - foreach (var reference in compilation.References) - { - var symbol = compilation.GetAssemblyOrModuleSymbol(reference) as IAssemblySymbol; - if (symbol != null) - { - if (string.Equals(symbol.Identity.Name, ViewComponentTypes.Assembly, StringComparison.Ordinal)) - { - enabled = symbol.Identity.Version >= ViewComponentTypes.AssemblyVersion; - break; - } - } - } - - var vcAttribute = enabled ? compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute) : null; - var nonVCAttribute = enabled ? compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute) : null; + var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute); + var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute); return new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, results); } diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs index 589798fffa..7729bdd5ce 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs @@ -4,6 +4,7 @@ using System; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language.Extensions; +using Microsoft.CodeAnalysis.Razor; namespace Microsoft.AspNetCore.Mvc.Razor.Extensions { @@ -24,7 +25,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions FunctionsDirective.Register(builder); InheritsDirective.Register(builder); SectionDirective.Register(builder); - + + builder.Features.Add(new DefaultTagHelperDescriptorProvider()); builder.Features.Add(new ViewComponentTagHelperDescriptorProvider()); builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension()); @@ -61,6 +63,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions InheritsDirective.Register(builder); SectionDirective.Register(builder); + builder.Features.Add(new DefaultTagHelperDescriptorProvider()); builder.Features.Add(new ViewComponentTagHelperDescriptorProvider()); builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension()); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs index 0abefc5fe5..a5c05dc585 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs @@ -11,10 +11,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions { public sealed class ViewComponentTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider { - // Hack for testability. The visitor will normally just no op if we're not referencing - // an appropriate version of MVC. - internal bool ForceEnabled { get; set; } - public int Order { get; set; } public void Execute(TagHelperDescriptorProviderContext context) @@ -33,10 +29,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions var types = new List(); var visitor = ViewComponentTypeVisitor.Create(compilation, types); - if (ForceEnabled) - { - visitor.Enabled = true; - } // We always visit the global namespace. visitor.Visit(compilation.Assembly.GlobalNamespace); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs index 9b44634095..2fd215d99d 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs @@ -1,7 +1,6 @@ // 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.Linq; using Microsoft.CodeAnalysis; @@ -29,12 +28,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions _viewComponentAttribute = viewComponentAttribute; _nonViewComponentAttribute = nonViewComponentAttribute; _results = results; - - Enabled = _viewComponentAttribute != null; } - public bool Enabled { get; set; } - public override void VisitNamedType(INamedTypeSymbol symbol) { if (IsViewComponent(symbol)) @@ -63,7 +58,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions internal bool IsViewComponent(INamedTypeSymbol symbol) { - if (!Enabled) + if (_viewComponentAttribute == null) { return false; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/TagHelperDescriptorProviderContext.cs b/src/Microsoft.AspNetCore.Razor.Language/TagHelperDescriptorProviderContext.cs index e8ad7b8219..4bf792c62d 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/TagHelperDescriptorProviderContext.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/TagHelperDescriptorProviderContext.cs @@ -8,6 +8,10 @@ namespace Microsoft.AspNetCore.Razor.Language { public abstract class TagHelperDescriptorProviderContext { + public virtual bool ExcludeHidden { get; set; } + + public virtual bool IncludeDocumentation { get; set; } + public abstract ItemCollection Items { get; } public abstract ICollection Results { get; } diff --git a/src/Microsoft.CodeAnalysis.Razor.Workspaces/TagHelperResolver.cs b/src/Microsoft.CodeAnalysis.Razor.Workspaces/TagHelperResolver.cs index d17dc90157..12cc172a51 100644 --- a/src/Microsoft.CodeAnalysis.Razor.Workspaces/TagHelperResolver.cs +++ b/src/Microsoft.CodeAnalysis.Razor.Workspaces/TagHelperResolver.cs @@ -41,6 +41,8 @@ namespace Microsoft.CodeAnalysis.Razor var results = new List(); var context = TagHelperDescriptorProviderContext.Create(results); + context.ExcludeHidden = true; + context.IncludeDocumentation = true; var compilation = await project.WorkspaceProject.GetCompilationAsync().ConfigureAwait(false); context.SetCompilation(compilation); diff --git a/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorFactory.cs b/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorFactory.cs index 1913b58bfa..9b6b07f639 100644 --- a/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorFactory.cs +++ b/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorFactory.cs @@ -28,9 +28,11 @@ namespace Microsoft.CodeAnalysis.Razor .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted) .WithMiscellaneousOptions(SymbolDisplayFormat.FullyQualifiedFormat.MiscellaneousOptions & (~SymbolDisplayMiscellaneousOptions.UseSpecialTypes)); - public DefaultTagHelperDescriptorFactory(Compilation compilation, bool designTime) + public DefaultTagHelperDescriptorFactory(Compilation compilation, bool includeDocumentation, bool excludeHidden) { - DesignTime = designTime; + IncludeDocumentation = includeDocumentation; + ExcludeHidden = excludeHidden; + _htmlAttributeNameAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlAttributeNameAttribute); _htmlAttributeNotBoundAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlAttributeNotBoundAttribute); _htmlTargetElementAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlTargetElementAttribute); @@ -40,7 +42,9 @@ namespace Microsoft.CodeAnalysis.Razor _iDictionarySymbol = compilation.GetTypeByMetadataName(TagHelperTypes.IDictionary); } - protected bool DesignTime { get; } + protected bool ExcludeHidden { get; } + + protected bool IncludeDocumentation { get; } /// public virtual TagHelperDescriptor CreateDescriptor(INamedTypeSymbol type) @@ -154,7 +158,7 @@ namespace Microsoft.CodeAnalysis.Razor private void AddDocumentation(INamedTypeSymbol type, TagHelperDescriptorBuilder builder) { - if (!DesignTime) + if (!IncludeDocumentation) { return; } @@ -169,10 +173,6 @@ namespace Microsoft.CodeAnalysis.Razor private void AddTagOutputHint(INamedTypeSymbol type, TagHelperDescriptorBuilder builder) { - if (!DesignTime) - { - return; - } string outputElementHint = null; var outputElementHintAttribute = type.GetAttributes().Where(a => a.AttributeClass == _outputElementHintAttributeSymbol).FirstOrDefault(); if (outputElementHintAttribute != null) @@ -221,7 +221,7 @@ namespace Microsoft.CodeAnalysis.Razor builder.IsEnum = true; } - if (DesignTime) + if (IncludeDocumentation) { var xml = property.GetDocumentationCommentXml(); @@ -416,7 +416,7 @@ namespace Microsoft.CodeAnalysis.Razor private bool ShouldSkipDescriptorCreation(ISymbol symbol) { - if (DesignTime) + if (ExcludeHidden) { var editorBrowsableAttribute = symbol.GetAttributes().Where(a => a.AttributeClass == _editorBrowsableAttributeSymbol).FirstOrDefault(); diff --git a/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs b/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs index c9effc07a1..b0d07db459 100644 --- a/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs +++ b/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs @@ -9,6 +9,10 @@ namespace Microsoft.CodeAnalysis.Razor { public sealed class DefaultTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider { + [Obsolete( + "This property is obsolete will not be honored. Documentation will be included if " + + "TagHelperDescriptorProviderContext.IncludeDocumentation is set to true. Hidden tag helpers will" + + "be excluded from the results if TagHelperDescriptorProviderContext.ExcludeHidden is set to true.")] public bool DesignTime { get; set; } public int Order { get; set; } @@ -44,7 +48,7 @@ namespace Microsoft.CodeAnalysis.Razor } } - var factory = new DefaultTagHelperDescriptorFactory(compilation, DesignTime); + var factory = new DefaultTagHelperDescriptorFactory(compilation, context.IncludeDocumentation, context.ExcludeHidden); for (var i = 0; i < types.Count; i++) { var descriptor = factory.CreateDescriptor(types[i]); diff --git a/src/Microsoft.CodeAnalysis.Remote.Razor/RemoteTagHelperResolver.cs b/src/Microsoft.CodeAnalysis.Remote.Razor/RemoteTagHelperResolver.cs index 5c8697db7f..f4ccaf7755 100644 --- a/src/Microsoft.CodeAnalysis.Remote.Razor/RemoteTagHelperResolver.cs +++ b/src/Microsoft.CodeAnalysis.Remote.Razor/RemoteTagHelperResolver.cs @@ -53,11 +53,7 @@ namespace Microsoft.CodeAnalysis.Razor // // Most notably, we are going to find the Tag Helpers using a compilation, and we have // no editor settings. - Action configure = (b) => - { - b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true }); - }; - + // // The default configuration currently matches MVC-2.0. Beyond MVC-2.0 we added SDK support for // properly detecting project versions, so that's a good version to assume when we can't find a // configuration. @@ -68,7 +64,7 @@ namespace Microsoft.CodeAnalysis.Razor // This will stop a crash from happening in this case (misconfigured project), but will still make // it obvious to the user that something is wrong. var factory = CreateFactory(configuration, factoryTypeName) ?? _fallbackFactory; - return factory.Create(configuration, RazorProjectFileSystem.Empty, configure); + return factory.Create(configuration, RazorProjectFileSystem.Empty, b => { }); } private IProjectEngineFactory CreateFactory(RazorConfiguration configuration, string factoryTypeName) diff --git a/src/Microsoft.VisualStudio.Editor.Razor/DefaultTagHelperResolver.cs b/src/Microsoft.VisualStudio.Editor.Razor/DefaultTagHelperResolver.cs index b303f7da60..a8cd965bae 100644 --- a/src/Microsoft.VisualStudio.Editor.Razor/DefaultTagHelperResolver.cs +++ b/src/Microsoft.VisualStudio.Editor.Razor/DefaultTagHelperResolver.cs @@ -36,10 +36,7 @@ namespace Microsoft.VisualStudio.Editor.Razor return Task.FromResult(TagHelperResolutionResult.Empty); } - var engine = _engineFactory.Create(project, RazorProjectFileSystem.Empty, b => - { - b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true, }); - }); + var engine = _engineFactory.Create(project, RazorProjectFileSystem.Empty, b => { }); return GetTagHelpersAsync(project, engine); } } diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorConfiguration.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorConfiguration.cs index 7a9ca712f1..11aeaf7828 100644 --- a/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorConfiguration.cs +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorConfiguration.cs @@ -177,7 +177,7 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem.Rules { Microsoft.Build.Framework.XamlTypes.Rule t = ((Microsoft.Build.Framework.XamlTypes.Rule)(ruleEnumerator.Current)); if (System.StringComparer.OrdinalIgnoreCase.Equals(t.Name, SchemaName)) { unboundRule = t; - unboundRule.Name = "6ffb2641-19cd-4943-bc6d-446919acb77e"; + unboundRule.Name = "a424c63f-67e5-4a8c-a436-92f3b3edfc9a"; RazorConfiguration.deserializedFallbackRule = unboundRule; } } diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorGeneral.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorGeneral.cs index f28d4a90c3..8a0839b337 100644 --- a/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorGeneral.cs +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/Rules/RazorGeneral.cs @@ -200,7 +200,7 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem.Rules { Microsoft.Build.Framework.XamlTypes.Rule t = ((Microsoft.Build.Framework.XamlTypes.Rule)(ruleEnumerator.Current)); if (System.StringComparer.OrdinalIgnoreCase.Equals(t.Name, SchemaName)) { unboundRule = t; - unboundRule.Name = "15acc140-184e-44be-a4d3-62505276a0bb"; + unboundRule.Name = "5a884e7d-b817-44fa-af10-8ad97fe2e643"; RazorGeneral.deserializedFallbackRule = unboundRule; } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/IntegrationTests/CodeGenerationIntegrationTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/IntegrationTests/CodeGenerationIntegrationTest.cs index 0f6a1a5b1f..bcddca531e 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language.IntegrationTests; @@ -13,22 +12,22 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis.Razor; -using Microsoft.Extensions.DependencyModel; using Xunit; namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.IntegrationTests { public class CodeGenerationIntegrationTest : IntegrationTestBase { - private const string CurrentMvcShim = "Microsoft.AspNetCore.Razor.Test.MvcShim.dll"; private static readonly RazorSourceDocument DefaultImports = MvcRazorTemplateEngine.GetDefaultImports(); + private CSharpCompilation BaseCompilation => MvcShim.BaseCompilation.WithAssemblyName("AppCode"); + #region Runtime [Fact] public void InvalidNamespaceAtEOF_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunRuntimeTest(references); + var compilation = BaseCompilation; + RunRuntimeTest(compilation); } [Fact] @@ -39,9 +38,9 @@ public class MyService { public string Html { get; set; } }"; - var compilationReferences = CreateCompilationReferences(CurrentMvcShim, appCode); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); - RunRuntimeTest(compilationReferences); + RunRuntimeTest(compilation); } [Fact] @@ -63,9 +62,9 @@ public class MyModel } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); - RunRuntimeTest(references); + RunRuntimeTest(compilation); } [Fact] @@ -87,44 +86,50 @@ public class MyModel { }"; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); - RunRuntimeTest(references); + RunRuntimeTest(compilation); } [Fact] public void MalformedPageDirective_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunRuntimeTest(references); + var compilation = BaseCompilation; + + RunRuntimeTest(compilation); } [Fact] public void Basic_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunRuntimeTest(references); + var compilation = BaseCompilation; + + RunRuntimeTest(compilation); } [Fact] public void Sections_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" using Microsoft.AspNetCore.Mvc.ViewFeatures; public class InputTestTagHelper : {typeof(TagHelper).FullName} {{ public ModelExpression For {{ get; set; }} }} -"); - RunRuntimeTest(references); +"; + + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunRuntimeTest(compilation); } [Fact] public void _ViewImports_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunRuntimeTest(references); + var compilation = BaseCompilation; + + RunRuntimeTest(compilation); } [Fact] @@ -136,9 +141,9 @@ public class MyApp public string MyProperty { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); - RunRuntimeTest(references); + RunRuntimeTest(compilation); } [Fact] @@ -159,9 +164,9 @@ public class MyApp { public string MyProperty { get; set; } }"; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); - RunRuntimeTest(references); + RunRuntimeTest(compilation); } [Fact] @@ -183,75 +188,81 @@ public class MyService public string Html { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); - RunRuntimeTest(references); + RunRuntimeTest(compilation); } [Fact] public void Model_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim); + var compilation = BaseCompilation; - RunRuntimeTest(references); + RunRuntimeTest(compilation); } [Fact] public void ModelExpressionTagHelper_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" using Microsoft.AspNetCore.Mvc.ViewFeatures; public class InputTestTagHelper : {typeof(TagHelper).FullName} {{ public ModelExpression For {{ get; set; }} }} -"); - RunRuntimeTest(references); +"; + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunRuntimeTest(compilation); } [Fact] public void RazorPages_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" public class DivTagHelper : {typeof(TagHelper).FullName} {{ }} -"); - RunRuntimeTest(references); +"; + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunRuntimeTest(compilation); } [Fact] public void RazorPagesWithoutModel_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" public class DivTagHelper : {typeof(TagHelper).FullName} {{ }} -"); - RunRuntimeTest(references); +"; + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunRuntimeTest(compilation); } [Fact] public void PageWithNamespace_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunRuntimeTest(references); + var compilation = BaseCompilation; + RunRuntimeTest(compilation); } [Fact] public void ViewWithNamespace_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunRuntimeTest(references); + var compilation = BaseCompilation; + RunRuntimeTest(compilation); } [Fact] public void ViewComponentTagHelper_Runtime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" public class TestViewComponent {{ public string Invoke(string firstName) @@ -265,8 +276,11 @@ public class AllTagHelper : {typeof(TagHelper).FullName} {{ public string Bar {{ get; set; }} }} -"); - RunRuntimeTest(references); +"; + + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunRuntimeTest(compilation); } #endregion @@ -274,8 +288,8 @@ public class AllTagHelper : {typeof(TagHelper).FullName} [Fact] public void InvalidNamespaceAtEOF_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] @@ -288,8 +302,9 @@ public class MyService } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] @@ -312,8 +327,9 @@ public class MyModel } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] @@ -337,43 +353,47 @@ public abstract class MyPageModel : Page } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void MalformedPageDirective_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] public void Basic_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] public void Sections_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" using Microsoft.AspNetCore.Mvc.ViewFeatures; public class InputTestTagHelper : {typeof(TagHelper).FullName} {{ public ModelExpression For {{ get; set; }} }} -"); - RunDesignTimeTest(references); +"; + + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void _ViewImports_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] @@ -385,8 +405,9 @@ public class MyApp public string MyProperty { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] @@ -408,8 +429,9 @@ public class MyApp public string MyProperty { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] @@ -431,15 +453,16 @@ public class MyApp public string MyProperty { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void Model_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] @@ -451,66 +474,74 @@ public class ThisShouldBeGenerated }"; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void ModelExpressionTagHelper_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" using Microsoft.AspNetCore.Mvc.ViewFeatures; public class InputTestTagHelper : {typeof(TagHelper).FullName} {{ public ModelExpression For {{ get; set; }} }} -"); - RunDesignTimeTest(references); +"; + + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void RazorPages_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" public class DivTagHelper : {typeof(TagHelper).FullName} {{ }} -"); - RunDesignTimeTest(references); +"; + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void RazorPagesWithoutModel_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" public class DivTagHelper : {typeof(TagHelper).FullName} {{ }} -"); - RunDesignTimeTest(references); +"; + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void PageWithNamespace_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] public void ViewWithNamespace_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] public void ViewComponentTagHelper_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" public class TestViewComponent {{ public string Invoke(string firstName) @@ -524,17 +555,22 @@ public class AllTagHelper : {typeof(TagHelper).FullName} {{ public string Bar {{ get; set; }} }} -"); - RunDesignTimeTest(references); +"; + + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } #endregion private void RunRuntimeTest( - IEnumerable compilationReferences, + CSharpCompilation baseCompilation, IEnumerable expectedErrors = null) { + Assert.Empty(baseCompilation.GetDiagnostics()); + // Arrange - var engine = CreateRuntimeEngine(compilationReferences); + var engine = CreateRuntimeEngine(baseCompilation); var document = CreateCodeDocument(); // Act @@ -543,15 +579,17 @@ public class AllTagHelper : {typeof(TagHelper).FullName} // Assert AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode()); AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument()); - AssertDocumentCompiles(document, compilationReferences, expectedErrors); + AssertDocumentCompiles(document, baseCompilation, expectedErrors); } private void RunDesignTimeTest( - IEnumerable compilationReferences, + CSharpCompilation baseCompilation, IEnumerable expectedErrors = null) { + Assert.Empty(baseCompilation.GetDiagnostics()); + // Arrange - var engine = CreateDesignTimeEngine(compilationReferences); + var engine = CreateDesignTimeEngine(baseCompilation); var document = CreateCodeDocument(); // Act @@ -561,25 +599,21 @@ public class AllTagHelper : {typeof(TagHelper).FullName} AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode()); AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument()); AssertSourceMappingsMatchBaseline(document); - AssertDocumentCompiles(document, compilationReferences, expectedErrors); - } - - private static IEnumerable CreateCompilationReferences(string mvcShimName, string appCode = null) - { - var shimReferences = CreateMvcShimReferences(mvcShimName); - return CreateAppCodeReferences(appCode, shimReferences); + AssertDocumentCompiles(document, baseCompilation, expectedErrors); } private void AssertDocumentCompiles( RazorCodeDocument document, - IEnumerable compilationReferences, + CSharpCompilation baseCompilation, IEnumerable expectedErrors = null) { var cSharp = document.GetCSharpDocument().GeneratedCode; var syntaxTree = CSharpSyntaxTree.ParseText(cSharp); var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); - var compilation = CSharpCompilation.Create("CodeGenerationTestAssembly", new[] { syntaxTree }, compilationReferences, options); + + var references = baseCompilation.References.Concat(new[] { baseCompilation.ToMetadataReference() }); + var compilation = CSharpCompilation.Create("CodeGenerationTestAssembly", new[] { syntaxTree }, references, options); var diagnostics = compilation.GetDiagnostics(); @@ -595,29 +629,29 @@ public class AllTagHelper : {typeof(TagHelper).FullName} } } - protected RazorEngine CreateDesignTimeEngine(IEnumerable references) + protected RazorEngine CreateDesignTimeEngine(CSharpCompilation compilation) { + var references = compilation.References.Concat(new[] { compilation.ToMetadataReference() }); + return RazorEngine.CreateDesignTime(b => { RazorExtensions.Register(b); b.Features.Add(GetMetadataReferenceFeature(references)); b.Features.Add(new CompilationTagHelperFeature()); - b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true }); - b.Features.Add(new ViewComponentTagHelperDescriptorProvider() { ForceEnabled = true }); }); } - protected RazorEngine CreateRuntimeEngine(IEnumerable references) + protected RazorEngine CreateRuntimeEngine(CSharpCompilation compilation) { + var references = compilation.References.Concat(new[] { compilation.ToMetadataReference() }); + return RazorEngine.Create(b => { RazorExtensions.Register(b); b.Features.Add(GetMetadataReferenceFeature(references)); b.Features.Add(new CompilationTagHelperFeature()); - b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true }); - b.Features.Add(new ViewComponentTagHelperDescriptorProvider() { ForceEnabled = true }); }); } @@ -664,48 +698,5 @@ public class AllTagHelper : {typeof(TagHelper).FullName} References = references.ToList() }; } - - private static IEnumerable CreateAppCodeReferences(string appCode, IEnumerable shimReferences) - { - var references = new List(shimReferences); - - if (appCode != null) - { - var appCodeSyntaxTrees = new List { CSharpSyntaxTree.ParseText(appCode) }; - - var compilation = CSharpCompilation.Create( - "AppCode", - appCodeSyntaxTrees, - shimReferences, - options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - var stream = new MemoryStream(); - var compilationResult = compilation.Emit(stream, options: new EmitOptions()); - stream.Position = 0; - - var diagString = string.Join(";", compilationResult.Diagnostics.Where(s => s.Severity == DiagnosticSeverity.Error).Select(s => s.ToString())); - Assert.True(compilationResult.Success, string.Format("Application code needed for tests didn't compile!: {0}", diagString)); - - references.Add(MetadataReference.CreateFromStream(stream)); - } - - return references; - } - - private static IEnumerable CreateMvcShimReferences(string mvcShimName) - { - var dllPath = Path.Combine(Directory.GetCurrentDirectory(), mvcShimName); - var assembly = Assembly.LoadFile(dllPath); - var assemblyDependencyContext = DependencyContext.Load(assembly); - - var assemblyReferencePaths = assemblyDependencyContext.CompileLibraries.SelectMany(l => l.ResolveReferencePaths()); - - var references = assemblyReferencePaths - .Select(assemblyPath => MetadataReference.CreateFromFile(assemblyPath)) - .ToList(); - - Assert.NotEmpty(references); - - return references; - } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcShim.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcShim.cs new file mode 100644 index 0000000000..0ee1e1a66d --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcShim.cs @@ -0,0 +1,45 @@ +// 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.IO; +using System.Reflection; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; + +namespace Microsoft.AspNetCore.Mvc.Razor.Extensions +{ + internal static class MvcShim + { + public static readonly string AssemblyName = "Microsoft.AspNetCore.Razor.Test.MvcShim"; + + private static Assembly _assembly; + private static CSharpCompilation _baseCompilation; + + public static Assembly Assembly + { + get + { + if (_assembly == null) + { + var filePath = Path.Combine(Directory.GetCurrentDirectory(), AssemblyName + ".dll"); + _assembly = Assembly.LoadFrom(filePath); + } + + return _assembly; + } + } + + public static CSharpCompilation BaseCompilation + { + get + { + if (_baseCompilation == null) + { + _baseCompilation = TestCompilation.Create(Assembly); + } + + return _baseCompilation; + } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ViewComponentTagHelperDescriptorProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ViewComponentTagHelperDescriptorProviderTest.cs index 3d156cb804..d2943b213d 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ViewComponentTagHelperDescriptorProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ViewComponentTagHelperDescriptorProviderTest.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.IO; using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Razor.Language; @@ -14,8 +15,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions // This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory. public class ViewComponentTagHelperDescriptorProviderTest { - private static readonly Assembly _assembly = typeof(ViewComponentTagHelperDescriptorProviderTest).GetTypeInfo().Assembly; - [Fact] public void DescriptorProvider_FindsVCTH() { @@ -27,15 +26,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions } "; - var testCompilation = TestCompilation.Create(_assembly, CSharpSyntaxTree.ParseText(code)); + var compilation = MvcShim.BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code)); var context = TagHelperDescriptorProviderContext.Create(); - context.SetCompilation(testCompilation); + context.SetCompilation(compilation); var provider = new ViewComponentTagHelperDescriptorProvider() { Engine = RazorEngine.CreateEmpty(b => { }), - ForceEnabled = true, }; var expectedDescriptor = TagHelperDescriptorBuilder.Create( @@ -68,8 +66,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions provider.Execute(context); // Assert - var descriptor = context.Results.FirstOrDefault(d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor)); - Assert.NotNull(descriptor); + Assert.Single(context.Results, d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor)); } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/IntegrationTests/CodeGenerationIntegrationTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/IntegrationTests/CodeGenerationIntegrationTest.cs index 6910a84284..e5ccbd73da 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -20,14 +20,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.IntegrationTests { public class CodeGenerationIntegrationTest : IntegrationTestBase { - private const string CurrentMvcShim = "Microsoft.AspNetCore.Razor.Test.MvcShim.Version1_X.dll"; private static readonly RazorSourceDocument DefaultImports = MvcRazorTemplateEngine.GetDefaultImports(); + private CSharpCompilation BaseCompilation => MvcShim.BaseCompilation.WithAssemblyName("AppCode"); + [Fact] public void InvalidNamespaceAtEOF_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] @@ -40,8 +41,8 @@ public class MyService } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + RunDesignTimeTest(compilation); } [Fact] @@ -64,8 +65,8 @@ public class MyModel } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + RunDesignTimeTest(compilation); } [Fact] @@ -89,36 +90,39 @@ public class MyModel } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + RunDesignTimeTest(compilation); } [Fact] public void Basic_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] public void Sections_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" using Microsoft.AspNetCore.Mvc.ViewFeatures; public class InputTestTagHelper : {typeof(TagHelper).FullName} {{ public ModelExpression For {{ get; set; }} }} -"); - RunDesignTimeTest(references); +"; + + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void _ViewImports_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] @@ -130,8 +134,8 @@ public class MyApp public string MyProperty { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + RunDesignTimeTest(compilation); } [Fact] @@ -153,8 +157,8 @@ public class MyApp public string MyProperty { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + RunDesignTimeTest(compilation); } [Fact] @@ -176,15 +180,15 @@ public class MyApp public string MyProperty { get; set; } } "; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + RunDesignTimeTest(compilation); } [Fact] public void Model_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim); - RunDesignTimeTest(references); + var compilation = BaseCompilation; + RunDesignTimeTest(compilation); } [Fact] @@ -196,28 +200,30 @@ public class ThisShouldBeGenerated }"; - var references = CreateCompilationReferences(CurrentMvcShim, appCode); - RunDesignTimeTest(references); + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + RunDesignTimeTest(compilation); } [Fact] public void ModelExpressionTagHelper_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" using Microsoft.AspNetCore.Mvc.ViewFeatures; public class InputTestTagHelper : {typeof(TagHelper).FullName} {{ public ModelExpression For {{ get; set; }} }} -"); - RunDesignTimeTest(references); +"; + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } [Fact] public void ViewComponentTagHelper_DesignTime() { - var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" + var appCode = $@" public class TestViewComponent {{ public string Invoke(string firstName) @@ -231,16 +237,21 @@ public class AllTagHelper : {typeof(TagHelper).FullName} {{ public string Bar {{ get; set; }} }} -"); - RunDesignTimeTest(references); +"; + + var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode)); + + RunDesignTimeTest(compilation); } private void RunDesignTimeTest( - IEnumerable compilationReferences, + CSharpCompilation baseCompilation, IEnumerable expectedErrors = null) { + Assert.Empty(baseCompilation.GetDiagnostics()); + // Arrange - var engine = CreateDesignTimeEngine(compilationReferences); + var engine = CreateDesignTimeEngine(baseCompilation); var document = CreateCodeDocument(); // Act @@ -250,25 +261,21 @@ public class AllTagHelper : {typeof(TagHelper).FullName} AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode()); AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument()); AssertSourceMappingsMatchBaseline(document); - AssertDocumentCompiles(document, compilationReferences, expectedErrors); - } - - private static IEnumerable CreateCompilationReferences(string mvcShimName, string appCode = null) - { - var shimReferences = CreateMvcShimReferences(mvcShimName); - return CreateAppCodeReferences(appCode, shimReferences); + AssertDocumentCompiles(document, baseCompilation, expectedErrors); } private void AssertDocumentCompiles( RazorCodeDocument document, - IEnumerable compilationReferences, + CSharpCompilation baseCompilation, IEnumerable expectedErrors = null) { var cSharp = document.GetCSharpDocument().GeneratedCode; var syntaxTree = CSharpSyntaxTree.ParseText(cSharp); var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); - var compilation = CSharpCompilation.Create("CodeGenerationTestAssembly", new[] { syntaxTree }, compilationReferences, options); + + var references = baseCompilation.References.Concat(new[] { baseCompilation.ToMetadataReference() }); + var compilation = CSharpCompilation.Create("CodeGenerationTestAssembly", new[] { syntaxTree }, references, options); var diagnostics = compilation.GetDiagnostics(); @@ -284,8 +291,10 @@ public class AllTagHelper : {typeof(TagHelper).FullName} } } - protected RazorEngine CreateDesignTimeEngine(IEnumerable references) + protected RazorEngine CreateDesignTimeEngine(CSharpCompilation compilation) { + var references = compilation.References.Concat(new[] { compilation.ToMetadataReference() }); + return RazorEngine.CreateDesignTime(b => { RazorExtensions.Register(b); @@ -293,8 +302,6 @@ public class AllTagHelper : {typeof(TagHelper).FullName} b.Features.Add(GetMetadataReferenceFeature(references)); b.Features.Add(new CompilationTagHelperFeature()); - b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true }); - b.Features.Add(new ViewComponentTagHelperDescriptorProvider() { ForceEnabled = true }); }); } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/MvcShim.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/MvcShim.cs new file mode 100644 index 0000000000..a50560e593 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/MvcShim.cs @@ -0,0 +1,45 @@ +// 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.IO; +using System.Reflection; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; + +namespace Microsoft.AspNetCore.Mvc.Razor.Extensions +{ + internal static class MvcShim + { + public static readonly string AssemblyName = "Microsoft.AspNetCore.Razor.Test.MvcShim.Version1_X"; + + private static Assembly _assembly; + private static CSharpCompilation _baseCompilation; + + public static Assembly Assembly + { + get + { + if (_assembly == null) + { + var filePath = Path.Combine(Directory.GetCurrentDirectory(), AssemblyName + ".dll"); + _assembly = Assembly.LoadFrom(filePath); + } + + return _assembly; + } + } + + public static CSharpCompilation BaseCompilation + { + get + { + if (_baseCompilation == null) + { + _baseCompilation = TestCompilation.Create(Assembly); + } + + return _baseCompilation; + } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/ViewComponentTagHelperDescriptorProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/ViewComponentTagHelperDescriptorProviderTest.cs index 4689e7c3c5..97a73797bf 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/ViewComponentTagHelperDescriptorProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.Test/ViewComponentTagHelperDescriptorProviderTest.cs @@ -14,8 +14,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X // This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory. public class ViewComponentTagHelperDescriptorProviderTest { - private static readonly Assembly _assembly = typeof(ViewComponentTagHelperDescriptorProviderTest).GetTypeInfo().Assembly; - [Fact] public void DescriptorProvider_FindsVCTH() { @@ -27,15 +25,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X } "; - var testCompilation = TestCompilation.Create(_assembly, CSharpSyntaxTree.ParseText(code)); + var compilation = MvcShim.BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code)); var context = TagHelperDescriptorProviderContext.Create(); - context.SetCompilation(testCompilation); + context.SetCompilation(compilation); var provider = new ViewComponentTagHelperDescriptorProvider() { Engine = RazorEngine.CreateEmpty(b => { }), - ForceEnabled = true, }; var expectedDescriptor = TagHelperDescriptorBuilder.Create( @@ -68,8 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X provider.Execute(context); // Assert - var descriptor = context.Results.FirstOrDefault(d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor)); - Assert.NotNull(descriptor); + Assert.Single(context.Results, d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor)); } } } diff --git a/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs b/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs index 1943118015..ffa7fd66d4 100644 --- a/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs +++ b/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs @@ -32,7 +32,7 @@ namespace Microsoft.CodeAnalysis public static string AssemblyName => "TestAssembly"; - public static Compilation Create(Assembly assembly, SyntaxTree syntaxTree = null) + public static CSharpCompilation Create(Assembly assembly, SyntaxTree syntaxTree = null) { IEnumerable syntaxTrees = null; @@ -47,7 +47,7 @@ namespace Microsoft.CodeAnalysis _referenceCache.TryAdd(assembly, metadataReferences); } - var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, metadataReferences); + var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, metadataReferences, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); EnsureValidCompilation(compilation); diff --git a/test/Microsoft.AspNetCore.Razor.Test.MvcShim.Version1_X/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs b/test/Microsoft.AspNetCore.Razor.Test.MvcShim.Version1_X/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs new file mode 100644 index 0000000000..5b470744c6 --- /dev/null +++ b/test/Microsoft.AspNetCore.Razor.Test.MvcShim.Version1_X/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs @@ -0,0 +1,11 @@ +// 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.Mvc +{ + public sealed class ViewComponentAttribute : Attribute + { + } +} diff --git a/test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc.Core/ActionResult.cs b/test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/ActionResult.cs similarity index 100% rename from test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc.Core/ActionResult.cs rename to test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/ActionResult.cs diff --git a/test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc.Core/RedirectResult.cs b/test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/RedirectResult.cs similarity index 100% rename from test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc.Core/RedirectResult.cs rename to test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/RedirectResult.cs diff --git a/test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs b/test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs new file mode 100644 index 0000000000..5b470744c6 --- /dev/null +++ b/test/Microsoft.AspNetCore.Razor.Test.MvcShim/Microsoft.AspNetCore.Mvc/ViewComponentAttribute.cs @@ -0,0 +1,11 @@ +// 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.Mvc +{ + public sealed class ViewComponentAttribute : Attribute + { + } +} diff --git a/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs b/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs index 4f1945747c..64f092cdb3 100644 --- a/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs +++ b/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs @@ -392,7 +392,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces TagHelperDescriptor expectedDescriptor) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -443,7 +443,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces TagHelperDescriptor expectedDescriptor) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -499,7 +499,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces TagHelperDescriptor expectedDescriptor) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -559,7 +559,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces TagHelperDescriptor expectedDescriptor) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -734,7 +734,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces TagHelperDescriptor expectedDescriptor) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime, designTime); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -931,7 +931,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces TagHelperDescriptor expectedDescriptor) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -968,7 +968,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces string expectedAttributeName) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -1005,7 +1005,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(validProperty2.Name) .TypeName(validProperty2.PropertyType.FullName), }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(OverriddenAttributeTagHelper).FullName); // Act @@ -1039,7 +1039,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(validProperty2.Name) .TypeName(validProperty2.PropertyType.FullName), }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedOverriddenAttributeTagHelper).FullName); // Act @@ -1073,7 +1073,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(validProperty2.Name) .TypeName(validProperty2.PropertyType.FullName), }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedNotOverriddenAttributeTagHelper).FullName); // Act @@ -1098,7 +1098,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(nameof(InheritedSingleAttributeTagHelper.IntAttribute)) .TypeName(typeof(int).FullName) }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedSingleAttributeTagHelper).FullName); // Act @@ -1124,7 +1124,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(intProperty.Name) .TypeName(intProperty.PropertyType.FullName) }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(SingleAttributeTagHelper).FullName); // Act @@ -1151,7 +1151,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(validProperty.Name) .TypeName(validProperty.PropertyType.FullName) }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(MissingAccessorTagHelper).FullName); // Act @@ -1178,7 +1178,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(validProperty.Name) .TypeName(validProperty.PropertyType.FullName) }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(NonPublicAccessorTagHelper).FullName); // Act @@ -1203,7 +1203,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(nameof(NotBoundAttributeTagHelper.BoundProperty)) .TypeName(typeof(object).FullName) }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(NotBoundAttributeTagHelper).FullName); // Act @@ -1234,7 +1234,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces builder => builder.RequireTagName("p"), builder => builder.RequireTagName("div"), }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(MultiTagTagHelper).FullName); // Act @@ -1260,7 +1260,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .PropertyName(validProp.Name) .TypeName(validProp.PropertyType.FullName), }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedMultiTagTagHelper).FullName); // Act @@ -1284,7 +1284,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces builder => builder.RequireTagName("div"), }); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(DuplicateTagNameTagHelper).FullName); // Act @@ -1303,7 +1303,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces "data-condition", typeof(OverrideNameTagHelper).FullName, AssemblyName); - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(OverrideNameTagHelper).FullName); // Act @@ -1345,7 +1345,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var compilation = TestCompilation.Create(_assembly, syntaxTree); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var attribute = tagHelperType.GetAttributes().Single(); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false); // Act var descriptor = factory.CreateDescriptor(tagHelperType); @@ -1366,21 +1366,21 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces { // name, expectedNames return new TheoryData> - { - { "p", new[] { "p" } }, - { " p", new[] { "p" } }, - { "p ", new[] { "p" } }, - { " p ", new[] { "p" } }, - { "p,div", new[] { "p", "div" } }, - { " p,div", new[] { "p", "div" } }, - { "p ,div", new[] { "p", "div" } }, - { " p ,div", new[] { "p", "div" } }, - { "p, div", new[] { "p", "div" } }, - { "p,div ", new[] { "p", "div" } }, - { "p, div ", new[] { "p", "div" } }, - { " p, div ", new[] { "p", "div" } }, - { " p , div ", new[] { "p", "div" } }, - }; + { + { "p", new[] { "p" } }, + { " p", new[] { "p" } }, + { "p ", new[] { "p" } }, + { " p ", new[] { "p" } }, + { "p,div", new[] { "p", "div" } }, + { " p,div", new[] { "p", "div" } }, + { "p ,div", new[] { "p", "div" } }, + { " p ,div", new[] { "p", "div" } }, + { "p, div", new[] { "p", "div" } }, + { "p,div ", new[] { "p", "div" } }, + { "p, div ", new[] { "p", "div" } }, + { " p, div ", new[] { "p", "div" } }, + { " p , div ", new[] { "p", "div" } }, + }; } } @@ -1477,7 +1477,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces IEnumerable expectedAttributeDescriptors) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(type.FullName); // Act @@ -1527,7 +1527,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var syntaxTree = CSharpSyntaxTree.ParseText(text); var compilation = TestCompilation.Create(_assembly, syntaxTree); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false); // Act var descriptor = factory.CreateDescriptor(tagHelperType); @@ -1567,7 +1567,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var syntaxTree = CSharpSyntaxTree.ParseText(text); var compilation = TestCompilation.Create(_assembly, syntaxTree); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false); // Act var descriptor = factory.CreateDescriptor(tagHelperType); @@ -1610,7 +1610,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var syntaxTree = CSharpSyntaxTree.ParseText(text); var compilation = TestCompilation.Create(_assembly, syntaxTree); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false); // Act var descriptor = factory.CreateDescriptor(tagHelperType); @@ -1656,7 +1656,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var syntaxTree = CSharpSyntaxTree.ParseText(text); var compilation = TestCompilation.Create(_assembly, syntaxTree); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false); // Act var descriptor = factory.CreateDescriptor(tagHelperType); @@ -1698,7 +1698,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var syntaxTree = CSharpSyntaxTree.ParseText(text); var compilation = TestCompilation.Create(_assembly, syntaxTree); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false); // Act var descriptor = factory.CreateDescriptor(tagHelperType); @@ -1739,7 +1739,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var syntaxTree = CSharpSyntaxTree.ParseText(text); var compilation = TestCompilation.Create(_assembly, syntaxTree); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false); // Act var descriptor = factory.CreateDescriptor(tagHelperType); @@ -1754,7 +1754,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces { // Arrange var objectAssemblyName = typeof(Enumerable).GetTypeInfo().Assembly.GetName().Name; - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(Enumerable).FullName); var expectedDescriptor = CreateTagHelperDescriptor("enumerable", "System.Linq.Enumerable", typeSymbol.ContainingAssembly.Identity.Name); @@ -2010,7 +2010,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces IEnumerable expectedDiagnostics) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -2069,12 +2069,12 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces [Theory] [MemberData(nameof(TagOutputHintData))] - public void CreateDescriptor_CreatesDesignTimeDescriptorsWithOutputElementHint( + public void CreateDescriptor_CreatesDescriptorsWithOutputElementHint( Type tagHelperType, TagHelperDescriptor expectedDescriptor) { // Arrange - var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: true); + var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); // Act @@ -2102,7 +2102,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces { }"); var compilation = TestCompilation.Create(_assembly, syntaxTree); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: true); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: true, excludeHidden: false); var typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper"); var expectedDocumentation = @" @@ -2151,7 +2151,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces public List RemarksAndSummaryProperty { get; set; } }"); var compilation = TestCompilation.Create(_assembly, syntaxTree); - var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: true); + var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: true, excludeHidden: false); var typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper"); var expectedDocumentations = new[] { diff --git a/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorProviderTest.cs b/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorProviderTest.cs index 58e850876d..b7cbf6a9f6 100644 --- a/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorProviderTest.cs +++ b/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorProviderTest.cs @@ -18,11 +18,10 @@ namespace Microsoft.CodeAnalysis.Razor // Arrange var editorBrowsableTypeName = "Microsoft.CodeAnalysis.Razor.Workspaces.Test.EditorBrowsableTagHelper"; var compilation = TestCompilation.Create(_assembly); - var descriptorProvider = new DefaultTagHelperDescriptorProvider() - { - DesignTime = true, - }; + var descriptorProvider = new DefaultTagHelperDescriptorProvider(); + var context = TagHelperDescriptorProviderContext.Create(); + context.ExcludeHidden = true; // Act descriptorProvider.Execute(context); diff --git a/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultProjectEngineFactoryServiceTest.cs b/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultProjectEngineFactoryServiceTest.cs index 1f418b2855..5c7e4da5a2 100644 --- a/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultProjectEngineFactoryServiceTest.cs +++ b/test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultProjectEngineFactoryServiceTest.cs @@ -187,6 +187,7 @@ namespace Microsoft.VisualStudio.Editor.Razor // Assert Assert.Single(engine.Engine.Features.OfType()); + Assert.Single(engine.Engine.Features.OfType()); Assert.Single(engine.Engine.Features.OfType()); Assert.Single(engine.Engine.Features.OfType()); Assert.Single(engine.Engine.Features.OfType());