-
-
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.
- Loading branch information
Showing
10 changed files
with
118 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Riok.Mapperly.Descriptors.TypeMappings; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors; | ||
|
||
internal class MethodNameBuilder | ||
{ | ||
private const string MethodNamePrefix = "MapTo"; | ||
private readonly HashSet<string> _usedNames = new(); | ||
|
||
internal void Add(string name) | ||
=> _usedNames.Add(name); | ||
|
||
internal string Build(MethodMapping mapping) | ||
{ | ||
var i = 0; | ||
var prefix = MethodNamePrefix + mapping.TargetType.NonNullable().Name; | ||
var name = prefix; | ||
while (!_usedNames.Add(name)) | ||
{ | ||
i++; | ||
name = prefix + i; | ||
} | ||
|
||
return name; | ||
} | ||
} |
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
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
48 changes: 48 additions & 0 deletions
48
test/Riok.Mapperly.Tests/Descriptors/MethodNameBuilderTest.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,48 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Moq; | ||
using Riok.Mapperly.Descriptors; | ||
using Riok.Mapperly.Descriptors.TypeMappings; | ||
|
||
namespace Riok.Mapperly.Tests.Descriptors; | ||
|
||
public class MethodNameBuilderTest | ||
{ | ||
[Fact] | ||
public void ShouldGenerateUniqueMethodNames() | ||
{ | ||
var builder = new MethodNameBuilder(); | ||
builder.Add("MapToA"); | ||
builder.Build(NewMethodMappingMock("A")) | ||
.Should() | ||
.BeEquivalentTo("MapToA1"); | ||
builder.Build(NewMethodMappingMock("A")) | ||
.Should() | ||
.BeEquivalentTo("MapToA2"); | ||
builder.Build(NewMethodMappingMock("B")) | ||
.Should() | ||
.BeEquivalentTo("MapToB"); | ||
builder.Build(NewMethodMappingMock("B")) | ||
.Should() | ||
.BeEquivalentTo("MapToB1"); | ||
} | ||
|
||
private MethodMapping NewMethodMappingMock(string targetTypeName) | ||
{ | ||
var targetTypeMock = new Mock<ITypeSymbol>(); | ||
targetTypeMock.Setup(x => x.Name).Returns(targetTypeName); | ||
targetTypeMock.Setup(x => x.NullableAnnotation).Returns(NullableAnnotation.NotAnnotated); | ||
|
||
return new MockedMethodMapping(targetTypeMock.Object); | ||
} | ||
|
||
private class MockedMethodMapping : MethodMapping | ||
{ | ||
public MockedMethodMapping(ITypeSymbol t) : base(t, t) | ||
{ | ||
} | ||
|
||
public override IEnumerable<StatementSyntax> BuildBody(ExpressionSyntax source) | ||
=> Enumerable.Empty<StatementSyntax>(); | ||
} | ||
} |
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