Skip to content

Commit

Permalink
Add tests for 'AllowsUnsafeBlocks' analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jun 17, 2024
1 parent b6bcc1c commit 2a9f622
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +48,7 @@ public Float4 Execute()
}

[TestMethod]
public async Task MissingComputeShaderDescriptor_ManuallyImplemented_DoesNotWarn()
public async Task MissingD2DPixelShaderDescriptor_ManuallyImplemented_DoesNotWarn()
{
const string source = """
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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()
{
Expand All @@ -45,10 +58,14 @@ protected override ParseOptions CreateParseOptions()

/// <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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 2a9f622

Please sign in to comment.