Skip to content

Commit 956975b

Browse files
multiple invalid properties
Co-authored-by: Simon MᶜKenzie <17579442+simonmckenzie@users.noreply.github.com>
1 parent a384948 commit 956975b

File tree

1 file changed

+121
-83
lines changed

1 file changed

+121
-83
lines changed

src/UnitTests/ConfigurationValidation.cs

Lines changed: 121 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected override MapperConfiguration CreateConfiguration() =>
1717

1818
[Fact]
1919
public void Should_throw_unmapped_member_and_mismatched_type_exceptions()
20-
{
20+
{
2121
new Action(AssertConfigurationIsValid)
2222
.ShouldThrow<AggregateException>()
2323
.ShouldSatisfyAllConditions(
@@ -35,6 +35,44 @@ public void Should_throw_unmapped_member_and_mismatched_type_exceptions()
3535
);
3636
}
3737
}
38+
39+
public class When_testing_a_dto_with_mismatches_in_multiple_children : AutoMapperSpecBase
40+
{
41+
public class Source
42+
{
43+
public Type Foo { get; set; }
44+
public Type Bar { get; set; }
45+
}
46+
47+
public class Destination
48+
{
49+
public int Foo { get; set; }
50+
public int Bar { get; set; }
51+
}
52+
53+
protected override MapperConfiguration CreateConfiguration() =>
54+
new(cfg => { cfg.CreateMap<Source, Destination>(); });
55+
56+
[Fact]
57+
public void Should_throw_for_both_mismatched_children()
58+
{
59+
new Action(AssertConfigurationIsValid)
60+
.ShouldThrow<AggregateException>()
61+
.ShouldSatisfyAllConditions(
62+
aex => aex.InnerExceptions.ShouldBeOfLength(2),
63+
aex => aex.InnerExceptions[0]
64+
.ShouldBeOfType<AutoMapperConfigurationException>()
65+
.ShouldSatisfyAllConditions(
66+
ex => ex.MemberMap.ShouldNotBeNull(),
67+
ex => ex.MemberMap.DestinationName.ShouldBe("Foo")),
68+
aex => aex.InnerExceptions[1]
69+
.ShouldBeOfType<AutoMapperConfigurationException>()
70+
.ShouldSatisfyAllConditions(
71+
ex => ex.MemberMap.ShouldNotBeNull(),
72+
ex => ex.MemberMap.DestinationName.ShouldBe("Bar"))
73+
);
74+
}
75+
}
3876
public class ConstructorMappingValidation : NonValidatingSpecBase
3977
{
4078
public class Destination
@@ -63,7 +101,7 @@ private ComplexType(int someMember)
63101
});
64102

65103
[Fact]
66-
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrowException<AutoMapperConfigurationException>(ex=>
104+
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrowException<AutoMapperConfigurationException>(ex=>
67105
ex.MemberMap.ToString().ShouldBe("Void .ctor(ComplexType), parameter myComplexMember"));
68106
}
69107

@@ -80,7 +118,7 @@ public class B
80118
public class C { }
81119

82120
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<A, B>().ConvertUsing(x => new B { Foo = new C() }));
83-
[Fact]
121+
[Fact]
84122
public void Validate() => AssertConfigurationIsValid();
85123
}
86124

@@ -102,7 +140,7 @@ class Converter : ITypeConverter<A, B>
102140
{
103141
public B Convert(A source, B dest, ResolutionContext context) => new B { Foo = new C() };
104142
}
105-
[Fact]
143+
[Fact]
106144
public void Validate() => AssertConfigurationIsValid();
107145
}
108146

@@ -216,7 +254,7 @@ public Dest(int value, int blarg)
216254
}
217255

218256
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
219-
{
257+
{
220258
cfg.CreateMap<Source, Dest>()
221259
.ForCtorParam("blarg", opt => opt.MapFrom(src => src.Value));
222260
});
@@ -316,84 +354,84 @@ public void Should_fail_a_configuration_check()
316354
{
317355
typeof(AutoMapperConfigurationException).ShouldBeThrownBy(AssertConfigurationIsValid);
318356
}
319-
}
320-
357+
}
358+
321359
public class ResolversWithSourceValidation : AutoMapperSpecBase
322360
{
323361
class Source
324362
{
325-
public int Resolved { get; set; }
326-
public int TypedResolved { get; set; }
327-
public int Converted { get; set; }
363+
public int Resolved { get; set; }
364+
public int TypedResolved { get; set; }
365+
public int Converted { get; set; }
328366
public int TypedConverted { get; set; }
329367
}
330368
class Destination
331369
{
332-
public int ResolvedDest { get; set; }
333-
public int TypedResolvedDest { get; set; }
334-
public int ConvertedDest { get; set; }
370+
public int ResolvedDest { get; set; }
371+
public int TypedResolvedDest { get; set; }
372+
public int ConvertedDest { get; set; }
335373
public int TypedConvertedDest { get; set; }
336374
}
337375
class MemberResolver : IMemberValueResolver<Source, Destination, int, int>
338376
{
339377
public int Resolve(Source source, Destination destination, int sourceMember, int destinationMember, ResolutionContext context) => 5;
340-
}
341-
class ValueConverter : IValueConverter<int, int>
342-
{
343-
public int Convert(int sourceMember, ResolutionContext context) => 5;
378+
}
379+
class ValueConverter : IValueConverter<int, int>
380+
{
381+
public int Convert(int sourceMember, ResolutionContext context) => 5;
344382
}
345383
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
346384
{
347385
cfg.CreateMap<Source, Destination>(MemberList.Source)
348-
.ForMember(d => d.ResolvedDest, o => o.MapFrom<MemberResolver, int>("Resolved"))
349-
.ForMember(d=>d.TypedResolvedDest, o => o.MapFrom<MemberResolver, int>(s => s.TypedResolved))
350-
.ForMember(d => d.ConvertedDest, o => o.ConvertUsing<ValueConverter, int>("Converted"))
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"))
351389
.ForMember(d => d.TypedConvertedDest, o => o.ConvertUsing<ValueConverter, int>(s => s.TypedConverted));
352390
});
353391
[Fact]
354392
public void Should_work()
355393
{
356394
var result = Mapper.Map<Source, Destination>(new Source());
357-
result.ResolvedDest.ShouldBe(5);
358-
result.TypedResolvedDest.ShouldBe(5);
359-
result.ConvertedDest.ShouldBe(5);
395+
result.ResolvedDest.ShouldBe(5);
396+
result.TypedResolvedDest.ShouldBe(5);
397+
result.ConvertedDest.ShouldBe(5);
360398
result.TypedConvertedDest.ShouldBe(5);
361399
}
362400
}
363-
364-
public class NonMemberExpressionWithSourceValidation : NonValidatingSpecBase
365-
{
366-
class Source
367-
{
368-
public string Value { get; set; }
369-
}
370-
class Destination
371-
{
372-
public string OtherValue { get; set; }
373-
}
374-
protected override MapperConfiguration CreateConfiguration() => new(c=>c.CreateMap<Source, Destination>(MemberList.Source)
375-
.ForMember(d=>d.OtherValue, o=>o.MapFrom(s=>s.Value ?? "")));
376-
[Fact]
377-
public void Should_be_ignored() => new Action(AssertConfigurationIsValid)
378-
.ShouldThrow<AutoMapperConfigurationException>().Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Source.Value));
379-
}
380-
381-
public class MatchingNonMemberExpressionWithSourceValidation : NonValidatingSpecBase
382-
{
383-
class Source
384-
{
385-
public string Value { get; set; }
386-
}
387-
class Destination
388-
{
389-
public string Value { get; set; }
390-
}
391-
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap<Source, Destination>(MemberList.Source)
392-
.ForMember(d => d.Value, o => o.MapFrom(s => s.Value ?? "")));
393-
[Fact]
394-
public void Should_be_ignored() => new Action(AssertConfigurationIsValid)
395-
.ShouldThrow<AutoMapperConfigurationException>().Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Source.Value));
396-
}
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+
}
397435

398436
public class When_testing_a_dto_with_fully_mapped_and_custom_matchers : AutoMapperSpecBase
399437
{
@@ -414,7 +452,7 @@ public class ModelDto
414452
cfg.CreateMap<ModelObject, ModelDto>()
415453
.ForMember(dto => dto.Bar, opt => opt.MapFrom(m => m.Barr));
416454
});
417-
[Fact]
455+
[Fact]
418456
public void Validate() => AssertConfigurationIsValid();
419457
}
420458

@@ -802,14 +840,14 @@ interface IAbstractDest
802840
{
803841
string DifferentName { get; set; }
804842
}
805-
}
806-
843+
}
844+
807845
public class When_configuring_a_resolver : AutoMapperSpecBase
808-
{
846+
{
809847
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
810848
{
811849
cfg.CreateMap<Query, Command>().ForMember(d => d.Details, o => o.MapFrom<DetailsValueResolver>());
812-
});
850+
});
813851
public class DetailsValueResolver : IValueResolver<Query, Command, List<KeyValuePair<string, string>>>
814852
{
815853
public List<KeyValuePair<string, string>> Resolve(Query source, Command destination, List<KeyValuePair<string, string>> destMember, ResolutionContext context)
@@ -827,29 +865,29 @@ public class Query
827865
public class Command
828866
{
829867
public List<KeyValuePair<string, string>> Details { get; private set; }
830-
}
831-
[Fact]
832-
public void Validate() => AssertConfigurationIsValid();
833-
}
868+
}
869+
[Fact]
870+
public void Validate() => AssertConfigurationIsValid();
871+
}
834872
public class ObjectPropertyAndNestedTypes : AutoMapperSpecBase
835-
{
873+
{
836874
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<RootLevel, RootLevelDto>());
837-
public class RootLevel
838-
{
839-
public object ObjectProperty { get; set; }
840-
public SecondLevel SecondLevel { get; set; }
841-
}
842-
public class RootLevelDto
843-
{
844-
public object ObjectProperty { get; set; }
845-
public SecondLevelDto SecondLevel { get; set; }
846-
}
847-
public class SecondLevel
848-
{
849-
}
850-
public class SecondLevelDto
851-
{
852-
}
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+
}
853891
[Fact]
854892
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrow<AutoMapperConfigurationException>().MemberMap.DestinationName.ShouldBe(nameof(RootLevelDto.SecondLevel));
855893
}

0 commit comments

Comments
 (0)