Skip to content

Commit cbd5690

Browse files
committed
Adding guards for WhenMapping.LookForDerivedTypesIn()
1 parent 9b44f50 commit cbd5690

File tree

5 files changed

+131
-61
lines changed

5 files changed

+131
-61
lines changed

AgileMapper.UnitTests/AgileMapper.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="..\VersionInfo.cs">
7575
<Link>VersionInfo.cs</Link>
7676
</Compile>
77+
<Compile Include="Configuration\WhenConfiguringDerivedTypesIncorrectly.cs" />
7778
<Compile Include="Dictionaries\Configuration\WhenConfiguringSourceDictionaryMapping.cs" />
7879
<Compile Include="Dictionaries\Configuration\WhenConfiguringDictionaryMappingIncorrectly.cs" />
7980
<Compile Include="Dictionaries\Configuration\WhenConfiguringNestedDictionaryMapping.cs" />

AgileMapper.UnitTests/Configuration/WhenConfiguringDerivedTypes.cs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace AgileObjects.AgileMapper.UnitTests.Configuration
22
{
3-
using AgileMapper.Configuration;
43
using MoreTestClasses;
54
using Shouldly;
65
using TestClasses;
@@ -101,64 +100,5 @@ public void ShouldMapADerivedTypePairConditionally()
101100
personResult.Name.ShouldBe("Datey");
102101
}
103102
}
104-
105-
[Fact]
106-
public void ShouldErrorIfSameSourceTypeSpecified()
107-
{
108-
var pairingEx = Should.Throw<MappingConfigurationException>(() =>
109-
{
110-
using (var mapper = Mapper.CreateNew())
111-
{
112-
mapper.WhenMapping
113-
.From<Product>()
114-
.To<ProductDto>()
115-
.Map<Product>()
116-
.To<ProductDtoMega>();
117-
118-
}
119-
});
120-
121-
pairingEx.Message.ShouldContain("derived source type must be specified");
122-
}
123-
124-
[Fact]
125-
public void ShouldErrorIfSameTargetTypeSpecified()
126-
{
127-
var pairingEx = Should.Throw<MappingConfigurationException>(() =>
128-
{
129-
using (var mapper = Mapper.CreateNew())
130-
{
131-
mapper.WhenMapping
132-
.From<Product>()
133-
.To<ProductDto>()
134-
.Map<MegaProduct>()
135-
.To<ProductDto>();
136-
137-
}
138-
});
139-
140-
pairingEx.Message.ShouldContain("derived target type must be specified");
141-
}
142-
143-
[Fact]
144-
public void ShouldErrorIfUnnecessaryPairingSpecified()
145-
{
146-
var pairingEx = Should.Throw<MappingConfigurationException>(() =>
147-
{
148-
using (var mapper = Mapper.CreateNew())
149-
{
150-
mapper.WhenMapping
151-
.From<Person>()
152-
.To<PersonViewModel>()
153-
.Map<Customer>()
154-
.To<CustomerViewModel>();
155-
156-
}
157-
});
158-
159-
pairingEx.Message.ShouldContain("Customer is automatically mapped to CustomerViewModel");
160-
pairingEx.Message.ShouldContain("when mapping Person to PersonViewModel");
161-
pairingEx.Message.ShouldContain("does not need to be configured");
162-
}
163103
}
164104
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
namespace AgileObjects.AgileMapper.UnitTests.Configuration
2+
{
3+
using System;
4+
using System.Reflection;
5+
using AgileMapper.Configuration;
6+
using Shouldly;
7+
using TestClasses;
8+
using Xunit;
9+
10+
public class WhenConfiguringDerivedTypesIncorrectly
11+
{
12+
[Fact]
13+
public void ShouldErrorIfNoAssembliesToScanSupplied()
14+
{
15+
var configEx = Should.Throw<MappingConfigurationException>(() =>
16+
{
17+
using (var mapper = Mapper.CreateNew())
18+
{
19+
mapper.WhenMapping.LookForDerivedTypesIn();
20+
}
21+
});
22+
23+
configEx.Message.ShouldContain("assemblies must be specified");
24+
configEx.InnerException.ShouldBeOfType<ArgumentException>();
25+
}
26+
27+
[Fact]
28+
public void ShouldErrorIfNullAssemblyToScanSupplied()
29+
{
30+
var configEx = Should.Throw<MappingConfigurationException>(() =>
31+
{
32+
using (var mapper = Mapper.CreateNew())
33+
{
34+
mapper.WhenMapping.LookForDerivedTypesIn(default(Assembly));
35+
}
36+
});
37+
38+
configEx.Message.ShouldContain("assemblies must be non-null");
39+
configEx.InnerException.ShouldBeOfType<ArgumentNullException>();
40+
}
41+
42+
[Fact]
43+
public void ShouldErrorIfSameSourceTypeSpecified()
44+
{
45+
var pairingEx = Should.Throw<MappingConfigurationException>(() =>
46+
{
47+
using (var mapper = Mapper.CreateNew())
48+
{
49+
mapper.WhenMapping
50+
.From<Product>()
51+
.To<ProductDto>()
52+
.Map<Product>()
53+
.To<ProductDtoMega>();
54+
55+
}
56+
});
57+
58+
pairingEx.Message.ShouldContain("derived source type must be specified");
59+
}
60+
61+
[Fact]
62+
public void ShouldErrorIfSameTargetTypeSpecified()
63+
{
64+
var pairingEx = Should.Throw<MappingConfigurationException>(() =>
65+
{
66+
using (var mapper = Mapper.CreateNew())
67+
{
68+
mapper.WhenMapping
69+
.From<Product>()
70+
.To<ProductDto>()
71+
.Map<MegaProduct>()
72+
.To<ProductDto>();
73+
74+
}
75+
});
76+
77+
pairingEx.Message.ShouldContain("derived target type must be specified");
78+
}
79+
80+
[Fact]
81+
public void ShouldErrorIfUnnecessaryPairingSpecified()
82+
{
83+
var pairingEx = Should.Throw<MappingConfigurationException>(() =>
84+
{
85+
using (var mapper = Mapper.CreateNew())
86+
{
87+
mapper.WhenMapping
88+
.From<Person>()
89+
.To<PersonViewModel>()
90+
.Map<Customer>()
91+
.To<CustomerViewModel>();
92+
93+
}
94+
});
95+
96+
pairingEx.Message.ShouldContain("Customer is automatically mapped to CustomerViewModel");
97+
pairingEx.Message.ShouldContain("when mapping Person to PersonViewModel");
98+
pairingEx.Message.ShouldContain("does not need to be configured");
99+
}
100+
}
101+
}

AgileMapper/Api/Configuration/MappingConfigStartingPoint.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ public EnumPairSpecifier<TFirstEnum> PairEnums<TFirstEnum>(params TFirstEnum[] e
257257
/// </returns>
258258
public IGlobalConfigSettings LookForDerivedTypesIn(params Assembly[] assemblies)
259259
{
260+
if (assemblies.None())
261+
{
262+
throw new MappingConfigurationException(
263+
"One or more assemblies must be specified.",
264+
new ArgumentException(nameof(assemblies)));
265+
}
266+
267+
if (assemblies.Any(a => a == null))
268+
{
269+
throw new MappingConfigurationException(
270+
"All supplied assemblies must be non-null.",
271+
new ArgumentNullException(nameof(assemblies)));
272+
}
273+
260274
_mapperContext.DerivedTypes.AddAssemblies(assemblies);
261275
return this;
262276
}

AgileMapper/Configuration/MappingConfigurationException.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ public MappingConfigurationException()
2020
/// </summary>
2121
/// <param name="message">The message that describes the mapping configuration error.</param>
2222
public MappingConfigurationException(string message)
23-
: base(message)
23+
: this(message, null)
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Initializes a new instance of the MappingConfigurationException class with the given
29+
/// <paramref name="message"/>.
30+
/// </summary>
31+
/// <param name="message">The message that describes the mapping configuration error.</param>
32+
/// <param name="innerException">
33+
/// The exception that is the cause of the current exception, or null if no inner
34+
/// exception exists.
35+
/// </param>
36+
public MappingConfigurationException(string message, Exception innerException)
37+
: base(message, innerException)
2438
{
2539
}
2640
}

0 commit comments

Comments
 (0)