-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPropertyAssociationTests.cs
101 lines (82 loc) · 3.03 KB
/
PropertyAssociationTests.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
namespace EntityFunctors.Tests
{
using System;
using System.Linq.Expressions;
using Associations.Impl;
using EntityFunctors.Associations;
using EntityFunctors.Extensions;
using FluentAssertions;
using Helpers;
using Mappers;
using NUnit.Framework;
[TestFixture]
public class PropertyAssociationTests
{
[Test]
public void TestIntMapping()
{
Foo foo;
Bar bar;
var sut = new PropertyToPropertyAssociation<Foo, Bar, int>(_ => _.Id,_ => _.Id);
foo = new Foo { Id = 5 };
bar = CreateReader(sut)(foo);
bar.Should().NotBeNull();
bar.Id.Should().Be(foo.Id);
foo = new Foo { Id = 5 };
bar = new Bar { Id = 6 };
CreateWriter(sut)(bar, foo);
foo.Id.Should().Be(bar.Id);
}
[Test]
public void TestStringMapping()
{
Foo foo;
Bar bar;
var sut = new PropertyToPropertyAssociation<Foo, Bar, string>(_ => _.Name,_ => _.Name);
foo = new Foo { Name = "aaa" };
bar = CreateReader(sut)(foo);
bar.Should().NotBeNull();
bar.Id.Should().Be(foo.Id);
foo = new Foo { Name = "aaa" };
bar = new Bar { Name = "bbb" };
CreateWriter(sut)(bar, foo);
foo.Id.Should().Be(bar.Id);
}
[Test]
public void TestIntToStringMapping()
{
Foo foo;
Bar bar;
var sut = new PropertyToPropertyWithConversionAssociation<Foo, int, Bar, string>(_ => _.Id, _ => _.ToString(), _ => _.Name, _ => int.Parse(_));
foo = new Foo { Id = 123 };
bar = CreateReader(sut)(foo);
bar.Should().NotBeNull();
bar.Name.Should().Be(foo.Id.ToString());
foo = new Foo { Id = 123 };
bar = new Bar { Name = "555" };
CreateWriter(sut)(bar, foo);
foo.Id.Should().Be(int.Parse(bar.Name));
}
[Test]
public void TestStringToIntDefaultMapping()
{
Foo foo;
Bar bar;
var sut = new PropertyToPropertyWithConversionAssociation<Foo, int, Bar, string>(_ => _.Id, _ => _.ToString(), _ => _.Name, _ => int.Parse(_));
foo = new Foo { Id = 123 };
bar = new Bar { Name = string.Empty };
CreateWriter(sut)(bar, foo);
foo.Id.Should().Be(default(int));
}
private static Func<Foo, Bar> CreateReader(IMappingAssociation association)
{
var factory = new MapperFactory(new TestMap(typeof(Foo), typeof(Bar), association));
return _ => factory.GetReader<Foo, Bar>()(_, null);
}
private static Action<Bar, Foo> CreateWriter(IMappingAssociation association)
{
var factory = new MapperFactory(new TestMap(typeof(Foo), typeof(Bar), association));
return (source, target) => factory.GetWriter<Bar, Foo>()(source, target, null);
}
}
}