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 @@ -112,7 +112,48 @@ public void ShouldErrorIfDuplicateDataSourceIsConfigured()
[Fact]
public void ShouldErrorIfRedundantDataSourceIsConfigured()
{
Should.Throw<MappingConfigurationException>(() =>
var configEx = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicProperty<int>>()
.To<PublicField<string>>()
.Map(pp => pp.Value, pf => pf.Value);
}
});

configEx.Message.ShouldContain("PublicProperty<int>.Value");
configEx.Message.ShouldContain("PublicField<string>.Value");
configEx.Message.ShouldContain("does not need to be configured");
}

[Fact]
public void ShouldErrorIfRedundantConstructorParameterDataSourceIsConfigured()
{
var configEx = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicProperty<int>>()
.To<PublicCtor<string>>()
.Map(ctx => ctx.Source.Value)
.ToCtor<string>();
}
});

configEx.Message.ShouldContain("PublicProperty<int>.Value");
configEx.Message.ShouldContain("will automatically be mapped");
configEx.Message.ShouldContain("target constructor parameter");
configEx.Message.ShouldContain("PublicCtor<string>.value");
configEx.Message.ShouldContain("does not need to be configured");
}

[Fact]
public void ShouldErrorIfRedundantDerivedTypeDataSourceIsConfigured()
{
var configEx = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
Expand All @@ -129,6 +170,8 @@ public void ShouldErrorIfRedundantDataSourceIsConfigured()
.To(x => x.Value);
}
});

configEx.Message.ShouldContain("already has configured data source");
}

[Fact]
Expand Down Expand Up @@ -262,6 +305,24 @@ public void ShouldErrorIfUnconvertibleConstructorValueConstantSpecified()
configurationException.Message.ShouldContain("Unable to convert");
}

[Fact]
public void ShouldErrorIfUnconvertibleConstructorSourceValueSpecified()
{
var configurationException = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicProperty<int>>()
.To<PublicCtor<Guid>>()
.Map(ctx => ctx.Source.Value)
.ToCtor<Guid>();
}
});

configurationException.Message.ShouldContain("Unable to convert");
}

[Fact]
public void ShouldErrorIfSimpleTypeConfiguredForComplexTarget()
{
Expand All @@ -281,6 +342,25 @@ public void ShouldErrorIfSimpleTypeConfiguredForComplexTarget()
"Person.Id of type 'Guid' cannot be mapped to target type 'Address'");
}

[Fact]
public void ShouldErrorIfSimpleTypeConfiguredForComplexConstructorParameter()
{
var configurationException = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicField<int>>()
.To<PublicCtor<Address>>()
.Map(ctx => ctx.Source.Value)
.ToCtor<Address>();
}
});

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

[Fact]
public void ShouldErrorIfSimpleTypeConfiguredForEnumerableTarget()
{
Expand All @@ -300,6 +380,25 @@ public void ShouldErrorIfSimpleTypeConfiguredForEnumerableTarget()
"PublicField<int>.Value of type 'int' cannot be mapped to target type 'int[]'");
}

[Fact]
public void ShouldErrorIfSimpleTypeConfiguredForEnumerableConstructorParameter()
{
var configurationException = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicField<string>>()
.To<PublicCtor<int[]>>()
.Map(ctx => ctx.Source.Value)
.ToCtor("value");
}
});

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

[Fact]
public void ShouldErrorIfUnconvertibleEnumerableElementTypeConfigured()
{
Expand All @@ -308,9 +407,9 @@ public void ShouldErrorIfUnconvertibleEnumerableElementTypeConfigured()
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicField<PublicField<int>[]>>()
.From<PublicTwoFields<PublicField<int>[], int[]>>()
.To<PublicField<int[]>>()
.Map(s => s.Value, t => t.Value);
.Map(s => s.Value1, t => t.Value);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ public void ShouldUseAConfiguredFactoryForAGivenType()
}
}

[Fact]
public void ShouldUseAConfiguredFactoryWithASimpleSourceType()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicField<string>>()
.ToANew<PublicProperty<PublicCtor<string>>>()
.Map(ctx => new PublicCtor<string>(ctx.Source.Value))
.To(t => t.Value);

var source = new PublicField<string> { Value = "Hello!" };
var result = mapper.Map(source).ToANew<PublicProperty<PublicCtor<string>>>();

result.Value.ShouldNotBeNull();
result.Value.Value.ShouldBe("Hello!");
}
}

[Fact]
public void ShouldUseAConfiguredFactoryWithAComplexTypeMemberBinding()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ public void ShouldErrorOnMemberScopeOptInOfConfiguredSourceMemberDataSourceForWr
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicProperty<decimal>>()
.From<PublicTwoFields<decimal, decimal>>()
.To<PublicWriteOnlyProperty<int>>()
.Map((pp, pwop) => pp.Value).To(pwop => pwop.Value)
.Map((pp, pwop) => pp.Value1).To(pwop => pwop.Value)
.AndViceVersa();
}
});
Expand Down
8 changes: 4 additions & 4 deletions AgileMapper.UnitTests/WhenUsingPartialTrust.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ public MappingException TestMappingException()
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.From<PublicProperty<string>>()
.From<PublicTwoFields<string, string>>()
.To<PublicField<int>>()
.If((s, t) => int.Parse(s.Value) > 0)
.Map(ctx => ctx.Source.Value)
.If((s, t) => int.Parse(s.Value1) > 0)
.Map(ctx => ctx.Source.Value1)
.To(x => x.Value);

var source = new PublicProperty<string> { Value = "CantParseThis" };
var source = new PublicTwoFields<string, string> { Value1 = "CantParseThis" };

mapper.Map(source).ToANew<PublicField<int>>();
}
Expand Down
Loading