|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CodeActions;
- using Microsoft.CodeAnalysis.CodeFixes;
- using Microsoft.CodeAnalysis.Diagnostics;
- using Microsoft.CodeAnalysis.Formatting;
- ;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using Xunit;
-
- namespace TestHelper
- {
- >
- .
- s
- >
- public abstract partial class CodeFixVerifier : DiagnosticVerifier
- {
- >
- s
- >
- >
- protected virtual CodeFixProvider GetCSharpCodeFixProvider()
- {
- return null;
- }
-
- >
- s
- >
- >
- protected virtual CodeFixProvider GetBasicCodeFixProvider()
- {
- return null;
- }
-
- >
- e
- >
- >
- >
- >
- >
- protected void VerifyCSharpFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false)
- {
- VerifyFix(LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics);
- }
-
- >
- e
- >
- >
- >
- >
- >
- protected void VerifyBasicFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false)
- {
- VerifyFix(LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), GetBasicCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics);
- }
-
- >
- .
- .
- .
- .
- >
- >
- >
- >
- >
- >
- >
- >
- private void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics)
- {
- var document = CreateDocument(oldSource, language);
- var analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
- var compilerDiagnostics = GetCompilerDiagnostics(document);
- var attempts = analyzerDiagnostics.Length;
-
- for (int i = 0; i < attempts; ++i)
- {
- var actions = new List<CodeAction>();
- var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None);
- codeFixProvider.RegisterCodeFixesAsync(context).Wait();
-
- if (!actions.Any())
- {
- break;
- }
-
- if (codeFixIndex != null)
- {
- document = ApplyFix(document, actions.ElementAt((int)codeFixIndex));
- break;
- }
-
- document = ApplyFix(document, actions.ElementAt(0));
- analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
-
- var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));
-
- s
- if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any())
- {
- t
- document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace));
- newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));
-
- Assert.True(false,
- string.Format("Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n",
- string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())),
- document.GetSyntaxRootAsync().Result.ToFullString()));
- }
-
- x
- if (!analyzerDiagnostics.Any())
- {
- break;
- }
- }
-
- e
- var actual = GetStringFromDocument(document);
- Assert.Equal(newSource, actual);
- }
- }
- }
|