private static void VerifyFix(Solution sln, IReadOnlyList <ImmutableArray <Diagnostic> > diagnostics, DiagnosticAnalyzer analyzer, CodeFixProvider fix, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { var fixableDiagnostics = diagnostics.SelectMany(x => x) .Where(x => fix.FixableDiagnosticIds.Contains(x.Id)) .ToArray(); if (fixableDiagnostics.Length == 0) { var message = $"Code analyzed with {analyzer} did not generate any diagnostics fixable by {fix}.{Environment.NewLine}" + $"The analyzed code contained the following diagnostics: {{{string.Join(", ", diagnostics.SelectMany(x => x).Select(d => d.Id))}}}{Environment.NewLine}" + $"The code fix supports the following diagnostics: {{{string.Join(", ", fix.FixableDiagnosticIds)}}}"; throw new AssertException(message); } if (fixableDiagnostics.Length > 1) { var message = $"Code analyzed with {analyzer} generated more than one diagnostic fixable by {fix}.{Environment.NewLine}" + $"The analyzed code contained the following diagnostics: {{{string.Join(", ", diagnostics.SelectMany(x => x).Select(d => d.Id))}}}{Environment.NewLine}" + $"The code fix supports the following diagnostics: {{{string.Join(", ", fix.FixableDiagnosticIds)}}}{Environment.NewLine}" + $"Maybe you meant to call AnalyzerAssert.FixAll?"; throw new AssertException(message); } var diagnostic = fixableDiagnostics.Single(); var fixedSolution = Fix.Apply(sln, fix, diagnostic, fixTitle); if (ReferenceEquals(sln, fixedSolution)) { throw new AssertException($"{fix} did not change any document."); } var fixedSource = CodeReader.GetStringFromDocument( fixedSolution.GetDocument(sln.GetDocument(diagnostic.Location.SourceTree).Id), Formatter.Annotation, CancellationToken.None); CodeAssert.AreEqual(fixedCode, fixedSource); if (allowCompilationErrors == AllowCompilationErrors.No) { VerifyNoCompilerErrorsAsync(fix, fixedSolution).GetAwaiter().GetResult(); } }