Skip to content

Commit 21bbe5f

Browse files
committed
Erroring if no custom formatting string is specified
1 parent 3d3c28c commit 21bbe5f

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

AgileMapper.UnitTests/Configuration/WhenConfiguringStringFormatting.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace AgileObjects.AgileMapper.UnitTests.Configuration
22
{
33
using System;
4+
using AgileMapper.Configuration;
5+
using Shouldly;
46
using TestClasses;
57
using Xunit;
68

@@ -51,5 +53,20 @@ public void ShouldFormatDoublesMapperWideUsingDecimalPlaces()
5153
result.Value.ShouldBe("1.00");
5254
}
5355
}
56+
57+
[Fact]
58+
public void ShouldErrorIfNoFormatSpecified()
59+
{
60+
var noFormatEx = Should.Throw<MappingConfigurationException>(() =>
61+
{
62+
using (var mapper = Mapper.CreateNew())
63+
{
64+
mapper.WhenMapping
65+
.StringsFrom<double>(c => { });
66+
}
67+
});
68+
69+
noFormatEx.Message.ShouldContain("No format string specified");
70+
}
5471
}
5572
}

AgileMapper/Api/Configuration/MappingConfigStartingPoint.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ public IGlobalConfigSettings StringsFrom<TSourceValue>(Action<StringFormatSpecif
325325

326326
formatSelector.Invoke(formatSpecifier);
327327

328+
formatSpecifier.ErrorIfInvalid();
329+
328330
return this;
329331
}
330332

AgileMapper/Configuration/StringFormatSpecifier.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AgileObjects.AgileMapper.Configuration
22
{
33
using System;
4+
using ReadableExpressions.Extensions;
45
using TypeConversion;
56

67
/// <summary>
@@ -11,6 +12,7 @@ public class StringFormatSpecifier
1112
{
1213
private readonly MapperContext _mapperContext;
1314
private readonly Type _sourceType;
15+
private bool _isValid;
1416

1517
internal StringFormatSpecifier(MapperContext mapperContext, Type sourceType)
1618
{
@@ -26,7 +28,19 @@ internal StringFormatSpecifier(MapperContext mapperContext, Type sourceType)
2628
/// </param>
2729
public void FormatUsing(string format)
2830
{
31+
_isValid = true;
2932
_mapperContext.ValueConverters.Add(new ToFormattedStringConverter(_sourceType, format));
3033
}
34+
35+
internal void ErrorIfInvalid()
36+
{
37+
if (_isValid)
38+
{
39+
return;
40+
}
41+
42+
throw new MappingConfigurationException(
43+
"No format string specified for custom string mapping from " + _sourceType.GetFriendlyName());
44+
}
3145
}
3246
}

0 commit comments

Comments
 (0)