|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace AutoMapper.Extensions.ExpressionMapping.UnitTests |
| 8 | +{ |
| 9 | + public class CanMapIfASourceTypeTargetsMultipleDestinationTypesInTheSameExpression |
| 10 | + { |
| 11 | +#pragma warning disable xUnit1004 // Test methods should not be skipped |
| 12 | + [Fact(Skip = "This test is currently skipped due to unsupported scenario.")] |
| 13 | +#pragma warning restore xUnit1004 // Test methods should not be skipped |
| 14 | + public void Can_map_if_source_type_targets_multiple_destination_types_in_the_same_expression() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + var mapper = ConfigurationHelper.GetMapperConfiguration(cfg => |
| 18 | + { |
| 19 | + cfg.CreateMap<SourceType, TargetType>().ReverseMap(); |
| 20 | + cfg.CreateMap<SourceChildType, TargetChildType>().ReverseMap(); |
| 21 | + |
| 22 | + // Same source type can map to different target types. This seems unsupported currently. |
| 23 | + cfg.CreateMap<SourceListItemType, TargetListItemType>().ReverseMap(); |
| 24 | + cfg.CreateMap<SourceListItemType, TargetChildListItemType>().ReverseMap(); |
| 25 | + |
| 26 | + }).CreateMapper(); |
| 27 | + Expression<Func<SourceType, bool>> sourcesWithListItemsExpr = src => src.Id != 0 && src.ItemList.Any() && src.Child.ItemList.Any(); // Sources with non-empty ItemList |
| 28 | + |
| 29 | + // Act |
| 30 | + Expression<Func<TargetType, bool>> target1sWithListItemsExpr = mapper.MapExpression<Expression<Func<TargetType, bool>>>(sourcesWithListItemsExpr); |
| 31 | + |
| 32 | + // Assert |
| 33 | + Assert.NotNull(target1sWithListItemsExpr); |
| 34 | + } |
| 35 | + |
| 36 | +#pragma warning disable xUnit1004 // Test methods should not be skipped |
| 37 | + [Fact(Skip = "This test is currently skipped due to unsupported scenario.")] |
| 38 | +#pragma warning restore xUnit1004 // Test methods should not be skipped |
| 39 | + public void Can_map_if_source_type_targets_multiple_destination_types_in_the_same_expression_including_nested_parameters() |
| 40 | + { |
| 41 | + // Arrange |
| 42 | + var mapper = ConfigurationHelper.GetMapperConfiguration(cfg => |
| 43 | + { |
| 44 | + cfg.CreateMap<SourceType, TargetType>().ReverseMap(); |
| 45 | + cfg.CreateMap<SourceChildType, TargetChildType>().ReverseMap(); |
| 46 | + |
| 47 | + // Same source type can map to different target types. This seems unsupported currently. |
| 48 | + cfg.CreateMap<SourceListItemType, TargetListItemType>().ReverseMap(); |
| 49 | + cfg.CreateMap<SourceListItemType, TargetChildListItemType>().ReverseMap(); |
| 50 | + |
| 51 | + }).CreateMapper(); |
| 52 | + Expression<Func<SourceType, bool>> sourcesWithListItemsExpr = src => src.Id != 0 && src.ItemList.FirstOrDefault(i => i.Id == 1) == null && src.Child.ItemList.FirstOrDefault(i => i.Id == 1) == null; // Sources with non-empty ItemList |
| 53 | + |
| 54 | + // Act |
| 55 | + Expression<Func<TargetType, bool>> target1sWithListItemsExpr = mapper.MapExpression<Expression<Func<TargetType, bool>>>(sourcesWithListItemsExpr); |
| 56 | + |
| 57 | + //Assert |
| 58 | + Assert.NotNull(target1sWithListItemsExpr); |
| 59 | + } |
| 60 | + |
| 61 | + private class SourceChildType |
| 62 | + { |
| 63 | + public int Id { get; set; } |
| 64 | + public IEnumerable<SourceListItemType> ItemList { get; set; } // Uses same type (SourceListItemType) for its itemlist as SourceType |
| 65 | + } |
| 66 | + |
| 67 | + private class SourceType |
| 68 | + { |
| 69 | + public int Id { get; set; } |
| 70 | + public SourceChildType Child { set; get; } |
| 71 | + public IEnumerable<SourceListItemType> ItemList { get; set; } |
| 72 | + } |
| 73 | + |
| 74 | + private class SourceListItemType |
| 75 | + { |
| 76 | + public int Id { get; set; } |
| 77 | + } |
| 78 | + |
| 79 | + private class TargetChildType |
| 80 | + { |
| 81 | + public virtual int Id { get; set; } |
| 82 | + public virtual ICollection<TargetChildListItemType> ItemList { get; set; } = []; |
| 83 | + } |
| 84 | + |
| 85 | + private class TargetChildListItemType |
| 86 | + { |
| 87 | + public virtual int Id { get; set; } |
| 88 | + } |
| 89 | + |
| 90 | + private class TargetType |
| 91 | + { |
| 92 | + public virtual int Id { get; set; } |
| 93 | + |
| 94 | + public virtual TargetChildType Child { get; set; } |
| 95 | + |
| 96 | + public virtual ICollection<TargetListItemType> ItemList { get; set; } = []; |
| 97 | + } |
| 98 | + |
| 99 | + private class TargetListItemType |
| 100 | + { |
| 101 | + public virtual int Id { get; set; } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments