|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using Shouldly; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace Mapster.Tests |
| 7 | +{ |
| 8 | + [TestClass] |
| 9 | + public class WhenUsingMapWithOverrideTypesSettings |
| 10 | + { |
| 11 | + [TestMethod] |
| 12 | + public void OverrideDestinationTramsformIsWorked() |
| 13 | + { |
| 14 | + var config = new TypeAdapterConfig(); |
| 15 | + config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull); |
| 16 | + |
| 17 | + config |
| 18 | + .NewConfig<CollectionPocoOverride, CollectionDtoOverride>() |
| 19 | + .MapWithTypeSettingsOverride(src => src.Children, dest => dest.Children, |
| 20 | + cfg => |
| 21 | + { |
| 22 | + cfg.SkipSettings(x => x.DestinationTransforms); |
| 23 | + }) |
| 24 | + .MapWithTypeSettingsOverride(src => src.Array, dest => dest.Array, |
| 25 | + cfg => |
| 26 | + { |
| 27 | + cfg |
| 28 | + .ReConfigurate() |
| 29 | + .MapWith(x => x ?? new[] { 42 }); |
| 30 | + }); |
| 31 | + |
| 32 | + |
| 33 | + var source = new CollectionPocoOverride(); |
| 34 | + var destination = source.Adapt<CollectionDtoOverride>(config); |
| 35 | + |
| 36 | + destination.MultiDimentionalArray.Length.ShouldBe(0); |
| 37 | + destination.ChildDict.Count.ShouldBe(0); |
| 38 | + destination.Set.Count.ShouldBe(0); |
| 39 | + |
| 40 | + |
| 41 | + destination.Children.ShouldBeNull(); // Destination Transforms from global context settings is skipped for this property |
| 42 | + destination.Array[0].ShouldBe(42); // Custom converter for types is worked, Destination Transforms is not achievable because the custom converter never returns null |
| 43 | + |
| 44 | + |
| 45 | + var destWithNotTypesSettingOverride = new CollectionPocoOverride().Adapt<CollectionDtoWithArray>(config); |
| 46 | + |
| 47 | + // Destination Transforms correct work from other mapping types |
| 48 | + destWithNotTypesSettingOverride.Array.Length.ShouldBe(0); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + #region TestClasses |
| 53 | + |
| 54 | + class CollectionPocoWithArray |
| 55 | + { |
| 56 | + public int[] Array { get; set; } |
| 57 | + } |
| 58 | + |
| 59 | + class CollectionDtoWithArray |
| 60 | + { |
| 61 | + public int[] Array { get; set; } |
| 62 | + } |
| 63 | + |
| 64 | + class CollectionPocoOverride |
| 65 | + { |
| 66 | + public Guid Id { get; set; } |
| 67 | + public string Name { get; set; } |
| 68 | + |
| 69 | + public List<ChildPoco> Children { get; set; } |
| 70 | + public int[] Array { get; set; } |
| 71 | + public double[,] MultiDimentionalArray { get; set; } |
| 72 | + public Dictionary<string, ChildPoco> ChildDict { get; set; } |
| 73 | + public HashSet<string> Set { get; set; } |
| 74 | + } |
| 75 | + |
| 76 | + class CollectionDtoOverride |
| 77 | + { |
| 78 | + public Guid Id { get; set; } |
| 79 | + public string Name { get; set; } |
| 80 | + |
| 81 | + public IReadOnlyList<ChildDto> Children { get; internal set; } |
| 82 | + public int[] Array { get; set; } |
| 83 | + public double[,] MultiDimentionalArray { get; set; } |
| 84 | + public IReadOnlyDictionary<string, ChildDto> ChildDict { get; set; } |
| 85 | + public ISet<string> Set { get; set; } |
| 86 | + } |
| 87 | + #endregion TestClasses |
| 88 | + } |
| 89 | +} |
0 commit comments