public async Task DetectFeaturesAsync_FindsNoFeatures() { // Arrange var source = @" using Microsoft.AspNetCore.Builder; namespace Microsoft.AspNetCore.Analyzers.TestFiles.CompilationFeatureDetectorTest { public class StartupWithNoFeatures { public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapFallbackToFile(""index.html""); }); } } }"; var compilation = TestCompilation.Create(source); var symbols = new StartupSymbols(compilation); var type = (INamedTypeSymbol)compilation.GetSymbolsWithName("StartupWithNoFeatures").Single(); Assert.True(StartupFacts.IsStartupClass(symbols, type)); // Act var features = await CompilationFeatureDetector.DetectFeaturesAsync(compilation); // Assert Assert.Empty(features); }
public void IsStartupClass_RejectsNotStartupClass(string source) { // Arrange var compilation = TestCompilation.Create(TestSources[source]); var symbols = new StartupSymbols(compilation); var type = (INamedTypeSymbol)compilation.GetSymbolsWithName(source).Single(); // Act var result = StartupFacts.IsStartupClass(symbols, type); // Assert Assert.False(result); }
public void IsConfigure_RejectsNonConfigureMethod(string source, string methodName) { // Arrange var compilation = TestCompilation.Create(TestSources[source]); var symbols = new StartupSymbols(compilation); var type = (INamedTypeSymbol)compilation.GetSymbolsWithName(source).Single(); var methods = type.GetMembers(methodName).Cast <IMethodSymbol>(); foreach (var method in methods) { // Act var result = StartupFacts.IsConfigure(symbols, method); // Assert Assert.False(result); } }
public async Task DetectFeatureAsync_StartupWithMapHub_FindsSignalR() { var source = @" using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.SignalR; namespace Microsoft.AspNetCore.Analyzers.TestFiles.CompilationFeatureDetectorTest { public class StartupWithMapHub { public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapHub<MyHub>("" / test""); }); } } public class MyHub : Hub { } } "; var compilation = TestCompilation.Create(source); var symbols = new StartupSymbols(compilation); var type = (INamedTypeSymbol)compilation.GetSymbolsWithName("StartupWithMapHub").Single(); Assert.True(StartupFacts.IsStartupClass(symbols, type)); // Act var features = await CompilationFeatureDetector.DetectFeaturesAsync(compilation); // Assert Assert.Collection(features, f => Assert.Equal(WellKnownFeatures.SignalR, f)); }
public void FindConfigureMethods_AtDifferentScopes() { // Arrange var source = @" using Microsoft.AspNetCore.Builder; public class GlobalStartup { public void Configure(IApplicationBuilder app) { } } namespace Another { public class AnotherStartup { public void Configure(IApplicationBuilder app) { } } } namespace ANamespace { public class Startup { public void ConfigureDevelopment(IApplicationBuilder app) { } public class NestedStartup { public void ConfigureTest(IApplicationBuilder app) { } } } } namespace ANamespace.Nested { public class Startup { public void Configure(IApplicationBuilder app) { } public class NestedStartup { public void Configure(IApplicationBuilder app) { } } } }"; var expected = new string[] { "global::ANamespace.Nested.Startup.Configure", "global::ANamespace.Nested.Startup.NestedStartup.Configure", "global::ANamespace.Startup.ConfigureDevelopment", "global::ANamespace.Startup.NestedStartup.ConfigureTest", "global::Another.AnotherStartup.Configure", "global::GlobalStartup.Configure", }; var compilation = TestCompilation.Create(source); var symbols = new StartupSymbols(compilation); // Act var results = ConfigureMethodVisitor.FindConfigureMethods(symbols, compilation.Assembly); // Assert var actual = results .Select(m => m.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) + "." + m.Name) .OrderBy(s => s) .ToArray(); Assert.Equal(expected, actual); }