Skip to content

Commit

Permalink
fix: prefer property without auto flattening (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed Aug 10, 2022
1 parent 82f0a4f commit 414cdbb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 15 deletions.
Expand Up @@ -9,7 +9,7 @@ internal static IEnumerable<IEnumerable<string>> BuildMemberPathCandidates(strin
var chunks = StringChunker.ChunkPascalCase(name).ToList();
for (var i = 1 << chunks.Count - 1; i > 0; i--)
{
yield return BuildName(chunks, i);
yield return BuildName(chunks, i - 1);
}
}

Expand Down
Expand Up @@ -6,9 +6,9 @@ public class MapPropertyAttributeTest
public void ShouldSplitMemberAccess()
{
var attr = new MapPropertyAttribute("a.b.c", "d.e.f");
attr.Source.Should().BeEquivalentTo("a", "b", "c");
attr.SourceFullName.Should().BeEquivalentTo("a.b.c");
attr.Target.Should().BeEquivalentTo("d", "e", "f");
attr.TargetFullName.Should().BeEquivalentTo("d.e.f");
attr.Source.Should().BeEquivalentTo(new[] { "a", "b", "c" }, o => o.WithStrictOrdering());
attr.SourceFullName.Should().Be("a.b.c");
attr.Target.Should().BeEquivalentTo(new[] { "d", "e", "f" }, o => o.WithStrictOrdering());
attr.TargetFullName.Should().Be("d.e.f");
}
}
Expand Up @@ -13,6 +13,6 @@ public void BuildMemberPathCandidatesShouldWork(string name, string[] chunks)
MemberPathCandidateBuilder.BuildMemberPathCandidates(name)
.Select(x => string.Join(".", x))
.Should()
.BeEquivalentTo(chunks);
.BeEquivalentTo(chunks, o => o.WithStrictOrdering());
}
}
8 changes: 4 additions & 4 deletions test/Riok.Mapperly.Tests/Descriptors/MethodNameBuilderTest.cs
Expand Up @@ -15,16 +15,16 @@ public void ShouldGenerateUniqueMethodNames()
builder.Reserve("MapToA");
builder.Build(NewMethodMappingMock("A"))
.Should()
.BeEquivalentTo("MapToA1");
.Be("MapToA1");
builder.Build(NewMethodMappingMock("A"))
.Should()
.BeEquivalentTo("MapToA2");
.Be("MapToA2");
builder.Build(NewMethodMappingMock("B"))
.Should()
.BeEquivalentTo("MapToB");
.Be("MapToB");
builder.Build(NewMethodMappingMock("B"))
.Should()
.BeEquivalentTo("MapToB1");
.Be("MapToB1");
}

private MethodMapping NewMethodMappingMock(string targetTypeName)
Expand Down
6 changes: 3 additions & 3 deletions test/Riok.Mapperly.Tests/Helpers/EnumerableExtensionsTest.cs
Expand Up @@ -32,7 +32,7 @@ public void DistinctByShouldWork()
.DistinctBy(x => x.Item2)
.Select(x => x.Item1)
.Should()
.BeEquivalentTo(new[] { "item10", "item20", "item30" });
.BeEquivalentTo(new[] { "item10", "item20", "item30" }, o => o.WithStrictOrdering());
}

[Fact]
Expand All @@ -47,7 +47,7 @@ public void ChunkShouldWork()
new[] { 0, 1 },
new[] { 2, 3 },
new[] { 4 }
});
}, o => o.WithStrictOrdering());
}

[Fact]
Expand All @@ -57,7 +57,7 @@ public void SkipLastShouldWork()
items
.SkipLast()
.Should()
.BeEquivalentTo(items.Take(items.Length - 1));
.BeEquivalentTo(items.Take(items.Length - 1), o => o.WithoutStrictOrdering());
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion test/Riok.Mapperly.Tests/Helpers/StringChunkerTest.cs
Expand Up @@ -13,6 +13,6 @@ public void ChunkPascalCaseShouldWork(string str, string[] expected)
{
StringChunker.ChunkPascalCase(str)
.Should()
.BeEquivalentTo(expected);
.BeEquivalentTo(expected, o => o.WithStrictOrdering());
}
}
26 changes: 26 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs
Expand Up @@ -38,6 +38,32 @@ public void AutoFlattenedProperty()
return target;".ReplaceLineEndings());
}

[Fact]
public void AutoFlattenedPropertyAvailableShouldPreferNonFlattened()
{
var source = TestSourceBuilder.Mapping(
"A",
"B",
"class A { public C Value { get; set; } public string ValueId { get; set; } }",
"class B { public string ValueId { get; set; } }",
"class C { public string Id { get; set; }");

var options = TestHelperOptions.Default with
{
AllowedDiagnostics = new HashSet<DiagnosticSeverity>
{
DiagnosticSeverity.Hidden,
DiagnosticSeverity.Info,
}
};

TestHelper.GenerateSingleMapperMethodBody(source, options)
.Should()
.Be(@"var target = new B();
target.ValueId = source.ValueId;
return target;".ReplaceLineEndings());
}

[Fact]
public void AutoFlattenedPropertyNullablePath()
{
Expand Down
2 changes: 1 addition & 1 deletion test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs
Expand Up @@ -122,7 +122,7 @@ public void WithSameNamesShouldGenerateUniqueMethodNames()
TestHelper.GenerateMapperMethodBodies(source)
.Select(x => x.Name)
.Should()
.BeEquivalentTo("MapToB", "MapToB1");
.BeEquivalentTo(new[] { "MapToB", "MapToB1" }, o => o.WithStrictOrdering());
}

[Fact]
Expand Down

0 comments on commit 414cdbb

Please sign in to comment.