ScriptPathAttributeGeneratorTests.cs 3.3 KB

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