private async Task <(bool result, IList <ActualApiResponseMetadata> responseMetadatas, TestSource testSource)> TryGetActualResponseMetadata(string typeName, string methodName) { var testSource = MvcTestSource.Read(GetType().Name, "TryGetActualResponseMetadataTests"); var project = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source }); var compilation = await GetCompilation("TryGetActualResponseMetadataTests"); var type = compilation.GetTypeByMetadataName(typeName); var method = (IMethodSymbol)type.GetMembers(methodName).First(); Assert.True(ApiControllerSymbolCache.TryCreate(compilation, out var symbolCache)); var syntaxTree = method.DeclaringSyntaxReferences[0].SyntaxTree; var methodSyntax = (MethodDeclarationSyntax)syntaxTree.GetRoot().FindNode(method.Locations[0].SourceSpan); var semanticModel = compilation.GetSemanticModel(syntaxTree); var result = ActualApiResponseMetadataFactory.TryGetActualResponseMetadata(symbolCache, semanticModel, methodSyntax, CancellationToken.None, out var responseMetadatas); return(result, responseMetadatas, testSource); }
private async Task RunTest(DiagnosticDescriptor descriptor, object[] args, [CallerMemberName] string testMethod = "") { // Arrange var testSource = MvcTestSource.Read(GetType().Name, testMethod); var expectedLocation = testSource.DefaultMarkerLocation; // Act var result = await Executor.GetDiagnosticsAsync(testSource.Source); // Assert Assert.Collection( result, diagnostic => { Assert.Equal(descriptor.Id, diagnostic.Id); Assert.Same(descriptor, diagnostic.Descriptor); AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); Assert.Equal(string.Format(descriptor.MessageFormat.ToString(), args), diagnostic.GetMessage()); }); }
private async Task RunTest([CallerMemberName] string testMethod = "") { // Arrange var descriptor = ApiDiagnosticDescriptors.API1003_ApiActionsDoNotRequireExplicitModelValidationCheck; var testSource = MvcTestSource.Read(GetType().Name, testMethod); var expectedLocation = testSource.DefaultMarkerLocation; // Act var result = await AnalyzerRunner.GetDiagnosticsAsync(testSource.Source); // Assert Assert.Collection( result, diagnostic => { Assert.Equal(descriptor.Id, diagnostic.Id); Assert.Same(descriptor, diagnostic.Descriptor); AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); }); }
private async Task RunTest(string typeName, string parameterName, [CallerMemberName] string testMethod = "") { // Arrange var descriptor = DiagnosticDescriptors.MVC1004_ParameterNameCollidesWithTopLevelProperty; var testSource = MvcTestSource.Read(GetType().Name, testMethod); var expectedLocation = testSource.DefaultMarkerLocation; // Act var result = await Runner.GetDiagnosticsAsync(testSource.Source); // Assert Assert.Collection( result, diagnostic => { Assert.Equal(descriptor.Id, diagnostic.Id); Assert.Same(descriptor, diagnostic.Descriptor); AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); Assert.Equal(string.Format(descriptor.MessageFormat.ToString(), typeName, parameterName), diagnostic.GetMessage()); }); }
public async Task IsProblematicParameter_ReturnsFalse_ForSimpleTypes() { var testName = nameof(IsProblematicParameter_ReturnsFalse_ForSimpleTypes); var testSource = MvcTestSource.Read(GetType().Name, testName); var project = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source }); var compilation = await project.GetCompilationAsync(); var modelType = compilation.GetTypeByMetadataName($"Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzerTestFiles.{testName}"); var method = (IMethodSymbol)modelType.GetMembers("ActionMethod").First(); Assert.True(TopLevelParameterNameAnalyzer.SymbolCache.TryCreate(compilation, out var symbolCache)); Assert.Collection( method.Parameters, p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)), p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)), p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)), p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)), p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p))); }
private async Task RunTestFor1006(int statusCode, [CallerMemberName] string testMethod = "") { // Arrange var descriptor = DiagnosticDescriptors.MVC1006_ActionDoesNotReturnDocumentedStatusCode; var testSource = MvcTestSource.Read(GetType().Name, testMethod); var expectedLocation = testSource.DefaultMarkerLocation; var executor = new ApiCoventionWith1006DiagnosticEnabledRunner(); // Act var result = await executor.GetDiagnosticsAsync(testSource.Source); // Assert Assert.Collection( result, diagnostic => { Assert.Equal(descriptor.Id, diagnostic.Id); Assert.Same(descriptor, diagnostic.Descriptor); AnalyzerAssert.DiagnosticLocation(expectedLocation, diagnostic.Location); Assert.Equal(string.Format(descriptor.MessageFormat.ToString(), new[] { statusCode.ToString() }), diagnostic.GetMessage()); }); }
private TestSource ReadSource(string fileName) { return(MvcTestSource.Read(nameof(StartupAnalyzerTest), fileName)); }
private static TestSource ReadTestSource([CallerMemberName] string testMethod = "") => MvcTestSource.Read(nameof(TagHelpersInCodeBlocksAnalyzerTest), testMethod);
private static TestSource ReadTestSource([CallerMemberName] string testMethod = "") => MvcTestSource.Read(nameof(AvoidHtmlPartialAnalyzerTest), testMethod);