ScriptPathAttributeGeneratorTests.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.CodeAnalysis.Text;
  6. using Xunit;
  7. namespace Godot.SourceGenerators.Tests;
  8. public class ScriptPathAttributeGeneratorTests
  9. {
  10. private static (string, SourceText) MakeAssemblyScriptTypesGeneratedSource(ICollection<string> types)
  11. {
  12. return (
  13. Path.Combine("Godot.SourceGenerators", "Godot.SourceGenerators.ScriptPathAttributeGenerator", "AssemblyScriptTypes.generated.cs"),
  14. SourceText.From($$"""
  15. [assembly:Godot.AssemblyHasScriptsAttribute(new System.Type[] {{{string.Join(", ", types.Select(type => $"typeof({type})"))}}})]
  16. """, Encoding.UTF8)
  17. );
  18. }
  19. [Fact]
  20. public async void ScriptBoilerplate()
  21. {
  22. var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier(
  23. new string[] { "ScriptBoilerplate.cs" },
  24. new string[] { "ScriptBoilerplate_ScriptPath.generated.cs" }
  25. );
  26. verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::ScriptBoilerplate" }));
  27. await verifier.RunAsync();
  28. }
  29. [Fact]
  30. public async void FooBar()
  31. {
  32. var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier(
  33. new string[] { "Foo.cs", "Bar.cs" },
  34. new string[] { "Foo_ScriptPath.generated.cs", "Bar_ScriptPath.generated.cs" }
  35. );
  36. verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::Foo", "global::Bar" }));
  37. await verifier.RunAsync();
  38. }
  39. [Fact]
  40. public async void Generic()
  41. {
  42. var verifier = CSharpSourceGeneratorVerifier<ScriptPathAttributeGenerator>.MakeVerifier(
  43. new string[] { "Generic.cs" },
  44. new string[] { "Generic_ScriptPath.generated.cs" }
  45. );
  46. verifier.TestState.GeneratedSources.Add(MakeAssemblyScriptTypesGeneratedSource(new string[] { "global::Generic" }));
  47. await verifier.RunAsync();
  48. }
  49. }