extern alias Core; #if D2D1_TESTS extern alias D2D1; #else extern alias D3D12; #endif using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Testing.Verifiers; using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeAnalysis; namespace ComputeSharp.Tests.SourceGenerators.Helpers; /// /// A custom that uses a specific C# language version to parse code. /// /// The type of the analyzer to test. internal sealed class CSharpAnalyzerWithLanguageVersionTest : CSharpAnalyzerTest where TAnalyzer : DiagnosticAnalyzer, new() { /// /// Whether to enable unsafe blocks. /// private readonly bool allowUnsafeBlocks; /// /// The C# language version to use to parse code. /// private readonly LanguageVersion languageVersion; /// /// Creates a new instance with the specified paramaters. /// /// Whether to enable unsafe blocks. /// The C# language version to use to parse code. private CSharpAnalyzerWithLanguageVersionTest(bool allowUnsafeBlocks, LanguageVersion languageVersion) { this.allowUnsafeBlocks = allowUnsafeBlocks; this.languageVersion = languageVersion; } /// protected override CompilationOptions CreateCompilationOptions() { return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: this.allowUnsafeBlocks); } /// protected override ParseOptions CreateParseOptions() { return new CSharpParseOptions(this.languageVersion, DocumentationMode.Diagnose); } /// /// The source code to analyze. /// Whether to enable unsafe blocks. /// The language version to use to run the test. public static Task VerifyAnalyzerAsync( string source, bool allowUnsafeBlocks = true, LanguageVersion languageVersion = LanguageVersion.CSharp12) { CSharpAnalyzerWithLanguageVersionTest test = new(allowUnsafeBlocks, languageVersion) { TestCode = source }; test.TestState.ReferenceAssemblies = ReferenceAssemblies.Net.Net80; test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location)); #if D2D1_TESTS test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location)); #else test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(D3D12::ComputeSharp.IComputeShader).Assembly.Location)); #endif return test.RunAsync(CancellationToken.None); } }