Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Nov 9, 2024
1 parent 559952a commit 62fe728
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
16 changes: 13 additions & 3 deletions FartingUnicorn.Tests/FartingUnicorn.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<!-- https://andrewlock.net/creating-a-source-generator-part-6-saving-source-generator-output-in-source-control/ -->
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<!-- https://andrewlock.net/creating-a-source-generator-part-6-saving-source-generator-output-in-source-control/ -->
<Compile Remove="$(CompilerGeneratedFilesOutputPath)\**\*.cs"></Compile>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FartingUnicorn\FartingUnicorn.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FartingUnicorn\FartingUnicorn.csproj" />
<ProjectReference Include="..\MapperGenerator\MapperGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace DotNetThoughts.FartingUnicorn
{
[System.AttributeUsage(System.AttributeTargets.Class)]
public class CreateMapperAttribute : System.Attribute
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using DotNetThoughts.Results;
using System.Text.Json;

namespace FartingUnicorn.Generated;
public static partial class Mappers
{
public static Result<FartingUnicorn.Tests.SingleField.ReferenceType.NonNullableNonOptional_Tests.BlogPost> MapToBlogPost(JsonElement jsonElement)
{
var result = new FartingUnicorn.Tests.SingleField.ReferenceType.NonNullableNonOptional_Tests.BlogPost();
return Result<FartingUnicorn.Tests.SingleField.ReferenceType.NonNullableNonOptional_Tests.BlogPost>.Ok(result);
}
}
52 changes: 31 additions & 21 deletions FartingUnicorn.Tests/SingleField.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
using FluentAssertions;
using DotNetThoughts.FartingUnicorn;
using DotNetThoughts.Results;

using FluentAssertions;

using System.Text.Json;

using Xunit;

using static FartingUnicorn.Mapper;

using FartingUnicorn.Generated;
namespace FartingUnicorn.Tests;

public class SingleField
{
public class ReferenceType
{
public class NonNullableNonOptional_Tests

{
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, null, null))],
[Mappers.MapToBlogPost]
];

[CreateMapper]
public class BlogPost
{
/// <summary>
Expand All @@ -24,15 +33,16 @@ public class BlogPost
public string Title { get; set; }
}

[Fact]
public void ValidSingleField()
[Theory]
[MemberData(nameof(GetMappers))]
public void ValidSingleField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"Title": "Farting Unicorns"
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);
{
"Title": "Farting Unicorns"
}
""");
var blogPost = map(jsonElement);

Assert.True(blogPost.Success);
Assert.Equal("Farting Unicorns", blogPost.Value.Title);
Expand All @@ -42,9 +52,9 @@ public void ValidSingleField()
public void MissingNonNullableField()
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
}
""");
{
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);

Assert.False(blogPost.Success);
Expand All @@ -57,10 +67,10 @@ public void MissingNonNullableField()
public void NulledNonNullableField()
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"Title": null
}
""");
{
"Title": null
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);

Assert.False(blogPost.Success);
Expand All @@ -73,10 +83,10 @@ public void NulledNonNullableField()
public void InvalidFieldType()
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"Title": 123456
}
""");
{
"Title": 123456
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);

Assert.False(blogPost.Success);
Expand Down
18 changes: 10 additions & 8 deletions MapperGenerator/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ public class MapperGenerator : IIncrementalGenerator
{
public static class SourceGenerationHelper
{
public const string Attribute = @"
namespace DotNetThoughts.FartingUnicorn
{
[System.AttributeUsage(System.AttributeTargets.Class)]
public class CreateMapperAttribute : System.Attribute
{
}
}";
public const string Attribute = """
namespace DotNetThoughts.FartingUnicorn
{
[System.AttributeUsage(System.AttributeTargets.Class)]
public class CreateMapperAttribute : System.Attribute
{
}
}
""";
public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classToGenerateMapperFor)
{
var sb = new SourceBuilder();
sb.AppendLine("using DotNetThoughts.Results;");
sb.AppendLine("using System.Text.Json;");
sb.AppendLine();
sb.AppendLine("namespace FartingUnicorn.Generated;");
sb.AppendLine();
sb.AppendLine($"public static partial class Mappers");
sb.AppendLine("{");
using (var _ = sb.Indent())
Expand Down

0 comments on commit 62fe728

Please sign in to comment.