forked from AutoMapper/AutoMapper.Archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShouldMapMethod.cs
More file actions
149 lines (127 loc) · 4.17 KB
/
Copy pathShouldMapMethod.cs
File metadata and controls
149 lines (127 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace AutoMapper.UnitTests;
public class ShouldIgnoreOpenGenericMethods : NonValidatingSpecBase
{
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap<Source, Destination>());
class Source
{
public int GetValue<T>() => 42;
}
class Destination
{
public int Value { get; set; }
}
[Fact]
public void Works() => Map<Destination>(new Source()).Value.ShouldBe(0);
}
public class ShouldMapMethodInstanceMethods : NonValidatingSpecBase
{
public int SomeValue = 2354;
public int AnotherValue = 6798;
private Destination _destination;
class Source
{
private int _someValue;
private int _anotherValue;
public Source(int someValue, int anotherValue)
{
_someValue = someValue;
anotherValue = _anotherValue;
}
public int SomeNumber()
{
return _someValue;
}
public int AnotherNumber() {
return _anotherValue;
}
}
class Destination
{
public int SomeNumber { get; set; }
public int AnotherNumber { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.ShouldMapMethod = (m => m.Name != nameof(Source.AnotherNumber));
cfg.CreateMap<Source, Destination>();
});
protected override void Because_of()
{
_destination = Mapper.Map<Source, Destination>(new Source(SomeValue, AnotherValue));
}
[Fact]
public void Should_report_unmapped_property()
{
new Action(AssertConfigurationIsValid)
.ShouldThrowException<AutoMapperConfigurationException>(ex =>
{
ex.Errors.ShouldNotBeNull();
ex.Errors.ShouldNotBeEmpty();
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeNull();
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeEmpty();
ex.Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Destination.AnotherNumber));
});
}
[Fact]
public void Should_not_map_another_number_method()
{
_destination.SomeNumber.ShouldBe(SomeValue);
_destination.AnotherNumber.ShouldNotBe(AnotherValue);
}
}
static class SourceExtensions
{
public static int SomeNumber(this ShouldMapMethodExtensionMethods.Source source)
{
return source.SomeValue;
}
public static int AnotherNumber(this ShouldMapMethodExtensionMethods.Source source)
{
return source.AnotherValue;
}
}
public class ShouldMapMethodExtensionMethods : NonValidatingSpecBase
{
public int SomeValue = 4698;
public int AnotherValue = 2374;
private Destination _destination;
public class Source
{
public int SomeValue { get; set; }
public int AnotherValue { get; set; }
}
public class Destination
{
public int SomeNumber { get; set; }
public int AnotherNumber { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.IncludeSourceExtensionMethods(typeof(SourceExtensions));
cfg.ShouldMapMethod = (m => m.Name != nameof(SourceExtensions.AnotherNumber));
cfg.CreateMap<Source, Destination>();
});
protected override void Because_of()
{
_destination = Mapper.Map<Source, Destination>(new Source { SomeValue = SomeValue, AnotherValue = AnotherValue });
}
[Fact]
public void Should_report_unmapped_property()
{
new Action(AssertConfigurationIsValid)
.ShouldThrowException<AutoMapperConfigurationException>(ex =>
{
ex.Errors.ShouldNotBeNull();
ex.Errors.ShouldNotBeEmpty();
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeNull();
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeEmpty();
ex.Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Destination.AnotherNumber));
});
}
[Fact]
public void Should_not_map_another_number_method()
{
_destination.SomeNumber.ShouldBe(SomeValue);
_destination.AnotherNumber.ShouldNotBe(AnotherValue);
}
}