From 9e5ca4ff1bbee116741ba34c52381569ab9cbb99 Mon Sep 17 00:00:00 2001 From: Timothy Makkison Date: Sun, 19 Nov 2023 17:48:16 +0000 Subject: [PATCH] feat: add tests --- .../Mapping/DerivedExistingTargetTypeTest.cs | 90 +------------------ ...icInterfaceShouldWork#Mapper.g.verified.cs | 22 +++++ ...ctBaseClassShouldWork#Mapper.g.verified.cs | 22 +++++ ...eTypeConfigShouldWork#Mapper.g.verified.cs | 22 +++++ ...thInterfaceShouldWork#Mapper.g.verified.cs | 22 +++++ ...getNullableShouldWork#Mapper.g.verified.cs | 24 +++++ ...rceNullableShouldWork#Mapper.g.verified.cs | 24 +++++ 7 files changed, 140 insertions(+), 86 deletions(-) create mode 100644 test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassAndNonGenericInterfaceShouldWork#Mapper.g.verified.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassShouldWork#Mapper.g.verified.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithBaseTypeConfigShouldWork#Mapper.g.verified.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceShouldWork#Mapper.g.verified.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceAndTargetNullableShouldWork#Mapper.g.verified.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceNullableShouldWork#Mapper.g.verified.cs diff --git a/test/Riok.Mapperly.Tests/Mapping/DerivedExistingTargetTypeTest.cs b/test/Riok.Mapperly.Tests/Mapping/DerivedExistingTargetTypeTest.cs index 9d28e626f1..79d4fdf330 100644 --- a/test/Riok.Mapperly.Tests/Mapping/DerivedExistingTargetTypeTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/DerivedExistingTargetTypeTest.cs @@ -100,20 +100,6 @@ public Task WithInterfaceSourceAndTargetNullableShouldWork() return TestHelper.VerifyGenerator(source); } - [Fact] - public Task WithObjectShouldWork() - { - var source = TestSourceBuilder.MapperWithBody( - """ - [MapDerivedType] - [MapDerivedType] - [MapDerivedType, IEnumerable>] - public partial Map(object src, B trg); - """ - ); - return TestHelper.VerifyGenerator(source); - } - [Fact] public void NotAssignableTargetTypeShouldDiagnostic() { @@ -137,50 +123,6 @@ public void NotAssignableTargetTypeShouldDiagnostic() .HaveAssertedAllDiagnostics(); } - [Fact] - public Task WithEnumerableOfInterfaceShouldWork() - { - var source = TestSourceBuilder.MapperWithBodyAndTypes( - """ - public partial IEnumerable Map(IEnumerable source); - - [MapDerivedType] - [MapDerivedType] - private partial B Map(A src); - """, - "interface A { string BaseValue { get; set; } }", - "interface B { string BaseValue { get; set; }}", - "class AImpl1 : A { public string BaseValue { get; set; } public string Value1 { get; set; } }", - "class AImpl2 : A { public string BaseValue { get; set; } public string Value2 { get; set; } }", - "class BImpl1 : B { public string BaseValue { get; set; } public string Value1 { get; set; } }", - "class BImpl2 : B { public string BaseValue { get; set; } public string Value2 { get; set; } }" - ); - return TestHelper.VerifyGenerator(source); - } - - [Fact] - public Task WithInterfacePropertyShouldWork() - { - var source = TestSourceBuilder.MapperWithBodyAndTypes( - """ - public partial B Map(A source); - - [MapDerivedType] - [MapDerivedType] - private partial BIntf Map(AIntf src); - """, - "class A { public AIntf Value { get; set; } }", - "class B { public BIntf Value { get; set; } }", - "interface AIntf { string BaseValue { get; set; } }", - "interface BIntf { string BaseValue { get; set; }}", - "class AImpl1 : AIntf { public string BaseValue { get; set; } public string Value1 { get; set; } }", - "class AImpl2 : AIntf { public string BaseValue { get; set; } public string Value2 { get; set; } }", - "class BImpl1 : BIntf { public string BaseValue { get; set; } public string Value1 { get; set; } }", - "class BImpl2 : BIntf { public string BaseValue { get; set; } public string Value2 { get; set; } }" - ); - return TestHelper.VerifyGenerator(source); - } - [Fact] public Task WithBaseTypeConfigShouldWork() { @@ -189,31 +131,7 @@ public Task WithBaseTypeConfigShouldWork() [MapDerivedType] [MapDerivedType] [MapProperty(nameof(A.BaseValueA), nameof(B.BaseValueB)] - public partial B Map(A src); - """, - "abstract class A { public string BaseValueA { get; set; } }", - "abstract class B { public string BaseValueB { get; set; } }", - "class ASubType1 : A { public string Value1 { get; set; } }", - "class ASubType2 : A { public string Value2 { get; set; } }", - "class BSubType1 : B { public string Value1 { get; set; } }", - "class BSubType2 : B { public string Value2 { get; set; } }" - ); - return TestHelper.VerifyGenerator(source); - } - - [Fact] - public Task WithBaseTypeConfigAndSeparateMethodShouldWork() - { - var source = TestSourceBuilder.MapperWithBodyAndTypes( - """ - [MapDerivedType] - [MapDerivedType] - [MapProperty(nameof(A.BaseValueA), nameof(B.BaseValueB)] - public partial B Map(A src); - - [MapperIgnoreSource(nameof(A.BaseValueA)] - [MapperIgnoreTarget(nameof(B.BaseValueB)] - public partial BSubType1 Map(ASubType1 src); + public partial void Map(A src, B trg); """, "abstract class A { public string BaseValueA { get; set; } }", "abstract class B { public string BaseValueB { get; set; } }", @@ -231,7 +149,7 @@ public void NotAssignableSourceTypeShouldDiagnostic() var source = TestSourceBuilder.MapperWithBodyAndTypes( """ [MapDerivedType] - public partial B Map(A src); + public partial void Map(A src, B trg); """, "interface A {}", "interface B {}", @@ -255,7 +173,7 @@ public void DuplicatedSourceTypeShouldDiagnostic() """ [MapDerivedType] [MapDerivedType] - public partial B Map(A src); + public partial void Map(A src, B trg); """, "interface A {}", "interface B {}", @@ -279,7 +197,7 @@ public void NotMappableShouldDiagnostic() var source = TestSourceBuilder.MapperWithBody( """ [MapDerivedType] - public partial object Map(object src); + public partial void Map(object src, object trg); """ ); TestHelper diff --git a/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassAndNonGenericInterfaceShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassAndNonGenericInterfaceShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..c5a1463bf0 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassAndNonGenericInterfaceShouldWork#Mapper.g.verified.cs @@ -0,0 +1,22 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + public partial void Map(global::A src, global::B trg) + { + switch (src, trg) + { + case (global::ASubType1 source, global::BSubType1 target): + target.Value1 = source.Value1; + target.BaseValue = source.BaseValue; + break; + case (global::ASubType2 source, global::BSubType2 target): + target.Value2 = source.Value2; + target.BaseValue = source.BaseValue; + break; + default: + throw new System.ArgumentException($"Cannot map {src.GetType()} to {trg.GetType()} as there is no known derived type mapping", nameof(src)); + } + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..ad567c88d3 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithAbstractBaseClassShouldWork#Mapper.g.verified.cs @@ -0,0 +1,22 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + public partial void Map(global::A src, global::B trg) + { + switch (src, trg) + { + case (global::ASubType1 source, global::BSubType1 target): + target.Value1 = source.Value1; + target.BaseValue = source.BaseValue; + break; + case (global::ASubType2 source, global::BSubType2 target): + target.Value1 = source.Value1; + target.BaseValue = source.BaseValue; + break; + default: + throw new System.ArgumentException($"Cannot map {src.GetType()} to {trg.GetType()} as there is no known derived type mapping", nameof(src)); + } + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithBaseTypeConfigShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithBaseTypeConfigShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..02bec65def --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithBaseTypeConfigShouldWork#Mapper.g.verified.cs @@ -0,0 +1,22 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + public partial void Map(global::A src, global::B trg) + { + switch (src, trg) + { + case (global::ASubType1 source, global::BSubType1 target): + target.Value1 = source.Value1; + target.BaseValueB = source.BaseValueA; + break; + case (global::ASubType2 source, global::BSubType2 target): + target.Value2 = source.Value2; + target.BaseValueB = source.BaseValueA; + break; + default: + throw new System.ArgumentException($"Cannot map {src.GetType()} to {trg.GetType()} as there is no known derived type mapping", nameof(src)); + } + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..8b23fa146f --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceShouldWork#Mapper.g.verified.cs @@ -0,0 +1,22 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + public partial void Map(global::A src, global::B trg) + { + switch (src, trg) + { + case (global::AImpl1 source, global::BImpl1 target): + target.BaseValue = source.BaseValue; + target.Value1 = source.Value1; + break; + case (global::AImpl2 source, global::BImpl2 target): + target.BaseValue = source.BaseValue; + target.Value2 = source.Value2; + break; + default: + throw new System.ArgumentException($"Cannot map {src.GetType()} to {trg.GetType()} as there is no known derived type mapping", nameof(src)); + } + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceAndTargetNullableShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceAndTargetNullableShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..c1f830d413 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceAndTargetNullableShouldWork#Mapper.g.verified.cs @@ -0,0 +1,24 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + public partial void Map(global::A? src, global::B? trg) + { + if (src == null || trg == null) + return; + switch (src, trg) + { + case (global::AImpl1 source, global::BImpl1 target): + target.BaseValue = source.BaseValue; + target.Value1 = source.Value1; + break; + case (global::AImpl2 source, global::BImpl2 target): + target.BaseValue = source.BaseValue; + target.Value2 = source.Value2; + break; + default: + throw new System.ArgumentException($"Cannot map {src.GetType()} to {trg.GetType()} as there is no known derived type mapping", nameof(src)); + } + } +} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceNullableShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceNullableShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..42f847da81 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/DerivedExistingTargetTypeTest.WithInterfaceSourceNullableShouldWork#Mapper.g.verified.cs @@ -0,0 +1,24 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + public partial void Map(global::A? src, global::B trg) + { + if (src == null) + return; + switch (src, trg) + { + case (global::AImpl1 source, global::BImpl1 target): + target.BaseValue = source.BaseValue; + target.Value1 = source.Value1; + break; + case (global::AImpl2 source, global::BImpl2 target): + target.BaseValue = source.BaseValue; + target.Value2 = source.Value2; + break; + default: + throw new System.ArgumentException($"Cannot map {src.GetType()} to {trg.GetType()} as there is no known derived type mapping", nameof(src)); + } + } +} \ No newline at end of file