-
Notifications
You must be signed in to change notification settings - Fork 0
chore(tests): Extract test utils to a new test project for reusability #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.