Merge branch 'rel/vs15.7' into dev

This commit is contained in:
Ryan Nowak 2018-03-08 15:49:01 -08:00
commit 2f79b90af5
26 changed files with 402 additions and 323 deletions

View File

@ -4,6 +4,7 @@
using System; using System;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions; using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.CodeAnalysis.Razor;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
{ {
@ -22,6 +23,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
FunctionsDirective.Register(builder); FunctionsDirective.Register(builder);
InheritsDirective.Register(builder); InheritsDirective.Register(builder);
builder.Features.Add(new DefaultTagHelperDescriptorProvider());
// Register section directive with the 1.x compatible target extension. // Register section directive with the 1.x compatible target extension.
builder.AddDirective(SectionDirective.Directive); builder.AddDirective(SectionDirective.Directive);
builder.Features.Add(new SectionDirectivePass()); builder.Features.Add(new SectionDirectivePass());
@ -67,6 +70,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
FunctionsDirective.Register(builder); FunctionsDirective.Register(builder);
InheritsDirective.Register(builder); InheritsDirective.Register(builder);
builder.Features.Add(new DefaultTagHelperDescriptorProvider());
// Register section directive with the 1.x compatible target extension. // Register section directive with the 1.x compatible target extension.
builder.AddDirective(SectionDirective.Directive); builder.AddDirective(SectionDirective.Directive);
builder.Features.Add(new SectionDirectivePass()); builder.Features.Add(new SectionDirectivePass());

View File

@ -11,10 +11,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
{ {
public sealed class ViewComponentTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider 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 int Order { get; set; }
public void Execute(TagHelperDescriptorProviderContext context) public void Execute(TagHelperDescriptorProviderContext context)
@ -33,10 +29,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
var types = new List<INamedTypeSymbol>(); var types = new List<INamedTypeSymbol>();
var visitor = ViewComponentTypeVisitor.Create(compilation, types); var visitor = ViewComponentTypeVisitor.Create(compilation, types);
if (ForceEnabled)
{
visitor.Enabled = true;
}
// We always visit the global namespace. // We always visit the global namespace.
visitor.Visit(compilation.Assembly.GlobalNamespace); visitor.Visit(compilation.Assembly.GlobalNamespace);

View File

@ -18,22 +18,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
public static ViewComponentTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> results) public static ViewComponentTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> results)
{ {
var enabled = false; var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute);
foreach (var reference in compilation.References) var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute);
{
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;
return new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, results); return new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, results);
} }

View File

@ -4,6 +4,7 @@
using System; using System;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions; using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.CodeAnalysis.Razor;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
@ -25,6 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
InheritsDirective.Register(builder); InheritsDirective.Register(builder);
SectionDirective.Register(builder); SectionDirective.Register(builder);
builder.Features.Add(new DefaultTagHelperDescriptorProvider());
builder.Features.Add(new ViewComponentTagHelperDescriptorProvider()); builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());
builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension()); builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());
@ -61,6 +63,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
InheritsDirective.Register(builder); InheritsDirective.Register(builder);
SectionDirective.Register(builder); SectionDirective.Register(builder);
builder.Features.Add(new DefaultTagHelperDescriptorProvider());
builder.Features.Add(new ViewComponentTagHelperDescriptorProvider()); builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());
builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension()); builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());

View File

@ -11,10 +11,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
public sealed class ViewComponentTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider 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 int Order { get; set; }
public void Execute(TagHelperDescriptorProviderContext context) public void Execute(TagHelperDescriptorProviderContext context)
@ -33,10 +29,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
var types = new List<INamedTypeSymbol>(); var types = new List<INamedTypeSymbol>();
var visitor = ViewComponentTypeVisitor.Create(compilation, types); var visitor = ViewComponentTypeVisitor.Create(compilation, types);
if (ForceEnabled)
{
visitor.Enabled = true;
}
// We always visit the global namespace. // We always visit the global namespace.
visitor.Visit(compilation.Assembly.GlobalNamespace); visitor.Visit(compilation.Assembly.GlobalNamespace);

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
@ -29,12 +28,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
_viewComponentAttribute = viewComponentAttribute; _viewComponentAttribute = viewComponentAttribute;
_nonViewComponentAttribute = nonViewComponentAttribute; _nonViewComponentAttribute = nonViewComponentAttribute;
_results = results; _results = results;
Enabled = _viewComponentAttribute != null;
} }
public bool Enabled { get; set; }
public override void VisitNamedType(INamedTypeSymbol symbol) public override void VisitNamedType(INamedTypeSymbol symbol)
{ {
if (IsViewComponent(symbol)) if (IsViewComponent(symbol))
@ -63,7 +58,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
internal bool IsViewComponent(INamedTypeSymbol symbol) internal bool IsViewComponent(INamedTypeSymbol symbol)
{ {
if (!Enabled) if (_viewComponentAttribute == null)
{ {
return false; return false;
} }

View File

@ -8,6 +8,10 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
public abstract class TagHelperDescriptorProviderContext public abstract class TagHelperDescriptorProviderContext
{ {
public virtual bool ExcludeHidden { get; set; }
public virtual bool IncludeDocumentation { get; set; }
public abstract ItemCollection Items { get; } public abstract ItemCollection Items { get; }
public abstract ICollection<TagHelperDescriptor> Results { get; } public abstract ICollection<TagHelperDescriptor> Results { get; }

View File

@ -41,6 +41,8 @@ namespace Microsoft.CodeAnalysis.Razor
var results = new List<TagHelperDescriptor>(); var results = new List<TagHelperDescriptor>();
var context = TagHelperDescriptorProviderContext.Create(results); var context = TagHelperDescriptorProviderContext.Create(results);
context.ExcludeHidden = true;
context.IncludeDocumentation = true;
var compilation = await project.WorkspaceProject.GetCompilationAsync().ConfigureAwait(false); var compilation = await project.WorkspaceProject.GetCompilationAsync().ConfigureAwait(false);
context.SetCompilation(compilation); context.SetCompilation(compilation);

View File

@ -28,9 +28,11 @@ namespace Microsoft.CodeAnalysis.Razor
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted) .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)
.WithMiscellaneousOptions(SymbolDisplayFormat.FullyQualifiedFormat.MiscellaneousOptions & (~SymbolDisplayMiscellaneousOptions.UseSpecialTypes)); .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); _htmlAttributeNameAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlAttributeNameAttribute);
_htmlAttributeNotBoundAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlAttributeNotBoundAttribute); _htmlAttributeNotBoundAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlAttributeNotBoundAttribute);
_htmlTargetElementAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlTargetElementAttribute); _htmlTargetElementAttributeSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.HtmlTargetElementAttribute);
@ -40,7 +42,9 @@ namespace Microsoft.CodeAnalysis.Razor
_iDictionarySymbol = compilation.GetTypeByMetadataName(TagHelperTypes.IDictionary); _iDictionarySymbol = compilation.GetTypeByMetadataName(TagHelperTypes.IDictionary);
} }
protected bool DesignTime { get; } protected bool ExcludeHidden { get; }
protected bool IncludeDocumentation { get; }
/// <inheritdoc /> /// <inheritdoc />
public virtual TagHelperDescriptor CreateDescriptor(INamedTypeSymbol type) public virtual TagHelperDescriptor CreateDescriptor(INamedTypeSymbol type)
@ -154,7 +158,7 @@ namespace Microsoft.CodeAnalysis.Razor
private void AddDocumentation(INamedTypeSymbol type, TagHelperDescriptorBuilder builder) private void AddDocumentation(INamedTypeSymbol type, TagHelperDescriptorBuilder builder)
{ {
if (!DesignTime) if (!IncludeDocumentation)
{ {
return; return;
} }
@ -169,10 +173,6 @@ namespace Microsoft.CodeAnalysis.Razor
private void AddTagOutputHint(INamedTypeSymbol type, TagHelperDescriptorBuilder builder) private void AddTagOutputHint(INamedTypeSymbol type, TagHelperDescriptorBuilder builder)
{ {
if (!DesignTime)
{
return;
}
string outputElementHint = null; string outputElementHint = null;
var outputElementHintAttribute = type.GetAttributes().Where(a => a.AttributeClass == _outputElementHintAttributeSymbol).FirstOrDefault(); var outputElementHintAttribute = type.GetAttributes().Where(a => a.AttributeClass == _outputElementHintAttributeSymbol).FirstOrDefault();
if (outputElementHintAttribute != null) if (outputElementHintAttribute != null)
@ -221,7 +221,7 @@ namespace Microsoft.CodeAnalysis.Razor
builder.IsEnum = true; builder.IsEnum = true;
} }
if (DesignTime) if (IncludeDocumentation)
{ {
var xml = property.GetDocumentationCommentXml(); var xml = property.GetDocumentationCommentXml();
@ -416,7 +416,7 @@ namespace Microsoft.CodeAnalysis.Razor
private bool ShouldSkipDescriptorCreation(ISymbol symbol) private bool ShouldSkipDescriptorCreation(ISymbol symbol)
{ {
if (DesignTime) if (ExcludeHidden)
{ {
var editorBrowsableAttribute = symbol.GetAttributes().Where(a => a.AttributeClass == _editorBrowsableAttributeSymbol).FirstOrDefault(); var editorBrowsableAttribute = symbol.GetAttributes().Where(a => a.AttributeClass == _editorBrowsableAttributeSymbol).FirstOrDefault();

View File

@ -9,6 +9,10 @@ namespace Microsoft.CodeAnalysis.Razor
{ {
public sealed class DefaultTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider 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 bool DesignTime { get; set; }
public int Order { 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++) for (var i = 0; i < types.Count; i++)
{ {
var descriptor = factory.CreateDescriptor(types[i]); var descriptor = factory.CreateDescriptor(types[i]);

View File

@ -53,11 +53,7 @@ namespace Microsoft.CodeAnalysis.Razor
// //
// Most notably, we are going to find the Tag Helpers using a compilation, and we have // Most notably, we are going to find the Tag Helpers using a compilation, and we have
// no editor settings. // no editor settings.
Action<RazorProjectEngineBuilder> 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 // 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 // properly detecting project versions, so that's a good version to assume when we can't find a
// configuration. // 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 // 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. // it obvious to the user that something is wrong.
var factory = CreateFactory(configuration, factoryTypeName) ?? _fallbackFactory; 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) private IProjectEngineFactory CreateFactory(RazorConfiguration configuration, string factoryTypeName)

View File

@ -36,10 +36,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
return Task.FromResult(TagHelperResolutionResult.Empty); return Task.FromResult(TagHelperResolutionResult.Empty);
} }
var engine = _engineFactory.Create(project, RazorProjectFileSystem.Empty, b => var engine = _engineFactory.Create(project, RazorProjectFileSystem.Empty, b => { });
{
b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true, });
});
return GetTagHelpersAsync(project, engine); return GetTagHelpersAsync(project, engine);
} }
} }

View File

@ -4,7 +4,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.IntegrationTests; using Microsoft.AspNetCore.Razor.Language.IntegrationTests;
@ -13,22 +12,22 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Razor; using Microsoft.CodeAnalysis.Razor;
using Microsoft.Extensions.DependencyModel;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.IntegrationTests namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.IntegrationTests
{ {
public class CodeGenerationIntegrationTest : IntegrationTestBase public class CodeGenerationIntegrationTest : IntegrationTestBase
{ {
private const string CurrentMvcShim = "Microsoft.AspNetCore.Razor.Test.MvcShim.dll";
private static readonly RazorSourceDocument DefaultImports = MvcRazorTemplateEngine.GetDefaultImports(); private static readonly RazorSourceDocument DefaultImports = MvcRazorTemplateEngine.GetDefaultImports();
private CSharpCompilation BaseCompilation => MvcShim.BaseCompilation.WithAssemblyName("AppCode");
#region Runtime #region Runtime
[Fact] [Fact]
public void InvalidNamespaceAtEOF_Runtime() public void InvalidNamespaceAtEOF_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunRuntimeTest(references); RunRuntimeTest(compilation);
} }
[Fact] [Fact]
@ -39,9 +38,9 @@ public class MyService<TModel>
{ {
public string Html { get; set; } public string Html { get; set; }
}"; }";
var compilationReferences = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(compilationReferences); RunRuntimeTest(compilation);
} }
[Fact] [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] [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] [Fact]
public void MalformedPageDirective_Runtime() public void MalformedPageDirective_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunRuntimeTest(references);
RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void Basic_Runtime() public void Basic_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunRuntimeTest(references);
RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void Sections_Runtime() public void Sections_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class InputTestTagHelper : {typeof(TagHelper).FullName} public class InputTestTagHelper : {typeof(TagHelper).FullName}
{{ {{
public ModelExpression For {{ get; set; }} public ModelExpression For {{ get; set; }}
}} }}
"); ";
RunRuntimeTest(references);
var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void _ViewImports_Runtime() public void _ViewImports_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunRuntimeTest(references);
RunRuntimeTest(compilation);
} }
[Fact] [Fact]
@ -136,9 +141,9 @@ public class MyApp
public string MyProperty { get; set; } public string MyProperty { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(references); RunRuntimeTest(compilation);
} }
[Fact] [Fact]
@ -159,9 +164,9 @@ public class MyApp
{ {
public string MyProperty { get; set; } public string MyProperty { get; set; }
}"; }";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(references); RunRuntimeTest(compilation);
} }
[Fact] [Fact]
@ -183,75 +188,81 @@ public class MyService<TModel>
public string Html { get; set; } public string Html { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(references); RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void Model_Runtime() public void Model_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunRuntimeTest(references); RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void ModelExpressionTagHelper_Runtime() public void ModelExpressionTagHelper_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class InputTestTagHelper : {typeof(TagHelper).FullName} public class InputTestTagHelper : {typeof(TagHelper).FullName}
{{ {{
public ModelExpression For {{ get; set; }} public ModelExpression For {{ get; set; }}
}} }}
"); ";
RunRuntimeTest(references); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void RazorPages_Runtime() public void RazorPages_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
public class DivTagHelper : {typeof(TagHelper).FullName} public class DivTagHelper : {typeof(TagHelper).FullName}
{{ {{
}} }}
"); ";
RunRuntimeTest(references); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void RazorPagesWithoutModel_Runtime() public void RazorPagesWithoutModel_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
public class DivTagHelper : {typeof(TagHelper).FullName} public class DivTagHelper : {typeof(TagHelper).FullName}
{{ {{
}} }}
"); ";
RunRuntimeTest(references); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void PageWithNamespace_Runtime() public void PageWithNamespace_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunRuntimeTest(references); RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void ViewWithNamespace_Runtime() public void ViewWithNamespace_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunRuntimeTest(references); RunRuntimeTest(compilation);
} }
[Fact] [Fact]
public void ViewComponentTagHelper_Runtime() public void ViewComponentTagHelper_Runtime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
public class TestViewComponent public class TestViewComponent
{{ {{
public string Invoke(string firstName) public string Invoke(string firstName)
@ -265,8 +276,11 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
{{ {{
public string Bar {{ get; set; }} public string Bar {{ get; set; }}
}} }}
"); ";
RunRuntimeTest(references);
var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunRuntimeTest(compilation);
} }
#endregion #endregion
@ -274,8 +288,8 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
[Fact] [Fact]
public void InvalidNamespaceAtEOF_DesignTime() public void InvalidNamespaceAtEOF_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -288,8 +302,9 @@ public class MyService<TModel>
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references);
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -312,8 +327,9 @@ public class MyModel
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references);
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -337,43 +353,47 @@ public abstract class MyPageModel<T> : Page
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references);
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void MalformedPageDirective_DesignTime() public void MalformedPageDirective_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void Basic_DesignTime() public void Basic_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void Sections_DesignTime() public void Sections_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class InputTestTagHelper : {typeof(TagHelper).FullName} public class InputTestTagHelper : {typeof(TagHelper).FullName}
{{ {{
public ModelExpression For {{ get; set; }} public ModelExpression For {{ get; set; }}
}} }}
"); ";
RunDesignTimeTest(references);
var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void _ViewImports_DesignTime() public void _ViewImports_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -385,8 +405,9 @@ public class MyApp
public string MyProperty { get; set; } public string MyProperty { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references);
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -408,8 +429,9 @@ public class MyApp
public string MyProperty { get; set; } public string MyProperty { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references);
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -431,15 +453,16 @@ public class MyApp
public string MyProperty { get; set; } public string MyProperty { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references);
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void Model_DesignTime() public void Model_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -451,66 +474,74 @@ public class ThisShouldBeGenerated
}"; }";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references);
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void ModelExpressionTagHelper_DesignTime() public void ModelExpressionTagHelper_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class InputTestTagHelper : {typeof(TagHelper).FullName} public class InputTestTagHelper : {typeof(TagHelper).FullName}
{{ {{
public ModelExpression For {{ get; set; }} public ModelExpression For {{ get; set; }}
}} }}
"); ";
RunDesignTimeTest(references);
var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void RazorPages_DesignTime() public void RazorPages_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
public class DivTagHelper : {typeof(TagHelper).FullName} public class DivTagHelper : {typeof(TagHelper).FullName}
{{ {{
}} }}
"); ";
RunDesignTimeTest(references); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void RazorPagesWithoutModel_DesignTime() public void RazorPagesWithoutModel_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
public class DivTagHelper : {typeof(TagHelper).FullName} public class DivTagHelper : {typeof(TagHelper).FullName}
{{ {{
}} }}
"); ";
RunDesignTimeTest(references); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void PageWithNamespace_DesignTime() public void PageWithNamespace_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void ViewWithNamespace_DesignTime() public void ViewWithNamespace_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void ViewComponentTagHelper_DesignTime() public void ViewComponentTagHelper_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
public class TestViewComponent public class TestViewComponent
{{ {{
public string Invoke(string firstName) public string Invoke(string firstName)
@ -524,17 +555,22 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
{{ {{
public string Bar {{ get; set; }} public string Bar {{ get; set; }}
}} }}
"); ";
RunDesignTimeTest(references);
var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
#endregion #endregion
private void RunRuntimeTest( private void RunRuntimeTest(
IEnumerable<MetadataReference> compilationReferences, CSharpCompilation baseCompilation,
IEnumerable<string> expectedErrors = null) IEnumerable<string> expectedErrors = null)
{ {
Assert.Empty(baseCompilation.GetDiagnostics());
// Arrange // Arrange
var engine = CreateRuntimeEngine(compilationReferences); var engine = CreateRuntimeEngine(baseCompilation);
var document = CreateCodeDocument(); var document = CreateCodeDocument();
// Act // Act
@ -543,15 +579,17 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
// Assert // Assert
AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode()); AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode());
AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument()); AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument());
AssertDocumentCompiles(document, compilationReferences, expectedErrors); AssertDocumentCompiles(document, baseCompilation, expectedErrors);
} }
private void RunDesignTimeTest( private void RunDesignTimeTest(
IEnumerable<MetadataReference> compilationReferences, CSharpCompilation baseCompilation,
IEnumerable<string> expectedErrors = null) IEnumerable<string> expectedErrors = null)
{ {
Assert.Empty(baseCompilation.GetDiagnostics());
// Arrange // Arrange
var engine = CreateDesignTimeEngine(compilationReferences); var engine = CreateDesignTimeEngine(baseCompilation);
var document = CreateCodeDocument(); var document = CreateCodeDocument();
// Act // Act
@ -561,25 +599,21 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode()); AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode());
AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument()); AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument());
AssertSourceMappingsMatchBaseline(document); AssertSourceMappingsMatchBaseline(document);
AssertDocumentCompiles(document, compilationReferences, expectedErrors); AssertDocumentCompiles(document, baseCompilation, expectedErrors);
}
private static IEnumerable<MetadataReference> CreateCompilationReferences(string mvcShimName, string appCode = null)
{
var shimReferences = CreateMvcShimReferences(mvcShimName);
return CreateAppCodeReferences(appCode, shimReferences);
} }
private void AssertDocumentCompiles( private void AssertDocumentCompiles(
RazorCodeDocument document, RazorCodeDocument document,
IEnumerable<MetadataReference> compilationReferences, CSharpCompilation baseCompilation,
IEnumerable<string> expectedErrors = null) IEnumerable<string> expectedErrors = null)
{ {
var cSharp = document.GetCSharpDocument().GeneratedCode; var cSharp = document.GetCSharpDocument().GeneratedCode;
var syntaxTree = CSharpSyntaxTree.ParseText(cSharp); var syntaxTree = CSharpSyntaxTree.ParseText(cSharp);
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); 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(); var diagnostics = compilation.GetDiagnostics();
@ -595,29 +629,29 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
} }
} }
protected RazorEngine CreateDesignTimeEngine(IEnumerable<MetadataReference> references) protected RazorEngine CreateDesignTimeEngine(CSharpCompilation compilation)
{ {
var references = compilation.References.Concat(new[] { compilation.ToMetadataReference() });
return RazorEngine.CreateDesignTime(b => return RazorEngine.CreateDesignTime(b =>
{ {
RazorExtensions.Register(b); RazorExtensions.Register(b);
b.Features.Add(GetMetadataReferenceFeature(references)); b.Features.Add(GetMetadataReferenceFeature(references));
b.Features.Add(new CompilationTagHelperFeature()); b.Features.Add(new CompilationTagHelperFeature());
b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true });
b.Features.Add(new ViewComponentTagHelperDescriptorProvider() { ForceEnabled = true });
}); });
} }
protected RazorEngine CreateRuntimeEngine(IEnumerable<MetadataReference> references) protected RazorEngine CreateRuntimeEngine(CSharpCompilation compilation)
{ {
var references = compilation.References.Concat(new[] { compilation.ToMetadataReference() });
return RazorEngine.Create(b => return RazorEngine.Create(b =>
{ {
RazorExtensions.Register(b); RazorExtensions.Register(b);
b.Features.Add(GetMetadataReferenceFeature(references)); b.Features.Add(GetMetadataReferenceFeature(references));
b.Features.Add(new CompilationTagHelperFeature()); 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() References = references.ToList()
}; };
} }
private static IEnumerable<MetadataReference> CreateAppCodeReferences(string appCode, IEnumerable<MetadataReference> shimReferences)
{
var references = new List<MetadataReference>(shimReferences);
if (appCode != null)
{
var appCodeSyntaxTrees = new List<SyntaxTree> { 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<MetadataReference> 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<MetadataReference>();
Assert.NotEmpty(references);
return references;
}
} }
} }

View File

@ -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;
}
}
}
}

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Razor.Language; 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. // This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory.
public class ViewComponentTagHelperDescriptorProviderTest public class ViewComponentTagHelperDescriptorProviderTest
{ {
private static readonly Assembly _assembly = typeof(ViewComponentTagHelperDescriptorProviderTest).GetTypeInfo().Assembly;
[Fact] [Fact]
public void DescriptorProvider_FindsVCTH() 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(); var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(testCompilation); context.SetCompilation(compilation);
var provider = new ViewComponentTagHelperDescriptorProvider() var provider = new ViewComponentTagHelperDescriptorProvider()
{ {
Engine = RazorEngine.CreateEmpty(b => { }), Engine = RazorEngine.CreateEmpty(b => { }),
ForceEnabled = true,
}; };
var expectedDescriptor = TagHelperDescriptorBuilder.Create( var expectedDescriptor = TagHelperDescriptorBuilder.Create(
@ -68,8 +66,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
provider.Execute(context); provider.Execute(context);
// Assert // Assert
var descriptor = context.Results.FirstOrDefault(d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor)); Assert.Single(context.Results, d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor));
Assert.NotNull(descriptor);
} }
} }
} }

View File

@ -20,14 +20,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.IntegrationTests
{ {
public class CodeGenerationIntegrationTest : IntegrationTestBase public class CodeGenerationIntegrationTest : IntegrationTestBase
{ {
private const string CurrentMvcShim = "Microsoft.AspNetCore.Razor.Test.MvcShim.Version1_X.dll";
private static readonly RazorSourceDocument DefaultImports = MvcRazorTemplateEngine.GetDefaultImports(); private static readonly RazorSourceDocument DefaultImports = MvcRazorTemplateEngine.GetDefaultImports();
private CSharpCompilation BaseCompilation => MvcShim.BaseCompilation.WithAssemblyName("AppCode");
[Fact] [Fact]
public void InvalidNamespaceAtEOF_DesignTime() public void InvalidNamespaceAtEOF_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -40,8 +41,8 @@ public class MyService<TModel>
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -64,8 +65,8 @@ public class MyModel
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -89,36 +90,39 @@ public class MyModel
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void Basic_DesignTime() public void Basic_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void Sections_DesignTime() public void Sections_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class InputTestTagHelper : {typeof(TagHelper).FullName} public class InputTestTagHelper : {typeof(TagHelper).FullName}
{{ {{
public ModelExpression For {{ get; set; }} public ModelExpression For {{ get; set; }}
}} }}
"); ";
RunDesignTimeTest(references);
var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void _ViewImports_DesignTime() public void _ViewImports_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -130,8 +134,8 @@ public class MyApp
public string MyProperty { get; set; } public string MyProperty { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -153,8 +157,8 @@ public class MyApp
public string MyProperty { get; set; } public string MyProperty { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -176,15 +180,15 @@ public class MyApp
public string MyProperty { get; set; } public string MyProperty { get; set; }
} }
"; ";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void Model_DesignTime() public void Model_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim); var compilation = BaseCompilation;
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
@ -196,28 +200,30 @@ public class ThisShouldBeGenerated
}"; }";
var references = CreateCompilationReferences(CurrentMvcShim, appCode); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(references); RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void ModelExpressionTagHelper_DesignTime() public void ModelExpressionTagHelper_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class InputTestTagHelper : {typeof(TagHelper).FullName} public class InputTestTagHelper : {typeof(TagHelper).FullName}
{{ {{
public ModelExpression For {{ get; set; }} public ModelExpression For {{ get; set; }}
}} }}
"); ";
RunDesignTimeTest(references); var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
[Fact] [Fact]
public void ViewComponentTagHelper_DesignTime() public void ViewComponentTagHelper_DesignTime()
{ {
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@" var appCode = $@"
public class TestViewComponent public class TestViewComponent
{{ {{
public string Invoke(string firstName) public string Invoke(string firstName)
@ -231,16 +237,21 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
{{ {{
public string Bar {{ get; set; }} public string Bar {{ get; set; }}
}} }}
"); ";
RunDesignTimeTest(references);
var compilation = BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(appCode));
RunDesignTimeTest(compilation);
} }
private void RunDesignTimeTest( private void RunDesignTimeTest(
IEnumerable<MetadataReference> compilationReferences, CSharpCompilation baseCompilation,
IEnumerable<string> expectedErrors = null) IEnumerable<string> expectedErrors = null)
{ {
Assert.Empty(baseCompilation.GetDiagnostics());
// Arrange // Arrange
var engine = CreateDesignTimeEngine(compilationReferences); var engine = CreateDesignTimeEngine(baseCompilation);
var document = CreateCodeDocument(); var document = CreateCodeDocument();
// Act // Act
@ -250,25 +261,21 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode()); AssertDocumentNodeMatchesBaseline(document.GetDocumentIntermediateNode());
AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument()); AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument());
AssertSourceMappingsMatchBaseline(document); AssertSourceMappingsMatchBaseline(document);
AssertDocumentCompiles(document, compilationReferences, expectedErrors); AssertDocumentCompiles(document, baseCompilation, expectedErrors);
}
private static IEnumerable<MetadataReference> CreateCompilationReferences(string mvcShimName, string appCode = null)
{
var shimReferences = CreateMvcShimReferences(mvcShimName);
return CreateAppCodeReferences(appCode, shimReferences);
} }
private void AssertDocumentCompiles( private void AssertDocumentCompiles(
RazorCodeDocument document, RazorCodeDocument document,
IEnumerable<MetadataReference> compilationReferences, CSharpCompilation baseCompilation,
IEnumerable<string> expectedErrors = null) IEnumerable<string> expectedErrors = null)
{ {
var cSharp = document.GetCSharpDocument().GeneratedCode; var cSharp = document.GetCSharpDocument().GeneratedCode;
var syntaxTree = CSharpSyntaxTree.ParseText(cSharp); var syntaxTree = CSharpSyntaxTree.ParseText(cSharp);
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); 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(); var diagnostics = compilation.GetDiagnostics();
@ -284,8 +291,10 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
} }
} }
protected RazorEngine CreateDesignTimeEngine(IEnumerable<MetadataReference> references) protected RazorEngine CreateDesignTimeEngine(CSharpCompilation compilation)
{ {
var references = compilation.References.Concat(new[] { compilation.ToMetadataReference() });
return RazorEngine.CreateDesignTime(b => return RazorEngine.CreateDesignTime(b =>
{ {
RazorExtensions.Register(b); RazorExtensions.Register(b);
@ -293,8 +302,6 @@ public class AllTagHelper : {typeof(TagHelper).FullName}
b.Features.Add(GetMetadataReferenceFeature(references)); b.Features.Add(GetMetadataReferenceFeature(references));
b.Features.Add(new CompilationTagHelperFeature()); b.Features.Add(new CompilationTagHelperFeature());
b.Features.Add(new DefaultTagHelperDescriptorProvider() { DesignTime = true });
b.Features.Add(new ViewComponentTagHelperDescriptorProvider() { ForceEnabled = true });
}); });
} }

View File

@ -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;
}
}
}
}

View File

@ -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. // This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory.
public class ViewComponentTagHelperDescriptorProviderTest public class ViewComponentTagHelperDescriptorProviderTest
{ {
private static readonly Assembly _assembly = typeof(ViewComponentTagHelperDescriptorProviderTest).GetTypeInfo().Assembly;
[Fact] [Fact]
public void DescriptorProvider_FindsVCTH() 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(); var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(testCompilation); context.SetCompilation(compilation);
var provider = new ViewComponentTagHelperDescriptorProvider() var provider = new ViewComponentTagHelperDescriptorProvider()
{ {
Engine = RazorEngine.CreateEmpty(b => { }), Engine = RazorEngine.CreateEmpty(b => { }),
ForceEnabled = true,
}; };
var expectedDescriptor = TagHelperDescriptorBuilder.Create( var expectedDescriptor = TagHelperDescriptorBuilder.Create(
@ -68,8 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
provider.Execute(context); provider.Execute(context);
// Assert // Assert
var descriptor = context.Results.FirstOrDefault(d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor)); Assert.Single(context.Results, d => TagHelperDescriptorComparer.CaseSensitive.Equals(d, expectedDescriptor));
Assert.NotNull(descriptor);
} }
} }
} }

View File

@ -32,7 +32,7 @@ namespace Microsoft.CodeAnalysis
public static string AssemblyName => "TestAssembly"; public static string AssemblyName => "TestAssembly";
public static Compilation Create(Assembly assembly, SyntaxTree syntaxTree = null) public static CSharpCompilation Create(Assembly assembly, SyntaxTree syntaxTree = null)
{ {
IEnumerable<SyntaxTree> syntaxTrees = null; IEnumerable<SyntaxTree> syntaxTrees = null;
@ -47,7 +47,7 @@ namespace Microsoft.CodeAnalysis
_referenceCache.TryAdd(assembly, metadataReferences); _referenceCache.TryAdd(assembly, metadataReferences);
} }
var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, metadataReferences); var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, metadataReferences, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
EnsureValidCompilation(compilation); EnsureValidCompilation(compilation);

View File

@ -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
{
}
}

View File

@ -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
{
}
}

View File

@ -392,7 +392,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
TagHelperDescriptor expectedDescriptor) TagHelperDescriptor expectedDescriptor)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -443,7 +443,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
TagHelperDescriptor expectedDescriptor) TagHelperDescriptor expectedDescriptor)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -499,7 +499,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
TagHelperDescriptor expectedDescriptor) TagHelperDescriptor expectedDescriptor)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -559,7 +559,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
TagHelperDescriptor expectedDescriptor) TagHelperDescriptor expectedDescriptor)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -734,7 +734,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
TagHelperDescriptor expectedDescriptor) TagHelperDescriptor expectedDescriptor)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime); var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime, designTime);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -931,7 +931,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
TagHelperDescriptor expectedDescriptor) TagHelperDescriptor expectedDescriptor)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -968,7 +968,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
string expectedAttributeName) string expectedAttributeName)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -1005,7 +1005,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(validProperty2.Name) .PropertyName(validProperty2.Name)
.TypeName(validProperty2.PropertyType.FullName), .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(OverriddenAttributeTagHelper).FullName);
// Act // Act
@ -1039,7 +1039,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(validProperty2.Name) .PropertyName(validProperty2.Name)
.TypeName(validProperty2.PropertyType.FullName), .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedOverriddenAttributeTagHelper).FullName);
// Act // Act
@ -1073,7 +1073,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(validProperty2.Name) .PropertyName(validProperty2.Name)
.TypeName(validProperty2.PropertyType.FullName), .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedNotOverriddenAttributeTagHelper).FullName);
// Act // Act
@ -1098,7 +1098,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(nameof(InheritedSingleAttributeTagHelper.IntAttribute)) .PropertyName(nameof(InheritedSingleAttributeTagHelper.IntAttribute))
.TypeName(typeof(int).FullName) .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedSingleAttributeTagHelper).FullName);
// Act // Act
@ -1124,7 +1124,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(intProperty.Name) .PropertyName(intProperty.Name)
.TypeName(intProperty.PropertyType.FullName) .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(SingleAttributeTagHelper).FullName);
// Act // Act
@ -1151,7 +1151,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(validProperty.Name) .PropertyName(validProperty.Name)
.TypeName(validProperty.PropertyType.FullName) .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(MissingAccessorTagHelper).FullName);
// Act // Act
@ -1178,7 +1178,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(validProperty.Name) .PropertyName(validProperty.Name)
.TypeName(validProperty.PropertyType.FullName) .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(NonPublicAccessorTagHelper).FullName);
// Act // Act
@ -1203,7 +1203,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(nameof(NotBoundAttributeTagHelper.BoundProperty)) .PropertyName(nameof(NotBoundAttributeTagHelper.BoundProperty))
.TypeName(typeof(object).FullName) .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(NotBoundAttributeTagHelper).FullName);
// Act // Act
@ -1234,7 +1234,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
builder => builder.RequireTagName("p"), builder => builder.RequireTagName("p"),
builder => builder.RequireTagName("div"), 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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(MultiTagTagHelper).FullName);
// Act // Act
@ -1260,7 +1260,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
.PropertyName(validProp.Name) .PropertyName(validProp.Name)
.TypeName(validProp.PropertyType.FullName), .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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(InheritedMultiTagTagHelper).FullName);
// Act // Act
@ -1284,7 +1284,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
builder => builder.RequireTagName("div"), 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); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(DuplicateTagNameTagHelper).FullName);
// Act // Act
@ -1303,7 +1303,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
"data-condition", "data-condition",
typeof(OverrideNameTagHelper).FullName, typeof(OverrideNameTagHelper).FullName,
AssemblyName); AssemblyName);
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(typeof(OverrideNameTagHelper).FullName); var typeSymbol = Compilation.GetTypeByMetadataName(typeof(OverrideNameTagHelper).FullName);
// Act // Act
@ -1345,7 +1345,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
var compilation = TestCompilation.Create(_assembly, syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var attribute = tagHelperType.GetAttributes().Single(); var attribute = tagHelperType.GetAttributes().Single();
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false);
// Act // Act
var descriptor = factory.CreateDescriptor(tagHelperType); var descriptor = factory.CreateDescriptor(tagHelperType);
@ -1366,21 +1366,21 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{ {
// name, expectedNames // name, expectedNames
return new TheoryData<string, IEnumerable<string>> return new TheoryData<string, IEnumerable<string>>
{ {
{ "p", new[] { "p" } }, { "p", new[] { "p" } },
{ " p", new[] { "p" } }, { " p", new[] { "p" } },
{ "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, 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<BoundAttributeDescriptor> expectedAttributeDescriptors) IEnumerable<BoundAttributeDescriptor> expectedAttributeDescriptors)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(type.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(type.FullName);
// Act // Act
@ -1527,7 +1527,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(_assembly, syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false);
// Act // Act
var descriptor = factory.CreateDescriptor(tagHelperType); var descriptor = factory.CreateDescriptor(tagHelperType);
@ -1567,7 +1567,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(_assembly, syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false);
// Act // Act
var descriptor = factory.CreateDescriptor(tagHelperType); var descriptor = factory.CreateDescriptor(tagHelperType);
@ -1610,7 +1610,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(_assembly, syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false);
// Act // Act
var descriptor = factory.CreateDescriptor(tagHelperType); var descriptor = factory.CreateDescriptor(tagHelperType);
@ -1656,7 +1656,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(_assembly, syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false);
// Act // Act
var descriptor = factory.CreateDescriptor(tagHelperType); var descriptor = factory.CreateDescriptor(tagHelperType);
@ -1698,7 +1698,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(_assembly, syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false);
// Act // Act
var descriptor = factory.CreateDescriptor(tagHelperType); var descriptor = factory.CreateDescriptor(tagHelperType);
@ -1739,7 +1739,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(_assembly, syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, includeDocumentation: false, excludeHidden: false);
// Act // Act
var descriptor = factory.CreateDescriptor(tagHelperType); var descriptor = factory.CreateDescriptor(tagHelperType);
@ -1754,7 +1754,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{ {
// Arrange // Arrange
var objectAssemblyName = typeof(Enumerable).GetTypeInfo().Assembly.GetName().Name; 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 typeSymbol = Compilation.GetTypeByMetadataName(typeof(Enumerable).FullName);
var expectedDescriptor = var expectedDescriptor =
CreateTagHelperDescriptor("enumerable", "System.Linq.Enumerable", typeSymbol.ContainingAssembly.Identity.Name); CreateTagHelperDescriptor("enumerable", "System.Linq.Enumerable", typeSymbol.ContainingAssembly.Identity.Name);
@ -2010,7 +2010,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
IEnumerable<RazorDiagnostic> expectedDiagnostics) IEnumerable<RazorDiagnostic> expectedDiagnostics)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -2069,12 +2069,12 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
[Theory] [Theory]
[MemberData(nameof(TagOutputHintData))] [MemberData(nameof(TagOutputHintData))]
public void CreateDescriptor_CreatesDesignTimeDescriptorsWithOutputElementHint( public void CreateDescriptor_CreatesDescriptorsWithOutputElementHint(
Type tagHelperType, Type tagHelperType,
TagHelperDescriptor expectedDescriptor) TagHelperDescriptor expectedDescriptor)
{ {
// Arrange // Arrange
var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: true); var factory = new DefaultTagHelperDescriptorFactory(Compilation, includeDocumentation: false, excludeHidden: false);
var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName); var typeSymbol = Compilation.GetTypeByMetadataName(tagHelperType.FullName);
// Act // Act
@ -2102,7 +2102,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{ {
}"); }");
var compilation = TestCompilation.Create(_assembly, syntaxTree); 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 typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper");
var expectedDocumentation = var expectedDocumentation =
@"<member name=""T:DocumentedTagHelper""> @"<member name=""T:DocumentedTagHelper"">
@ -2151,7 +2151,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public List<bool> RemarksAndSummaryProperty { get; set; } public List<bool> RemarksAndSummaryProperty { get; set; }
}"); }");
var compilation = TestCompilation.Create(_assembly, syntaxTree); 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 typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper");
var expectedDocumentations = new[] var expectedDocumentations = new[]
{ {

View File

@ -18,11 +18,10 @@ namespace Microsoft.CodeAnalysis.Razor
// Arrange // Arrange
var editorBrowsableTypeName = "Microsoft.CodeAnalysis.Razor.Workspaces.Test.EditorBrowsableTagHelper"; var editorBrowsableTypeName = "Microsoft.CodeAnalysis.Razor.Workspaces.Test.EditorBrowsableTagHelper";
var compilation = TestCompilation.Create(_assembly); var compilation = TestCompilation.Create(_assembly);
var descriptorProvider = new DefaultTagHelperDescriptorProvider() var descriptorProvider = new DefaultTagHelperDescriptorProvider();
{
DesignTime = true,
};
var context = TagHelperDescriptorProviderContext.Create(); var context = TagHelperDescriptorProviderContext.Create();
context.ExcludeHidden = true;
// Act // Act
descriptorProvider.Execute(context); descriptorProvider.Execute(context);

View File

@ -187,6 +187,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
// Assert // Assert
Assert.Single(engine.Engine.Features.OfType<MyCoolNewFeature>()); Assert.Single(engine.Engine.Features.OfType<MyCoolNewFeature>());
Assert.Single(engine.Engine.Features.OfType<DefaultTagHelperDescriptorProvider>());
Assert.Single(engine.Engine.Features.OfType<MvcLatest.ViewComponentTagHelperDescriptorProvider>()); Assert.Single(engine.Engine.Features.OfType<MvcLatest.ViewComponentTagHelperDescriptorProvider>());
Assert.Single(engine.Engine.Features.OfType<MvcLatest.MvcViewDocumentClassifierPass>()); Assert.Single(engine.Engine.Features.OfType<MvcLatest.MvcViewDocumentClassifierPass>());
Assert.Single(engine.Engine.Features.OfType<MvcLatest.ViewComponentTagHelperPass>()); Assert.Single(engine.Engine.Features.OfType<MvcLatest.ViewComponentTagHelperPass>());