Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple analyzers to the test verifiers #802

Merged
merged 5 commits into from
May 17, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ public abstract partial class DiagnosticVerifier
/// <param name="sources">Classes in the form of strings.</param>
/// <param name="language">The language the source classes are in. Values may be taken from the
/// <see cref="LanguageNames"/> class.</param>
/// <param name="analyzer">The analyzer to be run on the sources.</param>
/// <param name="analyzers">The analyzers to be run on the sources.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
/// <returns>A collection of <see cref="Diagnostic"/>s that surfaced in the source code, sorted by
/// <see cref="Diagnostic.Location"/>.</returns>
private static Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsAsync(string[] sources, string language, DiagnosticAnalyzer analyzer, CancellationToken cancellationToken)
private static Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsAsync(string[] sources, string language, DiagnosticAnalyzer[] analyzers, CancellationToken cancellationToken)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ Lets use ImmutableArray<DiagnosticAnalyzer> analyzers instead of DiagnosticAnalyzer[].

{
return GetSortedDiagnosticsFromDocumentsAsync(analyzer, GetDocuments(sources, language), cancellationToken);
return GetSortedDiagnosticsFromDocumentsAsync(analyzers, GetDocuments(sources, language), cancellationToken);
}

/// <summary>
/// Given an analyzer and a collection of documents to apply it to, run the analyzer and gather an array of
/// diagnostics found. The returned diagnostics are then ordered by location in the source documents.
/// </summary>
/// <param name="analyzer">The analyzer to run on the documents.</param>
/// <param name="analyzers">The analyzer to run on the documents.</param>
/// <param name="documents">The <see cref="Document"/>s that the analyzer will be run on.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
/// <returns>A collection of <see cref="Diagnostic"/>s that surfaced in the source code, sorted by
/// <see cref="Diagnostic.Location"/>.</returns>
protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFromDocumentsAsync(DiagnosticAnalyzer analyzer, Document[] documents, CancellationToken cancellationToken)
protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFromDocumentsAsync(DiagnosticAnalyzer[] analyzers, Document[] documents, CancellationToken cancellationToken)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 ImmutableArray<DiagnosticAnalyzer>

{
var projects = new HashSet<Project>();
foreach (var document in documents)
Expand All @@ -65,10 +65,13 @@ protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFrom
}

var supportedDiagnosticsSpecificOptions = new Dictionary<string, ReportDiagnostic>();
foreach (var diagnostic in analyzer.SupportedDiagnostics)
foreach (var analyzer in analyzers)
{
// make sure the analyzers we are testing are enabled
supportedDiagnosticsSpecificOptions[diagnostic.Id] = ReportDiagnostic.Default;
foreach (var diagnostic in analyzer.SupportedDiagnostics)
{
// make sure the analyzers we are testing are enabled
supportedDiagnosticsSpecificOptions[diagnostic.Id] = ReportDiagnostic.Default;
}
}

var diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
Expand All @@ -80,7 +83,7 @@ protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFrom
var processedProject = project.WithCompilationOptions(modifiedCompilationOptions);

var compilation = await processedProject.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
var compilationWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzer), null, cancellationToken);
var compilationWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzers), null, cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 You'll be able to remove this call to ImmutableArray.Create. 😄

var compilerDiagnostics = compilation.GetDiagnostics(cancellationToken);
var compilerErrors = compilerDiagnostics.Where(i => i.Severity == DiagnosticSeverity.Error);
var diags = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected virtual CodeFixProvider GetCSharpCodeFixProvider()
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
protected Task VerifyCSharpFixAsync(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false, CancellationToken cancellationToken = default(CancellationToken))
{
return this.VerifyFixAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzer(), this.GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics, cancellationToken);
return this.VerifyFixAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers(), this.GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics, cancellationToken);
}

/// <summary>
Expand All @@ -52,7 +52,7 @@ protected virtual CodeFixProvider GetCSharpCodeFixProvider()
/// </summary>
/// <param name="language">The language the source classes are in. Values may be taken from the
/// <see cref="LanguageNames"/> class.</param>
/// <param name="analyzer">The analyzer to be applied to the source code.</param>
/// <param name="analyzers">The analyzer to be applied to the source code.</param>
/// <param name="codeFixProvider">The code fix to be applied to the code wherever the relevant
/// <see cref="Diagnostic"/> is found.</param>
/// <param name="oldSource">A class in the form of a string before the code fix was applied to it.</param>
Expand All @@ -62,10 +62,10 @@ protected virtual CodeFixProvider GetCSharpCodeFixProvider()
/// fix introduces other warnings after being applied.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
private async Task VerifyFixAsync(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics, CancellationToken cancellationToken)
private async Task VerifyFixAsync(string language, DiagnosticAnalyzer[] analyzers, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics, CancellationToken cancellationToken)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 ImmutableArray<DiagnosticAnalyzer>

{
var document = CreateDocument(oldSource, language);
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzer, new[] { document }, cancellationToken).ConfigureAwait(false);
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, new[] { document }, cancellationToken).ConfigureAwait(false);
var compilerDiagnostics = await GetCompilerDiagnosticsAsync(document, cancellationToken).ConfigureAwait(false);
var attempts = analyzerDiagnostics.Length;

Expand All @@ -87,7 +87,7 @@ private async Task VerifyFixAsync(string language, DiagnosticAnalyzer analyzer,
}

document = await ApplyFixAsync(document, actions.ElementAt(0), cancellationToken).ConfigureAwait(false);
analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzer, new[] { document }, cancellationToken).ConfigureAwait(false);
analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, new[] { document }, cancellationToken).ConfigureAwait(false);

// check if there are analyzer diagnostics left after the code fix
if (!analyzerDiagnostics.Any())
Expand Down
Loading