| 1 |
using Microsoft.CodeAnalysis.Text; |
| 2 |
using System; |
| 3 |
using System.Collections.Generic; |
| 4 |
using System.IO; |
| 5 |
using System.Linq; |
| 6 |
using System.Text; |
| 7 |
using System.Threading.Tasks; |
| 8 |
using Xunit; |
| 9 |
|
| 10 |
namespace Godot.SourceGenerators.Tests; |
| 11 |
|
| 12 |
public class ScriptPathAttributeGeneratorTests |
| 13 |
{ |
| 14 |
private static (string, SourceText) MakeAssemblyScriptTypesGeneratedSource(ICollection<string> types) |
| 15 |
{ |
| 16 |
return ( |
| 17 |
Path.Combine("Godot.SourceGenerators", "Godot.SourceGenerators.ScriptPathAttributeGenerator", "AssemblyScriptTypes.generated.cs"), |
| 18 |
SourceText.From($$""" |
| 19 |
[assembly:Godot.AssemblyHasScriptsAttribute(new System.Type[] {{{string.Join(", ", types.Select(type => $"typeof({type})"))}}})] |
| 20 |
|
| 21 |
""", Encoding.UTF8) |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
[Fact] |
| 26 |
public async Task ScriptBoilerplate() |
| 27 |
{ |
| 28 |
var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier( |
| 29 |
new string[] { "ScriptBoilerplate.cs" }, |
| 30 |
new string[] { "ScriptBoilerplate_ScriptPath.generated.cs" } |
| 31 |
); |
| 32 |
verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::ScriptBoilerplate" })); |
| 33 |
await verifier.RunAsync(); |
| 34 |
} |
| 35 |
|
| 36 |
[Fact] |
| 37 |
public async Task FooBar() |
| 38 |
{ |
| 39 |
var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier( |
| 40 |
new string[] { "Foo.cs", "Bar.cs" }, |
| 41 |
new string[] { "Foo_ScriptPath.generated.cs", "Bar_ScriptPath.generated.cs" } |
| 42 |
); |
| 43 |
verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::Foo", "global::Bar" })); |
| 44 |
await verifier.RunAsync(); |
| 45 |
} |
| 46 |
|
| 47 |
[Fact] |
| 48 |
public async Task Generic() |
| 49 |
{ |
| 50 |
var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier( |
| 51 |
new string[] { "Generic.cs" }, |
| 52 |
new string[] { "Generic(Of T)_ScriptPath.generated.cs" } |
| 53 |
); |
| 54 |
verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::Generic<>" })); |
| 55 |
await verifier.RunAsync(); |
| 56 |
} |
| 57 |
|
| 58 |
[Fact] |
| 59 |
public async Task GenericMultipleClassesSameName() |
| 60 |
{ |
| 61 |
var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier( |
| 62 |
Array.Empty<string>(), |
| 63 |
new string[] { "Generic(Of T)_ScriptPath.generated.cs" } |
| 64 |
); |
| 65 |
verifier.TestState.Sources.Add(("Generic.cs", File.ReadAllText(Path.Combine(Constants.SourceFolderPath, "Generic.GD0003.cs")))); |
| 66 |
verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::Generic<>", "global::Generic<,>", "global::Generic" })); |
| 67 |
await verifier.RunAsync(); |
| 68 |
} |
| 69 |
|
| 70 |
[Fact] |
| 71 |
public async Task NamespaceMultipleClassesSameName() |
| 72 |
{ |
| 73 |
var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier( |
| 74 |
Array.Empty<string>(), |
| 75 |
new string[] { "NamespaceA.SameName_ScriptPath.generated.cs" } |
| 76 |
); |
| 77 |
verifier.TestState.Sources.Add(("SameName.cs", File.ReadAllText(Path.Combine(Constants.SourceFolderPath, "SameName.GD0003.cs")))); |
| 78 |
verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::NamespaceA.SameName", "global::NamespaceB.SameName" })); |
| 79 |
await verifier.RunAsync(); |
| 80 |
} |
| 81 |
} |