-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not build existing target mappings for immutable targets and …
…do not report RMG009 for auto matched members
- Loading branch information
Showing
4 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
test/Riok.Mapperly.Tests/Mapping/ObjectPropertyExistingInstanceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
namespace Riok.Mapperly.Tests.Mapping; | ||
|
||
public class ObjectPropertyExistingInstanceTest | ||
{ | ||
[Fact] | ||
public void ReadOnlyPropertyShouldMap() | ||
{ | ||
var source = TestSourceBuilder.Mapping( | ||
"A", | ||
"B", | ||
""" | ||
public class A | ||
{ | ||
public int? IntValue { get; set; } | ||
public C NestedValue { get; } = null!; | ||
} | ||
""", | ||
""" | ||
public class B | ||
{ | ||
public int? IntValue { get; set; } | ||
public D NestedValue { get; }= null!; | ||
} | ||
""", | ||
""" | ||
public class C | ||
{ | ||
public int Value { get; set; } | ||
public int? Value2 { get; set; } | ||
public int? NullableValue { get; set; } | ||
} | ||
""", | ||
""" | ||
public class D | ||
{ | ||
public string? Value { get; set; } | ||
public string Value2 { get; set; } | ||
public string? NullableValue { get; set; } | ||
} | ||
""" | ||
); | ||
|
||
TestHelper | ||
.GenerateMapper(source, TestHelperOptions.AllowAndIncludeAllDiagnostics) | ||
.Should() | ||
.HaveAssertedAllDiagnostics() | ||
.HaveMapMethodBody( | ||
""" | ||
var target = new global::B(); | ||
target.IntValue = source.IntValue; | ||
target.NestedValue.Value = source.NestedValue.Value.ToString(); | ||
if (source.NestedValue.Value2 != null) | ||
{ | ||
target.NestedValue.Value2 = source.NestedValue.Value2.Value.ToString(); | ||
} | ||
if (source.NestedValue.NullableValue != null) | ||
{ | ||
target.NestedValue.NullableValue = source.NestedValue.NullableValue.Value.ToString(); | ||
} | ||
else | ||
{ | ||
target.NestedValue.NullableValue = null; | ||
} | ||
return target; | ||
""" | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ReadOnlyNullablePropertyShouldMap() | ||
{ | ||
var source = TestSourceBuilder.Mapping( | ||
"A", | ||
"B", | ||
""" | ||
public class A | ||
{ | ||
public int? IntValue { get; set; } | ||
public C? CValue { get; } | ||
} | ||
""", | ||
""" | ||
public class B | ||
{ | ||
public int? IntValue { get; set; } | ||
public C? CValue { get; } | ||
} | ||
""", | ||
""" | ||
public class C | ||
{ | ||
public int IntValue { get; set; } | ||
} | ||
""" | ||
); | ||
|
||
TestHelper | ||
.GenerateMapper(source, TestHelperOptions.AllowAndIncludeAllDiagnostics) | ||
.Should() | ||
.HaveAssertedAllDiagnostics() | ||
.HaveMapMethodBody( | ||
""" | ||
var target = new global::B(); | ||
target.IntValue = source.IntValue; | ||
if (source.CValue != null && target.CValue != null) | ||
{ | ||
target.CValue.IntValue = source.CValue.IntValue; | ||
} | ||
return target; | ||
""" | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ObjectsWithoutPropertiesShouldNotDiagnostic() | ||
{ | ||
var source = TestSourceBuilder.Mapping( | ||
"A", | ||
"B", | ||
"public class A { public C? IntValue { get; } }", | ||
"public class B { public C? IntValue { get; } }", | ||
"public class C;" | ||
); | ||
|
||
TestHelper | ||
.GenerateMapper(source, TestHelperOptions.AllowAndIncludeAllDiagnostics) | ||
.Should() | ||
.HaveAssertedAllDiagnostics() | ||
.HaveMapMethodBody( | ||
""" | ||
var target = new global::B(); | ||
return target; | ||
""" | ||
); | ||
} | ||
|
||
[Fact] | ||
public void SameReadOnlyPropertyShouldNotDiagnostic() | ||
{ | ||
var source = TestSourceBuilder.Mapping( | ||
"A", | ||
"B", | ||
""" | ||
public class A | ||
{ | ||
public int? IntValue { get; set; } | ||
public bool? ComputedIntValue => IntValue == 42; | ||
} | ||
""", | ||
""" | ||
public class B | ||
{ | ||
public int? IntValue { get; set; } | ||
public bool? ComputedIntValue => IntValue == 42; | ||
} | ||
""" | ||
); | ||
|
||
TestHelper | ||
.GenerateMapper(source, TestHelperOptions.AllowAndIncludeAllDiagnostics) | ||
.Should() | ||
.HaveAssertedAllDiagnostics() | ||
.HaveMapMethodBody( | ||
""" | ||
var target = new global::B(); | ||
target.IntValue = source.IntValue; | ||
return target; | ||
""" | ||
); | ||
} | ||
} |