Skip to content

Commit d832fda

Browse files
committed
Test coverage for conditional type pairs in nested, derived type members
1 parent 2b3ad47 commit d832fda

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

AgileMapper.UnitTests/WhenMappingDerivedTypes.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void ShouldMapAComplexTypeEnumerableMemberFromItsSourceType()
9797
}
9898

9999
[Fact]
100-
public void ShouldConditionallyMapDerivedTypesFromNestedMembers()
100+
public void ShouldMapDerivedTypesFromNestedMembers()
101101
{
102102
using (var mapper = Mapper.CreateNew())
103103
{
@@ -117,5 +117,30 @@ public void ShouldConditionallyMapDerivedTypesFromNestedMembers()
117117
((Customer)result.Value).Discount.ShouldBe(0.2);
118118
}
119119
}
120+
121+
[Fact]
122+
public void ShouldConditionallyMapDerivedTypesFromNestedMembers()
123+
{
124+
using (var mapper = Mapper.CreateNew())
125+
{
126+
mapper.WhenMapping
127+
.From<CustomerViewModel>()
128+
.To<CustomerViewModel>()
129+
.If((s, t) => s.Name == "Mystery Customer")
130+
.MapTo<MysteryCustomerViewModel>()
131+
.And
132+
.If((s, t) => s.Name == "Customer Mystery!")
133+
.MapTo<MysteryCustomerViewModel>();
134+
135+
var personSource = new PublicField<PersonViewModel>
136+
{
137+
Value = new CustomerViewModel { Name = "Mystery Customer", Discount = 0.5 }
138+
};
139+
var result = mapper.Map(personSource).ToANew<PublicProperty<PersonViewModel>>();
140+
141+
result.Value.ShouldBeOfType<MysteryCustomerViewModel>();
142+
((MysteryCustomerViewModel)result.Value).Discount.ShouldBe(0.5);
143+
}
144+
}
120145
}
121146
}

AgileMapper/Configuration/MappingConfigInfo.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ private Expression GetTypeCheckConditionOrNull(IMemberMapperData mapperData)
194194
}
195195

196196
var contextAccess = mapperData.GetAppropriateMappingContextAccess(contextTypes);
197+
198+
if (contextAccess == mapperData.MappingDataObject)
199+
{
200+
return null;
201+
}
202+
197203
var targetAccess = mapperData.GetTargetAccess(contextAccess, _targetType);
198204
var targetAccessNotNull = targetAccess.GetIsNotDefaultComparison();
199205

AgileMapper/Members/MemberMapperDataExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public static IMemberMapperData GetAppropriateMappingContext(this IMemberMapperD
146146
public static bool TypesMatch(this IBasicMapperData mapperData, IList<Type> contextTypes)
147147
=> TypesMatch(mapperData, contextTypes[0], contextTypes[1]);
148148

149-
public static bool TypesMatch(this IBasicMapperData mapperData, Type sourceType, Type targetType)
149+
private static bool TypesMatch(IBasicMapperData mapperData, Type sourceType, Type targetType)
150150
{
151-
return sourceType.IsAssignableFrom(mapperData.SourceType) &&
151+
return (sourceType.IsAssignableFrom(mapperData.SourceType) || mapperData.SourceType.IsAssignableFrom(sourceType)) &&
152152
(targetType.IsAssignableFrom(mapperData.TargetType) || mapperData.TargetType.IsAssignableFrom(targetType));
153153
}
154154

0 commit comments

Comments
 (0)