Skip to content

Commit

Permalink
chore: Use c# string syntax helpers for tests (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz authored Aug 9, 2023
1 parent 1677632 commit f7c566f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static class EnumerableMappingBuilder
if (elementMapping == null)
return null;

if (TryBuildCastMapping(ctx, elementMapping) is { } implicitMapping)
return implicitMapping;
if (TryBuildCastMapping(ctx, elementMapping) is { } castMapping)
return castMapping;

if (TryBuildFastConversion(ctx, elementMapping) is { } fastLoopMapping)
return fastLoopMapping;
Expand Down Expand Up @@ -149,7 +149,9 @@ ForEachAddEnumerableExistingTargetMapping CreateForEach(string methodName)
|| !ctx.CollectionInfos!.Source.CountIsKnown
|| ctx.CollectionInfos.Target.CollectionType == CollectionType.None
)
{
return null;
}

// if target is a list or type implemented by list
if (ctx.CollectionInfos.Target.CollectionType is CollectionType.List or CollectionType.ICollection or CollectionType.IList)
Expand Down Expand Up @@ -185,15 +187,15 @@ or CollectionType.IEnumerable
return null;

// upgrade nullability of element type
var targetValue = ((INamedTypeSymbol)ctx.CollectionInfos!.Target.Type.OriginalDefinition).Construct(elementMapping.TargetType);
var targetCollection = ctx.Types.Get(typeof(List<>)).Construct(elementMapping.TargetType);
var targetType = ((INamedTypeSymbol)ctx.CollectionInfos!.Target.Type.OriginalDefinition).Construct(elementMapping.TargetType);
var targetTypeToInstantiate = ctx.Types.Get(typeof(List<>)).Construct(elementMapping.TargetType);

return new ForEachAddEnumerableMapping(
ctx.Source,
targetValue,
targetType,
elementMapping,
AddMethodName,
targetCollection,
targetTypeToInstantiate,
ctx.CollectionInfos.Source.CountPropertyName
);
}
Expand All @@ -206,11 +208,11 @@ private static NewInstanceMapping BuildArrayToArrayMapping(MappingBuilderContext
if (!elementMapping.IsSynthetic)
{
// upgrade nullability of element type
var targetValue =
var targetType =
ctx.CollectionInfos!.Target.CollectionType == CollectionType.Array
? ctx.Types.GetArrayType(elementMapping.TargetType)
: ((INamedTypeSymbol)ctx.Target).ConstructedFrom.Construct(elementMapping.TargetType);
return new ArrayForMapping(ctx.Source, targetValue, elementMapping, elementMapping.TargetType);
return new ArrayForMapping(ctx.Source, targetType, elementMapping, elementMapping.TargetType);
}

return ctx.MapperConfiguration.UseDeepCloning
Expand All @@ -225,14 +227,14 @@ private static NewInstanceMapping BuildArrayToArrayMapping(MappingBuilderContext
return null;

// upgrade nullability of element type
var targetValue =
var targetType =
ctx.CollectionInfos!.Target.CollectionType == CollectionType.Array
? ctx.Types.GetArrayType(elementMapping.TargetType)
: ((INamedTypeSymbol)ctx.Target).ConstructedFrom.Construct(elementMapping.TargetType);

return new ArrayForEachMapping(
ctx.Source,
targetValue,
targetType,
elementMapping,
elementMapping.TargetType,
ctx.CollectionInfos.Source.CountPropertyName!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// <summary>
/// An object mapping creating the target instance via a new() call.
/// </summary>
public interface INewInstanceObjectMemberMapping : IMapping
public interface INewInstanceObjectMemberMapping : INewInstanceMapping
{
void AddConstructorParameterMapping(ConstructorParameterMapping mapping);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// <summary>
/// A tuple mapping creating the target instance via a tuple expression (eg. (A: 10, B: 20)).
/// </summary>
public interface INewValueTupleMapping : IMapping
public interface INewValueTupleMapping : INewInstanceMapping
{
void AddConstructorParameterMapping(ValueTupleConstructorParameterMapping mapping);
}
3 changes: 3 additions & 0 deletions src/Riok.Mapperly/Helpers/CompilationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ internal static class CompilationExtensions

return type;
#else
// RS0030 banned api: GetTypesByMetadataName is not supported for Roslyn < 4.4
#pragma warning disable RS0030
return compilation.GetTypeByMetadataName(fullyQualifiedMetadataName);
#pragma warning restore RS0030
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Helpers/FieldSymbolEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Riok.Mapperly.Helpers;

internal static class FieldSymbolEqualityComparer
{
public static IEqualityComparer<IFieldSymbol?> Default = SymbolEqualityComparer.Default;
public static readonly IEqualityComparer<IFieldSymbol?> Default = SymbolEqualityComparer.Default;
}
63 changes: 34 additions & 29 deletions test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,56 @@ public Task WithNamespaceShouldWork()
[Fact]
public Task InstanceMapperShouldEmitDiagnosticForPartialStaticMethods()
{
var source =
@"
using System;
using System.Collections.Generic;
using Riok.Mapperly.Abstractions;
[Mapper]
public partial class MyMapper
{
public static object StaticToObject(string s);
var source = TestSourceBuilder.CSharp(
"""
using System;
using System.Collections.Generic;
using Riok.Mapperly.Abstractions;

public partial object InstanceToObject(string s);
}
";
[Mapper]
public partial class MyMapper
{
public static object StaticToObject(string s);

public partial object InstanceToObject(string s);
}
"""
);
return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task InstanceMapperShouldSupportUserDefinedStaticMethods()
{
var source =
@"
using System;
using System.Collections.Generic;
using Riok.Mapperly.Abstractions;
[Mapper]
public partial class MyMapper
{
public static int StaticMapper(int s) => s;
var source = TestSourceBuilder.CSharp(
"""
using System;
using System.Collections.Generic;
using Riok.Mapperly.Abstractions;

public partial B Map(A s);
}
[Mapper]
public partial class MyMapper
{
public static int MapInt(int s) => s;

public partial B Map(A s);
}

public record A(int Value);
public record B(int Value);
";
public record A(int Value);
public record B(int Value);
"""
);
return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task InstanceMapperShouldUseStaticExistingTargetMethod()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial B Map(A s);" + "static void StaticMapper(List<int> src, List<string> dst) { }",
"""
partial B Map(A s);"
static void MapList(List<int> src, List<string> dst) { }
""",
"class A { public List<int> Value { get; set; } }",
"public class B { public List<string> Value { get; } }"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public partial class MyMapper
{
public partial global::B Map(global::A s)
{
var target = new global::B(StaticMapper(s.Value));
var target = new global::B(MapInt(s.Value));
return target;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public partial class Mapper
private partial global::B Map(global::A s)
{
var target = new global::B();
StaticMapper(s.Value, target.Value);
MapList(s.Value, target.Value);
return target;
}
}
}

0 comments on commit f7c566f

Please sign in to comment.