From 3f907b4c3421152d503a0ab13715826fffd12c3b Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Mon, 13 Nov 2023 20:17:43 +0100 Subject: [PATCH] Add UseStaticGenericMapperStaticMethod, IgnoreInstanceMethodFromStaticMapper --- ...InstantiableMapperWithStaticMethodsTest.cs | 57 +++++++++++++++++++ ...ethodFromStaticMapper#Mapper.g.verified.cs | 17 ++++++ ...ricMapperStaticMethod#Mapper.g.verified.cs | 11 ++++ 3 files changed, 85 insertions(+) create mode 100644 test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.IgnoreInstanceMethodFromStaticMapper#Mapper.g.verified.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.UseStaticGenericMapperStaticMethod#Mapper.g.verified.cs diff --git a/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs b/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs index b4101c2e8ab..bc448c3ce03 100644 --- a/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs @@ -126,6 +126,63 @@ class B { public string StringValue { get; set; } } return TestHelper.VerifyGenerator(source); } + [Fact] + public Task UseStaticGenericMapperStaticMethod() + { + var source = TestSourceBuilder.CSharp( + """ + using Riok.Mapperly.Abstractions; + + record A(AExternal Value); + record B(BExternal Value); + record AExternal(); + record BExternal(); + + class OtherMapper { + public static BExternal ToBExternal(AExternal source) => new BExternal(); + } + + [Mapper] + [UseStaticMapper] + public partial class Mapper + { + static partial B Map(A source); + } + """ + ); + + return TestHelper.VerifyGenerator(source); + } + + [Fact] + public Task IgnoreInstanceMethodFromStaticMapper() + { + var source = TestSourceBuilder.CSharp( + """ + using Riok.Mapperly.Abstractions; + using Riok.Mapperly.Abstractions.ReferenceHandling; + + record A(AExternal Value); + record B(BExternal Value); + record AExternal(); + record BExternal(); + + class OtherMapper { + public BExternal ToBExternal(AExternal source) => new BExternal(); + } + + [Mapper] + [UseStaticMapper] + public partial class Mapper + { + static partial B Map(A source); + } + """ + ); + + return TestHelper.VerifyGenerator(source); + } + [Fact] public void MixedStaticMethodWithPartialInstanceMethod() { diff --git a/test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.IgnoreInstanceMethodFromStaticMapper#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.IgnoreInstanceMethodFromStaticMapper#Mapper.g.verified.cs new file mode 100644 index 00000000000..890d6024f6c --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.IgnoreInstanceMethodFromStaticMapper#Mapper.g.verified.cs @@ -0,0 +1,17 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + static partial global::B Map(global::A source) + { + var target = new global::B(MapToBExternal(source.Value)); + return target; + } + + private static global::BExternal MapToBExternal(global::AExternal source) + { + var target = new global::BExternal(); + return target; + } +} diff --git a/test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.UseStaticGenericMapperStaticMethod#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.UseStaticGenericMapperStaticMethod#Mapper.g.verified.cs new file mode 100644 index 00000000000..a8b59243f46 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/InstantiableMapperWithStaticMethodsTest.UseStaticGenericMapperStaticMethod#Mapper.g.verified.cs @@ -0,0 +1,11 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + static partial global::B Map(global::A source) + { + var target = new global::B(global::OtherMapper.ToBExternal(source.Value)); + return target; + } +}