From bfd6dafe78467515e3de794fa7b51250af35f35d Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Fri, 2 Jul 2021 12:37:55 +0100 Subject: [PATCH] Add mapping implicit type conversion tests. --- test/MapTo.Tests/MapPropertyTests.cs | 100 +++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/test/MapTo.Tests/MapPropertyTests.cs b/test/MapTo.Tests/MapPropertyTests.cs index 634818e..a8d7595 100644 --- a/test/MapTo.Tests/MapPropertyTests.cs +++ b/test/MapTo.Tests/MapPropertyTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using MapTo.Sources; using MapTo.Tests.Extensions; @@ -86,5 +87,104 @@ namespace MapTo diagnostics.ShouldBeSuccessful(); compilation.SyntaxTrees.Last().ShouldContainPartialSource(expectedResult); } + + [Theory] + [MemberData(nameof(MapPropertyWithImplicitConversionFoundData))] + public void When_MapPropertyWithImplicitConversionFound_Should_UseItToMapToSourceProperty(string source, string expectedResult, LanguageVersion languageVersion) + { + // Arrange + source = source.Trim(); + + // Act + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions, languageVersion: languageVersion); + + // Assert + diagnostics.ShouldBeSuccessful(); + compilation.SyntaxTrees.Last().ShouldContainPartialSource(expectedResult); + } + + public static IEnumerable MapPropertyWithImplicitConversionFoundData => new List + { + new object[] + { + @" +namespace Test +{ + using System.Collections.Generic; + + public class InnerClass { public int Prop1 { get; set; } } + public class OuterClass + { + public int Id { get; set; } + public List InnerProp { get; set; } + } +} + +namespace Test.Models +{ + using MapTo; + using System.Collections.Generic; + + [MapFrom(typeof(Test.InnerClass))] + public partial class InnerClass { public int Prop1 { get; set; } } + + [MapFrom(typeof(Test.OuterClass))] + public partial class OuterClass + { + public int Id { get; set; } + public IReadOnlyList InnerProp { get; set; } + } +} +", + @" + private protected OuterClass(MappingContext context, Test.OuterClass outerClass) + { + if (context == null) throw new ArgumentNullException(nameof(context)); + if (outerClass == null) throw new ArgumentNullException(nameof(outerClass)); + + context.Register(outerClass, this); + + Id = outerClass.Id; + InnerProp = outerClass.InnerProp.Select(context.MapFromWithContext).ToList(); + } +", + LanguageVersion.CSharp7_3 + }, + new object[] + { + @" +namespace Test +{ + using System.Collections.Generic; + + public record InnerClass(int Prop1); + public record OuterClass(int Id, List InnerProp); +} + +namespace Test.Models +{ + using MapTo; + using System.Collections.Generic; + + [MapFrom(typeof(Test.InnerClass))] + public partial record InnerClass(int Prop1); + + [MapFrom(typeof(Test.OuterClass))] + public partial record OuterClass(int Id, IReadOnlyList InnerProp); +} +", + @" + private protected OuterClass(MappingContext context, Test.OuterClass outerClass) + : this(Id: outerClass.Id, InnerProp: outerClass.InnerProp.Select(context.MapFromWithContext).ToList()) + { + if (context == null) throw new ArgumentNullException(nameof(context)); + if (outerClass == null) throw new ArgumentNullException(nameof(outerClass)); + + context.Register(outerClass, this); + } +", + LanguageVersion.CSharp9 + } + }; } } \ No newline at end of file