|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.ComponentModel; |
1 | 3 | using System.Diagnostics.CodeAnalysis; |
2 | 4 | using Immediate.Handlers.Generators; |
| 5 | +using Microsoft.CodeAnalysis; |
3 | 6 | using Microsoft.CodeAnalysis.CSharp.Testing; |
4 | 7 | using Microsoft.CodeAnalysis.Diagnostics; |
5 | 8 | using Microsoft.CodeAnalysis.Testing; |
@@ -35,4 +38,118 @@ private sealed class ImmediateHandlersGeneratorAnalyzerTest<TAnalyzer> : CSharpA |
35 | 38 | protected override IEnumerable<Type> GetSourceGenerators() => |
36 | 39 | [typeof(ImmediateHandlersGenerator)]; |
37 | 40 | } |
| 41 | + |
| 42 | + public sealed class CSharpSuppressorTest<TSuppressor, TVerifier> : CSharpAnalyzerTest<TSuppressor, TVerifier> |
| 43 | + where TSuppressor : DiagnosticSuppressor, new() |
| 44 | + where TVerifier : IVerifier, new() |
| 45 | + { |
| 46 | + private readonly List<DiagnosticAnalyzer> _analyzers = []; |
| 47 | + |
| 48 | + protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers() => |
| 49 | + base.GetDiagnosticAnalyzers().Concat(_analyzers); |
| 50 | + |
| 51 | + public CSharpSuppressorTest<TSuppressor, TVerifier> WithAnalyzer<TAnalyzer>(bool enableDiagnostics = false) |
| 52 | + where TAnalyzer : DiagnosticAnalyzer, new() |
| 53 | + { |
| 54 | + var analyzer = new TAnalyzer(); |
| 55 | + _analyzers.Add(analyzer); |
| 56 | + |
| 57 | + if (enableDiagnostics) |
| 58 | + { |
| 59 | + var diagnosticOptions = analyzer.SupportedDiagnostics |
| 60 | + .ToImmutableDictionary( |
| 61 | + descriptor => descriptor.Id, |
| 62 | + descriptor => descriptor.DefaultSeverity.ToReportDiagnostic() |
| 63 | + ); |
| 64 | + |
| 65 | + SolutionTransforms.Clear(); |
| 66 | + SolutionTransforms.Add(EnableDiagnostics(diagnosticOptions)); |
| 67 | + } |
| 68 | + |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + public CSharpSuppressorTest<TSuppressor, TVerifier> WithSpecificDiagnostics( |
| 73 | + params DiagnosticResult[] diagnostics |
| 74 | + ) |
| 75 | + { |
| 76 | + var diagnosticOptions = diagnostics |
| 77 | + .ToImmutableDictionary( |
| 78 | + descriptor => descriptor.Id, |
| 79 | + descriptor => descriptor.Severity.ToReportDiagnostic() |
| 80 | + ); |
| 81 | + |
| 82 | + SolutionTransforms.Clear(); |
| 83 | + SolutionTransforms.Add(EnableDiagnostics(diagnosticOptions)); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + private static Func<Solution, ProjectId, Solution> EnableDiagnostics( |
| 88 | + ImmutableDictionary<string, ReportDiagnostic> diagnostics |
| 89 | + ) => |
| 90 | + (solution, id) => |
| 91 | + { |
| 92 | + var options = solution.GetProject(id)?.CompilationOptions |
| 93 | + ?? throw new InvalidOperationException("Compilation options missing."); |
| 94 | + |
| 95 | + return solution |
| 96 | + .WithProjectCompilationOptions( |
| 97 | + id, |
| 98 | + options |
| 99 | + .WithSpecificDiagnosticOptions(diagnostics) |
| 100 | + ); |
| 101 | + }; |
| 102 | + |
| 103 | + public CSharpSuppressorTest<TSuppressor, TVerifier> WithExpectedDiagnosticsResults( |
| 104 | + params DiagnosticResult[] diagnostics |
| 105 | + ) |
| 106 | + { |
| 107 | + ExpectedDiagnostics.AddRange(diagnostics); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public CSharpSuppressorTest<TSuppressor, TVerifier> WithEditorConfig( |
| 112 | + string content |
| 113 | + ) |
| 114 | + { |
| 115 | + TestState.AnalyzerConfigFiles.Add(("/.editorconfig", content)); |
| 116 | + return this; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static CSharpSuppressorTest<TSuppressor, DefaultVerifier> CreateSuppressorTest<TSuppressor>( |
| 121 | + [StringSyntax("c#-test")] string inputSource |
| 122 | + ) |
| 123 | + where TSuppressor : DiagnosticSuppressor, new() |
| 124 | + { |
| 125 | + var test = new CSharpSuppressorTest<TSuppressor, DefaultVerifier> |
| 126 | + { |
| 127 | + TestCode = inputSource, |
| 128 | + ReferenceAssemblies = Utility.ReferenceAssemblies, |
| 129 | + CompilerDiagnostics = CompilerDiagnostics.Warnings, |
| 130 | + DisabledDiagnostics = |
| 131 | + { |
| 132 | + "CS1591", |
| 133 | + "CS8767", |
| 134 | + }, |
| 135 | + }; |
| 136 | + |
| 137 | + test.TestState.AdditionalReferences |
| 138 | + .AddRange(DriverReferenceAssemblies.Normal.GetAdditionalReferences()); |
| 139 | + |
| 140 | + return test; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +file static class DiagnosticSeverityExtensions |
| 145 | +{ |
| 146 | + public static ReportDiagnostic ToReportDiagnostic(this DiagnosticSeverity severity) |
| 147 | + => severity switch |
| 148 | + { |
| 149 | + DiagnosticSeverity.Hidden => ReportDiagnostic.Hidden, |
| 150 | + DiagnosticSeverity.Info => ReportDiagnostic.Info, |
| 151 | + DiagnosticSeverity.Warning => ReportDiagnostic.Warn, |
| 152 | + DiagnosticSeverity.Error => ReportDiagnostic.Error, |
| 153 | + _ => throw new InvalidEnumArgumentException(nameof(severity), (int)severity, typeof(DiagnosticSeverity)), |
| 154 | + }; |
38 | 155 | } |
0 commit comments