Add tests for 'AllowsUnsafeBlocks' analyzer
このコミットが含まれているのは:
1 親
b6bcc1c6cc
コミット
2a9f6229bc
3ファイル変更
+63
-5
検索条件に一致するファイルはありません
+22
-2
@@ -9,7 +9,27 @@ namespace ComputeSharp.D2D1.Tests.SourceGenerators;
|
||||
public class Test_D2DPixelShaderDescriptorGenerator_Analyzers
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task MissingD2DPixelShaderDescriptor_ComputeShader()
|
||||
public async Task AllowsUnsafeBlocksNotEnabled_Warns()
|
||||
{
|
||||
const string source = """
|
||||
using ComputeSharp;
|
||||
using ComputeSharp.D2D1;
|
||||
|
||||
[{|CMPSD2D0064:D2DGeneratedPixelShaderDescriptor|}]
|
||||
internal partial struct MyShader : ID2D1PixelShader
|
||||
{
|
||||
public Float4 Execute()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
await CSharpAnalyzerWithLanguageVersionTest<MissingAllowUnsafeBlocksCompilationOptionAnalyzer>.VerifyAnalyzerAsync(source, allowUnsafeBlocks: false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MissingD2DPixelShaderDescriptor_Warns()
|
||||
{
|
||||
const string source = """
|
||||
using ComputeSharp;
|
||||
@@ -28,7 +48,7 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MissingComputeShaderDescriptor_ManuallyImplemented_DoesNotWarn()
|
||||
public async Task MissingD2DPixelShaderDescriptor_ManuallyImplemented_DoesNotWarn()
|
||||
{
|
||||
const string source = """
|
||||
using System;
|
||||
|
||||
+20
-3
@@ -23,6 +23,11 @@ namespace ComputeSharp.Tests.SourceGenerators.Helpers;
|
||||
internal sealed class CSharpAnalyzerWithLanguageVersionTest<TAnalyzer> : CSharpAnalyzerTest<TAnalyzer, MSTestVerifier>
|
||||
where TAnalyzer : DiagnosticAnalyzer, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether to enable unsafe blocks.
|
||||
/// </summary>
|
||||
private readonly bool allowUnsafeBlocks;
|
||||
|
||||
/// <summary>
|
||||
/// The C# language version to use to parse code.
|
||||
/// </summary>
|
||||
@@ -31,12 +36,20 @@ internal sealed class CSharpAnalyzerWithLanguageVersionTest<TAnalyzer> : CSharpA
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="CSharpAnalyzerWithLanguageVersionTest{TAnalyzer}"/> instance with the specified paramaters.
|
||||
/// </summary>
|
||||
/// <param name="allowUnsafeBlocks">Whether to enable unsafe blocks.</param>
|
||||
/// <param name="languageVersion">The C# language version to use to parse code.</param>
|
||||
private CSharpAnalyzerWithLanguageVersionTest(LanguageVersion languageVersion)
|
||||
private CSharpAnalyzerWithLanguageVersionTest(bool allowUnsafeBlocks, LanguageVersion languageVersion)
|
||||
{
|
||||
this.allowUnsafeBlocks = allowUnsafeBlocks;
|
||||
this.languageVersion = languageVersion;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override CompilationOptions CreateCompilationOptions()
|
||||
{
|
||||
return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: this.allowUnsafeBlocks);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override ParseOptions CreateParseOptions()
|
||||
{
|
||||
@@ -45,10 +58,14 @@ internal sealed class CSharpAnalyzerWithLanguageVersionTest<TAnalyzer> : CSharpA
|
||||
|
||||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.VerifyAnalyzerAsync"/>
|
||||
/// <param name="source">The source code to analyze.</param>
|
||||
/// <param name="allowUnsafeBlocks">Whether to enable unsafe blocks.</param>
|
||||
/// <param name="languageVersion">The language version to use to run the test.</param>
|
||||
public static Task VerifyAnalyzerAsync(string source, LanguageVersion languageVersion = LanguageVersion.CSharp12)
|
||||
public static Task VerifyAnalyzerAsync(
|
||||
string source,
|
||||
bool allowUnsafeBlocks = true,
|
||||
LanguageVersion languageVersion = LanguageVersion.CSharp12)
|
||||
{
|
||||
CSharpAnalyzerWithLanguageVersionTest<TAnalyzer> test = new(languageVersion) { TestCode = source };
|
||||
CSharpAnalyzerWithLanguageVersionTest<TAnalyzer> test = new(allowUnsafeBlocks, languageVersion) { TestCode = source };
|
||||
|
||||
test.TestState.ReferenceAssemblies = ReferenceAssemblies.Net.Net80;
|
||||
test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location));
|
||||
|
||||
+21
@@ -8,6 +8,27 @@ namespace ComputeSharp.Tests.SourceGenerators;
|
||||
[TestClass]
|
||||
public class Test_ComputeShaderDescriptorGenerator_Analyzers
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task AllowsUnsafeBlocksNotEnabled_Warns()
|
||||
{
|
||||
const string source = """
|
||||
using ComputeSharp;
|
||||
|
||||
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
|
||||
[{|CMPS0052:GeneratedComputeShaderDescriptor|}]
|
||||
internal readonly partial struct MyShader : IComputeShader
|
||||
{
|
||||
private readonly ReadWriteBuffer<float> buffer;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
await CSharpAnalyzerWithLanguageVersionTest<MissingAllowUnsafeBlocksCompilationOptionAnalyzer>.VerifyAnalyzerAsync(source, allowUnsafeBlocks: false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MissingComputeShaderDescriptor_ComputeShader()
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする