Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Nov 19, 2023
1 parent 73b0e8d commit 9e5ca4f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 86 deletions.
90 changes: 4 additions & 86 deletions test/Riok.Mapperly.Tests/Mapping/DerivedExistingTargetTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,6 @@ public Task WithInterfaceSourceAndTargetNullableShouldWork()
return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task WithObjectShouldWork()
{
var source = TestSourceBuilder.MapperWithBody(
"""
[MapDerivedType<string, int>]
[MapDerivedType<int, string>]
[MapDerivedType<IEnumerable<string>, IEnumerable<int>>]
public partial Map(object src, B trg);
"""
);
return TestHelper.VerifyGenerator(source);
}

[Fact]
public void NotAssignableTargetTypeShouldDiagnostic()
{
Expand All @@ -137,50 +123,6 @@ public void NotAssignableTargetTypeShouldDiagnostic()
.HaveAssertedAllDiagnostics();
}

[Fact]
public Task WithEnumerableOfInterfaceShouldWork()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
public partial IEnumerable<B> Map(IEnumerable<A> source);

[MapDerivedType<AImpl1, BImpl1>]
[MapDerivedType<AImpl2, BImpl2>]
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<AImpl1, BImpl1>]
[MapDerivedType<AImpl2, BImpl2>]
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()
{
Expand All @@ -189,31 +131,7 @@ public Task WithBaseTypeConfigShouldWork()
[MapDerivedType<ASubType1, BSubType1>]
[MapDerivedType<ASubType2, BSubType2>]
[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<ASubType1, BSubType1>]
[MapDerivedType<ASubType2, BSubType2>]
[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; } }",
Expand All @@ -231,7 +149,7 @@ public void NotAssignableSourceTypeShouldDiagnostic()
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
[MapDerivedType<AImpl1, BImpl1>]
public partial B Map(A src);
public partial void Map(A src, B trg);
""",
"interface A {}",
"interface B {}",
Expand All @@ -255,7 +173,7 @@ public void DuplicatedSourceTypeShouldDiagnostic()
"""
[MapDerivedType<AImpl1, BImpl1>]
[MapDerivedType<AImpl1, BImpl2>]
public partial B Map(A src);
public partial void Map(A src, B trg);
""",
"interface A {}",
"interface B {}",
Expand All @@ -279,7 +197,7 @@ public void NotMappableShouldDiagnostic()
var source = TestSourceBuilder.MapperWithBody(
"""
[MapDerivedType<Version, int>]
public partial object Map(object src);
public partial void Map(object src, object trg);
"""
);
TestHelper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//HintName: Mapper.g.cs
// <auto-generated />
#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));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//HintName: Mapper.g.cs
// <auto-generated />
#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));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//HintName: Mapper.g.cs
// <auto-generated />
#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));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//HintName: Mapper.g.cs
// <auto-generated />
#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));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//HintName: Mapper.g.cs
// <auto-generated />
#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));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//HintName: Mapper.g.cs
// <auto-generated />
#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));
}
}
}

0 comments on commit 9e5ca4f

Please sign in to comment.