/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="code">The code to analyze.</param> public static void Valid(DiagnosticAnalyzer analyzer, IReadOnlyList <string> code) { var sln = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences); var diagnosticsAndErrors = Analyze.GetDiagnosticsAndErrors(analyzer, sln); NoDiagnosticsOrErrors(diagnosticsAndErrors); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="code"> /// The code to create the solution from. /// Can be a .cs, .csproj or .sln file. /// </param> public static void NoAnalyzerDiagnostics(DiagnosticAnalyzer analyzer, FileInfo code) { var sln = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences); var diagnostics = Analyze.GetDiagnostics(analyzer, sln); NoDiagnostics(diagnostics); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="code"/> with.</param> /// <param name="code"> /// The code to create the solution from. /// Can be a .cs, .csproj or .sln file. /// </param> public static void NoAnalyzerDiagnostics(DiagnosticAnalyzer analyzer, FileInfo code) { #pragma warning disable CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() var sln = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences); #pragma warning restore CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() var diagnostics = Analyze.GetDiagnostics(analyzer, sln); NoDiagnostics(diagnostics); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="code"/> with.</param> /// <param name="code">The code to analyze using <paramref name="analyzer"/>. Analyzing the code is expected to produce no errors or warnings.</param> public static void Valid(DiagnosticAnalyzer analyzer, params string[] code) { Valid( analyzer, #pragma warning disable CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences)); #pragma warning restore CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="descriptors">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void Valid(DiagnosticAnalyzer analyzer, IReadOnlyList <DiagnosticDescriptor> descriptors, params string[] code) { VerifyAnalyzerSupportsDiagnostics(analyzer, descriptors); var sln = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, descriptors, SuppressedDiagnostics), MetadataReferences); var diagnosticsAndErrors = Analyze.GetDiagnosticsAndErrors(analyzer, sln); NoDiagnosticsOrErrors(diagnosticsAndErrors); }
/// <summary> /// Creates a solution and adds the <paramref name="analyzer"/> as analyzer. /// Then compiles it and returns the diagnostics. /// </summary> /// <param name="analyzer">The analyzer to find diagnostics for.</param> /// <param name="sources">The sources as strings.</param> /// <param name="metadataReferences">The <see cref="MetadataReference"/> to use when compiling.</param> /// <returns>A list with diagnostics per document.</returns> public static Task <IReadOnlyList <ImmutableArray <Diagnostic> > > GetDiagnosticsAsync(DiagnosticAnalyzer analyzer, IReadOnlyList <string> sources, IReadOnlyList <MetadataReference> metadataReferences) { return(GetDiagnosticsAsync( analyzer, sources, CodeFactory.DefaultCompilationOptions(analyzer, null), metadataReferences)); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="descriptor">The expected diagnostic.</param> /// <param name="code"> /// The code to create the solution from. /// Can be a .cs, .csproj or .sln file. /// </param> public static void Valid(DiagnosticAnalyzer analyzer, DiagnosticDescriptor descriptor, FileInfo code) { VerifyAnalyzerSupportsDiagnostic(analyzer, descriptor); var sln = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, descriptor, SuppressedDiagnostics), MetadataReferences); var diagnosticsAndErrors = Analyze.GetDiagnosticsAndErrors(analyzer, sln); NoDiagnosticsOrErrors(diagnosticsAndErrors); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="descriptor">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void NoAnalyzerDiagnostics(DiagnosticAnalyzer analyzer, DiagnosticDescriptor descriptor, IReadOnlyList <string> code) { VerifyAnalyzerSupportsDiagnostic(analyzer, descriptor); var sln = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, descriptor, SuppressedDiagnostics), MetadataReferences); var diagnostics = Analyze.GetDiagnostics(analyzer, sln); NoDiagnostics(diagnostics); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="code">The code to analyze.</param> /// <param name="metadataReferences">The metadata references to use when compiling.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static Task ValidAsync(DiagnosticAnalyzer analyzer, IReadOnlyList <string> code, IReadOnlyList <MetadataReference> metadataReferences) { return(ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), metadataReferences)); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> to check <paramref name="code"/> with.</param> /// <param name="descriptor">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 using <paramref name="analyzer"/>. Analyzing the code is expected to produce no errors or warnings.</param> public static void NoAnalyzerDiagnostics(DiagnosticAnalyzer analyzer, DiagnosticDescriptor descriptor, IReadOnlyList <string> code) { VerifyAnalyzerSupportsDiagnostic(analyzer, descriptor); #pragma warning disable CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() var sln = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer, descriptor, SuppressedDiagnostics), MetadataReferences); #pragma warning restore CS0618 // Suppress until removed. Will be replaced with Metadatareferences.FromAttributes() var diagnostics = Analyze.GetDiagnostics(analyzer, sln); NoDiagnostics(diagnostics); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> /// <param name="metadataReferences">The metadata references to use when compiling.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static Task ValidAsync(DiagnosticAnalyzer analyzer, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> code, IReadOnlyList <MetadataReference> metadataReferences) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); return(ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), metadataReferences)); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="code">The code to analyze.</param> public static void Valid(DiagnosticAnalyzer analyzer, IReadOnlyList <string> code) { ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="code">The code to analyze.</param> /// <param name="compilationOptions">The <see cref="CSharpCompilationOptions"/> to use.</param> /// <param name="metadataReferences">The metadata references to use when compiling.</param> public static void Valid(DiagnosticAnalyzer analyzer, string code, CSharpCompilationOptions compilationOptions = null, IEnumerable <MetadataReference> metadataReferences = null) { var sln = CodeFactory.CreateSolution( code, compilationOptions ?? CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), metadataReferences ?? MetadataReferences); var diagnosticsAndErrors = Analyze.GetDiagnosticsAndErrors(analyzer, sln); NoDiagnosticsOrErrors(diagnosticsAndErrors); }
/// <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="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code"> /// The code to create the solution from. /// Can be a .cs, .csproj or .sln file /// </param> public static void Valid(DiagnosticAnalyzer analyzer, ExpectedDiagnostic expectedDiagnostic, FileInfo code) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="expectedDiagnostics">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void Valid(DiagnosticAnalyzer analyzer, IReadOnlyList <ExpectedDiagnostic> expectedDiagnostics, params string[] code) { AssertAnalyzerSupportsExpectedDiagnostics(analyzer, expectedDiagnostics, out var descriptors, out var suppressedDiagnostics); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptors, SuppressedDiagnostics.Concat(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="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 <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 <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 <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzerType"/>. /// </summary> /// <param name="analyzerType">The type of the analyzer.</param> /// <param name="code">The code to analyze.</param> public static void Valid(Type analyzerType, params string[] code) { var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(analyzer, SuppressedDiagnostics), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <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, string code) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); NoFixAsync( analyzer, codeFix, new DiagnosticsAndSources(new[] { expectedDiagnostic }, new[] { code }), CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <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="expectedDiagnostics">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <ExpectedDiagnostic> expectedDiagnostics, IReadOnlyList <string> code) { AssertAnalyzerSupportsExpectedDiagnostics(analyzer, expectedDiagnostics, out var descriptors, out var suppressedDiagnostics); NoFixAsync( analyzer, codeFix, new DiagnosticsAndSources(expectedDiagnostics, code), CodeFactory.DefaultCompilationOptions(descriptors, SuppressedDiagnostics.Concat(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="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 <paramref name="code"/> produces no diagnostics when analyzed with <typeparamref name="TAnalyzer"/>. /// </summary> /// <typeparam name="TAnalyzer">The type of the analyzer.</typeparam> /// <param name="code">The code to analyze.</param> public static void Valid <TAnalyzer>(params string[] code) where TAnalyzer : DiagnosticAnalyzer, new() { var analyzer = new TAnalyzer(); ValidAsync( analyzer, code, 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> /// <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); }
private static IReadOnlyList <CodeAction> CodeActions(CodeRefactoringProvider refactoring, string testCode, TextSpan span, IEnumerable <MetadataReference> metadataReferences) { var sln = CodeFactory.CreateSolutionWithOneProject( testCode, CodeFactory.DefaultCompilationOptions(Array.Empty <DiagnosticAnalyzer>()), metadataReferences); var document = sln.Projects.Single().Documents.Single(); var actions = new List <CodeAction>(); var context = new CodeRefactoringContext(document, span, a => actions.Add(a), CancellationToken.None); refactoring.ComputeRefactoringsAsync(context).GetAwaiter().GetResult(); return(actions); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzerType"/>. /// </summary> /// <param name="analyzerType">The type of the analyzer.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void Valid(Type analyzerType, ExpectedDiagnostic expectedDiagnostic, params string[] code) { var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType); AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <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(); }
/// <summary> /// Verifies that <paramref name="code"/> produces the expected diagnostics. /// </summary> /// <param name="analyzerType">The type of the analyzer.</param> /// <param name="expectedDiagnostics">The expected diagnostics</param> /// <param name="code">The code to analyze.</param> public static void Diagnostics(Type analyzerType, IReadOnlyList <ExpectedDiagnostic> expectedDiagnostics, params string[] code) { var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType); AssertAnalyzerSupportsExpectedDiagnostics(analyzer, expectedDiagnostics, out var descriptors, out var suppressedDiagnostics); DiagnosticsWithMetadataAsync( analyzer, new DiagnosticsAndSources(expectedDiagnostics, code), CodeFactory.DefaultCompilationOptions(descriptors, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences, null) .GetAwaiter() .GetResult(); }