forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhenPropertyNullablePropagationRegression.cs
More file actions
113 lines (92 loc) · 2.4 KB
/
WhenPropertyNullablePropagationRegression.cs
File metadata and controls
113 lines (92 loc) · 2.4 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System.Threading.Tasks;
namespace Mapster.Tests;
[TestClass]
public class WhenPropertyNullablePropagationRegression
{
/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/858
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task NotNullableStructMapToNotNullableCorrect()
{
TypeAdapterConfig<Foo858, Bar858>
.NewConfig()
.Map(dest => dest.Amount, src => src.Amount)
.Map(dest => dest.InnerAmount, src => src.Inner.Amount);
Foo858 foo = new()
{
Amount = new(1, Currency858.Usd),
Inner = new()
{
Amount = new(10, Currency858.Eur),
Int = 100,
}
};
// Act
var bar = foo.Adapt<Bar858>();
// Assert
bar.InnerAmount.Amount.ShouldBe(10m);
}
[TestMethod]
public async Task NotNullableStructMapToNullableCorrect()
{
TypeAdapterConfig<Foo858, Bar858Nullable>
.NewConfig()
.Map(dest => dest.Amount, src => src.Amount)
.Map(dest => dest.InnerAmount, src => src.Inner.Amount);
Foo858 foo = new()
{
Amount = new(1, Currency858.Usd),
Inner = new()
{
Amount = new(10, Currency858.Eur),
Int = 100,
}
};
// Act
var bar = foo.Adapt<Bar858Nullable>();
// Assert
bar.InnerAmount?.Amount.ShouldBe(10m);
}
}
#region TestClasses
public enum Currency858
{
Eur,
Usd,
Ron
}
file class Foo858
{
public required Money858 Amount { get; set; }
public required FooInner858 Inner { get; set; }
}
file class FooInner858
{
public required Money858 Amount { get; set; }
public int Int { get; set; }
}
file class Bar858
{
public Money858 Amount { get; set; }
public Money858 InnerAmount { get; set; }
}
file class Bar858Nullable
{
public Money858? Amount { get; set; }
public Money858? InnerAmount { get; set; }
}
public struct Money858
{
public decimal? Amount { get; set; }
public Currency858 Currency { get; set; } = Currency858.Ron;
public Money858(decimal? amount, Currency858 currency = Currency858.Eur)
{
Amount = amount;
Currency = currency;
}
}
#endregion TestClasses