/// <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));
 }
Ejemplo n.º 2
0
 /// <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="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="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));
 }
Ejemplo n.º 5
0
 /// <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);
 }
Ejemplo n.º 6
0
        /// <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>
 /// <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
 /// 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 <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();
 }
Ejemplo n.º 10
0
 /// <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="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="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 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);
        }
        /// <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>
        /// <param name="compilationOptions">The <see cref="CSharpCompilationOptions"/> to use.</param>
        /// <param name="metadataReferences">The meta data metadataReferences to use when compiling.</param>
        public static void Diagnostics(DiagnosticAnalyzer analyzer, IReadOnlyList <string> codeWithErrorsIndicated, CSharpCompilationOptions compilationOptions = null, IEnumerable <MetadataReference> metadataReferences = null)
        {
            var diagnosticsAndSources = DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated);

            VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics);
            var sln = CodeFactory.CreateSolution(
                diagnosticsAndSources.Code,
                compilationOptions ?? CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics),
                metadataReferences ?? MetadataReferences);
            var diagnostics = Analyze.GetDiagnostics(analyzer, sln);

            VerifyDiagnostics(diagnosticsAndSources, diagnostics);
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Verifies that
 /// 1. <paramref name="code"/> 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="code">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 codeFix, IReadOnlyList <string> code, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No)
 {
     CodeFixAsync(
         analyzer,
         codeFix,
         DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, code),
         fixedCode,
         fixTitle,
         CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics),
         MetadataReferences,
         allowCompilationErrors)
     .GetAwaiter()
     .GetResult();
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Verifies that
        /// 1. <paramref name="code"/> 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="code">The code to analyze.</param>
        public static void NoFix <TAnalyzer, TCodeFix>(IReadOnlyList <string> code)
            where TAnalyzer : DiagnosticAnalyzer, new()
            where TCodeFix : CodeFixProvider, new()
        {
            var analyzer = new TAnalyzer();

            NoFixAsync(
                analyzer,
                new TCodeFix(),
                DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, code),
                CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics),
                MetadataReferences)
            .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="fix">The <see cref="CodeFixProvider"/> to apply.</param>
        /// <param name="codeWithErrorsIndicated">The code to analyze.</param>
        /// <param name="fixedCode">The expected code produced by the code fix.</param>
        /// <param name="compilationOptions">The compilation options.</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, IReadOnlyList <string> codeWithErrorsIndicated, string fixedCode, CSharpCompilationOptions compilationOptions = null, IEnumerable <MetadataReference> metadataReferences = null, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No)
        {
            VerifyCodeFixSupportsAnalyzer(analyzer, fix);
            var diagnosticsAndSources = DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated);

            VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics);
            var sln = CodeFactory.CreateSolution(
                diagnosticsAndSources.Code,
                compilationOptions ?? CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics),
                metadataReferences ?? MetadataReferences);
            var diagnostics = Analyze.GetDiagnostics(analyzer, sln);

            VerifyDiagnostics(diagnosticsAndSources, diagnostics);
            VerifyFix(sln, diagnostics, analyzer, fix, fixedCode, fixTitle, allowCompilationErrors);
        }
Ejemplo n.º 19
0
 /// <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>
 /// <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,
     IReadOnlyList <string> code,
     AllowCompilationErrors allowCompilationErrors      = AllowCompilationErrors.No,
     IEnumerable <string>?suppressWarnings              = null,
     IEnumerable <MetadataReference>?metadataReferences = null,
     CSharpCompilationOptions?compilationOptions        = null)
 {
     Diagnostics(
         analyzer,
         DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, code),
         allowCompilationErrors: allowCompilationErrors,
         suppressWarnings: suppressWarnings,
         metadataReferences: metadataReferences,
         compilationOptions: compilationOptions);
 }
        /// <summary>
        /// Verifies that
        /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics
        /// 2. The code fix fixes 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 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 FixAll <TAnalyzer, TCodeFix>(IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No)
            where TAnalyzer : DiagnosticAnalyzer, new()
            where TCodeFix : CodeFixProvider, new()
        {
            var analyzer = new TAnalyzer();

            FixAll(
                analyzer,
                new TCodeFix(),
                DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated),
                fixedCode,
                SuppressedDiagnostics,
                MetadataReferences,
                fixTitle,
                allowCompilationErrors);
        }
Ejemplo n.º 21
0
        public static void CodeFix <TCodeFix>(string id, IReadOnlyList <string> code, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No)
            where TCodeFix : CodeFixProvider, new()
        {
            var analyzer = new PlaceholderAnalyzer(id);

            CodeFixAsync(
                analyzer,
                new TCodeFix(),
                DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, code),
                fixedCode,
                fixTitle,
                CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics),
                MetadataReferences,
                allowCompilationErrors)
            .GetAwaiter()
            .GetResult();
        }
        /// <summary>
        /// Verifies that
        /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics
        /// 2. The code fix fixes 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 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 FixAllOneByOne <TAnalyzer, TCodeFix>(string codeWithErrorsIndicated, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No)
            where TAnalyzer : DiagnosticAnalyzer, new()
            where TCodeFix : CodeFixProvider, new()
        {
            var analyzer = new TAnalyzer();
            var diagnosticsAndSources = DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated);

            VerifyAnalyzerSupportsDiagnostics(analyzer, diagnosticsAndSources.ExpectedDiagnostics);
            var codeFix = new TCodeFix();

            VerifyCodeFixSupportsAnalyzer(analyzer, codeFix);
            var sln         = CodeFactory.CreateSolution(diagnosticsAndSources, analyzer, SuppressedDiagnostics, MetadataReferences);
            var diagnostics = Analyze.GetDiagnostics(analyzer, sln);

            VerifyDiagnostics(diagnosticsAndSources, diagnostics);
            FixAllOneByOne(analyzer, codeFix, sln, new[] { fixedCode }, fixTitle, allowCompilationErrors);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Verifies that
        /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics
        /// 2. The code fix fixes 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 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 FixAll <TAnalyzer, TCodeFix>(IReadOnlyList <string> codeWithErrorsIndicated, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No)
            where TAnalyzer : DiagnosticAnalyzer, new()
            where TCodeFix : CodeFixProvider, new()
        {
            var analyzer = new TAnalyzer();

            FixAllAsync(
                analyzer,
                new TCodeFix(),
                DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated),
                MergeFixedCodeWithErrorsIndicated(codeWithErrorsIndicated, fixedCode),
                CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics),
                MetadataReferences,
                fixTitle,
                allowCompilationErrors)
            .GetAwaiter()
            .GetResult();
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Verifies that
        /// 1. <paramref name="code"/> produces the expected diagnostics
        /// 2. The code fix does not change the code.
        /// </summary>
        /// <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"/>.</param>
        /// <param name="code">The code to analyze. 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 NoFix(
            CodeFixProvider fix,
            ExpectedDiagnostic expectedDiagnostic,
            IReadOnlyList <string> code,
            AllowCompilationErrors allowCompilationErrors      = AllowCompilationErrors.No,
            IEnumerable <string>?suppressWarnings              = null,
            IEnumerable <MetadataReference>?metadataReferences = null,
            CSharpCompilationOptions?compilationOptions        = null)
        {
            var analyzer = new PlaceholderAnalyzer(expectedDiagnostic.Id);

            NoFix(
                analyzer: analyzer,
                fix: fix,
                diagnosticsAndSources: DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, code),
                allowCompilationErrors: allowCompilationErrors,
                suppressWarnings: suppressWarnings,
                metadataReferences: metadataReferences,
                compilationOptions: compilationOptions);
        }
Ejemplo n.º 25
0
        /// <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="compilationOptions">The <see cref="CSharpCompilationOptions"/> to use.</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 async Task FixAllAsync(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, CSharpCompilationOptions compilationOptions, IReadOnlyList <MetadataReference> metadataReference, string fixTitle, AllowCompilationErrors allowCompilationErrors)
        {
            var data = await CreateDiagnosticsMetadataAsync(
                analyzer,
                codeFix,
                DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated),
                compilationOptions,
                metadataReference);

            await FixAllOneByOneAsync(analyzer, codeFix, fixedCode, fixTitle, allowCompilationErrors, data).ConfigureAwait(false);

            var fixAllProvider = codeFix.GetFixAllProvider();

            if (fixAllProvider != null)
            {
                foreach (var scope in fixAllProvider.GetSupportedFixAllScopes())
                {
                    await FixAllByScopeAsync(analyzer, codeFix, fixedCode, fixTitle, allowCompilationErrors, data, scope);
                }
            }
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Verifies that
 /// 1. <paramref name="fix"/> supports fixing diagnostics reported by <paramref name="analyzer"/>.
 /// 2. <paramref name="before"/> 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="before"/> with.</param>
 /// <param name="fix">The <see cref="CodeFixProvider"/> to apply on the <see cref="Diagnostic"/> reported.</param>
 /// <param name="before">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,
     IReadOnlyList <string> before,
     string after,
     string?fixTitle = null,
     AllowCompilationErrors allowCompilationErrors      = AllowCompilationErrors.No,
     IEnumerable <string>?suppressWarnings              = null,
     IEnumerable <MetadataReference>?metadataReferences = null,
     CSharpCompilationOptions?compilationOptions        = null)
 {
     CodeFix(
         analyzer: analyzer,
         fix: fix,
         diagnosticsAndSources: DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, before),
         after: MergeFixedCode(before, after),
         fixTitle: fixTitle,
         allowCompilationErrors: allowCompilationErrors,
         suppressWarnings: suppressWarnings,
         metadataReferences: metadataReferences,
         compilationOptions: compilationOptions);
 }
 /// <summary>
 /// Verifies that <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics.
 /// </summary>
 /// <param name="analyzerType">The type of the analyzer.</param>
 /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param>
 public static void Diagnostics(Type analyzerType, params string[] codeWithErrorsIndicated)
 {
     Diagnostics(
         (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType),
         DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated((DiagnosticAnalyzer)Activator.CreateInstance(analyzerType), codeWithErrorsIndicated));
 }
 /// <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)
 {
     Diagnostics(
         analyzer,
         DiagnosticsAndSources.CreateFromCodeWithErrorsIndicated(analyzer, codeWithErrorsIndicated));
 }