Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.NET] Add tests for GodotDisabledSourceGenerators #88687

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public static Test MakeVerifier(ICollection<string> sources, ICollection<string>
{
var verifier = new Test();

verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $"""
is_global = true
build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath}
"""));
verifier.TestState.AddGlobalConfig(new Dictionary<string, string>()
{
{ "is_global", "true" },
{ "build_property.GodotProjectDir", Constants.ExecutingAssemblyPath }
});

verifier.TestState.Sources.AddRange(sources.Select(source => (
source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Godot.SourceGenerators.Tests;

public static class Constants
{
public const string GlobalConfigPath = "/.globalconfig";

public static Assembly GodotSharpAssembly => typeof(GodotObject).Assembly;

public static string ExecutingAssemblyPath { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;

namespace Godot.SourceGenerators.Tests;

Expand All @@ -9,4 +13,24 @@ public static MetadataReference CreateMetadataReference(this Assembly assembly)
{
return MetadataReference.CreateFromFile(assembly.Location);
}

public static void AddGlobalConfig(this SolutionState state, Dictionary<string, string> config)
{
var rawContent = new StringBuilder();
var index = state.AnalyzerConfigFiles.FindIndex(((string Name, SourceText) file) => file.Name == Constants.GlobalConfigPath);
if (index >= 0)
{
rawContent.AppendLine(state.AnalyzerConfigFiles[index].content.ToString());
state.AnalyzerConfigFiles.RemoveAt(index);
}

foreach (var (key, value) in config)
{
rawContent.Append(key);
rawContent.Append(" = ");
rawContent.AppendLine(value);
}

state.AnalyzerConfigFiles.Add((Constants.GlobalConfigPath, rawContent.ToString()));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
using System;
using Xunit;

namespace Godot.SourceGenerators.Tests;

public class ScriptMethodsGeneratorTests
{
[Fact]
public async void DisableGenerator()
{
var verifier = CSharpSourceGeneratorVerifier<ScriptMethodsGenerator>.MakeVerifier(
new string[] { "ScriptBoilerplate.cs" },
Array.Empty<string>()
);
verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptMethods"));
await verifier.RunAsync();
}

[Fact]
public async void Methods()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ private static (string, SourceText) MakeAssemblyScriptTypesGeneratedSource(IColl
);
}

[Fact]
private async void DisableGenerator()
{
var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier(
new string[] { "ScriptBoilerplate.cs" },
Array.Empty<string>()
);
verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptPathAttribute"));
await verifier.RunAsync();
}

// TODO: Remove the skip when the upstream issue is resolved, https://github.com/dotnet/roslyn/pull/51625
[Fact(Skip = "This is currently failing due to an upstream issue with ';' in the editorconfig file format.")]
private async void DisableGeneratorButNotFirst()
{
var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier(
new string[] { "ScriptBoilerplate.cs" },
Array.Empty<string>()
);
verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("SomePlaceholder", "ScriptPathAttribute"));
await verifier.RunAsync();
}
paulloz marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public async void ScriptBoilerplate()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
using System;
using Xunit;

namespace Godot.SourceGenerators.Tests;

public class ScriptPropertiesGeneratorTests
{
[Fact]
public async void DisableGenerator()
{
var verifier = CSharpSourceGeneratorVerifier<ScriptPropertiesGenerator>.MakeVerifier(
new string[] { "ScriptBoilerplate.cs" },
Array.Empty<string>()
);
verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptProperties"));
await verifier.RunAsync();
}

[Fact]
public async void ExportedFields()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
using System;
using Xunit;

namespace Godot.SourceGenerators.Tests;

public class ScriptPropertyDefValGeneratorTests
{
[Fact]
public async void DisableGenerator()
{
var verifier = CSharpSourceGeneratorVerifier<ScriptPropertyDefValGenerator>.MakeVerifier(
new string[] { "ExportedFields.cs", "ExportedProperties.cs" },
Array.Empty<string>()
);
verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptPropertyDefVal"));
await verifier.RunAsync();
}

[Fact]
public async void ExportedFields()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
using System;
using Xunit;

namespace Godot.SourceGenerators.Tests;

public class ScriptSerializationGeneratorTests
{
[Fact]
public async void DisableGenerator()
{
var verifier = CSharpSourceGeneratorVerifier<ScriptSerializationGenerator>.MakeVerifier(
new string[] { "ScriptBoilerplate.cs" },
Array.Empty<string>()
);
verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptSerialization"));
await verifier.RunAsync();
}

[Fact]
public async void ScriptBoilerplate()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
using System;
using Xunit;

namespace Godot.SourceGenerators.Tests;

public class ScriptSignalsGeneratorTests
{
[Fact]
public async void DisableGenerator()
{
var verifier = CSharpSourceGeneratorVerifier<ScriptSignalsGenerator>.MakeVerifier(
new string[] { "SimpleEventSignals.cs" },
paulloz marked this conversation as resolved.
Show resolved Hide resolved
Array.Empty<string>()
);
verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptSignals"));
await verifier.RunAsync();
}

[Fact]
public async void EventSignals()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Godot;

public partial class EventSignals : GodotObject
{
[Signal]
public delegate void MySignalEventHandler(string str, int num);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace Godot.SourceGenerators.Tests;

public static class Utils
{
public static Dictionary<string, string> DisabledGenerators(params string[] generators)
{
return new Dictionary<string, string>()
{
{ "build_property.GodotDisabledSourceGenerators", string.Join(";", generators) }
};
}
}
Loading