Skip to content

Commit

Permalink
Add tests for missing descriptor attribute code fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Oct 1, 2023
1 parent 6a73364 commit 462d724
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest" Version="1.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest" Version="1.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ComputeSharp.D2D1\ComputeSharp.D2D1.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.Core\ComputeSharp.Core.csproj" Aliases="Core" />
<ProjectReference Include="..\..\src\ComputeSharp.D2D1\ComputeSharp.D2D1.csproj" Aliases="D2D1" />
<ProjectReference Include="..\..\src\ComputeSharp.D2D1.CodeFixers\ComputeSharp.D2D1.CodeFixers.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.D2D1.SourceGenerators\ComputeSharp.D2D1.SourceGenerators.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
extern alias Core;
extern alias D2D1;

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CSharpCodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest<
ComputeSharp.D2D1.SourceGenerators.MissingPixelShaderDescriptorOnPixelShaderAnalyzer,
ComputeSharp.D2D1.CodeFixers.MissingPixelShaderDescriptorOnPixelShaderCodeFixer,
Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier>;

namespace ComputeSharp.D2D1.Tests.SourceGenerators;

[TestClass]
public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer
{
[TestMethod]
public async Task FileContainsComputeSharpD2D1UsingDirective()
{
string original = """
using ComputeSharp.D2D1;

partial struct {|CMPSD2D0065:MyShader|} : ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}
""";

string @fixed = """
using ComputeSharp.D2D1;

[D2DGeneratedPixelShaderDescriptor]
partial struct MyShader : ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}
""";

CSharpCodeFixTest test = new()
{
TestCode = original,
FixedCode = @fixed,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
};

test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly);
test.TestState.AdditionalReferences.Add(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly);
test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf"));

await test.RunAsync();
}

[TestMethod]
public async Task FileDoesNotContainComputeSharpD2D1UsingDirective()
{
string original = """
partial struct {|CMPSD2D0065:MyShader|} : ComputeSharp.D2D1.ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}
""";

string @fixed = """
using ComputeSharp.D2D1;

[D2DGeneratedPixelShaderDescriptor]
partial struct MyShader : ComputeSharp.D2D1.ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}
""";

CSharpCodeFixTest test = new()
{
TestCode = original,
FixedCode = @fixed,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
};

test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly);
test.TestState.AdditionalReferences.Add(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly);
test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf"));

await test.RunAsync();
}

[TestMethod]
public async Task InsertsAttributeAfterAllOtherD2DAttributes()
{
string original = """
using ComputeSharp.D2D1;

[D2DInputCount(1)]
[D2DInputSimple(0)]
partial struct {|CMPSD2D0065:MyShader|} : ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}
""";

string @fixed = """
using ComputeSharp.D2D1;

[D2DInputCount(1)]
[D2DInputSimple(0)]
[D2DGeneratedPixelShaderDescriptor]
partial struct MyShader : ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}
""";

CSharpCodeFixTest test = new()
{
TestCode = original,
FixedCode = @fixed,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
};

test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly);
test.TestState.AdditionalReferences.Add(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly);
test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf"));

await test.RunAsync();
}

[TestMethod]
public async Task InsertsAttributeAfterAllOtherD2DAttributes_WithOtherNonD2DAttribute()
{
string original = """
using System;
using ComputeSharp.D2D1;

[D2DInputCount(1)]
[D2DInputSimple(0)]
[Test]
partial struct {|CMPSD2D0065:MyShader|} : ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}

public class TestAttribute : Attribute
{
}
""";

string @fixed = """
using System;
using ComputeSharp.D2D1;

[D2DInputCount(1)]
[D2DInputSimple(0)]
[D2DGeneratedPixelShaderDescriptor]
[Test]
partial struct MyShader : ID2D1PixelShader
{
public ComputeSharp.Float4 Execute() => 0;
}

public class TestAttribute : Attribute
{
}
""";

CSharpCodeFixTest test = new()
{
TestCode = original,
FixedCode = @fixed,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
};

test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly);
test.TestState.AdditionalReferences.Add(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly);
test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf"));

await test.RunAsync();
}
}

0 comments on commit 462d724

Please sign in to comment.