Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
62c4027
Support for configuring complex type members as sources for root targ…
SteveWilkes Jun 28, 2018
9b8777c
Adding ToTarget() configuration API method
SteveWilkes Jun 28, 2018
1c22c58
Catching invalid .ToTarget() API configurations
SteveWilkes Jun 28, 2018
1ddf29d
Start of handling runtime-type-checking of configured root target sou…
SteveWilkes Jun 29, 2018
d041a5d
Support for overwrite mapping from configured root target sources
SteveWilkes Jun 29, 2018
1e79eeb
Short-circuiting Relative member chain creation when possible
SteveWilkes Jun 30, 2018
e03c3c5
Increased test coverage for root target data sources - invalid config…
SteveWilkes Jun 30, 2018
ad6b84f
Support for conditional configured root source members
SteveWilkes Jun 30, 2018
fc20aed
Tidying / Start of support for configured enumerable root data sources
SteveWilkes Jun 30, 2018
98a97ce
Replacing uses of Linq Select() and Where()
SteveWilkes Jun 30, 2018
82cccc5
Support for configured enumerable root source members
SteveWilkes Jun 30, 2018
b943ecc
Support for multiple configured root enumerable data sources
SteveWilkes Jun 30, 2018
cce73ea
Test coverage for configuring multiple complex type root source membe…
SteveWilkes Jun 30, 2018
4c46a19
Support for runtime-typed configured root source members
SteveWilkes Jun 30, 2018
2fd2d6d
Support for mapping configured root data sources to dictionaries
SteveWilkes Jul 1, 2018
1997b36
Test coverage for configured root source member mapping to dynamic
SteveWilkes Jul 1, 2018
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 @@ -2,6 +2,7 @@
{
using System;
using System.Linq.Expressions;
using AgileMapper.Extensions;
using AgileMapper.Members;
using TestClasses;
using Xunit;
Expand Down Expand Up @@ -327,6 +328,60 @@ public void ShouldHandleANullSourceMember()
}
}

// See https://github.com/agileobjects/AgileMapper/issues/64
[Fact]
public void ShouldApplyAConfiguredRootSourceMember()
{
using (var mapper = Mapper.CreateNew())
{
var source = new { Value1 = 8392, Value = new { Value2 = 5482 } };

var result = source
.MapUsing(mapper)
.ToANew<PublicTwoFields<int, int>>(cfg => cfg
.Map((s, ptf) => s.Value)
.ToRootTarget());

result.Value1.ShouldBe(8392);
result.Value2.ShouldBe(5482);
}
}

[Fact]
public void ShouldApplyAConfiguredRootSourceObjectMember()
{
using (var mapper = Mapper.CreateNew())
{
var source1 = new PublicProperty<object>
{
Value = new PublicField<string> { Value = "Hello!" }
};

var result1 = mapper
.Map(source1)
.ToANew<PublicProperty<string>>(cfg => cfg
.Map((s, t) => s.Value)
.ToRootTarget());

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

var source2 = new PublicProperty<object>
{
Value = new PublicProperty<string> { Value = "Goodbye!" }
};

var result2 = mapper
.Map(source2)
.ToANew<PublicProperty<string>>(cfg => cfg
.Map((s, t) => s.Value)
.ToRootTarget());

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

mapper.InlineContexts().ShouldHaveSingleItem();
}
}

#region Helper Members

private static Expression<Func<IMappingData<PublicProperty<int>, PublicField<int>>, object>> SubtractOne =>
Expand Down
219 changes: 219 additions & 0 deletions AgileMapper.UnitTests/Configuration/WhenConfiguringDataSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using AgileMapper.Extensions;
using AgileMapper.Members;
using TestClasses;
using Xunit;
Expand Down Expand Up @@ -993,6 +994,224 @@ public void ShouldAllowIdAndIdentifierConfiguration()
}
}

// See https://github.com/agileobjects/AgileMapper/issues/64
[Fact]
public void ShouldApplyAConfiguredRootSourceMember()
{
using (var mapper = Mapper.CreateNew())
{
var source = new { Value1 = 123, Value = new { Value2 = 456 } };

mapper.WhenMapping
.From(source)
.To<PublicTwoFields<int, int>>()
.Map((s, ptf) => s.Value)
.ToRootTarget();

var result = source
.MapUsing(mapper)
.ToANew<PublicTwoFields<int, int>>();

result.Value1.ShouldBe(123);
result.Value2.ShouldBe(456);
}
}

[Fact]
public void ShouldApplyANestedOverwriteConfiguredRootSourceMember()
{
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();

var source = new PublicTwoFields<int, PublicField<PublicTwoFields<int, int>>>
{
Value1 = 6372,
Value2 = new PublicField<PublicTwoFields<int, int>>
{
Value = new PublicTwoFields<int, int>
{
Value2 = 8262
}
}
};

var target = new PublicTwoFields<int, int>
{
Value1 = 637,
Value2 = 728
};

mapper.Map(source).Over(target);

target.Value1.ShouldBeDefault(); // <- Because Value2.Value.Value1 will overwrite 6372
target.Value2.ShouldBe(8262);
}
}

[Fact]
public void ShouldHandleAConfiguredRootSourceMemberNullValue()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<MysteryCustomer>()
.ToANew<PublicTwoFields<string, string>>()
.Map((mc, t) => mc.Name)
.To(t => t.Value1)
.And
.Map((mc, t) => mc.Address)
.ToRootTarget();

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

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

result.Value1.ShouldBe("Nelly");
result.Value2.ShouldBeNull();
}
}

[Fact]
public void ShouldApplyAConfiguredRootSourceMemberConditionally()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicTwoFieldsStruct<PublicPropertyStruct<int>, int>>()
.OnTo<PublicTwoFields<int, int>>()
.If((s, t) => s.Value1.Value > 5)
.Map((s, t) => s.Value1)
.ToRootTarget();

mapper.WhenMapping
.From<PublicPropertyStruct<int>>()
.OnTo<PublicTwoFields<int, int>>()
.Map((s, t) => s.Value)
.To(t => t.Value1);

var matchingSource = new PublicTwoFieldsStruct<PublicPropertyStruct<int>, int>
{
Value1 = new PublicPropertyStruct<int> { Value = 10 },
Value2 = 627
};

var target = new PublicTwoFields<int, int> { Value2 = 673282 };

mapper.Map(matchingSource).OnTo(target);

target.Value1.ShouldBe(10);
target.Value2.ShouldBe(673282);

var nonMatchingSource = new PublicTwoFieldsStruct<PublicPropertyStruct<int>, int>
{
Value1 = new PublicPropertyStruct<int> { Value = 1 },
Value2 = 9285
};

target.Value1 = target.Value2 = default(int);

mapper.Map(nonMatchingSource).OnTo(target);

target.Value1.ShouldBeDefault();
target.Value2.ShouldBe(9285);
}
}

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

var source = new PublicTwoFields<Address, Address[]>
{
Value1 = new Address { Line1 = "Here", Line2 = "There" },
Value2 = new[]
{
new Address { Line1 = "Somewhere", Line2 = "Else" },
new Address { Line1 = "Elsewhere"}
}
};

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

result.Count.ShouldBe(2);
result.First().Line1.ShouldBe("Somewhere");
result.First().Line2.ShouldBe("Else");
result.Second().Line1.ShouldBe("Elsewhere");
result.Second().Line2.ShouldBeNull();

source.Value2 = null;

var nullResult = mapper.Map(source).ToANew<List<Address>>();

nullResult.ShouldBeEmpty();
}
}

[Fact]
public void ShouldApplyMultipleConfiguredRootSourceComplexTypeMembers()
{
using (var mapper = Mapper.CreateNew())
{
var source = new
{
PropertyOne = new { Value1 = "Value 1!" },
PropertyTwo = new { Value2 = "Value 2!" },
};

mapper.WhenMapping
.From(source)
.To<PublicTwoFields<string, string>>()
.Map((s, t) => s.PropertyOne)
.ToRootTarget()
.And
.Map((s, t) => s.PropertyTwo)
.ToRootTarget();

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

result.Value1.ShouldBe("Value 1!");
result.Value2.ShouldBe("Value 2!");
}
}

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

var source = new PublicTwoFields<int[], long[]>
{
Value1 = new[] { 1, 2, 3 },
Value2 = new[] { 1L, 2L, 3L }
};

var result = mapper.Map(source).ToANew<decimal[]>();

result.Length.ShouldBe(6);
}
}

// ReSharper disable once ClassNeverInstantiated.Local
// ReSharper disable UnusedAutoPropertyAccessor.Local
private class IdTester
Expand Down
Loading