Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Nov 10, 2024
1 parent d00de3f commit ffa1ea3
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using DotNetThoughts.Results;
using System.Text.Json;

namespace FartingUnicorn.Generated;

public static partial class Mappers
{
public static Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_BoolType_NonNullableOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.BoolType.NonNullableOptional_Tests.BlogPost();

List<IError> errors = new();
var isIsDraftPropertyDefined = jsonElement.TryGetProperty("IsDraft", out var jsonIsDraftProperty);
if (isIsDraftPropertyDefined)
{
// type = Boolean, isOption = True, isNullable = False
if (jsonIsDraftProperty.ValueKind == JsonValueKind.Null)
{
obj.IsDraft = new None<Boolean>();
}
else if (jsonIsDraftProperty.ValueKind == JsonValueKind.True || jsonIsDraftProperty.ValueKind == JsonValueKind.False)
{
obj.IsDraft = new Some<bool>(jsonIsDraftProperty.GetBoolean());
}
else
{
errors.Add(new ValueHasWrongTypeError([.. path, "IsDraft"], "Boolean", jsonIsDraftProperty.ValueKind.ToString()));
}
}
else
{
errors.Add(new RequiredPropertyMissingError([.. path, "IsDraft"]));
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using DotNetThoughts.Results;
using System.Text.Json;

namespace FartingUnicorn.Generated;

public static partial class Mappers
{
public static Result<FartingUnicorn.Tests.SingleField.BoolType.NullableOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_BoolType_NullableOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NullableOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.BoolType.NullableOptional_Tests.BlogPost();

List<IError> errors = new();
var isIsDraftPropertyDefined = jsonElement.TryGetProperty("IsDraft", out var jsonIsDraftProperty);
if (isIsDraftPropertyDefined)
{
// type = Boolean, isOption = True, isNullable = True
if (jsonIsDraftProperty.ValueKind == JsonValueKind.Null)
{
obj.IsDraft = new None<Boolean>();
}
else if (jsonIsDraftProperty.ValueKind == JsonValueKind.True || jsonIsDraftProperty.ValueKind == JsonValueKind.False)
{
obj.IsDraft = new Some<bool>(jsonIsDraftProperty.GetBoolean());
}
else
{
errors.Add(new ValueHasWrongTypeError([.. path, "IsDraft"], "Boolean", jsonIsDraftProperty.ValueKind.ToString()));
}
}
else
{
obj.IsDraft = null;
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NullableOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NullableOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
103 changes: 62 additions & 41 deletions FartingUnicorn.Tests/SingleField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,21 +422,23 @@ public void ValidSingleField(Func<JsonElement, Result<BlogPost>> map)
blogPost.Value.IsDraft.Should().BeTrue();
}

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

Assert.True(blogPost.Success);
blogPost.Value.IsDraft.Should().BeNull();
}

[Fact]
public void NulledNullableField()
[Theory]
[MemberData(nameof(GetMappers))]
public void NulledNullableField(Func<JsonElement, Result<BlogPost>> map)
{
// Might seem counterintuitive, but remember, we said that
// clr-null should reflect a missing field. In this case, the field exists, but does not have a value.
Expand All @@ -448,7 +450,7 @@ public void NulledNullableField()
"IsDraft": null
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);
var blogPost = map(jsonElement);

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
Expand All @@ -459,6 +461,12 @@ public void NulledNullableField()

public class NonNullableOptional_Tests
{
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, null, null))],
[(Func<JsonElement, Result<BlogPost>>)(x => FartingUnicorn.Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_BoolType_NonNullableOptional_Tests_BlogPost(x, null))]
];
[CreateMapper]
public class BlogPost
{
/// <summary>
Expand All @@ -468,46 +476,49 @@ public class BlogPost
public Option<bool> IsDraft { get; set; }
}

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

Assert.True(blogPost.Success);
blogPost.Value.IsDraft.Should().BeOfType<Some<bool>>();
var someIsDraft = (blogPost.Value.IsDraft as Some<bool>)!;
someIsDraft.Value.Should().BeTrue();
}

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

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
blogPost.Errors.Single().Should().BeOfType<RequiredPropertyMissingError>();
blogPost.Errors.Single().Message.Should().Be("$.IsDraft is required");
}

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

Assert.True(blogPost.Success);
blogPost.Value.IsDraft.Should().BeOfType<None<bool>>();
Expand All @@ -516,6 +527,13 @@ public void NulledOptionalField()

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

[CreateMapper]
public class BlogPost
{
/// <summary>
Expand All @@ -525,44 +543,47 @@ public class BlogPost
public Option<bool>? IsDraft { get; set; }
}

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

blogPost.Should().BeSuccessful();
blogPost.Value.IsDraft.Should().BeOfType<Some<bool>>();
var someIsDraft = (blogPost.Value.IsDraft as Some<bool>)!;
someIsDraft.Value.Should().BeTrue();
}

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

blogPost.Should().BeSuccessful();
blogPost.Value.IsDraft.Should().BeNull();
}

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

blogPost.Should().BeSuccessful();
blogPost.Value.IsDraft.Should().BeOfType<None<bool>>();
Expand Down

0 comments on commit ffa1ea3

Please sign in to comment.