Skip to content

Commit

Permalink
Allowing configured condition type tests if mapping from an interface…
Browse files Browse the repository at this point in the history
…, re: #146
  • Loading branch information
SteveWilkes committed Jun 17, 2019
1 parent 58623d9 commit f115d2e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
34 changes: 32 additions & 2 deletions AgileMapper.UnitTests/Configuration/WhenConfiguringDataSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,33 @@ public void ShouldApplyMultipleConfiguredMembersBySourceType()
}
}

[Fact]
public void ShouldAllowConditionTypeTestsWhenMappingFromAnInterface()
{
using (var mapper = Mapper.CreateNew())
{
var sourceData = default(Issue146.Source.Data);

mapper.WhenMapping
.From<Issue146.Source.Container>().To<Issue146.Target.Cont>()
.Map(s => s.Empty, t => t.Info);

mapper.WhenMapping
.From<Issue146.Source.IEmpty>().To<Issue146.Target.Data>()
.After
.MappingEnds
.If(ctx => ctx.Source is Issue146.Source.Data)
.Call(ctx => sourceData = (Issue146.Source.Data)ctx.Source);

var source = new Issue146.Source.Container("xxx");
var result = mapper.Map(source).ToANew<Issue146.Target.Cont>();

result.ShouldNotBeNull();
sourceData.ShouldNotBeNull();
sourceData.ShouldBeSameAs(source.Empty);
}
}

// See https://github.com/agileobjects/AgileMapper/issues/111
[Fact]
public void ShouldConditionallyApplyAToTargetConfiguredSimpleTypeConstant()
Expand All @@ -168,7 +195,7 @@ public void ShouldConditionallyApplyAToTargetConfiguredSimpleTypeConstant()
}

[Fact]
public void ShouldApplyAToTargetConfiguredSimpleTypeConstant()
public void ShouldConditionallyApplyAToTargetConfiguredSimpleType()
{
using (var mapper = Mapper.CreateNew())
{
Expand Down Expand Up @@ -1178,7 +1205,7 @@ public void ShouldApplyAConfiguredSourceInterfaceMember()
result.Name.ShouldBe("input");
result.Info.ShouldNotBeNull();
result.Info.Id.ShouldBe("12321");

// Source has a .Value member, but we don't runtime-type interfaces
result.Info.Value.ShouldBeNull();
}
Expand Down Expand Up @@ -1766,6 +1793,7 @@ public class ParamDef
// ReSharper restore MemberCanBePrivate.Local
// ReSharper restore CollectionNeverQueried.Local

// ReSharper disable InconsistentNaming
internal static class Issue145
{
public class IdsSource
Expand All @@ -1785,6 +1813,7 @@ public class OtherDataSource

public class DataSourceContainer
{

public IdsSource ids;
public ResultSource res;
public OtherDataSource oth;
Expand Down Expand Up @@ -1817,6 +1846,7 @@ public class DataTarget
public OtherDataTarget oth;
}
}
// ReSharper restore InconsistentNaming

internal static class Issue146
{
Expand Down
6 changes: 4 additions & 2 deletions AgileMapper/Configuration/MappingConfigInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif
using Extensions.Internal;
using Members;
using NetStandardPolyfills;
using ObjectPopulation;
using ReadableExpressions;

Expand Down Expand Up @@ -123,9 +124,10 @@ public void AddConditionOrThrow(LambdaExpression conditionLambda)
_conditionLambda = ConfiguredLambdaInfo.For(conditionLambda);
}

private static void ErrorIfConditionHasTypeTest(LambdaExpression conditionLambda)
private void ErrorIfConditionHasTypeTest(LambdaExpression conditionLambda)
{
if (TypeTestFinder.HasNoTypeTest(conditionLambda))
if ((SourceType?.IsInterface() == true) ||
TypeTestFinder.HasNoTypeTest(conditionLambda))
{
return;
}
Expand Down

0 comments on commit f115d2e

Please sign in to comment.