Skip to content

Commit 31ce13e

Browse files
committed
Extra derived type mapping test coverage
1 parent 5f83a27 commit 31ce13e

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

AgileMapper.UnitTests/WhenMappingDerivedTypes.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,33 @@ public void ShouldMapAComplexTypeEnumerableMemberFromItsSourceType()
9595
resultValues.First().ShouldBeOfType<Customer>();
9696
resultValues.Second().ShouldBeOfType<Person>();
9797
}
98+
99+
[Fact]
100+
public void ShouldConditionallyMapDerivedTypesFromNestedMembers()
101+
{
102+
using (var mapper = Mapper.CreateNew())
103+
{
104+
var sourceData = new { Discount = default(string), Report = default(string) };
105+
106+
mapper.WhenMapping
107+
.From(sourceData)
108+
.To<PersonViewModel>()
109+
.If((d, p) => d.Report != default(string))
110+
.MapTo<MysteryCustomerViewModel>()
111+
.And
112+
.If((d, p) => d.Discount != default(string))
113+
.MapTo<CustomerViewModel>();
114+
115+
var customerSource = new { Value = new { Discount = "0.2", Report = default(string) } };
116+
var result = mapper.Map(customerSource).ToANew<PublicSetMethod<PersonViewModel>>();
117+
118+
result.Value.ShouldBeOfType<CustomerViewModel>();
119+
120+
var mysteryCustomerSource = new { Value = new { Discount = default(string), Report = "Lovely!" } };
121+
result = mapper.Map(mysteryCustomerSource).ToANew<PublicSetMethod<PersonViewModel>>();
122+
123+
result.Value.ShouldBeOfType<MysteryCustomerViewModel>();
124+
}
125+
}
98126
}
99127
}

AgileMapper/ObjectPopulation/DerivedComplexTypeMappingsFactory.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ private static Expression GetTypePairCondition(DerivedTypePair derivedTypePair,
8888

8989
var pairCondition = derivedTypePair.GetConditionOrNull(mapperData);
9090

91-
if (condition == null)
92-
{
93-
return pairCondition;
94-
}
95-
96-
return Expression.AndAlso(pairCondition, condition);
91+
return (condition != null) ? Expression.AndAlso(pairCondition, condition) : pairCondition;
9792
}
9893

9994
private static void AddDerivedSourceTypeMappings(

0 commit comments

Comments
 (0)