-
Notifications
You must be signed in to change notification settings - Fork 716
Expand file tree
/
Copy pathLocalizationTests.cs
More file actions
275 lines (231 loc) · 7.99 KB
/
Copy pathLocalizationTests.cs
File metadata and controls
275 lines (231 loc) · 7.99 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Collections.Generic;
using System.Globalization;
using NUnit.Framework;
using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Localization;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Utility;
namespace Robust.UnitTesting.Shared.Localization
{
[TestFixture]
internal sealed class LocalizationTests : OurRobustUnitTest
{
protected override Type[] ExtraComponents => new[] {typeof(GrammarComponent)};
[OneTimeSetUp]
public void Setup()
{
IoCManager.Resolve<ISerializationManager>().Initialize();
var res = IoCManager.Resolve<IResourceManagerInternal>();
res.MountString("/Locale/en-US/a.ftl", FluentCode);
res.MountString("/EnginePrototypes/a.yml", YAMLCode);
var protoMan = IoCManager.Resolve<IPrototypeManager>();
protoMan.RegisterKind(typeof(EntityPrototype), typeof(EntityCategoryPrototype));
protoMan.LoadDirectory(new ResPath("/EnginePrototypes"));
protoMan.ResolveResults();
var loc = IoCManager.Resolve<ILocalizationManager>();
var culture = new CultureInfo("en-US", false);
loc.Initialize();
loc.LoadCulture(culture);
}
private const string YAMLCode = @"
# Values specified in prototype
- type: entity
id: PropsInPrototype
name: A
description: B
suffix: C
loc:
gender: male
proper: true
# Values specified in fluent
- type: entity
id: PropsInLoc
# Values specified in YAML but overriden by fluent.
- type: entity
id: PropsInLocOverriding
name: XA
description: XB
suffix: XC
loc:
gender: female
proper: false
# Values specified in fluent at PARENT and replaced by child YAML.
- type: entity
id: TestInheritOverridingParent
- type: entity
id: TestInheritOverridingChild
parent: TestInheritOverridingParent
name: A
description: B
suffix: C
loc:
gender: male
proper: true
# Values specified in fluent at PARENT and supplemented by empty child fluent.
- type: entity
id: TestInheritEmptyValueParent
- type: entity
id: TestInheritEmptyValueChild
parent: TestInheritEmptyValueParent
# Attribures stored in grammar component
- type: entity
id: PropsInGrammar
name: A
description: B
suffix: C
loc:
gender: female
proper: false
components:
- type: Grammar
attributes:
gender: male
proper: true
- type: entity
id: GenderTestEntityNoComp
- type: entity
id: GenderTestEntityWithComp
components:
- type: Grammar
attributes:
gender: Female
";
private const string FluentCode = @"
enum-match = { $enum ->
[foo] A
*[bar] B
}
num-selector = { $num ->
[0] A
[1] B
*[2] C
}
ent-GenderTestEntityNoComp = Gender Test Entity
.gender = male
.otherAttrib = sausages
ent-GenderTestEntityWithComp = Gender Test Entity 2
ent-PropsInLoc = A
.desc = B
.suffix = C
.gender = male
.proper = true
ent-PropsInLocOverriding = A
.desc = B
.suffix = C
.gender = male
.proper = true
ent-TestInheritOverridingParent = XA
.desc = XB
.suffix = XC
.gender = female
.proper = false
ent-TestInheritEmptyValueParent = A
.desc = B
.gender = male
.proper = true
ent-TestInheritEmptyValueChild =
.suffix = C
test-message-gender = { GENDER($entity) ->
[male] male
[female] female
*[neuter] BAD
}
test-message-proper = { PROPER($entity) ->
[true] true
*[false] false
}
test-message-custom-attrib = { ATTRIB($entity, ""otherAttrib"") }
";
[Test]
public void TestEnumSelect()
{
var loc = IoCManager.Resolve<ILocalizationManager>();
Assert.That(loc.GetString("enum-match", ("enum", TestEnum.Foo)), Is.EqualTo("A"));
Assert.That(loc.GetString("enum-match", ("enum", TestEnum.Bar)), Is.EqualTo("B"));
Assert.That(loc.GetString("enum-match", ("enum", TestEnum.Baz)), Is.EqualTo("B"));
}
[Test]
public void TestCustomFunctions()
{
var entMan = IoCManager.Resolve<IEntityManager>();
var testEntNoComp = entMan.CreateEntityUninitialized("GenderTestEntityNoComp");
var testEntWithComp = entMan.CreateEntityUninitialized("GenderTestEntityWithComp");
var loc = IoCManager.Resolve<ILocalizationManager>();
var genderFromAttrib = loc.GetString("test-message-gender", ("entity", testEntNoComp));
var genderFromGrammar = loc.GetString("test-message-gender", ("entity", testEntWithComp));
var customAttrib = loc.GetString("test-message-custom-attrib", ("entity", testEntNoComp));
Assert.Multiple(() =>
{
Assert.That(genderFromAttrib, Is.EqualTo("male"));
Assert.That(genderFromGrammar, Is.EqualTo("female"));
Assert.That(customAttrib, Is.EqualTo("sausages"));
});
}
static IEnumerable<object[]> NumericTestSource()
{
const byte b1 = 0, b2 = 1, b3 = 5;
const sbyte sb1 = 0, sb2 = 1, sb3 = 5;
const short sh1 = 0, sh2 = 1, sh3 = 5;
const ushort ush1 = 0, ush2 = 1, ush3 = 5;
const int i1 = 0, i2 = 1, i3 = 5;
const uint ui1 = 0, ui2 = 1, ui3 = 5;
const long lng1 = 0, lng2 = 1, lng3 = 5;
const ulong ulng1 = 0, ulng2 = 1, ulng3 = 5;
const float f1 = 0, f2 = 1, f3 = 5;
const double d1 = 0, d2 = 1, d3 = 5;
yield return new object[] { b1, b2, b3 };
yield return new object[] { sb1, sb2, sb3 };
yield return new object[] { sh1, sh2, sh3 };
yield return new object[] { ush1, ush2, ush3 };
yield return new object[] { i1, i2, i3 };
yield return new object[] { ui1, ui2, ui3 };
yield return new object[] { lng1, lng2, lng3 };
yield return new object[] { ulng1, ulng2, ulng3 };
yield return new object[] { f1, f2, f3 };
yield return new object[] { d1, d2, d3 };
}
[Test]
[TestCaseSource(nameof(NumericTestSource))]
public void TestNumbers(object o1, object o2, object o3)
{
// Small test to check numbers are being properly converted
var loc = IoCManager.Resolve<ILocalizationManager>();
var func = new Func<object, string>(x1 => loc.GetString("num-selector", ("num", x1)));
Assert.Multiple(() =>
{
Assert.That(func(o1), Is.EqualTo("A"));
Assert.That(func(o2), Is.EqualTo("B"));
Assert.That(func(o3), Is.EqualTo("C"));
});
}
[Test]
[TestCase("PropsInPrototype")]
[TestCase("PropsInLoc")]
[TestCase("PropsInLocOverriding")]
[TestCase("PropsInGrammar")]
[TestCase("TestInheritOverridingChild")]
[TestCase("TestInheritEmptyValueChild")]
public void TestLocData(string prototype)
{
var loc = IoCManager.Resolve<ILocalizationManager>();
var entMan = IoCManager.Resolve<IEntityManager>();
var ent = entMan.CreateEntityUninitialized(prototype);
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<MetaDataComponent>(ent).EntityName, Is.EqualTo("A"));
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<MetaDataComponent>(ent).EntityDescription, Is.EqualTo("B"));
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<MetaDataComponent>(ent).EntityPrototype!.EditorSuffix, Is.EqualTo("C"));
Assert.That(loc.GetString("test-message-gender", ("entity", ent)), Is.EqualTo("male"));
Assert.That(loc.GetString("test-message-proper", ("entity", ent)), Is.EqualTo("true"));
}
private enum TestEnum
{
Foo,
Bar,
Baz
}
}
}