Skip to content

Commit

Permalink
Merge pull request #641 from SteveDunn/bug-639-implicit-operators-ski…
Browse files Browse the repository at this point in the history
…p-normalization

Implicit casts from the primitive now call the `NormalizeInput` method if one is provided
  • Loading branch information
SteveDunn authored Jul 16, 2024
2 parents e48a8ef + 917ca51 commit fc580aa
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/Vogen/GenerateCastingOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,25 @@ public static string GenerateImplementations(VoWorkItem item, TypeDeclarationSyn

if (item.Config.FromPrimitiveCasting == CastOperator.Implicit)
{
sb.AppendLine(
$$"""
public static implicit operator {{className}}({{itemUnderlyingType}} value) {
return new {{className}}(value);
}
""");
if (item.NormalizeInputMethod is not null)
{
sb.AppendLine(
$$"""
public static implicit operator {{className}}({{itemUnderlyingType}} value) {
return new {{className}}({{className}}.NormalizeInput(value));
}
""");

}
else
{
sb.AppendLine(
$$"""
public static implicit operator {{className}}({{itemUnderlyingType}} value) {
return new {{className}}(value);
}
""");
}
}

if (sb.Length == 0)
Expand Down
36 changes: 36 additions & 0 deletions tests/ConsumerTests/BugFix624.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using SystemTextJsonSerializer = System.Text.Json.JsonSerializer;

namespace ConsumerTests.BugFixTests.BugFix639;

[ValueObject<string>(fromPrimitiveCasting: CastOperator.Implicit)]
public partial class C_With
{
private static string NormalizeInput(string input) => input.ToUpper();
}

[ValueObject<string>(fromPrimitiveCasting: CastOperator.Implicit)]
public partial class C_Without;

/// <summary>
/// Fixes bug https://github.com/SteveDunn/Vogen/issues/639 where any
/// `NormalizeInput` method was not called when implicitly converting a primitive to a value object
/// </summary>
public class Tests
{
[Fact]
public void Should_call_if_present()
{
C_With vo = "abc";
vo.Value.Should().Be("ABC");
}

[Fact]
public void Should_not_call_if_not_present()
{
C_Without vo = "abc";
vo.Value.Should().Be("abc");
}
}
16 changes: 16 additions & 0 deletions tests/ConsumerTests/Casting/ForClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ public void Implicit_both_ways()
Class_implicit_both_ways vo2 = prim;
vo2.Should().Be(vo);
}

[Fact]
public void Implicit_both_ways_with_normalization()
{
using var _ = new AssertionScope();

var vo = Class_implicit_both_ways_with_normalization.From("abc");

string prim = vo;

prim.Should().Be(vo.Value);

Class_implicit_both_ways_with_normalization vo2 = prim;
vo2.Should().Be(vo);
vo2.Value.Should().Be("ABC");
}
}
17 changes: 17 additions & 0 deletions tests/ConsumerTests/Casting/ForStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,21 @@ public void Implicit_both_ways()
Struct_implicit_both_ways vo2 = prim;
vo2.Should().Be(vo);
}

[Fact]
public void Implicit_both_ways_with_normalization()
{
using var _ = new AssertionScope();

var vo = Struct_implicit_both_ways_with_normalization.From("abc");

string prim = vo;

prim.Should().Be(vo.Value);

Struct_implicit_both_ways_with_normalization vo2 = prim;
vo2.Should().Be(vo);
vo2.Value.Should().Be("ABC");
}

}
12 changes: 12 additions & 0 deletions tests/ConsumerTests/Casting/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public partial class Class_implicit_both_ways
{
}

[ValueObject(typeof(string), toPrimitiveCasting: CastOperator.Implicit, fromPrimitiveCasting: CastOperator.Implicit)]
public partial class Class_implicit_both_ways_with_normalization
{
private static string NormalizeInput(string input) => input.ToUpper();
}

[ValueObject<string>(toPrimitiveCasting: CastOperator.None, fromPrimitiveCasting: CastOperator.None)]
public partial class Class_default_generic
{
Expand All @@ -35,6 +41,12 @@ public partial class Struct_implicit_both_ways
{
}

[ValueObject(typeof(string), toPrimitiveCasting: CastOperator.Implicit, fromPrimitiveCasting: CastOperator.Implicit)]
public partial class Struct_implicit_both_ways_with_normalization
{
private static string NormalizeInput(string input) => input.ToUpper();
}

[ValueObject<string>(toPrimitiveCasting: CastOperator.None, fromPrimitiveCasting: CastOperator.None)]
public partial class Struct_default_generic
{
Expand Down

0 comments on commit fc580aa

Please sign in to comment.