-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCreatorMapperTests.cs
176 lines (142 loc) · 4.74 KB
/
CreatorMapperTests.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
namespace EntityFunctors.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using EntityFunctors;
using FluentAssertions;
using Helpers;
using Mappers;
using NUnit.Framework;
[TestFixture]
public class CreatorMapperTests
{
[Test]
public void TestProperty()
{
var creator = CreateSut();
var foo = new Foo {Id = 10};
var bar = creator(foo, new string[0]);
bar.Should().NotBeNull();
bar.Id.Should().Be(foo.Id);
}
[Test]
public void TestComponentProperty()
{
var foo = new Foo { Component = new Baz { Id = 1 } };
var bar = CreateSut()(foo, new string[0]);
bar.Should().NotBeNull();
bar.Component.Should().NotBeNull();
bar.Component.Id.Should().Be(foo.Component.Id);
}
[Test]
public void TestCollectionProperty()
{
var foo = new Foo
{
Bazes = new[]
{
new Baz
{
Id = 2
},
new Baz
{
Id = 3
}
}
};
var bar = CreateSut()(foo, new string[0]);
bar.Should().NotBeNull();
bar.Quxes.Should().NotBeNull();
bar.Quxes.Should().NotBeEmpty();
bar.Quxes.Count().Should().Be(foo.Bazes.Count());
bar.Quxes.Should().NotContainNulls();
bar.Quxes.First().Id.Should().Be(foo.Bazes.First().Id);
bar.Quxes.Last().Id.Should().Be(foo.Bazes.Last().Id);
}
[Test]
public void TestComponentExpand()
{
var foo = new Foo
{
Bazes = new[]
{
new Baz
{
Id = 2
},
new Baz
{
Id = 3
}
}
};
var sut = CreateSut(true);
var bar = sut(foo, new string[0]);
bar.Should().NotBeNull();
bar.Quxes.Should().BeNull();
bar = sut(foo, new[] { "Quxes" });
bar.Should().NotBeNull();
bar.Quxes.Should().NotBeNull();
bar.Quxes.Should().NotBeEmpty();
bar.Quxes.Count().Should().Be(foo.Bazes.Count());
bar.Quxes.Should().NotContainNulls();
bar.Quxes.First().Id.Should().Be(foo.Bazes.First().Id);
bar.Quxes.Last().Id.Should().Be(foo.Bazes.Last().Id);
}
[Test]
public void TestCollectionExpand()
{
var foo = new Foo { Component = new Baz { Id = 1 } };
var sut = CreateSut(true);
var bar = sut(foo, new string[0]);
bar.Should().NotBeNull();
bar.Component.Should().BeNull();
bar = sut(foo, new[] { "Component" });
bar.Should().NotBeNull();
bar.Component.Should().NotBeNull();
bar.Component.Id.Should().Be(foo.Component.Id);
}
[Test]
public void TestWriteOnly()
{
var foo = new Foo { Name = "i am foo" };
var bar = CreateSut()(foo, new string[0]);
bar.Should().NotBeNull();
bar.Name.Should().BeNullOrEmpty();
}
private static Func<Foo, IEnumerable<string>, Bar> CreateSut(bool withExpands = false)
{
var factory = new MapperFactory(
new FooBarMap(withExpands),
new BazQuxMap()
);
//var sut = factory.GetCreator<Foo, Bar>();
var sut = factory.GetReader<Foo, Bar>();
return sut;
}
private class FooBarMap : TypeMap<Foo, Bar>
{
public FooBarMap(bool withExpands)
{
MapProperties(_ => _.Id, _ => _.Id).ReadOnly();
MapProperties(_ => _.Name, _ => _ + _, _ => _.Name, _ => _.Substring(0, 1)).WriteOnly();
var expandable = MapComponents(_ => _.Component, _ => _.Component);
if (withExpands)
expandable.Expandable();
expandable = MapComponentCollections(_ => _.Bazes, _ => _.Quxes);
if (withExpands)
expandable.Expandable();
MapCollections(_ => _.Bazes, _ => _.Names, _ => _.Id.ToString());
}
}
private class BazQuxMap : TypeMap<Baz, Qux>
{
public BazQuxMap()
{
MapProperties(_ => _.Id, _ => _.Id);
}
}
}
}