forked from annulusgames/Alchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamespaceTests.cs
More file actions
192 lines (166 loc) · 6.68 KB
/
Copy pathNamespaceTests.cs
File metadata and controls
192 lines (166 loc) · 6.68 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
namespace Alchemy.SourceGenerator.Tests;
/// <summary>
/// Namespace emission — the generator reconstructs the containing namespace by hand,
/// so every namespace shape needs covering.
/// </summary>
public class NamespaceTests
{
[Test]
public async Task Block_scoped_namespace_is_reproduced()
{
var result = GeneratorUtils.Run("""
using System;
using System.Collections.Generic;
using Alchemy.Serialization;
namespace Outer.Inner.Deep
{
[AlchemySerialize]
public partial class Deeply
{
[AlchemySerializeField, NonSerialized]
public Dictionary<string, int> map = new();
}
}
""");
await Assert.That(result.AllGeneratedText).Contains("namespace Outer.Inner.Deep {");
await Assert.That(result.DescribeCompilationErrors()).IsEqualTo("<none>");
}
[Test]
public async Task File_scoped_namespace_is_supported()
{
var result = GeneratorUtils.Run("""
using System;
using System.Collections.Generic;
using Alchemy.Serialization;
namespace FileScoped;
[AlchemySerialize]
public partial class Sample
{
[AlchemySerializeField, NonSerialized]
public Dictionary<string, int> map = new();
}
""");
await Assert.That(result.AllGeneratedText).Contains("namespace FileScoped {");
await Assert.That(result.DescribeCompilationErrors()).IsEqualTo("<none>");
}
[Test]
public async Task Global_namespace_emits_no_namespace_block()
{
var result = GeneratorUtils.Run("""
using System;
using System.Collections.Generic;
using Alchemy.Serialization;
[AlchemySerialize]
public partial class NoNamespace
{
[AlchemySerializeField, NonSerialized]
public Dictionary<string, int> map = new();
}
""");
await Assert.That(result.AllGeneratedText).DoesNotContain("namespace ");
await Assert.That(result.GeneratedSources[0].HintName)
.IsEqualTo("NoNamespace.AlchemySerializeGenerator.g.cs");
await Assert.That(result.DescribeCompilationErrors()).IsEqualTo("<none>");
}
[Test]
public async Task Two_same_named_types_in_different_namespaces_do_not_collide()
{
// Both produce a type called "Sample"; the hint name must disambiguate them
// or AddSource throws on the duplicate.
var result = GeneratorUtils.Run("""
using System;
using System.Collections.Generic;
using Alchemy.Serialization;
namespace First
{
[AlchemySerialize]
public partial class Sample
{
[AlchemySerializeField, NonSerialized]
public Dictionary<string, int> map = new();
}
}
namespace Second
{
[AlchemySerialize]
public partial class Sample
{
[AlchemySerializeField, NonSerialized]
public HashSet<int> set = new();
}
}
""");
await Assert.That(result.GeneratedSources.Length).IsEqualTo(2);
await Assert.That(result.HasDiagnostic("AlchemySerializeGeneratorError")).IsFalse();
var hintNames = result.GeneratedSources.Select(s => s.HintName).OrderBy(x => x).ToArray();
await Assert.That(hintNames[0]).IsEqualTo("First.Sample.AlchemySerializeGenerator.g.cs");
await Assert.That(hintNames[1]).IsEqualTo("Second.Sample.AlchemySerializeGenerator.g.cs");
await Assert.That(result.DescribeCompilationErrors()).IsEqualTo("<none>");
}
[Test]
public async Task Backing_field_name_is_namespace_qualified_so_it_cannot_clash()
{
var result = GeneratorUtils.Run("""
using System;
using System.Collections.Generic;
using Alchemy.Serialization;
namespace My.Game
{
[AlchemySerialize]
public partial class Player
{
[AlchemySerializeField, NonSerialized]
public Dictionary<string, int> map = new();
}
}
""");
await Assert.That(result.AllGeneratedText).Contains("__alchemySerializationData_My_Game_Player");
}
[Test]
public async Task Dictionary_types_are_globally_qualified_when_root_namespace_is_shadowed()
{
var result = GeneratorUtils.Run("""
using System;
using System.Collections.Generic;
using Alchemy.Serialization;
using Artillery.Entities;
using Artillery.Entities.Units;
namespace Artillery.Artillery
{
public sealed class NamespaceCollision { }
}
namespace Artillery.Entities
{
public enum UnitTeam
{
Ally,
Enemy
}
}
namespace Artillery.Entities.Units
{
public class BasicUnit : UnityEngine.MonoBehaviour { }
}
namespace Artillery.Entities.Units.Configuration
{
[AlchemySerialize]
public partial class UnitConfiguration
{
[AlchemySerializeField, NonSerialized]
public Dictionary<UnitTeam, UnityEngine.GameObject> teamObjects = new();
[AlchemySerializeField, NonSerialized]
public Dictionary<int, BasicUnit> units = new();
[AlchemySerializeField, NonSerialized]
public Dictionary<UnitTeam, BasicUnit> teamUnits = new();
}
}
""");
await Assert.That(result.AllGeneratedText).Contains(
"FromJson<global::System.Collections.Generic.Dictionary<global::Artillery.Entities.UnitTeam, global::UnityEngine.GameObject>>");
await Assert.That(result.AllGeneratedText).Contains(
"FromJson<global::System.Collections.Generic.Dictionary<int, global::Artillery.Entities.Units.BasicUnit>>");
await Assert.That(result.AllGeneratedText).Contains(
"FromJson<global::System.Collections.Generic.Dictionary<global::Artillery.Entities.UnitTeam, global::Artillery.Entities.Units.BasicUnit>>");
await Assert.That(result.DescribeCompilationErrors()).IsEqualTo("<none>");
}
}