-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAssignedMapperTests.cs
148 lines (104 loc) · 3.61 KB
/
AssignedMapperTests.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
namespace EntityFunctors.Tests
{
using System;
using System.Collections.Generic;
using FluentAssertions;
using Helpers;
using Mappers;
using NUnit.Framework;
[TestFixture]
public class AssignedMapperTests
{
[Test]
public void TestProperty()
{
var bar = new Bar {Name = "123"};
var sut = CreateSut();
var foo = new Foo();
sut(bar, foo, null);
foo.Name.Should().Be(bar.Name.Substring(0,1));
}
[Test]
public void TestComponentProperty()
{
var bar = new Bar { Component = new Qux { Id = 1 } };
var foo = new Foo {Component = new Baz()};
var sut = CreateSut();
sut(bar, foo, null);
foo.Component.Id.Should().Be(bar.Component.Id);
}
[Test]
public void TestReadonlyPropertyNotAssigned()
{
var bar = new Bar {Id = 10};
var sut = CreateSut();
var foo = new Foo();
sut(bar, foo, null);
foo.Id.Should().Be(0);
}
[Test]
public void TestCollectionNotAssigned()
{
var bar = new Bar {Names = new[] {"1", "2", "3"}};
var foo = new Foo();
var sut = CreateSut();
sut(bar, foo, null);
foo.Bazes.Should().BeNull();
}
[Test]
public void TestComponentCollectionNotAssigned()
{
var bar = new Bar { Quxes = new[] { new Qux{Id = 1}, new Qux {Id = 2} } };
var foo = new Foo();
var sut = CreateSut();
sut(bar, foo, null);
foo.Bazes.Should().BeNull();
}
[Test]
public void TestPartialAssignment()
{
var bar = new Bar { Name = "123", Component = new Qux { Id = 1 } };
var sut = CreateSut();
var foo = new Foo { Component = new Baz() };
sut(bar, foo, new[] {"Name"});
foo.Name.Should().Be(bar.Name.Substring(0, 1));
foo.Component.Id.Should().Be(0);
foo = new Foo { Component = new Baz() };
sut(bar, foo, new[] { "Component" });
foo.Name.Should().BeNullOrEmpty();
foo.Component.Id.Should().Be(bar.Component.Id);
foo = new Foo { Component = new Baz() };
sut(bar, foo, new[] {"Name", "Component"});
foo.Name.Should().Be(bar.Name.Substring(0, 1));
foo.Component.Id.Should().Be(bar.Component.Id);
}
private static Action<Bar, Foo, IEnumerable<string>> CreateSut()
{
var factory = new MapperFactory(
new FooBarMap(),
new BazQuxMap()
);
//var sut = factory.GetAssigner<Bar, Foo>();
var sut = factory.GetWriter<Bar, Foo>();
return sut;
}
private class FooBarMap : TypeMap<Foo, Bar>
{
public FooBarMap()
{
MapProperties(_ => _.Id, _ => _.Id).ReadOnly();
MapProperties(_ => _.Name, _ => _ + _, _ => _.Name, _ => _.Substring(0, 1)).WriteOnly();
MapComponents(_ => _.Component, _ => _.Component);
MapComponentCollections(_ => _.Bazes, _ => _.Quxes);
MapCollections(_ => _.Bazes, _ => _.Names, _ => _.Id.ToString());
}
}
private class BazQuxMap : TypeMap<Baz, Qux>
{
public BazQuxMap()
{
MapProperties(_ => _.Id, _ => _.Id);
}
}
}
}