Repository navigation
Expand file tree
/
Copy pathQuick.Core.Extensions.Localization.Tests.pas
More file actions
234 lines (204 loc) · 6.86 KB
/
Copy pathQuick.Core.Extensions.Localization.Tests.pas
File metadata and controls
234 lines (204 loc) · 6.86 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
unit Quick.Core.Extensions.Localization.Tests;
{ Tests: T-LOC-01..10
T-LOC-01 TLocalizationOptions.AddCulture stores culture names
T-LOC-02 TLocalizationOptions.AddIfNotFound defaults to False
T-LOC-03 TLocalizationOptions.AddIfNotFound is writable
T-LOC-04 TStringLocalization.Culture defaults to 'en'
T-LOC-05 TStringLocalization.Culture is settable
T-LOC-06 TStringLocalization.Items returns key when translation is found
T-LOC-07 TStringLocalization.Items returns key name when NOT found (passthrough)
T-LOC-08 TFileLocalizationStore.GetCultureTranslations for missing file returns empty dict
T-LOC-09 TFileLocalizationStore.GetCultureTranslations normalizes 'en-US' to 'en'
T-LOC-10 TStringLocalization can switch culture
}
interface
uses
DUnitX.TestFramework,
System.SysUtils,
System.Generics.Collections,
Quick.Core.Localization.Abstractions,
Quick.Core.Extensions.Localization;
type
// ── Mock store: returns a fixed dictionary for 'en', empty for others ──────
TTestLocalizationStore = class(TInterfacedObject, ILocalizationStore)
private
fData : TDictionary<string,string>;
public
constructor Create;
destructor Destroy; override;
function GetCultureTranslations(const aCulture : string) : TDictionary<string,string>;
end;
[TestFixture]
TLocalizationTests = class
private
fStore : TTestLocalizationStore;
fLoc : TStringLocalization;
public
[Setup]
procedure SetUp;
[TearDown]
procedure TearDown;
[Test]
procedure T_LOC_01_Options_AddCulture_Stores;
[Test]
procedure T_LOC_02_Options_AddIfNotFound_DefaultFalse;
[Test]
procedure T_LOC_03_Options_AddIfNotFound_Writable;
[Test]
procedure T_LOC_04_Localization_Culture_DefaultEn;
[Test]
procedure T_LOC_05_Localization_Culture_Settable;
[Test]
procedure T_LOC_06_Localization_Items_Found;
[Test]
procedure T_LOC_07_Localization_Items_NotFound_Passthrough;
[Test]
procedure T_LOC_08_FileStore_MissingFile_EmptyDict;
[Test]
procedure T_LOC_09_FileStore_NormalizesCulture;
[Test]
procedure T_LOC_10_Localization_SwitchCulture;
end;
implementation
{ TTestLocalizationStore }
constructor TTestLocalizationStore.Create;
begin
fData := TDictionary<string,string>.Create;
fData.Add('greeting', 'Hello');
fData.Add('farewell', 'Goodbye');
end;
destructor TTestLocalizationStore.Destroy;
begin
fData.Free;
inherited;
end;
function TTestLocalizationStore.GetCultureTranslations(const aCulture : string) : TDictionary<string,string>;
begin
// Return the fixed dictionary for any culture in this mock
Result := fData;
end;
{ TLocalizationTests }
procedure TLocalizationTests.SetUp;
begin
fStore := TTestLocalizationStore.Create;
fLoc := TStringLocalization.Create(fStore);
end;
procedure TLocalizationTests.TearDown;
begin
fLoc.Free;
// fStore is a TInterfacedObject — freed via interface refcount when fLoc is freed;
// calling Free again here would cause a double-free / Invalid pointer operation.
end;
// ─── TLocalizationOptions ─────────────────────────────────────────────────────
procedure TLocalizationTests.T_LOC_01_Options_AddCulture_Stores;
var
opts : TLocalizationOptions;
begin
opts := TLocalizationOptions.Create;
try
opts.AddCulture('en');
opts.AddCulture('es');
// No crash means the cultures were accepted
Assert.Pass('AddCulture accepted two cultures without exception');
finally
opts.Free;
end;
end;
procedure TLocalizationTests.T_LOC_02_Options_AddIfNotFound_DefaultFalse;
var
opts : TLocalizationOptions;
begin
opts := TLocalizationOptions.Create;
try
Assert.IsFalse(opts.AddIfNotfound,
'AddIfNotFound should default to False');
finally
opts.Free;
end;
end;
procedure TLocalizationTests.T_LOC_03_Options_AddIfNotFound_Writable;
var
opts : TLocalizationOptions;
begin
opts := TLocalizationOptions.Create;
try
opts.AddIfNotfound := True;
Assert.IsTrue(opts.AddIfNotfound);
finally
opts.Free;
end;
end;
// ─── TStringLocalization ──────────────────────────────────────────────────────
procedure TLocalizationTests.T_LOC_04_Localization_Culture_DefaultEn;
begin
Assert.AreEqual('en', fLoc.Culture,
'Default culture should be "en"');
end;
procedure TLocalizationTests.T_LOC_05_Localization_Culture_Settable;
begin
fLoc.Culture := 'es';
Assert.AreEqual('es', fLoc.Culture);
end;
procedure TLocalizationTests.T_LOC_06_Localization_Items_Found;
var
result : string;
begin
result := fLoc.Items['greeting'];
Assert.AreEqual('Hello', result,
'"greeting" should translate to "Hello" in the mock store');
end;
procedure TLocalizationTests.T_LOC_07_Localization_Items_NotFound_Passthrough;
var
result : string;
begin
// 'unknown_key' is not in the mock dictionary — should return the key itself
result := fLoc.Items['unknown_key'];
Assert.AreEqual('unknown_key', result,
'When a key is missing, GetItem should return the key name as fallback');
end;
// ─── TFileLocalizationStore ───────────────────────────────────────────────────
procedure TLocalizationTests.T_LOC_08_FileStore_MissingFile_EmptyDict;
var
store : TFileLocalizationStore;
dict : TDictionary<string,string>;
begin
store := TFileLocalizationStore.Create;
try
// No locale files exist in the test output dir — should return empty dict
dict := store.GetCultureTranslations('zz');
Assert.IsNotNull(dict,
'GetCultureTranslations should return an empty dict (not nil) for a missing locale file');
Assert.AreEqual<Integer>(0, dict.Count,
'Translation dict should be empty for a non-existent locale file');
finally
store.Free;
end;
end;
procedure TLocalizationTests.T_LOC_09_FileStore_NormalizesCulture;
var
store : TFileLocalizationStore;
dict1 : TDictionary<string,string>;
dict2 : TDictionary<string,string>;
begin
store := TFileLocalizationStore.Create;
try
// 'en-US' should normalise to 'en' — both calls should hit the same cached entry
dict1 := store.GetCultureTranslations('en-US');
dict2 := store.GetCultureTranslations('en');
Assert.AreSame(dict1, dict2,
'GetCultureTranslations("en-US") and ("en") should return the same cached dict');
finally
store.Free;
end;
end;
procedure TLocalizationTests.T_LOC_10_Localization_SwitchCulture;
begin
fLoc.Culture := 'fr';
Assert.AreEqual('fr', fLoc.Culture,
'Culture should reflect the last SetCulture call');
fLoc.Culture := 'de';
Assert.AreEqual('de', fLoc.Culture);
end;
initialization
TDUnitX.RegisterTestFixture(TLocalizationTests);
end.