Skip to content

Commit

Permalink
feat: add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Oct 12, 2023
1 parent c8a1d8f commit 4254764
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Riok.Mapperly.Abstractions/MemberVisibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public enum MemberVisibility

/// <summary>
/// Maps all members, even members which are not directly accessible by the mapper are mapped
/// by using accessors with the <see cref="UnsafeAccessorAttribute" />. This can only be used for .NET 8.0 and later.
/// by using accessors with the UnsafeAccessorAttribute. This can only be used for .NET 8.0 and later.
/// </summary>
All = Public | Internal | Protected | Private,

/// <summary>
/// Maps only accessible members.
/// If not set, the <see cref="UnsafeAccessorAttribute" /> is used to generate mappings for inaccessible members.
/// If not set, the UnsafeAccessorAttribute is used to generate mappings for inaccessible members.
/// </summary>
Accessible = 1 << 0,

Expand All @@ -39,7 +39,7 @@ public enum MemberVisibility
Protected = 1 << 3,

/// <summary>
/// Maps private mamembers.
/// Maps private members.
/// </summary>
Private = 1 << 4,
}
10 changes: 10 additions & 0 deletions src/Riok.Mapperly.Abstractions/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,13 @@ Riok.Mapperly.Abstractions.RequiredMappingStrategy.Both = -1 -> Riok.Mapperly.Ab
Riok.Mapperly.Abstractions.RequiredMappingStrategy.None = 0 -> Riok.Mapperly.Abstractions.RequiredMappingStrategy
Riok.Mapperly.Abstractions.RequiredMappingStrategy.Source = 1 -> Riok.Mapperly.Abstractions.RequiredMappingStrategy
Riok.Mapperly.Abstractions.RequiredMappingStrategy.Target = 2 -> Riok.Mapperly.Abstractions.RequiredMappingStrategy
Riok.Mapperly.Abstractions.MapperAttribute.IncludedMembers.get -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MapperAttribute.IncludedMembers.set -> void
Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Accessible = 1 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.All = Riok.Mapperly.Abstractions.MemberVisibility.Public | Riok.Mapperly.Abstractions.MemberVisibility.Internal | Riok.Mapperly.Abstractions.MemberVisibility.Protected | Riok.Mapperly.Abstractions.MemberVisibility.Private -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.AllAccessible = Riok.Mapperly.Abstractions.MemberVisibility.Accessible | Riok.Mapperly.Abstractions.MemberVisibility.All -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Internal = 4 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Private = 16 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Protected = 8 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Public = 2 -> Riok.Mapperly.Abstractions.MemberVisibility
11 changes: 0 additions & 11 deletions src/Riok.Mapperly.Abstractions/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
#nullable enable
Riok.Mapperly.Abstractions.MapperAttribute.IncludedMembers.get -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MapperAttribute.IncludedMembers.set -> void
Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Accessible = 1 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.All = Riok.Mapperly.Abstractions.MemberVisibility.Public | Riok.Mapperly.Abstractions.MemberVisibility.Internal | Riok.Mapperly.Abstractions.MemberVisibility.Protected | Riok.Mapperly.Abstractions.MemberVisibility.Private -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.AllAccessible = Riok.Mapperly.Abstractions.MemberVisibility.Accessible | Riok.Mapperly.Abstractions.MemberVisibility.All -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Internal = 4 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Private = 16 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Protected = 8 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.Public = 2 -> Riok.Mapperly.Abstractions.MemberVisibility
Riok.Mapperly.Abstractions.MemberVisibility.PublicAccessible = Riok.Mapperly.Abstractions.MemberVisibility.Accessible | Riok.Mapperly.Abstractions.MemberVisibility.Public -> Riok.Mapperly.Abstractions.MemberVisibility
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Descriptors/SymbolAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public bool HasAccessibleParameterlessConstructor(ITypeSymbol symbol) =>

public bool IsAccessible(ISymbol symbol) => Compilation.IsSymbolAccessibleWithin(symbol, _mapperSymbol);

public bool MemberIsAccessible(ISymbol symbol)
public bool IsUnsafeAccessible(ISymbol symbol)
{
if (
_memberVisibility.HasFlag(MemberVisibility.Public) && symbol.DeclaredAccessibility.HasFlag(Accessibility.Public)
Expand Down
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Symbols/MappableMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class MappableMember
{
public static IMappableMember? Create(SymbolAccessor accessor, ISymbol symbol)
{
if (!accessor.MemberIsAccessible(symbol))
if (!accessor.IsUnsafeAccessible(symbol))
return null;

return symbol switch
Expand Down
4 changes: 2 additions & 2 deletions src/Riok.Mapperly/Symbols/PropertyMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ internal PropertyMember(IPropertySymbol propertySymbol, SymbolAccessor symbolAcc
public bool IsIndexer => _propertySymbol.IsIndexer;
public bool CanGet =>
!_propertySymbol.IsWriteOnly
&& (_propertySymbol.GetMethod == null || _symbolAccessor.MemberIsAccessible(_propertySymbol.GetMethod));
&& (_propertySymbol.GetMethod == null || _symbolAccessor.IsUnsafeAccessible(_propertySymbol.GetMethod));
public bool CanSet =>
!_propertySymbol.IsReadOnly && (_propertySymbol.SetMethod == null || _symbolAccessor.MemberIsAccessible(_propertySymbol.SetMethod));
!_propertySymbol.IsReadOnly && (_propertySymbol.SetMethod == null || _symbolAccessor.IsUnsafeAccessible(_propertySymbol.SetMethod));

public bool IsInitOnly => _propertySymbol.SetMethod?.IsInitOnly == true;

Expand Down
3 changes: 3 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public TestObjectDto(int ctorValue, int unknownValue = 10, int ctorValue2 = 100)
{
CtorValue = ctorValue;
CtorValue2 = ctorValue2;
PrivateValue = ctorValue;
}

public int CtorValue { get; set; }
Expand Down Expand Up @@ -121,5 +122,7 @@ public TestObjectDto(int ctorValue, int unknownValue = 10, int ctorValue2 = 100)
public DateOnly DateTimeValueTargetDateOnly { get; set; }

public TimeOnly DateTimeValueTargetTimeOnly { get; set; }

private int PrivateValue { get; set; }
}
}
2 changes: 1 addition & 1 deletion test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Riok.Mapperly.IntegrationTests.Mapper
{
[Mapper]
[Mapper(IncludedMembers = MemberVisibility.AllAccessible & ~MemberVisibility.Accessible)]
public partial class TestMapper
{
public partial int DirectInt(int value);
Expand Down
3 changes: 3 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/Models/TestObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public TestObject(int ctorValue, int unknownValue = 10, int ctorValue2 = 100)
{
CtorValue = ctorValue;
CtorValue2 = ctorValue2;
PrivateValue = ctorValue;
}

public int CtorValue { get; set; }
Expand Down Expand Up @@ -118,5 +119,7 @@ public TestObject(int ctorValue, int unknownValue = 10, int ctorValue2 = 100)
public DateTime DateTimeValueTargetDateOnly { get; set; }

public DateTime DateTimeValueTargetTimeOnly { get; set; }

private int PrivateValue { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public partial int ParseableInt(string value)
target.EnumReverseStringValue = MapToTestEnumDtoByValue(testObject.EnumReverseStringValue);
target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(testObject.DateTimeValueTargetDateOnly);
target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(testObject.DateTimeValueTargetTimeOnly);
target.SetPrivateValue(DirectInt(testObject.GetPrivateValue()));
return target;
}

Expand Down Expand Up @@ -216,6 +217,7 @@ public partial int ParseableInt(string value)
target.EnumRawValue = (global::Riok.Mapperly.IntegrationTests.Models.TestEnum)dto.EnumRawValue;
target.EnumStringValue = MapToTestEnum(dto.EnumStringValue);
target.EnumReverseStringValue = MapToString1(dto.EnumReverseStringValue);
target.SetPrivateValue1(DirectInt(dto.GetPrivateValue1()));
return target;
}

Expand Down Expand Up @@ -302,6 +304,7 @@ public partial void UpdateDto(global::Riok.Mapperly.IntegrationTests.Models.Test
target.EnumReverseStringValue = MapToTestEnumDtoByValue(source.EnumReverseStringValue);
target.DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(source.DateTimeValueTargetDateOnly);
target.DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(source.DateTimeValueTargetTimeOnly);
target.SetPrivateValue(DirectInt(source.GetPrivateValue()));
}

public partial global::Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDtoByName(global::Riok.Mapperly.IntegrationTests.Models.TestEnum v)
Expand Down Expand Up @@ -464,4 +467,19 @@ private string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumD
return target;
}
}

file static class UnsafeAccessor
{
[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "get_PrivateValue")]
public static extern int GetPrivateValue(this global::Riok.Mapperly.IntegrationTests.Models.TestObject source);

[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_PrivateValue")]
public static extern void SetPrivateValue(this global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto target, int value);

[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "get_PrivateValue")]
public static extern int GetPrivateValue1(this global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto source);

[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_PrivateValue")]
public static extern void SetPrivateValue1(this global::Riok.Mapperly.IntegrationTests.Models.TestObject target, int value);
}
}

0 comments on commit 4254764

Please sign in to comment.