/
BaseMapperTest.cs
130 lines (123 loc) · 5.06 KB
/
BaseMapperTest.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Runtime.CompilerServices;
using Riok.Mapperly.IntegrationTests.Dto;
using Riok.Mapperly.IntegrationTests.Models;
using VerifyTests;
using VerifyXunit;
#if !NET6_0_OR_GREATER
using Riok.Mapperly.IntegrationTests.Helpers;
#endif
namespace Riok.Mapperly.IntegrationTests
{
public abstract class BaseMapperTest
{
static BaseMapperTest()
{
#if !NET6_0_OR_GREATER
VerifierSettings.AddExtraSettings(settings =>
{
settings.Converters.Add(new PortableDateOnlyConverter());
settings.Converters.Add(new PortableTimeOnlyConverter());
});
#endif
VerifierSettings.DontScrubDateTimes();
Verifier.DerivePathInfo(
(file, _, type, method) =>
new PathInfo(Path.Combine(Path.GetDirectoryName(file)!, "_snapshots", GetPlatformVersion()), type.Name, method.Name)
);
}
protected string GetGeneratedMapperFilePath(string name, [CallerFilePath] string filePath = "")
{
return Path.Combine(
Path.GetDirectoryName(filePath)!,
"obj",
"GeneratedFiles",
"Riok.Mapperly",
"Riok.Mapperly.MapperGenerator",
name + ".g.cs"
);
}
public static TestObject NewTestObj()
{
return new TestObject(7)
{
IntValue = 10,
EnumName = TestEnum.Value10,
EnumValue = TestEnum.Value10,
IntInitOnlyValue = 3,
RequiredValue = 4,
NestedNullable = new TestObjectNested { IntValue = 100, },
StringValue = "fooBar",
SubObject = new InheritanceSubObject { BaseIntValue = 1, SubIntValue = 2, },
EnumRawValue = TestEnum.Value20,
EnumStringValue = TestEnum.Value30,
DateTimeValueTargetDateOnly = new DateTime(2020, 1, 3, 15, 10, 5, DateTimeKind.Utc),
DateTimeValueTargetTimeOnly = new DateTime(2020, 1, 3, 15, 10, 5, DateTimeKind.Utc),
IgnoredStringValue = "ignored",
RenamedStringValue = "fooBar2",
StringNullableTargetNotNullable = "fooBar3",
EnumReverseStringValue = nameof(TestEnumDtoByValue.DtoValue3),
NestedNullableTargetNotNullable = new(),
Flattening = new() { IdValue = 10 },
NullableFlattening = new() { IdValue = 100 },
UnflatteningIdValue = 20,
NullableUnflatteningIdValue = 200,
RecursiveObject = new(5)
{
EnumValue = TestEnum.Value10,
EnumName = TestEnum.Value30,
EnumReverseStringValue = nameof(TestEnumDtoByValue.DtoValue3),
RequiredValue = 4,
},
NullableReadOnlyObjectCollection = new[]
{
new TestObjectNested { IntValue = 10 },
new TestObjectNested { IntValue = 20 },
},
SourceTargetSameObjectType = new TestObject(8) { IntValue = 99, RequiredValue = 98, },
StackValue = new Stack<string>(new[] { "1", "2", "3" }),
QueueValue = new Queue<string>(new[] { "1", "2", "3" }),
ImmutableArrayValue = ImmutableArray.Create("1", "2", "3"),
ImmutableListValue = ImmutableList.Create("1", "2", "3"),
ImmutableHashSetValue = ImmutableHashSet.Create("1", "2", "3"),
ImmutableQueueValue = ImmutableQueue.Create("1", "2", "3"),
ImmutableStackValue = ImmutableStack.Create("1", "2", "3"),
ImmutableSortedSetValue = ImmutableSortedSet.Create("1", "2", "3"),
ImmutableDictionaryValue = new Dictionary<string, string>()
{
{ "1", "1" },
{ "2", "2" },
{ "3", "3" }
}.ToImmutableDictionary(),
ImmutableSortedDictionaryValue = new Dictionary<string, string>()
{
{ "1", "1" },
{ "2", "2" },
{ "3", "3" }
}.ToImmutableSortedDictionary(),
ExistingISet = { "1", "2", "3", },
ExistingHashSet = { "1", "2", "3", },
ExistingSortedSet = { "1", "2", "3", },
ISet = new HashSet<string> { "1", "2", "3", },
#if NET5_0_OR_GREATER
IReadOnlySet = new HashSet<string> { "1", "2", "3", },
#endif
HashSet = new HashSet<string> { "1", "2", "3", },
SortedSet = new SortedSet<string> { "1", "2", "3", }
};
}
private static string GetPlatformVersion()
{
#if NET48_OR_GREATER
return "NET_48";
#elif NET7_0_OR_GREATER
return "Roslyn_4_5";
#else
return "Roslyn_4_4_OR_LOWER";
#endif
}
}
}