Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void ShouldApplyAConfiguredRootSourceMember()
.MapUsing(mapper)
.ToANew<PublicTwoFields<int, int>>(cfg => cfg
.Map((s, ptf) => s.Value)
.ToRootTarget());
.ToTarget());

result.Value1.ShouldBe(8392);
result.Value2.ShouldBe(5482);
Expand All @@ -361,7 +361,7 @@ public void ShouldApplyAConfiguredRootSourceObjectMember()
.Map(source1)
.ToANew<PublicProperty<string>>(cfg => cfg
.Map((s, t) => s.Value)
.ToRootTarget());
.ToTarget());

result1.Value.ShouldBe("Hello!");

Expand All @@ -374,7 +374,7 @@ public void ShouldApplyAConfiguredRootSourceObjectMember()
.Map(source2)
.ToANew<PublicProperty<string>>(cfg => cfg
.Map((s, t) => s.Value)
.ToRootTarget());
.ToTarget());

result2.Value.ShouldBe("Goodbye!");

Expand Down
150 changes: 128 additions & 22 deletions AgileMapper.UnitTests/Configuration/WhenConfiguringDataSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ public void ShouldAllowIdAndIdentifierConfiguration()

// See https://github.com/agileobjects/AgileMapper/issues/64
[Fact]
public void ShouldApplyAConfiguredRootSourceMember()
public void ShouldApplyAConfiguredRootSource()
{
using (var mapper = Mapper.CreateNew())
{
Expand All @@ -1005,8 +1005,8 @@ public void ShouldApplyAConfiguredRootSourceMember()
mapper.WhenMapping
.From(source)
.To<PublicTwoFields<int, int>>()
.Map((s, ptf) => s.Value)
.ToRootTarget();
.Map(ctx => ctx.Source.Value)
.ToTarget();

var result = source
.MapUsing(mapper)
Expand All @@ -1018,15 +1018,15 @@ public void ShouldApplyAConfiguredRootSourceMember()
}

[Fact]
public void ShouldApplyANestedOverwriteConfiguredRootSourceMember()
public void ShouldApplyANestedOverwriteConfiguredRootSource()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicTwoFields<int, PublicField<PublicTwoFields<int, int>>>>()
.Over<PublicTwoFields<int, int>>()
.Map((s, t) => s.Value2.Value)
.ToRootTarget();
.ToTarget();

var source = new PublicTwoFields<int, PublicField<PublicTwoFields<int, int>>>
{
Expand Down Expand Up @@ -1054,7 +1054,7 @@ public void ShouldApplyANestedOverwriteConfiguredRootSourceMember()
}

[Fact]
public void ShouldHandleAConfiguredRootSourceMemberNullValue()
public void ShouldHandleAConfiguredRootSourceNullValue()
{
using (var mapper = Mapper.CreateNew())
{
Expand All @@ -1065,7 +1065,7 @@ public void ShouldHandleAConfiguredRootSourceMemberNullValue()
.To(t => t.Value1)
.And
.Map((mc, t) => mc.Address)
.ToRootTarget();
.ToTarget();

var source = new MysteryCustomer { Name = "Nelly", Address = default(Address) };

Expand All @@ -1077,7 +1077,7 @@ public void ShouldHandleAConfiguredRootSourceMemberNullValue()
}

[Fact]
public void ShouldApplyAConfiguredRootSourceMemberConditionally()
public void ShouldApplyAConfiguredRootSourceConditionally()
{
using (var mapper = Mapper.CreateNew())
{
Expand All @@ -1086,7 +1086,7 @@ public void ShouldApplyAConfiguredRootSourceMemberConditionally()
.OnTo<PublicTwoFields<int, int>>()
.If((s, t) => s.Value1.Value > 5)
.Map((s, t) => s.Value1)
.ToRootTarget();
.ToTarget();

mapper.WhenMapping
.From<PublicPropertyStruct<int>>()
Expand Down Expand Up @@ -1122,16 +1122,102 @@ public void ShouldApplyAConfiguredRootSourceMemberConditionally()
}
}

// See https://github.com/agileobjects/AgileMapper/issues/68
[Fact]
public void ShouldApplyAConfiguredRootSourceEnumerableMember()
public void ShouldSupportConfiguringARootSourceUsingMappingContext()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<Model>()
.To<ModelDto>()
.Map(ctx => ctx.Source.Statistics)
.ToTarget();

var source = new Model
{
SomeOtherProperties = "jyutrgf",
Statistics = new Statistics
{
Ranking = 0.5f,
SomeOtherRankingStuff = "uityjtgrf"
}
};

var result = mapper.Map(source).ToANew<ModelDto>();

result.SomeOtherProperties.ShouldBe("jyutrgf");
result.Ranking.ShouldBe(0.5f);
result.SomeOtherRankingStuff.ShouldBe("uityjtgrf");
}
}

[Fact]
public void ShouldApplyAConfiguredRootSourceToANestedMember()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicField<PublicField<string>>>()
.To<PublicField<int>>()
.Map(ctx => ctx.Source.Value)
.ToTarget();

var source = new PublicField<PublicField<PublicField<string>>>
{
Value = new PublicField<PublicField<string>>
{
Value = new PublicField<string> { Value = "53632" }
}
};

var result = mapper.Map(source).ToANew<PublicField<PublicField<int>>>();

result.Value.Value.ShouldBe(53632);
}
}

[Fact]
public void ShouldApplyAConfiguredRootSourceToAnEnumerableElement()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicField<PublicField<string>>>()
.ToANew<PublicField<string>>()
.Map(ctx => ctx.Source.Value)
.ToTarget();

var source = new[]
{
new PublicField<PublicField<string>>
{
Value = new PublicField<string> { Value = "kjfcrkjnad" }
},
new PublicField<PublicField<string>>
{
Value = new PublicField<string> { Value = "owkjwsnbsgtf" }
}
};

var result = mapper.Map(source).ToANew<Collection<PublicField<string>>>();

result.Count.ShouldBe(2);
result.First().Value.ShouldBe("kjfcrkjnad");
result.Second().Value.ShouldBe("owkjwsnbsgtf");
}
}

[Fact]
public void ShouldApplyAConfiguredEnumerableRootSource()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicTwoFields<Address, Address[]>>()
.To<List<Address>>()
.Map((s, r) => s.Value2)
.ToRootTarget();
.ToTarget();

var source = new PublicTwoFields<Address, Address[]>
{
Expand Down Expand Up @@ -1160,7 +1246,7 @@ public void ShouldApplyAConfiguredRootSourceEnumerableMember()
}

[Fact]
public void ShouldApplyMultipleConfiguredRootSourceComplexTypeMembers()
public void ShouldApplyMultipleConfiguredComplexTypeRootSources()
{
using (var mapper = Mapper.CreateNew())
{
Expand All @@ -1174,10 +1260,10 @@ public void ShouldApplyMultipleConfiguredRootSourceComplexTypeMembers()
.From(source)
.To<PublicTwoFields<string, string>>()
.Map((s, t) => s.PropertyOne)
.ToRootTarget()
.ToTarget()
.And
.Map((s, t) => s.PropertyTwo)
.ToRootTarget();
.ToTarget();

var result = mapper.Map(source).ToANew<PublicTwoFields<string, string>>();

Expand All @@ -1187,18 +1273,18 @@ public void ShouldApplyMultipleConfiguredRootSourceComplexTypeMembers()
}

[Fact]
public void ShouldApplyMultipleConfiguredRootSourceEnumerableMembers()
public void ShouldApplyMultipleConfiguredEnumerableRootSources()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicTwoFields<int[], long[]>>()
.To<decimal[]>()
.Map((s, t) => s.Value1)
.ToRootTarget()
.Map(xtx => xtx.Source.Value1)
.ToTarget()
.And
.Map((s, t) => s.Value2)
.ToRootTarget();
.ToTarget();

var source = new PublicTwoFields<int[], long[]>
{
Expand All @@ -1212,14 +1298,34 @@ public void ShouldApplyMultipleConfiguredRootSourceEnumerableMembers()
}
}

// ReSharper disable once ClassNeverInstantiated.Local
// ReSharper disable UnusedAutoPropertyAccessor.Local
private class IdTester
internal class IdTester
{
public int ClassId { get; set; }

public int ClassIdentifier { get; set; }
}
// ReSharper restore UnusedAutoPropertyAccessor.Local

internal class Statistics
{
public float Ranking { get; set; }

public string SomeOtherRankingStuff { get; set; }
}

internal class Model
{
public string SomeOtherProperties { get; set; }

public Statistics Statistics { get; set; }
}

internal class ModelDto
{
public string SomeOtherProperties { get; set; }

public float Ranking { get; set; }

public string SomeOtherRankingStuff { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,44 @@ public void ShouldErrorIfUnconvertibleConstructorValueConstantSpecified()
configurationException.Message.ShouldContain("Unable to convert");
}

[Fact]
public void ShouldErrorIfSimpleTypeConfiguredForComplexTarget()
{
var configurationException = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<Person>()
.To<Person>()
.Map((s, t) => s.Id)
.To(t => t.Address);
}
});

configurationException.Message.ShouldContain(
"Person.Id of type 'Guid' cannot be mapped to target type 'Address'");
}

[Fact]
public void ShouldErrorIfSimpleTypeConfiguredForEnumerableTarget()
{
var configurationException = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicField<int>>()
.To<PublicTwoFields<int[], int>>()
.Map((s, t) => s.Value)
.To(t => t.Value1);
}
});

configurationException.Message.ShouldContain(
"PublicField<int>.Value of type 'int' cannot be mapped to target type 'int[]'");
}

[Fact]
public void ShouldErrorIfTargetParameterConfiguredAsTarget()
{
Expand Down Expand Up @@ -283,12 +321,12 @@ public void ShouldErrorIfRootTargetSimpleTypeConstantDataSourceConfigured()
.From<PublicProperty<int>>()
.To<PublicField<Guid>>()
.Map("No no no no no")
.ToRootTarget();
.ToTarget();
}
});

configurationException.Message.ShouldContain(
"'string' cannot be mapped to root target type 'PublicField<Guid>'");
"'string' cannot be mapped to target type 'PublicField<Guid>'");
}

[Fact]
Expand All @@ -302,14 +340,14 @@ public void ShouldErrorIfRootTargetSimpleTypeMemberDataSourceConfigured()
.From<PublicProperty<int>>()
.To<PublicField<long>>()
.Map(ctx => ctx.Source.Value)
.ToRootTarget();
.ToTarget();
}
});

configurationException.Message.ShouldContain("PublicProperty<int>.Value");

configurationException.Message.ShouldContain(
"'int' cannot be mapped to root target type 'PublicField<long>'");
"'int' cannot be mapped to target type 'PublicField<long>'");
}
}
}
Loading