-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathWhenNullPropagationRegression.cs
More file actions
109 lines (87 loc) · 2.95 KB
/
WhenNullPropagationRegression.cs
File metadata and controls
109 lines (87 loc) · 2.95 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
using Mapster.Tests.Classes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
namespace Mapster.Tests;
[TestClass]
public class WhenNullPropagationRegression
{
[TestMethod]
public void WhenCustomDefaultWorked()
{
string customdefault = "42";
TypeAdapterConfig<SimplePocoToNullPropagation, SimpleDtoToNullPropagation>.NewConfig()
.Map(dest => dest.AnotherName, src => src.Name).SourceDefaultValue(customdefault)
.Map(dest => dest.LastModified, src => DateTime.Now)
.Compile();
var poco = new SimplePocoToNullPropagation { Id = Guid.NewGuid(), Name = null};
var dto = poco.Adapt<SimpleDtoToNullPropagation>();
dto.Id.ShouldBe(poco.Id);
dto.Name.ShouldBe(null);
dto.AnotherName.ShouldBe(customdefault);
}
[TestMethod]
public void WhenCustomDefaultMapPathWorked()
{
string customdefault = "Default Location";
TypeAdapterConfig<PocoToPathNullPropagation, DtoToPathNullPropagation>.NewConfig()
.Map(dest => dest.Location, src => src.Child.Address.Location).SourceDefaultValue(customdefault);
var poco = new PocoToPathNullPropagation
{
Id = Guid.NewGuid(),
Child = new()
};
var dto = poco.Adapt<DtoToPathNullPropagation>();
dto.Id.ShouldBe(poco.Id);
dto.Location.ShouldBe(customdefault);
}
[TestMethod]
public void WhenThrowWhenNullWorked()
{
TypeAdapterConfig<PocoToPathNullPropagation, DtoToPathNullPropagation>.NewConfig()
.Map(dest => dest.Location, src => src.Child.Address.Location).ThrowWhenNull();
var poco = new PocoToPathNullPropagation
{
Id = Guid.NewGuid(),
Child = new()
};
Should.Throw<NullReferenceException>(() =>
{
var dto = poco.Adapt<DtoToPathNullPropagation>();
});
}
#region Classes
public class SimplePocoToNullPropagation
{
public Guid Id { get; set; }
public string? Name { get; set; }
}
public class SimpleDtoToNullPropagation
{
public Guid Id { get; set; }
public string Name { get; set; }
public string AnotherName { get; set; }
public DateTime LastModified { get; set; }
}
public class PocoToPathNullPropagation
{
public Guid Id { get; set; }
public ChildPocoToNullPropagation? Child { get; set; }
}
public class DtoToPathNullPropagation
{
public Guid Id { get; set; }
public Address Address { get; set; }
public string Location { get; set; }
}
public class ChildPocoToNullPropagation
{
public AddressNullPropagation? Address { get; set; }
}
public class AddressNullPropagation
{
public string? Number { get; set; }
public string? Location { get; set; }
}
#endregion Classes
}