|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 4 | +using Microsoft.CodeAnalysis.Testing; |
| 5 | +using Microsoft.CodeAnalysis.Testing.Verifiers; |
| 6 | +using System.Collections.Immutable; |
| 7 | + |
| 8 | +namespace AddisonWesley.Michaelis.EssentialCSharp.Shared; |
| 9 | + |
| 10 | +public static class CompilerAssert2 |
| 11 | +{ |
| 12 | + public static async Task Compile2Async(string[] fileNames, string[] expectedErrorIds) |
| 13 | + { |
| 14 | + string code = string.Empty; |
| 15 | + foreach (string fileName in fileNames) |
| 16 | + { |
| 17 | + code += Environment.NewLine + await File.ReadAllTextAsync(fileName); |
| 18 | + } |
| 19 | + |
| 20 | + var test = new Test() |
| 21 | + { |
| 22 | + TestCode = code |
| 23 | + }; |
| 24 | + foreach (var errorId in expectedErrorIds) |
| 25 | + { |
| 26 | + test.ExpectedDiagnostics.Add(DiagnosticResult.CompilerError(errorId)); |
| 27 | + } |
| 28 | + |
| 29 | + await test.RunAsync(); |
| 30 | + } |
| 31 | + |
| 32 | + //Custom verifier to ignore diagnostic locations locations |
| 33 | + public class CustomMSTestVerifier : MSTestVerifier |
| 34 | + { |
| 35 | + public override void Equal<T>(T expected, T actual, string? message = null) |
| 36 | + { |
| 37 | + if (typeof(T) == typeof(Location)) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + base.Equal(expected, actual, message); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private class Test : AnalyzerTest<CustomMSTestVerifier> |
| 46 | + { |
| 47 | + protected override CompilationOptions CreateCompilationOptions() |
| 48 | + { |
| 49 | + var compilationOptions = new CSharpCompilationOptions( |
| 50 | + OutputKind.DynamicallyLinkedLibrary, |
| 51 | + allowUnsafe: true, |
| 52 | + //Setup set #nullable enable |
| 53 | + nullableContextOptions: NullableContextOptions.Enable); |
| 54 | + |
| 55 | + return compilationOptions |
| 56 | + .WithSpecificDiagnosticOptions( |
| 57 | + compilationOptions.SpecificDiagnosticOptions.SetItems(GetNullableWarningsFromCompiler())); |
| 58 | + } |
| 59 | + |
| 60 | + public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.Default; |
| 61 | + protected override string DefaultFileExt => "cs"; |
| 62 | + public override string Language => LanguageNames.CSharp; |
| 63 | + |
| 64 | + private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler() |
| 65 | + { |
| 66 | + string[] args = Array.Empty<string>(); |
| 67 | + //TODO: if we care to elevate NRT warnings, you can do it like this: |
| 68 | + //string[] args = { "/warnaserror:nullable" }; |
| 69 | + var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory); |
| 70 | + var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions; |
| 71 | + |
| 72 | + return nullableWarnings; |
| 73 | + } |
| 74 | + |
| 75 | + protected override ParseOptions CreateParseOptions() |
| 76 | + => new CSharpParseOptions(LanguageVersion, DocumentationMode.Diagnose) |
| 77 | + .WithPreprocessorSymbols("INCLUDE"); |
| 78 | + |
| 79 | + protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers() |
| 80 | + => Enumerable.Empty<DiagnosticAnalyzer>(); |
| 81 | + } |
| 82 | +} |
0 commit comments