-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86528 from avilches/must-be-variant-tests
Add unit tests for C# diagnostic analyzers
- Loading branch information
Showing
12 changed files
with
418 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/GlobalClass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Godot.SourceGenerators.Sample; | ||
|
||
[GlobalClass] | ||
public partial class CustomGlobalClass : GodotObject | ||
{ | ||
} | ||
|
||
// This doesn't works because global classes can't have any generic type parameter. | ||
/* | ||
[GlobalClass] | ||
public partial class CustomGlobalClass<T> : Node | ||
{ | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MustBeVariantSamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
using System; | ||
using Godot.Collections; | ||
using Array = Godot.Collections.Array; | ||
|
||
namespace Godot.SourceGenerators.Sample; | ||
|
||
public class MustBeVariantMethods | ||
{ | ||
public void MustBeVariantMethodCalls() | ||
{ | ||
Method<bool>(); | ||
Method<char>(); | ||
Method<sbyte>(); | ||
Method<byte>(); | ||
Method<short>(); | ||
Method<ushort>(); | ||
Method<int>(); | ||
Method<uint>(); | ||
Method<long>(); | ||
Method<ulong>(); | ||
Method<float>(); | ||
Method<double>(); | ||
Method<string>(); | ||
Method<Vector2>(); | ||
Method<Vector2I>(); | ||
Method<Rect2>(); | ||
Method<Rect2I>(); | ||
Method<Transform2D>(); | ||
Method<Vector3>(); | ||
Method<Vector3I>(); | ||
Method<Vector4>(); | ||
Method<Vector4I>(); | ||
Method<Basis>(); | ||
Method<Quaternion>(); | ||
Method<Transform3D>(); | ||
Method<Projection>(); | ||
Method<Aabb>(); | ||
Method<Color>(); | ||
Method<Plane>(); | ||
Method<Callable>(); | ||
Method<Signal>(); | ||
Method<GodotObject>(); | ||
Method<StringName>(); | ||
Method<NodePath>(); | ||
Method<Rid>(); | ||
Method<Dictionary>(); | ||
Method<Array>(); | ||
Method<byte[]>(); | ||
Method<int[]>(); | ||
Method<long[]>(); | ||
Method<float[]>(); | ||
Method<double[]>(); | ||
Method<string[]>(); | ||
Method<Vector2[]>(); | ||
Method<Vector3[]>(); | ||
Method<Color[]>(); | ||
Method<GodotObject[]>(); | ||
Method<StringName[]>(); | ||
Method<NodePath[]>(); | ||
Method<Rid[]>(); | ||
|
||
// This call fails because generic type is not Variant-compatible. | ||
//Method<object>(); | ||
} | ||
|
||
public void Method<[MustBeVariant] T>() | ||
{ | ||
} | ||
|
||
public void MustBeVariantClasses() | ||
{ | ||
new ClassWithGenericVariant<bool>(); | ||
new ClassWithGenericVariant<char>(); | ||
new ClassWithGenericVariant<sbyte>(); | ||
new ClassWithGenericVariant<byte>(); | ||
new ClassWithGenericVariant<short>(); | ||
new ClassWithGenericVariant<ushort>(); | ||
new ClassWithGenericVariant<int>(); | ||
new ClassWithGenericVariant<uint>(); | ||
new ClassWithGenericVariant<long>(); | ||
new ClassWithGenericVariant<ulong>(); | ||
new ClassWithGenericVariant<float>(); | ||
new ClassWithGenericVariant<double>(); | ||
new ClassWithGenericVariant<string>(); | ||
new ClassWithGenericVariant<Vector2>(); | ||
new ClassWithGenericVariant<Vector2I>(); | ||
new ClassWithGenericVariant<Rect2>(); | ||
new ClassWithGenericVariant<Rect2I>(); | ||
new ClassWithGenericVariant<Transform2D>(); | ||
new ClassWithGenericVariant<Vector3>(); | ||
new ClassWithGenericVariant<Vector3I>(); | ||
new ClassWithGenericVariant<Vector4>(); | ||
new ClassWithGenericVariant<Vector4I>(); | ||
new ClassWithGenericVariant<Basis>(); | ||
new ClassWithGenericVariant<Quaternion>(); | ||
new ClassWithGenericVariant<Transform3D>(); | ||
new ClassWithGenericVariant<Projection>(); | ||
new ClassWithGenericVariant<Aabb>(); | ||
new ClassWithGenericVariant<Color>(); | ||
new ClassWithGenericVariant<Plane>(); | ||
new ClassWithGenericVariant<Callable>(); | ||
new ClassWithGenericVariant<Signal>(); | ||
new ClassWithGenericVariant<GodotObject>(); | ||
new ClassWithGenericVariant<StringName>(); | ||
new ClassWithGenericVariant<NodePath>(); | ||
new ClassWithGenericVariant<Rid>(); | ||
new ClassWithGenericVariant<Dictionary>(); | ||
new ClassWithGenericVariant<Array>(); | ||
new ClassWithGenericVariant<byte[]>(); | ||
new ClassWithGenericVariant<int[]>(); | ||
new ClassWithGenericVariant<long[]>(); | ||
new ClassWithGenericVariant<float[]>(); | ||
new ClassWithGenericVariant<double[]>(); | ||
new ClassWithGenericVariant<string[]>(); | ||
new ClassWithGenericVariant<Vector2[]>(); | ||
new ClassWithGenericVariant<Vector3[]>(); | ||
new ClassWithGenericVariant<Color[]>(); | ||
new ClassWithGenericVariant<GodotObject[]>(); | ||
new ClassWithGenericVariant<StringName[]>(); | ||
new ClassWithGenericVariant<NodePath[]>(); | ||
new ClassWithGenericVariant<Rid[]>(); | ||
|
||
// This class fails because generic type is not Variant-compatible. | ||
//new ClassWithGenericVariant<object>(); | ||
} | ||
} | ||
|
||
public class ClassWithGenericVariant<[MustBeVariant] T> | ||
{ | ||
} | ||
|
||
public class MustBeVariantAnnotatedMethods | ||
{ | ||
[GenericTypeAttribute<string>()] | ||
public void MethodWithAttributeOk() | ||
{ | ||
} | ||
|
||
// This method definition fails because generic type is not Variant-compatible. | ||
/* | ||
[GenericTypeAttribute<object>()] | ||
public void MethodWithWrongAttribute() | ||
{ | ||
} | ||
*/ | ||
} | ||
|
||
[GenericTypeAttribute<string>()] | ||
public class ClassVariantAnnotated | ||
{ | ||
} | ||
|
||
// This class definition fails because generic type is not Variant-compatible. | ||
/* | ||
[GenericTypeAttribute<object>()] | ||
public class ClassNonVariantAnnotated | ||
{ | ||
} | ||
*/ | ||
|
||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] | ||
public class GenericTypeAttribute<[MustBeVariant] T> : Attribute | ||
{ | ||
} |
56 changes: 56 additions & 0 deletions
56
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Godot.SourceGenerators.Tests; | ||
|
||
public static class CSharpAnalyzerVerifier<TAnalyzer> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
public class Test : CSharpAnalyzerTest<TAnalyzer, XUnitVerifier> | ||
{ | ||
public Test() | ||
{ | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net60; | ||
|
||
SolutionTransforms.Add((Solution solution, ProjectId projectId) => | ||
{ | ||
Project project = | ||
solution.GetProject(projectId)!.AddMetadataReference(Constants.GodotSharpAssembly | ||
.CreateMetadataReference()); | ||
|
||
return project.Solution; | ||
}); | ||
} | ||
} | ||
|
||
public static Task Verify(string sources, params DiagnosticResult[] expected) | ||
{ | ||
return MakeVerifier(new string[] { sources }, expected).RunAsync(); | ||
} | ||
|
||
public static Test MakeVerifier(ICollection<string> sources, params DiagnosticResult[] expected) | ||
{ | ||
var verifier = new Test(); | ||
|
||
verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $""" | ||
is_global = true | ||
build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath} | ||
""")); | ||
|
||
verifier.TestState.Sources.AddRange(sources.Select(source => | ||
{ | ||
return (source, SourceText.From(File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source)))); | ||
})); | ||
|
||
verifier.ExpectedDiagnostics.AddRange(expected); | ||
return verifier; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/GlobalClassAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Xunit; | ||
|
||
namespace Godot.SourceGenerators.Tests; | ||
|
||
public class GlobalClassAnalyzerTests | ||
{ | ||
[Fact] | ||
public async void GlobalClassMustDeriveFromGodotObjectTest() | ||
{ | ||
const string GlobalClassGD0401 = "GlobalClass.GD0401.cs"; | ||
await CSharpAnalyzerVerifier<GlobalClassAnalyzer>.Verify(GlobalClassGD0401); | ||
} | ||
|
||
[Fact] | ||
public async void GlobalClassMustNotBeGenericTest() | ||
{ | ||
const string GlobalClassGD0402 = "GlobalClass.GD0402.cs"; | ||
await CSharpAnalyzerVerifier<GlobalClassAnalyzer>.Verify(GlobalClassGD0402); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/MustBeVariantAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Xunit; | ||
|
||
namespace Godot.SourceGenerators.Tests; | ||
|
||
public class MustBeVariantAnalyzerTests | ||
{ | ||
[Fact] | ||
public async void GenericTypeArgumentMustBeVariantTest() | ||
{ | ||
const string MustBeVariantGD0301 = "MustBeVariant.GD0301.cs"; | ||
await CSharpAnalyzerVerifier<MustBeVariantAnalyzer>.Verify(MustBeVariantGD0301); | ||
} | ||
|
||
[Fact] | ||
public async void GenericTypeParameterMustBeVariantAnnotatedTest() | ||
{ | ||
const string MustBeVariantGD0302 = "MustBeVariant.GD0302.cs"; | ||
await CSharpAnalyzerVerifier<MustBeVariantAnalyzer>.Verify(MustBeVariantGD0302); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/GlobalClass.GD0401.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Godot; | ||
|
||
// This works because it inherits from GodotObject. | ||
[GlobalClass] | ||
public partial class CustomGlobalClass1 : GodotObject | ||
{ | ||
|
||
} | ||
|
||
// This works because it inherits from an object that inherits from GodotObject | ||
[GlobalClass] | ||
public partial class CustomGlobalClass2 : Node | ||
{ | ||
|
||
} | ||
|
||
// This raises a GD0401 diagnostic error: global classes must inherit from GodotObject | ||
{|GD0401:[GlobalClass] | ||
public partial class CustomGlobalClass3 | ||
{ | ||
|
||
}|} |
15 changes: 15 additions & 0 deletions
15
.../editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/GlobalClass.GD0402.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Godot; | ||
|
||
// This works because it inherits from GodotObject and it doesn't have any generic type parameter. | ||
[GlobalClass] | ||
public partial class CustomGlobalClass : GodotObject | ||
{ | ||
|
||
} | ||
|
||
// This raises a GD0402 diagnostic error: global classes can't have any generic type parameter | ||
{|GD0402:[GlobalClass] | ||
public partial class CustomGlobalClass<T> : GodotObject | ||
{ | ||
|
||
}|} |
Oops, something went wrong.