Skip to content

Commit 501733e

Browse files
committed
no need to throw to report errors
1 parent 956975b commit 501733e

File tree

3 files changed

+87
-95
lines changed

3 files changed

+87
-95
lines changed

docs/source/Configuration-validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ To skip validation altogether for this map, use `MemberList.None`. That's the de
5555

5656
## Custom validations
5757

58-
You can add custom validations through an extension point. See [here](https://github.com/AutoMapper/AutoMapper/blob/bdc0120497d192a2741183415543f6119f50a982/src/UnitTests/CustomValidations.cs#L42).
58+
You can add custom validations through an extension point. See [here](https://github.com/AutoMapper/AutoMapper/blob/master/src/UnitTests/CustomValidations.cs).

src/UnitTests/ConfigurationValidation.cs

Lines changed: 84 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ public class Source
55
{
66
public decimal Foo { get; set; }
77
}
8-
98
public class Destination
109
{
1110
public Type Foo { get; set; }
1211
public string Bar { get; set; }
1312
}
14-
15-
protected override MapperConfiguration CreateConfiguration() =>
16-
new(cfg => { cfg.CreateMap<Source, Destination>(); });
17-
13+
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, Destination>());
1814
[Fact]
1915
public void Should_throw_unmapped_member_and_mismatched_type_exceptions()
2016
{
@@ -35,24 +31,19 @@ public void Should_throw_unmapped_member_and_mismatched_type_exceptions()
3531
);
3632
}
3733
}
38-
3934
public class When_testing_a_dto_with_mismatches_in_multiple_children : AutoMapperSpecBase
4035
{
4136
public class Source
4237
{
4338
public Type Foo { get; set; }
4439
public Type Bar { get; set; }
4540
}
46-
4741
public class Destination
4842
{
4943
public int Foo { get; set; }
5044
public int Bar { get; set; }
5145
}
52-
53-
protected override MapperConfiguration CreateConfiguration() =>
54-
new(cfg => { cfg.CreateMap<Source, Destination>(); });
55-
46+
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, Destination>());
5647
[Fact]
5748
public void Should_throw_for_both_mismatched_children()
5849
{
@@ -101,7 +92,7 @@ private ComplexType(int someMember)
10192
});
10293

10394
[Fact]
104-
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrowException<AutoMapperConfigurationException>(ex=>
95+
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrowException<AutoMapperConfigurationException>(ex=>
10596
ex.MemberMap.ToString().ShouldBe("Void .ctor(ComplexType), parameter myComplexMember"));
10697
}
10798

@@ -118,7 +109,7 @@ public class B
118109
public class C { }
119110

120111
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<A, B>().ConvertUsing(x => new B { Foo = new C() }));
121-
[Fact]
112+
[Fact]
122113
public void Validate() => AssertConfigurationIsValid();
123114
}
124115

@@ -140,7 +131,7 @@ class Converter : ITypeConverter<A, B>
140131
{
141132
public B Convert(A source, B dest, ResolutionContext context) => new B { Foo = new C() };
142133
}
143-
[Fact]
134+
[Fact]
144135
public void Validate() => AssertConfigurationIsValid();
145136
}
146137

@@ -254,7 +245,7 @@ public Dest(int value, int blarg)
254245
}
255246

256247
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
257-
{
248+
{
258249
cfg.CreateMap<Source, Dest>()
259250
.ForCtorParam("blarg", opt => opt.MapFrom(src => src.Value));
260251
});
@@ -354,84 +345,84 @@ public void Should_fail_a_configuration_check()
354345
{
355346
typeof(AutoMapperConfigurationException).ShouldBeThrownBy(AssertConfigurationIsValid);
356347
}
357-
}
358-
348+
}
349+
359350
public class ResolversWithSourceValidation : AutoMapperSpecBase
360351
{
361352
class Source
362353
{
363-
public int Resolved { get; set; }
364-
public int TypedResolved { get; set; }
365-
public int Converted { get; set; }
354+
public int Resolved { get; set; }
355+
public int TypedResolved { get; set; }
356+
public int Converted { get; set; }
366357
public int TypedConverted { get; set; }
367358
}
368359
class Destination
369360
{
370-
public int ResolvedDest { get; set; }
371-
public int TypedResolvedDest { get; set; }
372-
public int ConvertedDest { get; set; }
361+
public int ResolvedDest { get; set; }
362+
public int TypedResolvedDest { get; set; }
363+
public int ConvertedDest { get; set; }
373364
public int TypedConvertedDest { get; set; }
374365
}
375366
class MemberResolver : IMemberValueResolver<Source, Destination, int, int>
376367
{
377368
public int Resolve(Source source, Destination destination, int sourceMember, int destinationMember, ResolutionContext context) => 5;
378-
}
379-
class ValueConverter : IValueConverter<int, int>
380-
{
381-
public int Convert(int sourceMember, ResolutionContext context) => 5;
369+
}
370+
class ValueConverter : IValueConverter<int, int>
371+
{
372+
public int Convert(int sourceMember, ResolutionContext context) => 5;
382373
}
383374
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
384375
{
385376
cfg.CreateMap<Source, Destination>(MemberList.Source)
386-
.ForMember(d => d.ResolvedDest, o => o.MapFrom<MemberResolver, int>("Resolved"))
387-
.ForMember(d=>d.TypedResolvedDest, o => o.MapFrom<MemberResolver, int>(s => s.TypedResolved))
388-
.ForMember(d => d.ConvertedDest, o => o.ConvertUsing<ValueConverter, int>("Converted"))
377+
.ForMember(d => d.ResolvedDest, o => o.MapFrom<MemberResolver, int>("Resolved"))
378+
.ForMember(d=>d.TypedResolvedDest, o => o.MapFrom<MemberResolver, int>(s => s.TypedResolved))
379+
.ForMember(d => d.ConvertedDest, o => o.ConvertUsing<ValueConverter, int>("Converted"))
389380
.ForMember(d => d.TypedConvertedDest, o => o.ConvertUsing<ValueConverter, int>(s => s.TypedConverted));
390381
});
391382
[Fact]
392383
public void Should_work()
393384
{
394385
var result = Mapper.Map<Source, Destination>(new Source());
395-
result.ResolvedDest.ShouldBe(5);
396-
result.TypedResolvedDest.ShouldBe(5);
397-
result.ConvertedDest.ShouldBe(5);
386+
result.ResolvedDest.ShouldBe(5);
387+
result.TypedResolvedDest.ShouldBe(5);
388+
result.ConvertedDest.ShouldBe(5);
398389
result.TypedConvertedDest.ShouldBe(5);
399390
}
400391
}
401-
402-
public class NonMemberExpressionWithSourceValidation : NonValidatingSpecBase
403-
{
404-
class Source
405-
{
406-
public string Value { get; set; }
407-
}
408-
class Destination
409-
{
410-
public string OtherValue { get; set; }
411-
}
412-
protected override MapperConfiguration CreateConfiguration() => new(c=>c.CreateMap<Source, Destination>(MemberList.Source)
413-
.ForMember(d=>d.OtherValue, o=>o.MapFrom(s=>s.Value ?? "")));
414-
[Fact]
415-
public void Should_be_ignored() => new Action(AssertConfigurationIsValid)
416-
.ShouldThrow<AutoMapperConfigurationException>().Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Source.Value));
417-
}
418-
419-
public class MatchingNonMemberExpressionWithSourceValidation : NonValidatingSpecBase
420-
{
421-
class Source
422-
{
423-
public string Value { get; set; }
424-
}
425-
class Destination
426-
{
427-
public string Value { get; set; }
428-
}
429-
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap<Source, Destination>(MemberList.Source)
430-
.ForMember(d => d.Value, o => o.MapFrom(s => s.Value ?? "")));
431-
[Fact]
432-
public void Should_be_ignored() => new Action(AssertConfigurationIsValid)
433-
.ShouldThrow<AutoMapperConfigurationException>().Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Source.Value));
434-
}
392+
393+
public class NonMemberExpressionWithSourceValidation : NonValidatingSpecBase
394+
{
395+
class Source
396+
{
397+
public string Value { get; set; }
398+
}
399+
class Destination
400+
{
401+
public string OtherValue { get; set; }
402+
}
403+
protected override MapperConfiguration CreateConfiguration() => new(c=>c.CreateMap<Source, Destination>(MemberList.Source)
404+
.ForMember(d=>d.OtherValue, o=>o.MapFrom(s=>s.Value ?? "")));
405+
[Fact]
406+
public void Should_be_ignored() => new Action(AssertConfigurationIsValid)
407+
.ShouldThrow<AutoMapperConfigurationException>().Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Source.Value));
408+
}
409+
410+
public class MatchingNonMemberExpressionWithSourceValidation : NonValidatingSpecBase
411+
{
412+
class Source
413+
{
414+
public string Value { get; set; }
415+
}
416+
class Destination
417+
{
418+
public string Value { get; set; }
419+
}
420+
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap<Source, Destination>(MemberList.Source)
421+
.ForMember(d => d.Value, o => o.MapFrom(s => s.Value ?? "")));
422+
[Fact]
423+
public void Should_be_ignored() => new Action(AssertConfigurationIsValid)
424+
.ShouldThrow<AutoMapperConfigurationException>().Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Source.Value));
425+
}
435426

436427
public class When_testing_a_dto_with_fully_mapped_and_custom_matchers : AutoMapperSpecBase
437428
{
@@ -452,7 +443,7 @@ public class ModelDto
452443
cfg.CreateMap<ModelObject, ModelDto>()
453444
.ForMember(dto => dto.Bar, opt => opt.MapFrom(m => m.Barr));
454445
});
455-
[Fact]
446+
[Fact]
456447
public void Validate() => AssertConfigurationIsValid();
457448
}
458449

@@ -840,14 +831,14 @@ interface IAbstractDest
840831
{
841832
string DifferentName { get; set; }
842833
}
843-
}
844-
834+
}
835+
845836
public class When_configuring_a_resolver : AutoMapperSpecBase
846-
{
837+
{
847838
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
848839
{
849840
cfg.CreateMap<Query, Command>().ForMember(d => d.Details, o => o.MapFrom<DetailsValueResolver>());
850-
});
841+
});
851842
public class DetailsValueResolver : IValueResolver<Query, Command, List<KeyValuePair<string, string>>>
852843
{
853844
public List<KeyValuePair<string, string>> Resolve(Query source, Command destination, List<KeyValuePair<string, string>> destMember, ResolutionContext context)
@@ -865,29 +856,29 @@ public class Query
865856
public class Command
866857
{
867858
public List<KeyValuePair<string, string>> Details { get; private set; }
868-
}
869-
[Fact]
870-
public void Validate() => AssertConfigurationIsValid();
871-
}
859+
}
860+
[Fact]
861+
public void Validate() => AssertConfigurationIsValid();
862+
}
872863
public class ObjectPropertyAndNestedTypes : AutoMapperSpecBase
873-
{
864+
{
874865
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<RootLevel, RootLevelDto>());
875-
public class RootLevel
876-
{
877-
public object ObjectProperty { get; set; }
878-
public SecondLevel SecondLevel { get; set; }
879-
}
880-
public class RootLevelDto
881-
{
882-
public object ObjectProperty { get; set; }
883-
public SecondLevelDto SecondLevel { get; set; }
884-
}
885-
public class SecondLevel
886-
{
887-
}
888-
public class SecondLevelDto
889-
{
890-
}
866+
public class RootLevel
867+
{
868+
public object ObjectProperty { get; set; }
869+
public SecondLevel SecondLevel { get; set; }
870+
}
871+
public class RootLevelDto
872+
{
873+
public object ObjectProperty { get; set; }
874+
public SecondLevelDto SecondLevel { get; set; }
875+
}
876+
public class SecondLevel
877+
{
878+
}
879+
public class SecondLevelDto
880+
{
881+
}
891882
[Fact]
892883
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrow<AutoMapperConfigurationException>().MemberMap.DestinationName.ShouldBe(nameof(RootLevelDto.SecondLevel));
893884
}

src/UnitTests/CustomValidations.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Should_call_the_validator()
3838
cfg.CreateMap<Source, Dest>();
3939
});
4040

41-
config.AssertConfigurationIsValid();
41+
new Action(config.AssertConfigurationIsValid).ShouldThrow<AutoMapperConfigurationException>().Message.ShouldBe(nameof(When_using_custom_validation));
4242

4343
_calledForRoot.ShouldBeTrue();
4444
_calledForValues.ShouldBeTrue();
@@ -55,6 +55,7 @@ private void Validator(ValidationContext context)
5555
context.Types.DestinationType.ShouldBe(typeof(Dest));
5656
context.ObjectMapper.ShouldBeNull();
5757
context.MemberMap.ShouldBeNull();
58+
context.Exceptions.Add(new AutoMapperConfigurationException(nameof(When_using_custom_validation)));
5859
}
5960
else
6061
{

0 commit comments

Comments
 (0)