Skip to content

Commit 1832e55

Browse files
committed
Extra test coverage
1 parent c7dd12e commit 1832e55

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

AgileMapper.UnitTests/MapperCloning/WhenCloningMapperDataSources.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public void ShouldCloneADataSource()
1818
.Map((s, t) => s.Value1 * 2)
1919
.To(t => t.Value1);
2020

21+
// Populate the derived types cache so it can be cloned:
22+
baseMapper.GetPlanFor<Customer>().ToANew<CustomerViewModel>();
23+
2124
using (var childMapper1 = baseMapper.CloneSelf())
2225
using (var childMapper2 = baseMapper.CloneSelf())
2326
{

AgileMapper.UnitTests/WhenMappingDerivedTypes.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,40 @@ public void ShouldCreateADerivedTypeInAnExistingMemberEnumerableUsingRuntimeType
270270
}
271271
}
272272

273+
[Fact]
274+
public void ShouldMapADerivedTypeToAStruct()
275+
{
276+
using (var mapper = Mapper.CreateNew())
277+
{
278+
mapper.WhenMapping
279+
.From<MysteryCustomer>()
280+
.ToANew<PublicPropertyStruct<string>>()
281+
.Map((mc, pps) => mc.Name)
282+
.To(pps => pps.Value);
283+
284+
Customer customer = new MysteryCustomer { Id = Guid.NewGuid(), Name = "Mystery!" };
285+
var customerResult = mapper.Map(customer).ToANew<PublicPropertyStruct<string>>();
286+
287+
customerResult.Value.ShouldBe("Mystery!");
288+
}
289+
290+
var source = new MysteryCustomer { Discount = 0.2m };
291+
var result = Mapper.Map(source).ToANew<PersonViewModel>();
292+
293+
result.ShouldBeOfType<MysteryCustomerViewModel>();
294+
((MysteryCustomerViewModel)result).Discount.ShouldBe(0.2);
295+
}
296+
297+
[Fact]
298+
public void ShouldMapDerivedTypesToTheSameTargetType()
299+
{
300+
var source = new MysteryCustomer { Id = Guid.NewGuid(), Name = "Customer!" };
301+
var result = Mapper.Map(source).ToANew<PersonDto>();
302+
303+
result.Id.ShouldBe(source.Id.ToString());
304+
result.Name.ShouldBe("Customer!");
305+
}
306+
273307
[Fact]
274308
public void ShouldCreateADerivedTypeForARequestedParentType()
275309
{
@@ -322,5 +356,19 @@ public void ShouldMapMultipleRuntimeTypedChildMembers()
322356
result.Value2.ShouldHaveSingleItem();
323357
result.Value2.First().Name.ShouldBe("Mr Faff");
324358
}
359+
360+
#region Helper Classes
361+
362+
// ReSharper disable once ClassNeverInstantiated.Local
363+
// ReSharper disable UnusedAutoPropertyAccessor.Local
364+
private class PersonDto
365+
{
366+
public string Id { get; set; }
367+
368+
public string Name { get; set; }
369+
}
370+
// ReSharper restore UnusedAutoPropertyAccessor.Local
371+
372+
#endregion
325373
}
326374
}

AgileMapper/Configuration/DerivedTypePairSet.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ private void LookForDerivedTypePairs(IBasicMapperData mapperData, MapperContext
125125

126126
if (derivedTargetTypes.None())
127127
{
128-
// TODO: Test coverage - derived source types but no derived target types
129128
return;
130129
}
131130

@@ -238,6 +237,11 @@ private static Type GetRootType(Type type)
238237
return type;
239238
}
240239

240+
if (type.IsValueType())
241+
{
242+
return typeof(ValueType);
243+
}
244+
241245
var parentType = type;
242246

243247
while (parentType != typeof(object))

0 commit comments

Comments
 (0)