/// <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 NoAnalyzerDiagnostics(DiagnosticAnalyzer analyzer, string code, CSharpCompilationOptions compilationOptions, IEnumerable <MetadataReference> metadataReferences) { var sln = CodeFactory.CreateSolution(code, compilationOptions, 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 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 /// 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> /// Searches parent directories for <paramref name="name"/> the first file matching Foo.sln. /// </summary> /// <param name="name">The assembly.</param> /// <param name="sln">The <see cref="File"/> if found.</param> /// <returns>A value indicating if a file was found.</returns> public static bool TryFind(string name, [NotNullWhen(true)] out FileInfo?sln) { sln = null; return(Assembly.GetCallingAssembly().CodeBase is { } codeBase&& new FileInfo(new Uri(codeBase, UriKind.Absolute).LocalPath) is { } dll&& CodeFactory.TryFindFileInParentDirectory(dll.Directory, name, out sln)); }
/// <summary> /// Searches parent directories for <paramref name="assembly"/> the first file matching *.sln. /// </summary> /// <param name="assembly">The <see cref="Assembly"/>.</param> /// <param name="sln">The <see cref="File"/> if found.</param> /// <returns>A value indicating if a file was found.</returns> public static bool TryFind(Assembly assembly, [NotNullWhen(true)] out FileInfo?sln) { sln = null; return(assembly.CodeBase is { } codeBase&& new FileInfo(new Uri(codeBase, UriKind.Absolute).LocalPath) is { } dll&& CodeFactory.TryFindFileInParentDirectory(dll.Directory, "*.sln", out sln)); }
/// <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="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 <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="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 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 <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="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 /// 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 /// 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(); }