/// <summary> /// Called to test a C# code fix when applied on the input source as a string. /// </summary> /// <param name="oldSources">An array of sources in the form of strings before the code fix was applied to them.</param> /// <param name="verifyFixedProjectAsync">A validation function to verify the results of the code fix.</param> /// <param name="oldFileNames">An array of file names in the project before the code fix was applied.</param> /// <param name="codeFixIndex">Index determining which code fix to apply if there are multiple.</param> /// <param name="allowNewCompilerDiagnostics">A value indicating whether or not the test will fail if the code fix introduces other warnings after being applied.</param> /// <param name="numberOfIncrementalIterations">The number of iterations the incremental fixer will be called. /// If this value is less than 0, the negated value is treated as an upper limit as opposed to an exact /// value.</param> /// <param name="numberOfFixAllIterations">The number of iterations the Fix All fixer will be called. If this /// value is less than 0, the negated value is treated as an upper limit as opposed to an exact value.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> protected async Task VerifyCSharpFixAsync(string[] oldSources, VerifyCodeFixAsync verifyFixedProjectAsync, string[] oldFileNames = null, int?codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int numberOfIncrementalIterations = DefaultNumberOfIncrementalIterations, int numberOfFixAllIterations = 1, CancellationToken cancellationToken = default(CancellationToken)) { #pragma warning disable SA1101 // Prefix local calls with this Func <Project, CancellationToken, Task> verifyAsync = (project, ct) => verifyFixedProjectAsync(project, false, ct); #pragma warning restore SA1101 // Prefix local calls with this var t1 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, oldFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfIncrementalIterations, FixEachAnalyzerDiagnosticAsync, verifyAsync, cancellationToken).ConfigureAwait(false); var fixAllProvider = this.GetCSharpCodeFixProvider().GetFixAllProvider(); Assert.NotEqual(WellKnownFixAllProviders.BatchFixer, fixAllProvider); if (fixAllProvider == null) { await t1; } else { if (Debugger.IsAttached) { await t1; } #pragma warning disable SA1101 // Prefix local calls with this Func <Project, CancellationToken, Task> verifyFixAllAsync = (project, ct) => verifyFixedProjectAsync(project, true, ct); #pragma warning restore SA1101 // Prefix local calls with this var t2 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, oldFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInDocumentAsync, verifyFixAllAsync, cancellationToken).ConfigureAwait(false); if (Debugger.IsAttached) { await t2; } var t3 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, oldFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInProjectAsync, verifyFixAllAsync, cancellationToken).ConfigureAwait(false); if (Debugger.IsAttached) { await t3; } var t4 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, oldFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInSolutionAsync, verifyFixAllAsync, cancellationToken).ConfigureAwait(false); if (Debugger.IsAttached) { await t4; } if (!Debugger.IsAttached) { // Allow the operations to run in parallel await t1; await t2; await t3; await t4; } } }
private async Task VerifyCSharpUnshippedFileFixAsync(string source, string fixedUnshippedText, int numberOfFixAllIterations = 1, CancellationToken cancellationToken = default) { VerifyCodeFixAsync verifyAsync = async(Project project, bool fixAll, CancellationToken ct) => { var unshippedFile = project.AdditionalDocuments.Single(document => document.Name == DeclarePublicAPIAnalyzer.UnshippedFileName); Assert.Equal(fixedUnshippedText, (await unshippedFile.GetTextAsync(ct).ConfigureAwait(false)).ToString()); }; await this.VerifyCSharpFixAsync(source, verifyAsync, numberOfFixAllIterations : numberOfFixAllIterations, cancellationToken : CancellationToken.None).ConfigureAwait(false); }
/// <summary> /// Called to test a C# code fix when applied on the input source as a string. /// </summary> /// <param name="oldSource">A class in the form of a string before the code fix was applied to it.</param> /// <param name="verifyFixedProjectAsync">A validation function to verify the results of the code fix.</param> /// <param name="oldFileName">The name of the file in the project before the code fix was applied.</param> /// <param name="codeFixIndex">Index determining which code fix to apply if there are multiple.</param> /// <param name="allowNewCompilerDiagnostics">A value indicating whether or not the test will fail if the code fix introduces other warnings after being applied.</param> /// <param name="numberOfIncrementalIterations">The number of iterations the incremental fixer will be called. /// If this value is less than 0, the negated value is treated as an upper limit as opposed to an exact /// value.</param> /// <param name="numberOfFixAllIterations">The number of iterations the Fix All fixer will be called. If this /// value is less than 0, the negated value is treated as an upper limit as opposed to an exact value.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> protected Task VerifyCSharpFixAsync(string oldSource, VerifyCodeFixAsync verifyFixedProjectAsync, string oldFileName = null, int?codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int numberOfIncrementalIterations = DefaultNumberOfIncrementalIterations, int numberOfFixAllIterations = 1, CancellationToken cancellationToken = default(CancellationToken)) { var oldFileNames = oldFileName == null ? null : new[] { oldFileName }; return(this.VerifyCSharpFixAsync(new[] { oldSource }, verifyFixedProjectAsync, oldFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfIncrementalIterations, numberOfFixAllIterations, cancellationToken)); }