Skip to content
Merged
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
11 changes: 9 additions & 2 deletions Dango.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@


Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dango", "src\Dango\Dango.csproj", "{0C1C7D4D-55C2-4083-BB84-8FA2DC171B0E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dango.Abstractions", "src\Dango.Abstractions\Dango.Abstractions.csproj", "{91D3200F-AAF2-4C9C-9EF1-5549799FAF68}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{1802D0C6-8F63-40F7-A865-03FC7526E6BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dango.Tests", "test\Dango.Tests\Dango.Tests.csproj", "{436B51E5-ACF9-4F52-866B-A4C99F5C01E4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dango.Tests.Utils", "test\Dango.Tests.Utils\Dango.Tests.Utils.csproj", "{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dango.Unit.Tests", "test\Dango.Unit.Tests\Dango.Unit.Tests.csproj", "{436B51E5-ACF9-4F52-866B-A4C99F5C01E4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -22,12 +24,17 @@ Global
{91D3200F-AAF2-4C9C-9EF1-5549799FAF68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91D3200F-AAF2-4C9C-9EF1-5549799FAF68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91D3200F-AAF2-4C9C-9EF1-5549799FAF68}.Release|Any CPU.Build.0 = Release|Any CPU
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Release|Any CPU.Build.0 = Release|Any CPU
{436B51E5-ACF9-4F52-866B-A4C99F5C01E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{436B51E5-ACF9-4F52-866B-A4C99F5C01E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{436B51E5-ACF9-4F52-866B-A4C99F5C01E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{436B51E5-ACF9-4F52-866B-A4C99F5C01E4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D} = {1802D0C6-8F63-40F7-A865-03FC7526E6BE}
{436B51E5-ACF9-4F52-866B-A4C99F5C01E4} = {1802D0C6-8F63-40F7-A865-03FC7526E6BE}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions test/Dango.Tests.Utils/Dango.Tests.Utils.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Dango\Dango.csproj" />
<ProjectReference Include="..\..\src\Dango.Abstractions\Dango.Abstractions.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions test/Dango.Tests.Utils/EnumDefinitionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text;

namespace Dango.Tests.Utils;

public static class EnumDefinitionBuilder
{
public static string BuildEnumDefinition(string enumName, int valueCount, string? namespaceName = null)
{
var values = Enumerable.Range(0, valueCount)
.Select(i => $"Value{i}")
.ToList();

return BuildEnumDefinitionWithValues(enumName, values, namespaceName);
}

public static string BuildEnumDefinitionWithValues(string enumName, IEnumerable<string> values, string? namespaceName = null)
{
var sb = new StringBuilder();

if (!string.IsNullOrEmpty(namespaceName))
{
sb.AppendLine($"namespace {namespaceName};");
sb.AppendLine();
}

sb.AppendLine($"public enum {enumName}");
sb.AppendLine("{");
foreach (var value in values)
{
sb.AppendLine($" {value},");
}
sb.AppendLine("}");

return sb.ToString();
}
}
47 changes: 47 additions & 0 deletions test/Dango.Tests.Utils/GeneratorTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Dango.Tests.Utils;

public static class GeneratorTestHelper
{
public static Compilation CreateCompilation(string source, params string[] additionalSources)
{
var syntaxTrees = new[] { source }
.Concat(additionalSources)
.Select(s => CSharpSyntaxTree.ParseText(s))
.ToArray();

var references = AppDomain
.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic && !string.IsNullOrWhiteSpace(a.Location))
.Select(a => MetadataReference.CreateFromFile(a.Location))
.Cast<MetadataReference>()
.ToList();

references.Add(
MetadataReference.CreateFromFile(
typeof(Abstractions.IDangoMapperRegistrar).Assembly.Location
)
);

return CSharpCompilation.Create(
"Test.Assembly",
syntaxTrees,
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
}

public static GeneratorDriverRunResult RunGenerator(string source, params string[] additionalSources)
{
var compilation = CreateCompilation(source, additionalSources);
var generator = new DangoGenerator();
var driver = CSharpGeneratorDriver.Create(generator);

driver = (CSharpGeneratorDriver)
driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out _);

return driver.GetRunResult();
}
}
88 changes: 88 additions & 0 deletions test/Dango.Tests.Utils/MappingRegistrarBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Text;

namespace Dango.Tests.Utils;

public static class MappingRegistrarBuilder
{
public static string BuildRegistrar(
IEnumerable<(string SourceEnum, string DestinationEnum, MappingConfig Config)> mappings,
string? namespaceName = null,
string registrarClassName = "MyRegistrar")
{
var namespaceDeclaration = string.IsNullOrEmpty(namespaceName)
? ""
: $"namespace {namespaceName};\n\n";

var sb = new StringBuilder();

sb.AppendLine("using Dango.Abstractions;");
sb.AppendLine("using System.Collections.Generic;");

if (!string.IsNullOrEmpty(namespaceName))
{
sb.AppendLine();
sb.AppendLine($"namespace {namespaceName};");
sb.AppendLine();
}

sb.AppendLine($"public class {registrarClassName} : IDangoMapperRegistrar");
sb.AppendLine("{");
sb.AppendLine(" public void Register(IDangoMapperRegistry registry)");
sb.AppendLine(" {");

foreach (var (sourceEnum, destinationEnum, config) in mappings)
{
sb.Append($" registry.Enum<{sourceEnum}, {destinationEnum}>()");

if (config.Strategy == MappingStrategy.ByValue)
{
sb.Append(".MapByValue()");
}
else if (config.Strategy == MappingStrategy.ByName)
{
sb.Append(".MapByName()");
}

if (config.DefaultValue != null)
{
sb.Append($".WithDefault({destinationEnum}.{config.DefaultValue})");
}

if (config.Overrides != null && config.Overrides.Count > 0)
{
sb.AppendLine(" .WithOverrides(new Dictionary<");
sb.Append($"{sourceEnum}, {destinationEnum}>");
sb.AppendLine(" {");

foreach (var (source, dest) in config.Overrides)
{
sb.AppendLine($" {{ {sourceEnum}.{source}, {destinationEnum}.{dest} }},");
}

sb.Append(" })");
}

sb.AppendLine(";");
}

sb.AppendLine(" }");
sb.AppendLine("}");

return sb.ToString();
}
}

public class MappingConfig
{
public MappingStrategy Strategy { get; set; } = MappingStrategy.ByName;

public string? DefaultValue { get; set; }

public Dictionary<string, string>? Overrides { get; set; }
}

public enum MappingStrategy
{
ByName,
ByValue
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
Expand All @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dango.Tests.Utils\Dango.Tests.Utils.csproj" />
<ProjectReference Include="..\..\src\Dango\Dango.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,11 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Dango.ErrorHandling;
using Dango.Tests.Utils;

namespace Dango.Tests;
namespace Dango.Unit.Tests;

[TestFixture]
public partial class DangoGeneratorTests
{
private static GeneratorDriverRunResult RunGenerator(
string source,
params string[] additionalSources
)
{
var syntaxTrees = new[] { source }
.Concat(additionalSources)
.Select(s => CSharpSyntaxTree.ParseText(s))
.ToArray();

var references = AppDomain
.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic && !string.IsNullOrWhiteSpace(a.Location))
.Select(a => MetadataReference.CreateFromFile(a.Location))
.Cast<MetadataReference>()
.ToList();

references.Add(
MetadataReference.CreateFromFile(
typeof(Abstractions.IDangoMapperRegistrar).Assembly.Location
)
);

var compilation = CSharpCompilation.Create(
"Test.Assembly",
syntaxTrees,
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);

var generator = new DangoGenerator();
var driver = CSharpGeneratorDriver.Create(generator);

driver = (CSharpGeneratorDriver)
driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out _);

return driver.GetRunResult();
}

[Test]
public void Generator_WithNoRegistrars_GeneratesNoOutputWithWarning()
{
Expand All @@ -57,7 +17,7 @@ public class MyClass
}
}";

var result = RunGenerator(source);
var result = GeneratorTestHelper.RunGenerator(source);

Assert.That(
result.Diagnostics.Single().Id,
Expand All @@ -83,7 +43,7 @@ public class MyRegistrar : IDangoMapperRegistrar
}
}";

var result = RunGenerator(source);
var result = GeneratorTestHelper.RunGenerator(source);

Assert.That(
result.Diagnostics.Single().Id,
Expand All @@ -109,7 +69,7 @@ public void Register(IDangoMapperRegistry registry)
}
}";

var result = RunGenerator(source);
var result = GeneratorTestHelper.RunGenerator(source);

Assert.That(result.Diagnostics, Is.Empty);
Assert.That(result.GeneratedTrees, Is.Empty);
Expand Down
Loading