diff --git a/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs b/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs index ea3df77e68..2532be4241 100644 --- a/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs +++ b/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs @@ -30,7 +30,7 @@ public static IncrementalValuesProvider GetMapperDeclarations .SyntaxProvider .CreateSyntaxProvider( static (s, _) => s is CompilationUnitSyntax { AttributeLists.Count: > 0 }, - static (ctx, ct) => GetMapperDefaultDeclarations(ctx, ct) + static (ctx, ct) => GetMapperDefaultDeclarations(ctx) ) .Collect() .Select((x, _) => x.FirstOrDefault()); @@ -45,12 +45,9 @@ public static IncrementalValuesProvider GetMapperDeclarations return HasAttribute(symbol, MapperGenerator.MapperAttributeName) ? new MapperDeclaration(symbol, declaration) : null; } - private static IAssemblySymbol? GetMapperDefaultDeclarations(GeneratorSyntaxContext ctx, CancellationToken cancellationToken) + private static IAssemblySymbol? GetMapperDefaultDeclarations(GeneratorSyntaxContext ctx) { - var declaration = (CompilationUnitSyntax)ctx.Node; - if (ctx.SemanticModel.GetDeclaredSymbol(declaration, cancellationToken) is not IAssemblySymbol symbol) - return null; - + var symbol = ctx.SemanticModel.Compilation.Assembly; return HasAttribute(symbol, MapperGenerator.MapperDefaultsAttributeName) ? symbol : null; } diff --git a/test/Riok.Mapperly.IntegrationTests/EnumMapperTest.cs b/test/Riok.Mapperly.IntegrationTests/EnumMapperTest.cs new file mode 100644 index 0000000000..e385126195 --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/EnumMapperTest.cs @@ -0,0 +1,28 @@ +using System.Threading.Tasks; +using Riok.Mapperly.IntegrationTests.Helpers; +using Riok.Mapperly.IntegrationTests.Mapper; +using VerifyXunit; +using Xunit; + +namespace Riok.Mapperly.IntegrationTests +{ + [UsesVerify] + public class EnumMapperTest : BaseMapperTest + { + [Fact] + [VersionedSnapshot(Versions.NET7_0)] + public Task SnapshotGeneratedSource() + { + var path = GetGeneratedMapperFilePath(nameof(EnumMapper)); + return Verifier.VerifyFile(path); + } + + [Fact] + [VersionedSnapshot(Versions.NET7_0)] + public Task RunMappingShouldWork() + { + var enum2 = Enum1.Value1.Map(); + return Verifier.Verify(enum2); + } + } +} diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs new file mode 100644 index 0000000000..5d4b5efd0b --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs @@ -0,0 +1,24 @@ +using Riok.Mapperly.Abstractions; + +[assembly: MapperDefaults(EnumMappingStrategy = EnumMappingStrategy.ByName)] + +namespace Riok.Mapperly.IntegrationTests.Mapper +{ + [Mapper] + public static partial class EnumMapper + { + public static partial Enum2 Map(this Enum1 e); + } + + public enum Enum1 + { + Value1 = 4, + Value2 = 7, + } + + public enum Enum2 + { + Value1, + Value2, + } +} diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/EnumMapperTest.RunMappingShouldWork_NET7_0.verified.txt b/test/Riok.Mapperly.IntegrationTests/_snapshots/EnumMapperTest.RunMappingShouldWork_NET7_0.verified.txt new file mode 100644 index 0000000000..39d0344b5c --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/EnumMapperTest.RunMappingShouldWork_NET7_0.verified.txt @@ -0,0 +1 @@ +Value1 \ No newline at end of file diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/EnumMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/EnumMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs new file mode 100644 index 0000000000..f415799708 --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/EnumMapperTest.SnapshotGeneratedSource_NET7_0.verified.cs @@ -0,0 +1,17 @@ +// +#nullable enable +namespace Riok.Mapperly.IntegrationTests.Mapper +{ + public static partial class EnumMapper + { + public static partial global::Riok.Mapperly.IntegrationTests.Mapper.Enum2 Map(this global::Riok.Mapperly.IntegrationTests.Mapper.Enum1 e) + { + return e switch + { + global::Riok.Mapperly.IntegrationTests.Mapper.Enum1.Value1 => global::Riok.Mapperly.IntegrationTests.Mapper.Enum2.Value1, + global::Riok.Mapperly.IntegrationTests.Mapper.Enum1.Value2 => global::Riok.Mapperly.IntegrationTests.Mapper.Enum2.Value2, + _ => throw new System.ArgumentOutOfRangeException(nameof(e), e, "The value of enum Enum1 is not supported"), + }; + } + } +} \ No newline at end of file