private ImmutableArray <Diagnostic> RunDiagnostics([NotNull] AnalyzerTestContext context) { DocumentWithSpans documentWithSpans = TestHelpers.GetDocumentAndSpansFromMarkup(context.MarkupCode, context.LanguageName, context.References, context.FileName); ImmutableArray <Diagnostic> diagnostics = GetDiagnosticsForDocument(documentWithSpans.Document) .OrderBy(d => d.Location.SourceSpan) .ToImmutableArray(); ImmutableArray <TextSpan> spans = documentWithSpans.TextSpans.OrderBy(s => s).ToImmutableArray(); diagnostics.Should().HaveCount(spans.Length); for (int index = 0; index < diagnostics.Length; index++) { Diagnostic diagnostic = diagnostics[index]; TextSpan span = spans[index]; diagnostic.Id.Should().Be(DiagnosticId); diagnostic.Location.IsInSource.Should().BeTrue(); diagnostic.Location.SourceSpan.Should().Be(span); } return(diagnostics); }
protected void AssertDiagnostics([NotNull] AnalyzerTestContext context, [NotNull][ItemNotNull] params string[] messages) { Guard.NotNull(context, nameof(context)); Guard.NotNull(messages, nameof(messages)); RunDiagnostics(context, messages); }
private AnalysisResult GetAnalysisResult([NotNull] AnalyzerTestContext context, [NotNull][ItemNotNull] string[] messages) { DocumentWithSpans documentWithSpans = DocumentFactory.GetDocumentWithSpansFromMarkup(context); IList <Diagnostic> diagnostics = GetSortedAnalyzerDiagnostics(context, documentWithSpans); ImmutableArray <TextSpan> spans = documentWithSpans.TextSpans.OrderBy(s => s).ToImmutableArray(); return(new AnalysisResult(diagnostics, spans, messages)); }
public FixProviderTestContext([NotNull] AnalyzerTestContext analyzerTestContext, [NotNull][ItemNotNull] IEnumerable <string> expected, bool reformatExpected = true) { Guard.NotNull(analyzerTestContext, nameof(analyzerTestContext)); Guard.NotNull(expected, nameof(expected)); AnalyzerTestContext = analyzerTestContext; Expected = ImmutableList.CreateRange(expected); ReformatExpected = reformatExpected; }
private AnalysisResult RunDiagnostics([NotNull] AnalyzerTestContext context, [NotNull][ItemNotNull] params string[] messages) { AnalysisResult result = GetAnalysisResult(context, messages); VerifyDiagnosticCount(result, context.DiagnosticsCaptureMode); VerifyDiagnostics(result, context); return(result); }
protected void VerifyNullabilityDiagnostic([NotNull] ParsedSourceCode source) { Guard.NotNull(source, nameof(source)); externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap); AnalyzerTestContext analyzerTextContext = new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp) .WithReferences(source.References) .WithFileName(source.Filename); AssertDiagnostics(analyzerTextContext); }
private IList <Diagnostic> GetSortedAnalyzerDiagnostics([NotNull] AnalyzerTestContext context, [NotNull] DocumentWithSpans documentWithSpans) { IEnumerable <Diagnostic> diagnostics = EnumerateDiagnosticsForDocument(documentWithSpans.Document, context.ValidationMode, context.DiagnosticsCaptureMode, context.Options).Where(d => d.Id == DiagnosticId); if (context.DiagnosticsCaptureMode == DiagnosticsCaptureMode.RequireInSourceTree) { diagnostics = diagnostics.OrderBy(d => d.Location.SourceSpan); } return(diagnostics.ToImmutableArray()); }
private string RemoveMarkupFrom([NotNull] AnalyzerTestContext context, bool reformat) { Document document = GetDocumentWithSpansFromMarkup(context).Document; SyntaxNode syntaxRoot = document.GetSyntaxRootAsync().Result; if (reformat) { SyntaxNode formattedSyntaxRoot = Formatter.Format(syntaxRoot, document.Project.Solution.Workspace); return(formattedSyntaxRoot.ToFullString()); } return(syntaxRoot.ToFullString()); }
private static void VerifyDiagnostics([NotNull] AnalysisResult result, [NotNull] AnalyzerTestContext context) { for (int index = 0; index < result.Diagnostics.Count; index++) { Diagnostic diagnostic = result.Diagnostics[index]; if (context.DiagnosticsCaptureMode == DiagnosticsCaptureMode.RequireInSourceTree) { VerifyDiagnosticLocation(diagnostic, result.Spans[index]); } diagnostic.GetMessage().Should().Be(result.Messages[index]); } }
protected void VerifyNullabilityFix([NotNull] ParsedSourceCode source) { Guard.NotNull(source, nameof(source)); string fixNotNull = source.GetExpectedTextForAttribute(NotNullAttributeName); string fixCanBeNull = source.GetExpectedTextForAttribute(CanBeNullAttributeName); AnalyzerTestContext analyzeTextContext = new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp) .WithReferences(source.References) .WithFileName(source.Filename); var fixTestContext = new FixProviderTestContext(analyzeTextContext, new[] { fixNotNull, fixCanBeNull }, source.ReIndentExpected); AssertDiagnosticsWithCodeFixes(fixTestContext); }
protected void VerifyNullabilityDiagnostic([NotNull] ParsedSourceCode source) { Guard.NotNull(source, nameof(source)); externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap); string text = source.GetText(); AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings); AnalyzerTestContext analyzerContext = new AnalyzerTestContext(text, LanguageNames.CSharp, options) .WithReferences(source.References) .WithFileName(source.Filename); AssertDiagnostics(analyzerContext); }
protected void VerifyDiagnosticWithFix([NotNull] ParsedSourceCode source) { Guard.NotNull(source, nameof(source)); AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings); AnalyzerTestContext analyzeContext = new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp, options) .WithReferences(source.References) .WithFileName(source.Filename); var fixTestContext = new FixProviderTestContext(analyzeContext, new[] { string.Empty }, source.ReIndentExpected); AssertDiagnosticsWithCodeFixes(fixTestContext); }
public DocumentWithSpans GetDocumentWithSpansFromMarkup([NotNull] AnalyzerTestContext context) { Guard.NotNull(context, nameof(context)); var parser = new MarkupParser(context.MarkupCode); CodeWithSpans codeWithSpans = parser.Parse(); ParseOptions parseOptions = GetParseOptions(context.DocumentationMode, context.LanguageName, context.OperationFeature); CompilationOptions compilationOptions = GetCompilationOptions(context.CompilerWarningLevel, context.LanguageName); Document document = new AdhocWorkspace() .AddProject(context.AssemblyName, context.LanguageName) .WithParseOptions(parseOptions) .WithCompilationOptions(compilationOptions) .AddMetadataReferences(context.References) .AddDocument(context.FileName, codeWithSpans.Code); return(new DocumentWithSpans(document, codeWithSpans.Spans)); }
protected virtual void VerifyNullabilityFix([NotNull] ParsedSourceCode source) { Guard.NotNull(source, nameof(source)); externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap); string fixNotNull = source.GetExpectedTextForAttribute(NotNullAttributeName); string fixCanBeNull = source.GetExpectedTextForAttribute(CanBeNullAttributeName); string text = source.GetText(); AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings); AnalyzerTestContext analyzerContext = new AnalyzerTestContext(text, LanguageNames.CSharp, options) .WithReferences(source.References) .WithFileName(source.Filename); var fixContext = new FixProviderTestContext(analyzerContext, new[] { fixNotNull, fixCanBeNull }, source.ReIndentExpected); AssertDiagnosticsWithCodeFixes(fixContext); }
protected void AssertDiagnostics([NotNull] AnalyzerTestContext context) { Guard.NotNull(context, nameof(context)); RunDiagnostics(context); }