-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathWhenUsingDestinationValue.cs
More file actions
118 lines (98 loc) · 3.76 KB
/
WhenUsingDestinationValue.cs
File metadata and controls
118 lines (98 loc) · 3.76 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
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
namespace Mapster.Tests
{
[TestClass]
public class WhenUsingDestinationValue
{
[TestMethod]
public void MapUsingDestinationValue()
{
TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileWithDebugInfo();
TypeAdapterConfig.GlobalSettings.Default.ShallowCopyForSameType(true);
TypeAdapterConfig<Invoice, InvoiceDto>.NewConfig().TwoWays();
var strings = new[] { "One, Two, Three" };
var dto = new InvoiceDto
{
Id = 1,
DocumentNumber = "AA001",
SupplierCompany = "COM01",
SupplierName = "Apple",
Numbers = Enumerable.Range(1, 5).ToList(),
Strings = strings,
};
var poco = dto.Adapt<Invoice>();
poco.Id.ShouldBe(dto.Id);
poco.DocumentNumber.ShouldBe("FOO");
poco.Supplier.Name.ShouldBe(dto.SupplierName);
poco.Supplier.Company.ShouldBe(dto.SupplierCompany);
poco.Numbers.ShouldBe(Enumerable.Range(1, 5));
poco.Strings.ShouldBe(strings);
}
/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/410
/// </summary>
[TestMethod]
public void MappingToReadonlyPropertyWhenPocoDetectRegression()
{
var studentDto = new StudentDtoOrigin { Name = "Marta" };
Should.NotThrow(() =>
{
var student = studentDto.Adapt<StudentOrigin>(); // No exception.
});
}
[TestMethod]
public void MappingToReadonlyAutoPropertyPrimitiveOrImmutableType()
{
var studentDto = new StudentDtoOrigin { Name = "Marta" };
var UseDestinationValue = studentDto.Adapt<StudentOrigin>();
var NotUseDestinationValue = studentDto.Adapt<StudentOriginNoUseDestinationValue>();
UseDestinationValue.Name.ShouldBe(studentDto.Name);
NotUseDestinationValue.Name.ShouldBe("John"); // not modified Name == "John" - origin value
}
#region TestClasses
public class StudentOriginNoUseDestinationValue
{
public string Name { get; } = "John"; // readonly primitive type autoproperty
}
public class StudentOrigin
{
[UseDestinationValue]
public string Name { get; } = "John"; // readonly primitive type autoproperty
}
public class StudentDtoOrigin
{
public string Name { get; set; }
}
public class ContractingParty
{
public string Name { get; set; }
public string Company { get; set; }
}
public class Invoice
{
public long Id { get; set; }
[UseDestinationValue]
public string DocumentNumber { get; set; } = "FOO";
[UseDestinationValue]
public ContractingParty Supplier { get; } = new ContractingParty();
[UseDestinationValue]
public ICollection<int> Numbers { get; } = new List<int>();
[UseDestinationValue]
public ICollection<string> Strings { get; } = new List<string>();
}
public class InvoiceDto
{
public long Id { get; set; }
public string DocumentNumber { get; set; }
public string SupplierName { get; set; }
public string SupplierCompany { get; set; }
public IEnumerable<int> Numbers { get; set; }
public ICollection<string> Strings { get; set; }
}
#endregion TestClasses
}
}