Skip to content

Commit 4b31c4c

Browse files
committed
Extra test coverage for configuration of factory methods, re: #90
1 parent 931b2e4 commit 4b31c4c

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

AgileMapper.UnitTests/Configuration/WhenConfiguringObjectCreation.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,29 @@ public void ShouldUseConfiguredFactoriesForBaseAndDerivedTypes()
311311
}
312312
}
313313

314+
// See https://github.com/agileobjects/AgileMapper/issues/90
315+
[Fact]
316+
public void ShouldUseAConfiguredFactoryForAnUnconstructableType()
317+
{
318+
using (var mapper = Mapper.CreateNew())
319+
{
320+
mapper.WhenMapping
321+
.From<Issue90.Parent>()
322+
.ToANew<Issue90.ParentDto>()
323+
.CreateInstancesOf<Issue90.Status>()
324+
.Using(ctx => Issue90.Status.GetStatus(ctx.Source.ParentStatusId));
325+
326+
var source = new Issue90.Parent { ParentStatusId = Issue90.Status.StatusId.Approved };
327+
var result = mapper.Map(source).ToANew<Issue90.ParentDto>();
328+
329+
result.ShouldNotBeNull();
330+
result.ParentStatusId.ShouldBe(Issue90.Status.StatusId.Approved);
331+
result.ParentStatus.ShouldNotBeNull();
332+
result.ParentStatus.Id.ShouldBe(Issue90.Status.StatusId.Approved);
333+
result.ParentStatus.Name.ShouldBe("Approved");
334+
}
335+
}
336+
314337
[Fact]
315338
public void ShouldHandleAnObjectMappingDataCreationException()
316339
{
@@ -491,6 +514,65 @@ public T Value
491514
}
492515
}
493516

517+
private static class Issue90
518+
{
519+
public class Status
520+
{
521+
public enum StatusId
522+
{
523+
Unsubmitted = 1,
524+
Approved = 2,
525+
Withdrawn = 3
526+
}
527+
528+
private Status(StatusId id)
529+
{
530+
Id = id;
531+
Name = id.ToString();
532+
}
533+
534+
public StatusId Id { get; }
535+
536+
public string Name { get; }
537+
538+
private static readonly Status _unsubmitted = new Status(StatusId.Unsubmitted);
539+
private static readonly Status _approved = new Status(StatusId.Approved);
540+
private static readonly Status _withdrawn = new Status(StatusId.Withdrawn);
541+
542+
public static Status GetStatus(StatusId statusId)
543+
{
544+
switch (statusId)
545+
{
546+
case StatusId.Unsubmitted:
547+
return _unsubmitted;
548+
549+
case StatusId.Approved:
550+
return _approved;
551+
552+
case StatusId.Withdrawn:
553+
return _withdrawn;
554+
555+
default:
556+
throw new InvalidOperationException();
557+
}
558+
}
559+
}
560+
561+
public class Parent
562+
{
563+
public Status.StatusId ParentStatusId { get; set; }
564+
565+
public Status ParentStatus => Status.GetStatus(ParentStatusId);
566+
}
567+
568+
public class ParentDto
569+
{
570+
public Status.StatusId ParentStatusId { get; set; }
571+
572+
public Status ParentStatus { get; set; }
573+
}
574+
}
575+
494576
#endregion
495577
}
496578
}

0 commit comments

Comments
 (0)