forked from AutoMapper/AutoMapper
/
FillingExistingDestination.cs
156 lines (131 loc) · 4.05 KB
/
FillingExistingDestination.cs
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
150
151
152
153
154
155
156
using System.Collections.Generic;
using Should;
using Xunit;
using System.Linq;
namespace AutoMapper.UnitTests
{
namespace FillingExistingDestination
{
public class When_the_destination_object_is_specified : AutoMapperSpecBase
{
private Source _source;
private Destination _originalDest;
private Destination _dest;
public class Source
{
public int Value { get; set; }
}
public class Destination
{
public int Value { get; set; }
}
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, Destination>();
});
protected override void Because_of()
{
_source = new Source
{
Value = 10,
};
_originalDest = new Destination { Value = 1111 };
_dest = Mapper.Map<Source, Destination>(_source, _originalDest);
}
[Fact]
public void Should_do_the_translation()
{
_dest.Value.ShouldEqual(10);
}
[Fact]
public void Should_return_the_destination_object_that_was_passed_in()
{
_originalDest.ShouldBeSameAs(_dest);
}
}
public class When_the_destination_object_is_specified_with_child_objects : AutoMapperSpecBase
{
private Source _source;
private Destination _originalDest;
private Destination _dest;
public class Source
{
public int Value { get; set; }
public ChildSource Child { get; set; }
}
public class Destination
{
public int Value { get; set; }
public string Name { get; set; }
public ChildDestination Child { get; set; }
}
public class ChildSource
{
public int Value { get; set; }
}
public class ChildDestination
{
public int Value { get; set; }
public string Name { get; set; }
}
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, Destination>(MemberList.Source);
cfg.CreateMap<ChildSource, ChildDestination>(MemberList.Source);
});
protected override void Because_of()
{
_source = new Source
{
Value = 10,
Child = new ChildSource
{
Value = 20
}
};
_originalDest = new Destination
{
Value = 1111,
Name = "foo",
Child = new ChildDestination
{
Name = "bar"
}
};
_dest = Mapper.Map<Source, Destination>(_source, _originalDest);
}
[Fact]
public void Should_do_the_translation()
{
_dest.Value.ShouldEqual(10);
_dest.Child.Value.ShouldEqual(20);
}
[Fact]
public void Should_return_the_destination_object_that_was_passed_in()
{
_dest.Name.ShouldEqual("foo");
_dest.Child.Name.ShouldEqual("bar");
}
}
public class When_the_destination_object_is_specified_and_you_are_converting_an_enum : NonValidatingSpecBase
{
private string _result;
public enum SomeEnum
{
One,
Two,
Three
}
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => { });
protected override void Because_of()
{
_result = Mapper.Map<SomeEnum, string>(SomeEnum.Two, "test");
}
[Fact]
public void Should_return_the_enum_as_a_string()
{
_result.ShouldEqual("Two");
}
}
}
}