Skip to content

Commit

Permalink
Add GqlPlus.Generators project, tests and refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonGraal committed Feb 14, 2025
1 parent 5b3f56d commit 8aa3ee3
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 52 deletions.
21 changes: 21 additions & 0 deletions GqlPlus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.TestGeneratorsTests
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.PolyFill", "src\GqlPlus.PolyFill\GqlPlus.PolyFill.csproj", "{44D55F97-4EBA-48C8-A72C-7FBC12A44719}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.Generators", "src\GqlPlus.Generators\GqlPlus.Generators.csproj", "{B5DD5781-B8AF-459C-9A70-7C2B8F36F2F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.GeneratorsTestBase", "test\GqlPlus.GeneratorsTestBase\GqlPlus.GeneratorsTestBase.csproj", "{0336A900-AA73-4B44-99BD-B70A5B2D14D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.GeneratorsTests", "test\GqlPlus.GeneratorsTests\GqlPlus.GeneratorsTests.csproj", "{6A04326B-1202-41DD-A4C0-B0DFB68E76A1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -130,6 +136,18 @@ Global
{44D55F97-4EBA-48C8-A72C-7FBC12A44719}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44D55F97-4EBA-48C8-A72C-7FBC12A44719}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44D55F97-4EBA-48C8-A72C-7FBC12A44719}.Release|Any CPU.Build.0 = Release|Any CPU
{B5DD5781-B8AF-459C-9A70-7C2B8F36F2F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5DD5781-B8AF-459C-9A70-7C2B8F36F2F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5DD5781-B8AF-459C-9A70-7C2B8F36F2F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5DD5781-B8AF-459C-9A70-7C2B8F36F2F0}.Release|Any CPU.Build.0 = Release|Any CPU
{0336A900-AA73-4B44-99BD-B70A5B2D14D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0336A900-AA73-4B44-99BD-B70A5B2D14D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0336A900-AA73-4B44-99BD-B70A5B2D14D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0336A900-AA73-4B44-99BD-B70A5B2D14D3}.Release|Any CPU.Build.0 = Release|Any CPU
{6A04326B-1202-41DD-A4C0-B0DFB68E76A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A04326B-1202-41DD-A4C0-B0DFB68E76A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A04326B-1202-41DD-A4C0-B0DFB68E76A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A04326B-1202-41DD-A4C0-B0DFB68E76A1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -150,6 +168,9 @@ Global
{B061A811-3DBB-4E5A-AD96-91F0A74FE3C2} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
{BB113C8C-1B2C-4815-A4DF-051F9237AF0E} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
{44D55F97-4EBA-48C8-A72C-7FBC12A44719} = {B834B2ED-0D27-4281-8523-B706B42DCC15}
{B5DD5781-B8AF-459C-9A70-7C2B8F36F2F0} = {B834B2ED-0D27-4281-8523-B706B42DCC15}
{0336A900-AA73-4B44-99BD-B70A5B2D14D3} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
{6A04326B-1202-41DD-A4C0-B0DFB68E76A1} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {148B651C-DFAA-434F-B1ED-51E535FC2DE8}
Expand Down
25 changes: 25 additions & 0 deletions src/GqlPlus.Generators/GqlModelGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;

namespace GqlPlus;

[Generator(LanguageNames.CSharp)]
public class GqlModelGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValueProvider<ImmutableArray<string>> samples = context.AdditionalTextsProvider
.Select((text, token) => text.Path)
.Where(path => Path.GetExtension(path).Equals(".graphql+", StringComparison.OrdinalIgnoreCase))
.Collect();

context.RegisterSourceOutput(samples, GenerateCode);
}

private void GenerateCode(SourceProductionContext context, ImmutableArray<string> array)
{
if (array.IsDefaultOrEmpty) {
return;
}
}
}
18 changes: 18 additions & 0 deletions src/GqlPlus.Generators/GqlPlus.Generators.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IncludeBuildOutput>false</IncludeBuildOutput>
<IsRoslynComponent>true</IsRoslynComponent>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions test/GqlPlus.GeneratorsTestBase/GqlPlus.GeneratorsTestBase.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
<PackageReference Include="Verify.Xunit" Version="28.10.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
using Microsoft.CodeAnalysis;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;

namespace GqlPlus;

public static class TestGeneratorsHelper
{
public static Task Verify(string source, params string[] additionalPaths)
public static GeneratorDriver Generate(this IIncrementalGenerator generator, string source, ImmutableArray<AdditionalText> additionalPaths)
{
// Parse the provided string into a C# syntax tree
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);

IEnumerable<PortableExecutableReference> references =
[MetadataReference.CreateFromFile(typeof(object).Assembly.Location)];

// Create a Roslyn compilation for the syntax tree.
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName: "Tests",
syntaxTrees: [syntaxTree],
references: references);

// Create an instance of our EnumGenerator incremental source generator
BuildDataGenerator generator = new();

// The GeneratorDriver is used to run our generator against a compilation
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator)
.AddAdditionalTexts([.. additionalPaths.Select(PathToText)]);

// Run the source generator!
driver = driver.RunGenerators(compilation);
.AddAdditionalTexts(additionalPaths);

// Use verify to snapshot test the source generator output!
return Verifier.Verify(driver);

static AdditionalText PathToText(string path)
=> new StringAdditionalText(path);
return driver.RunGenerators(compilation);
}

public static ImmutableArray<AdditionalText> AdditionalPaths(this IEnumerable<string> paths)
=> [.. paths.Select(static path => new StringAdditionalText(path))];

private sealed class StringAdditionalText(string path)
: AdditionalText
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
18 changes: 18 additions & 0 deletions test/GqlPlus.GeneratorsTests/GqlModelGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.CodeAnalysis;

namespace GqlPlus;

public class GqlModelGeneratorTests
{

[Fact]
public Task NoAdditionalFiles()
{
string source = "using GqlPlusTests;";

GeneratorDriver driver = new GqlModelGenerator()
.Generate(source, []);

return Verifier.Verify(driver);
}
}
12 changes: 12 additions & 0 deletions test/GqlPlus.GeneratorsTests/GqlPlus.GeneratorsTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Verify.SourceGenerators" Version="2.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\GqlPlus.Generators\GqlPlus.Generators.csproj" />
<ProjectReference Include="..\GqlPlus.GeneratorsTestBase\GqlPlus.GeneratorsTestBase.csproj" />
</ItemGroup>

</Project>
10 changes: 9 additions & 1 deletion test/GqlPlus.TestGenerators/BuildDataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ namespace GqlPlus;
[Generator(LanguageNames.CSharp)]
public class BuildDataGenerator : IIncrementalGenerator
{
private readonly string _namespace;

public BuildDataGenerator(string ns)
=> _namespace = ns;

public BuildDataGenerator()
=> _namespace = "GqlPlus";

public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValueProvider<ImmutableArray<string>> gitDetails = context.AdditionalTextsProvider
Expand Down Expand Up @@ -55,7 +63,7 @@ private void GenerateCode(SourceProductionContext context, (ImmutableArray<strin
builder.Append("// Collected from ");
builder.AppendLine(collected);
builder.AppendLine();
builder.AppendLine("namespace GqlPlus;");
builder.AppendLine($"namespace {_namespace};");

foreach (IGrouping<string, DataPath> directory in parent.GroupBy(g => g.Directory)) {
string className = parent.Key + directory.Key + "Data";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Generated from Sample
// Collected from Sample/git-details.txt

namespace GqlPlus;
namespace GqlPlusTests;

public class SampleFilesData
: TheoryData<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Generated from Sample/Files
// Collected from Sample/git-details.txt

namespace GqlPlus;
namespace GqlPlusTests;

public class FilesDeeperData
: TheoryData<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Generated from Sample
// Collected from Sample/git-details.txt

namespace GqlPlus;
namespace GqlPlusTests;

public class SampleFilesData
: TheoryData<string>
Expand Down

This file was deleted.

39 changes: 29 additions & 10 deletions test/GqlPlus.TestGeneratorsTests/BuildDataGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
namespace GqlPlus;
using Microsoft.CodeAnalysis;

namespace GqlPlus;

public class BuildDataGeneratorTests
{
private static readonly string[] s_additionalFilesWithIncorrectOne = [
"Sample/Files/File3.gql+",
"Sample/Files/file2.graphql+",
"Sample/Files/file1.txt",
"Sample/git-details.txt"];
private static readonly string[] s_additionalSubdirectoryFiles = [
"Sample/Files/File3.gql",
"Sample/More/file2.graphql+",
"Sample/Files/Deeper/file1.gql+",
"Sample/git-details.txt"];

[Fact]
public Task NoAdditionalFiles()
{
string source = "using GqlPlus;";
string source = "using GqlPlusTests;";

GeneratorDriver driver = new BuildDataGenerator("GqlPlusTests")
.Generate(source, []);

// Pass the source code to our helper and snapshot test the output
return TestGeneratorsHelper.Verify(source);
return Verifier.Verify(driver);
}

[Fact]
public Task IgnoresIncorrectAdditionalFiles()
{
string source = "using GqlPlus;";
string source = "using GqlPlusTests;";

// Pass the source code to our helper and snapshot test the output
return TestGeneratorsHelper.Verify(source, "Sample/Files/File3.gql+", "Sample/Files/file2.graphql+", "Sample/Files/file1.txt", "Sample/git-details.txt");
GeneratorDriver driver = new BuildDataGenerator("GqlPlusTests")
.Generate(source, s_additionalFilesWithIncorrectOne.AdditionalPaths());

return Verifier.Verify(driver);
}

[Fact]
public Task SubDirectoryAdditionalFiles()
{
string source = "using GqlPlus;";
string source = "using GqlPlusTests;";

GeneratorDriver driver = new BuildDataGenerator("GqlPlusTests")
.Generate(source, s_additionalSubdirectoryFiles.AdditionalPaths());

// Pass the source code to our helper and snapshot test the output
return TestGeneratorsHelper.Verify(source, "Sample/Files/File3.gql", "Sample/More/file2.graphql+", "Sample/Files/Deeper/file1.gql+", "Sample/git-details.txt");
return Verifier.Verify(driver);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
<PackageReference Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageReference Include="Verify.Xunit" Version="28.10.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GqlPlus.GeneratorsTestBase\GqlPlus.GeneratorsTestBase.csproj" />
<ProjectReference Include="..\GqlPlus.TestGenerators\GqlPlus.TestGenerators.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 8aa3ee3

Please sign in to comment.