Skip to content

Commit 2b3ad47

Browse files
committed
Test coverage for configured derived type pairs in nested member mappings
1 parent 31ce13e commit 2b3ad47

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

AgileMapper.UnitTests/WhenMappingDerivedTypes.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,20 @@ public void ShouldConditionallyMapDerivedTypesFromNestedMembers()
101101
{
102102
using (var mapper = Mapper.CreateNew())
103103
{
104-
var sourceData = new { Discount = default(string), Report = default(string) };
105-
106104
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>>();
105+
.From<PersonViewModel>()
106+
.To<Person>()
107+
.Map<CustomerViewModel>()
108+
.To<Customer>();
117109

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>>();
110+
var personSource = new PublicField<CustomerViewModel>
111+
{
112+
Value = new MysteryCustomerViewModel { Discount = 0.2 }
113+
};
114+
var result = mapper.Map(personSource).ToANew<PublicSetMethod<Person>>();
122115

123-
result.Value.ShouldBeOfType<MysteryCustomerViewModel>();
116+
result.Value.ShouldBeOfType<Customer>();
117+
((Customer)result.Value).Discount.ShouldBe(0.2);
124118
}
125119
}
126120
}

AgileMapper/ObjectPopulation/DerivedComplexTypeMappingsFactory.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ public static Expression CreateFor(IObjectMappingData declaredTypeMappingData)
3737

3838
var derivedTypeMappings = new List<Expression>();
3939

40+
bool declaredTypeHasUnconditionalTypePair;
41+
4042
AddDeclaredSourceTypeMappings(
4143
derivedTypePairs,
4244
declaredTypeMappingData,
43-
derivedTypeMappings);
45+
derivedTypeMappings,
46+
out declaredTypeHasUnconditionalTypePair);
47+
48+
if (declaredTypeHasUnconditionalTypePair)
49+
{
50+
return derivedTypeMappings.First();
51+
}
4452

4553
var typedObjectVariables = new List<ParameterExpression>();
4654

@@ -56,7 +64,8 @@ public static Expression CreateFor(IObjectMappingData declaredTypeMappingData)
5664
private static void AddDeclaredSourceTypeMappings(
5765
IEnumerable<DerivedTypePair> derivedTypePairs,
5866
IObjectMappingData declaredTypeMappingData,
59-
ICollection<Expression> derivedTypeMappings)
67+
ICollection<Expression> derivedTypeMappings,
68+
out bool declaredTypeHasUnconditionalTypePair)
6069
{
6170
var declaredTypeMapperData = declaredTypeMappingData.MapperData;
6271

@@ -70,11 +79,20 @@ private static void AddDeclaredSourceTypeMappings(
7079
derivedTypePair.DerivedTargetType);
7180

7281
var returnMappingResult = Expression.Return(declaredTypeMapperData.ReturnLabelTarget, derivedTypeMapping);
82+
declaredTypeHasUnconditionalTypePair = (condition == null);
83+
84+
if (declaredTypeHasUnconditionalTypePair)
85+
{
86+
derivedTypeMappings.Add(returnMappingResult);
87+
return;
88+
}
7389

7490
var ifConditionThenMap = Expression.IfThen(condition, returnMappingResult);
7591

7692
derivedTypeMappings.Add(ifConditionThenMap);
7793
}
94+
95+
declaredTypeHasUnconditionalTypePair = false;
7896
}
7997

8098
private static Expression GetTypePairCondition(DerivedTypePair derivedTypePair, IMemberMapperData mapperData)

0 commit comments

Comments
 (0)