/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="code"/> with.</param> /// <param name="fix">The <see cref="CodeFixProvider"/> to apply on the <see cref="Diagnostic"/> reported.</param> /// <param name="expectedDiagnostic">The <see cref="ExpectedDiagnostic"/> with information about the expected <see cref="Diagnostic"/>. If <paramref name="analyzer"/> supports more than one <see cref="DiagnosticDescriptor.Id"/> this must be provided.</param> /// <param name="code">The code to analyze with <paramref name="analyzer"/>. Indicate error position with ↓ (alt + 25).</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider fix, ExpectedDiagnostic expectedDiagnostic, params string[] code) { NoFix( analyzer, fix, DiagnosticsAndSources.Create(expectedDiagnostic, code)); }
/// <summary> /// Verifies that <paramref name="diagnosticsAndSources"/> produces the expected diagnostics. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="diagnosticsAndSources"/> with.</param> /// <param name="diagnosticsAndSources">The code to analyze with <paramref name="analyzer"/>. Indicate error position with ↓ (alt + 25).</param> /// <param name="allowCompilationErrors">Specify if compilation errors are accepted in the fixed code. This can be for example syntax errors. Default value is <see cref="AllowCompilationErrors.No"/>.</param> /// <param name="suppressWarnings">A collection of <see cref="DiagnosticDescriptor.Id"/> to suppress when analyzing the code. Default is <see langword="null" /> meaning <see cref="SuppressedDiagnostics"/> are used.</param> /// <param name="metadataReferences">A collection of <see cref="MetadataReference"/> to use when compiling. Default is <see langword="null" /> meaning <see cref="MetadataReferences"/> are used.</param> /// <param name="compilationOptions">The <see cref="CSharpCompilationOptions"/>.</param> public static void Diagnostics( DiagnosticAnalyzer analyzer, DiagnosticsAndSources diagnosticsAndSources, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No, IEnumerable <string>?suppressWarnings = null, IEnumerable <MetadataReference>?metadataReferences = null, CSharpCompilationOptions?compilationOptions = null) { VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics); var sln = CodeFactory.CreateSolution( diagnosticsAndSources, analyzer, compilationOptions, #pragma warning disable CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() suppressWarnings ?? SuppressedDiagnostics, metadataReferences ?? MetadataReferences); #pragma warning restore CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() var diagnostics = Analyze.GetDiagnostics(analyzer, sln); VerifyDiagnostics(diagnosticsAndSources, diagnostics, sln); if (allowCompilationErrors == AllowCompilationErrors.No) { NoCompilerErrors(sln); } }
/// <summary> /// Verifies that <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics. /// </summary> /// <typeparam name="TAnalyzer">The type of the analyzer.</typeparam> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> public static void Diagnostics <TAnalyzer>(params string[] codeWithErrorsIndicated) where TAnalyzer : DiagnosticAnalyzer, new() { Diagnostics( new TAnalyzer(), DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(new TAnalyzer(), codeWithErrorsIndicated)); }
/// <summary> /// Verifies that <paramref name="code"/> produces the expected diagnostics. /// </summary> /// <typeparam name="TAnalyzer">The type of the analyzer.</typeparam> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void Diagnostics <TAnalyzer>(ExpectedDiagnostic expectedDiagnostic, params string[] code) where TAnalyzer : DiagnosticAnalyzer, new() { Diagnostics( new TAnalyzer(), DiagnosticsAndSources.Create(expectedDiagnostic, code)); }
/// <summary> /// Create a <see cref="Solution"/> for <paramref name="diagnosticsAndSources"/>. /// </summary> /// <param name="diagnosticsAndSources">The code to create the solution from with .</param> /// <param name="analyzer">The analyzer.</param> /// <param name="suppressedDiagnostics">The explicitly suppressed diagnostics.</param> /// <param name="metadataReferences">The metadata references.</param> /// <returns>A <see cref="Solution"/>.</returns> internal static Solution CreateSolution(DiagnosticsAndSources diagnosticsAndSources, DiagnosticAnalyzer analyzer, IEnumerable <string> suppressedDiagnostics, IEnumerable <MetadataReference> metadataReferences) { return(CreateSolution( diagnosticsAndSources.Code, DefaultCompilationOptions(analyzer, diagnosticsAndSources.ExpectedDiagnostics, suppressedDiagnostics), metadataReferences)); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="code"/> with.</param> /// <param name="fix">The <see cref="CodeFixProvider"/> to apply on the <see cref="Diagnostic"/> reported.</param> /// <param name="code">The code to analyze with <paramref name="analyzer"/>. Indicate error position with ↓ (alt + 25).</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider fix, params string[] code) { NoFix( analyzer, fix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, code)); }
/// <summary> /// Verifies that /// 1. <paramref name="fix"/> supports fixing diagnostics reported by <paramref name="analyzer"/>. /// 2. <paramref name="diagnosticsAndSources"/> produces diagnostics fixable by <paramref name="fix"/>. /// 3. Applying <paramref name="fix"/> results in <paramref name="after"/>. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="diagnosticsAndSources"/> with.</param> /// <param name="fix">The <see cref="CodeFixProvider"/> to apply on the <see cref="Diagnostic"/> reported.</param> /// <param name="diagnosticsAndSources">The code to analyze with <paramref name="analyzer"/>. Indicate error position with ↓ (alt + 25).</param> /// <param name="after">The expected code produced by applying <paramref name="fix"/>.</param> /// <param name="fixTitle">The expected title of the fix. Must be provided if more than one code action is registered.</param> /// <param name="allowCompilationErrors">Specify if compilation errors are accepted in the fixed code. This can be for example syntax errors. Default value is <see cref="AllowCompilationErrors.No"/>.</param> /// <param name="suppressWarnings">A collection of <see cref="DiagnosticDescriptor.Id"/> to suppress when analyzing the code. Default is <see langword="null" /> meaning <see cref="SuppressedDiagnostics"/> are used.</param> /// <param name="metadataReferences">A collection of <see cref="MetadataReference"/> to use when compiling. Default is <see langword="null" /> meaning <see cref="MetadataReferences"/> are used.</param> /// <param name="compilationOptions">The <see cref="CSharpCompilationOptions"/>.</param> public static void CodeFix( DiagnosticAnalyzer analyzer, CodeFixProvider fix, DiagnosticsAndSources diagnosticsAndSources, IReadOnlyList <string> after, string?fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No, IEnumerable <string>?suppressWarnings = null, IEnumerable <MetadataReference>?metadataReferences = null, CSharpCompilationOptions?compilationOptions = null) { VerifyCodeFixSupportsAnalyzer(analyzer, fix); VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics); var sln = CodeFactory.CreateSolution( diagnosticsAndSources: diagnosticsAndSources, analyzer: analyzer, compilationOptions: compilationOptions, #pragma warning disable CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() suppressWarnings ?? SuppressedDiagnostics, metadataReferences ?? MetadataReferences); #pragma warning restore CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() var diagnostics = Analyze.GetDiagnostics(analyzer, sln); VerifyDiagnostics(diagnosticsAndSources, diagnostics, sln); VerifyFix(sln, diagnostics, analyzer, fix, after, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that <paramref name="diagnosticsAndSources"/> produces the expected diagnostics. /// </summary> /// <param name="analyzer">The analyzer to apply.</param> /// <param name="diagnosticsAndSources">The code to analyze.</param> public static void Diagnostics(DiagnosticAnalyzer analyzer, DiagnosticsAndSources diagnosticsAndSources) { VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics); var sln = CodeFactory.CreateSolution(diagnosticsAndSources, analyzer, SuppressedDiagnostics, MetadataReferences); var diagnostics = Analyze.GetDiagnostics(analyzer, sln); VerifyDiagnostics(diagnosticsAndSources, diagnostics); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The type of the analyzer.</param> /// <param name="codeFix">The type of the code fix.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="compilationOptions">The <see cref="CSharpCompilationOptions"/> to use.</param> /// <param name="metadataReferences">The meta data references to use when compiling.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static Task NoFixAsync(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated, CSharpCompilationOptions compilationOptions, IReadOnlyList <MetadataReference> metadataReferences) { return(NoFixAsync( analyzer, codeFix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), compilationOptions, metadataReferences)); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The type of the analyzer.</param> /// <param name="codeFix">The type of the code fix.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, params string[] codeWithErrorsIndicated) { NoFix( analyzer, codeFix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), SuppressedDiagnostics, MetadataReferences); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The type of the analyzer.</param> /// <param name="codeFix">The type of the code fix.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> code) { NoFix( analyzer, codeFix, DiagnosticsAndSources.Create(expectedDiagnostic, code), SuppressedDiagnostics, MetadataReferences); }
/// <summary> /// Verifies that /// 1. <paramref name="diagnosticsAndSources"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="fix">The <see cref="CodeFixProvider"/> to apply.</param> /// <param name="diagnosticsAndSources">The code to analyze.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="suppressedDiagnostics">Ids of diagnostics to suppress.</param> /// <param name="metadataReferences">Collection of <see cref="MetadataReference"/> to use when compiling.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void CodeFix(DiagnosticAnalyzer analyzer, CodeFixProvider fix, DiagnosticsAndSources diagnosticsAndSources, string fixedCode, IEnumerable <string> suppressedDiagnostics, IEnumerable <MetadataReference> metadataReferences, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { VerifyCodeFixSupportsAnalyzer(analyzer, fix); VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics); var sln = CodeFactory.CreateSolution(diagnosticsAndSources, analyzer, suppressedDiagnostics, metadataReferences); var diagnostics = Analyze.GetDiagnostics(analyzer, sln); VerifyDiagnostics(diagnosticsAndSources, diagnostics); VerifyFix(sln, diagnostics, analyzer, fix, fixedCode, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that <paramref name="code"/> produces the expected diagnostics. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="code"/> with.</param> /// <param name="code">The code to analyze with <paramref name="analyzer"/>. Indicate error position with ↓ (alt + 25).</param> public static void Diagnostics(DiagnosticAnalyzer analyzer, params string[] code) { Diagnostics( analyzer, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, code), allowCompilationErrors: AllowCompilationErrors.No, suppressWarnings: null, metadataReferences: null, compilationOptions: null); }
/// <summary> /// Verifies that <paramref name="code"/> produces the expected diagnostics. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="code"/> with.</param> /// <param name="expectedDiagnostic">The <see cref="ExpectedDiagnostic"/> with information about the expected <see cref="Diagnostic"/>. If <paramref name="analyzer"/> supports more than one <see cref="DiagnosticDescriptor.Id"/> this must be provided.</param> /// <param name="code">The code to analyze with <paramref name="analyzer"/>. Indicate error position with ↓ (alt + 25).</param> public static void Diagnostics(DiagnosticAnalyzer analyzer, ExpectedDiagnostic expectedDiagnostic, params string[] code) { Diagnostics( analyzer, DiagnosticsAndSources.Create(expectedDiagnostic, code), allowCompilationErrors: AllowCompilationErrors.No, suppressWarnings: null, metadataReferences: null, compilationOptions: null); }
/// <summary> /// Verifies that /// 1. <paramref name="solution"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="fix">The code fix to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="solution">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void CodeFix(DiagnosticAnalyzer analyzer, CodeFixProvider fix, ExpectedDiagnostic expectedDiagnostic, Solution solution, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { VerifyAnalyzerSupportsDiagnostic(analyzer, expectedDiagnostic); VerifyCodeFixSupportsAnalyzer(analyzer, fix); var diagnostics = Analyze.GetDiagnostics(analyzer, solution); var diagnosticsAndSources = DiagnosticsAndSources.Create(expectedDiagnostic, solution.Projects.SelectMany(x => x.Documents).Select(x => CodeReader.GetCode(x, null)).ToArray()); VerifyDiagnostics(diagnosticsAndSources, diagnostics); VerifyFix(solution, diagnostics, analyzer, fix, fixedCode, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <typeparam name="TCodeFix">The type of the code fix.</typeparam> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void NoFix <TCodeFix>(ExpectedDiagnostic expectedDiagnostic, params string[] code) where TCodeFix : CodeFixProvider, new() { NoFix( new PlaceholderAnalyzer(expectedDiagnostic.Id), new TCodeFix(), DiagnosticsAndSources.Create(expectedDiagnostic, code), SuppressedDiagnostics, MetadataReferences); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="metadataReference">The meta data metadataReference to add to the compilation.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> /// <param name="scope">The scope to apply fixes for.</param> public static async Task FixAllByScopeAsync(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, IReadOnlyList <MetadataReference> metadataReference, string fixTitle, AllowCompilationErrors allowCompilationErrors, FixAllScope scope) { var data = await CreateDiagnosticsMetadataAsync( analyzer, codeFix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), metadataReference); await FixAllByScopeAsync(analyzer, codeFix, fixedCode, fixTitle, allowCompilationErrors, data, scope); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <typeparam name="TAnalyzer">The type of the analyzer.</typeparam> /// <typeparam name="TCodeFix">The type of the code fix.</typeparam> /// <param name="codeWithErrorsIndicated">The code to analyze.</param> public static void NoFix <TAnalyzer, TCodeFix>(IReadOnlyList <string> codeWithErrorsIndicated) where TAnalyzer : DiagnosticAnalyzer, new() where TCodeFix : CodeFixProvider, new() { NoFix( new TAnalyzer(), new TCodeFix(), DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(new TAnalyzer(), codeWithErrorsIndicated), SuppressedDiagnostics, MetadataReferences); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The type of the analyzer.</param> /// <param name="codeFix">The type of the code fix.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated) { NoFixAsync( analyzer, codeFix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics. /// </summary> /// <param name="analyzer">The analyzer to apply.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> public static void Diagnostics(DiagnosticAnalyzer analyzer, params string[] codeWithErrorsIndicated) { DiagnosticsWithMetadataAsync( analyzer, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences, null) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="diagnosticsAndSources"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="fix">The <see cref="CodeFixProvider"/> to apply.</param> /// <param name="diagnosticsAndSources">The code to analyze.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void CodeFix(DiagnosticAnalyzer analyzer, CodeFixProvider fix, DiagnosticsAndSources diagnosticsAndSources, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { CodeFix( analyzer, fix, diagnosticsAndSources, fixedCode, SuppressedDiagnostics, MetadataReferences, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that <paramref name="code"/> produces the expected diagnostics. /// </summary> /// <param name="analyzer">The analyzer to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic</param> /// <param name="code">The code to analyze.</param> public static void Diagnostics(DiagnosticAnalyzer analyzer, ExpectedDiagnostic expectedDiagnostic, params string[] code) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); DiagnosticsWithMetadataAsync( analyzer, DiagnosticsAndSources.Create(expectedDiagnostic, code), CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences, null) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="metadataReference">The meta data metadataReference to add to the compilation.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static Task FixAllAsync(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, IReadOnlyList <MetadataReference> metadataReference, string fixTitle, AllowCompilationErrors allowCompilationErrors) { return(FixAllAsync( analyzer, codeFix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), fixedCode, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), metadataReference, fixTitle, allowCompilationErrors)); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="suppressedDiagnostics">The diagnostics to suppress when compiling.</param> /// <param name="metadataReferences">The meta data metadataReferences to add to the compilation.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> /// <param name="scope">The scope to apply fixes for.</param> public static void FixAllByScope(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, string fixTitle, IEnumerable <string> suppressedDiagnostics, IEnumerable <MetadataReference> metadataReferences, AllowCompilationErrors allowCompilationErrors, FixAllScope scope) { var diagnosticsAndSources = DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated); VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics); VerifyCodeFixSupportsAnalyzer(analyzer, codeFix); var sln = CodeFactory.CreateSolution(diagnosticsAndSources, analyzer, suppressedDiagnostics, metadataReferences); var diagnostics = Analyze.GetDiagnostics(sln, analyzer); VerifyDiagnostics(diagnosticsAndSources, diagnostics); FixAllByScope(analyzer, codeFix, sln, fixedCode, fixTitle, allowCompilationErrors, scope); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="fix">The code fix to apply.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void CodeFix(DiagnosticAnalyzer analyzer, CodeFixProvider fix, string codeWithErrorsIndicated, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { CodeFix( analyzer, fix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), fixedCode, SuppressedDiagnostics, MetadataReferences, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="metadataReferences">The meta data references to use when compiling the code.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void FixAll(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, IEnumerable <MetadataReference> metadataReferences, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { FixAll( analyzer, codeFix, DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated), fixedCode, SuppressedDiagnostics, metadataReferences, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="metadataReferences">The meta data references to use when compiling the code.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void FixAll(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, ExpectedDiagnostic expectedDiagnostic, string codeWithErrorsIndicated, string fixedCode, IEnumerable <MetadataReference> metadataReferences = null, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { FixAll( analyzer, codeFix, DiagnosticsAndSources.Create(expectedDiagnostic, new[] { codeWithErrorsIndicated }), MergeFixedCodeWithErrorsIndicated(new[] { codeWithErrorsIndicated }, fixedCode), SuppressedDiagnostics, metadataReferences ?? MetadataReferences, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="fix">The <see cref="CodeFixProvider"/> to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void CodeFix(CodeFixProvider fix, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> code, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { CodeFix( new PlaceholderAnalyzer(expectedDiagnostic.Id), fix, DiagnosticsAndSources.Create(expectedDiagnostic, code), fixedCode, SuppressedDiagnostics, MetadataReferences, fixTitle, allowCompilationErrors); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="metadataReference">The meta data metadataReference to add to the compilation.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static Task FixAllAsync(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, IReadOnlyList <MetadataReference> metadataReference, string fixTitle, AllowCompilationErrors allowCompilationErrors) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); return(FixAllAsync( analyzer, codeFix, DiagnosticsAndSources.Create(expectedDiagnostic, codeWithErrorsIndicated), fixedCode, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), metadataReference, fixTitle, allowCompilationErrors)); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="codeFix">The type of the code fix.</param> /// <param name="codeWithErrorsIndicated">The code to analyze.</param> /// <param name="compilationOptions">The <see cref="CSharpCompilationOptions"/> to use.</param> /// <param name="metadataReferences">The meta data references to use when compiling.</param> public static void NoFix(CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated, CSharpCompilationOptions compilationOptions, IEnumerable <MetadataReference> metadataReferences) { var diagnosticsAndSources = DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(new PlaceholderAnalyzer("None"), codeWithErrorsIndicated); var sln = CodeFactory.CreateSolution(diagnosticsAndSources.Code, compilationOptions, metadataReferences); var diagnostics = Analyze.GetDiagnostics(sln); if (diagnostics.SelectMany(x => x).All(d => !codeFix.FixableDiagnosticIds.Contains(d.Id))) { throw new InvalidOperationException("Analyzing the code did not produce any diagnostics fixable by the code fix."); } VerifyNoFix(sln, diagnostics, codeFix); }