Repository navigation
Expand file tree
/
Copy pathQuick.Core.Serialization.Tests.pas
More file actions
317 lines (287 loc) · 7.77 KB
/
Copy pathQuick.Core.Serialization.Tests.pas
File metadata and controls
317 lines (287 loc) · 7.77 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
unit Quick.Core.Serialization.Tests;
{ Tests: T-SER-01..14
T-SER-01 TJsonSerializer.FromObject serializes published properties to JSON
T-SER-02 TJsonSerializer.FromObject indented - JSON is non-empty
T-SER-03 TJsonSerializer.ToObject(Class, JSON) deserializes back
T-SER-04 TJsonSerializer.ToObject(Instance, JSON) fills existing instance
T-SER-05 TJsonSerializer.FromValue serializes an Integer TValue
T-SER-06 TJsonSerializer.Options returns non-nil
T-SER-07 TYamlSerializer.FromObject serializes to YAML (non-empty)
T-SER-08 TYamlSerializer.ToObject(Class, YAML) deserializes back
T-SER-09 TYamlSerializer.ToObject(Instance, YAML) fills existing instance
T-SER-10 TSerializers.Json returns non-nil TJsonSerializer
T-SER-11 TSerializers.Yaml returns non-nil TYamlSerializer
T-SER-12 TSerializers.Json and .Yaml are stable across multiple calls
T-SER-13 TJsonSerializer serialized output contains field value
T-SER-14 TYamlSerializer serialized output contains field value
}
interface
uses
DUnitX.TestFramework,
System.Rtti,
System.SysUtils,
Quick.Core.Serialization.Json,
Quick.Core.Serialization.Yaml,
Quick.Core.Serialization;
type
{$M+}
TSerPerson = class
private
fName : string;
fAge : Integer;
published
property Name : string read fName write fName;
property Age : Integer read fAge write fAge;
end;
{$M-}
[TestFixture]
TSerializationTests = class
private
fJson : TJsonSerializer;
fYaml : TYamlSerializer;
fHub : TSerializers;
public
[Setup]
procedure SetUp;
[TearDown]
procedure TearDown;
[Test]
procedure T_SER_01_Json_FromObject_NonEmpty;
[Test]
procedure T_SER_02_Json_FromObject_Indented_NonEmpty;
[Test]
procedure T_SER_03_Json_ToObject_Class_Deserializes;
[Test]
procedure T_SER_04_Json_ToObject_Instance_Fills;
[Test]
procedure T_SER_05_Json_FromValue_Integer;
[Test]
procedure T_SER_06_Json_Options_NotNil;
[Test]
procedure T_SER_07_Yaml_FromObject_NonEmpty;
[Test]
procedure T_SER_08_Yaml_ToObject_Class_Deserializes;
[Test]
procedure T_SER_09_Yaml_ToObject_Instance_Fills;
[Test]
procedure T_SER_10_Serializers_Json_NotNil;
[Test]
procedure T_SER_11_Serializers_Yaml_NotNil;
[Test]
procedure T_SER_12_Serializers_Json_StableAcrossCalls;
[Test]
procedure T_SER_13_Json_Output_ContainsFieldValue;
[Test]
procedure T_SER_14_Yaml_Output_ContainsFieldValue;
end;
implementation
{ TSerializationTests }
procedure TSerializationTests.SetUp;
begin
fJson := TJsonSerializer.Create;
fYaml := TYamlSerializer.Create;
fHub := TSerializers.Create;
end;
procedure TSerializationTests.TearDown;
begin
fHub.Free;
fYaml.Free;
fJson.Free;
end;
// ─── JSON ─────────────────────────────────────────────────────────────────────
procedure TSerializationTests.T_SER_01_Json_FromObject_NonEmpty;
var
p : TSerPerson;
json : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Alice';
p.Age := 30;
json := fJson.FromObject(p);
Assert.IsNotEmpty(json, 'JSON output should not be empty');
finally
p.Free;
end;
end;
procedure TSerializationTests.T_SER_02_Json_FromObject_Indented_NonEmpty;
var
p : TSerPerson;
json : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Bob';
p.Age := 25;
json := fJson.FromObject(p, True {aIndent});
Assert.IsNotEmpty(json, 'Indented JSON output should not be empty');
finally
p.Free;
end;
end;
procedure TSerializationTests.T_SER_03_Json_ToObject_Class_Deserializes;
var
p : TSerPerson;
p2 : TSerPerson;
json : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Carol';
p.Age := 40;
json := fJson.FromObject(p);
finally
p.Free;
end;
p2 := fJson.ToObject(TSerPerson, json) as TSerPerson;
try
Assert.AreEqual('Carol', p2.Name);
Assert.AreEqual(40, p2.Age);
finally
p2.Free;
end;
end;
procedure TSerializationTests.T_SER_04_Json_ToObject_Instance_Fills;
var
p : TSerPerson;
p2 : TSerPerson;
json : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Dan';
p.Age := 50;
json := fJson.FromObject(p);
finally
p.Free;
end;
p2 := TSerPerson.Create;
try
fJson.ToObject(p2, json);
Assert.AreEqual('Dan', p2.Name);
Assert.AreEqual(50, p2.Age);
finally
p2.Free;
end;
end;
procedure TSerializationTests.T_SER_05_Json_FromValue_Integer;
var
v : TValue;
json : string;
begin
v := TValue.From<Integer>(99);
json := fJson.FromValue(v);
Assert.IsTrue(json.Contains('99'), 'JSON of integer 99 should contain "99"');
end;
procedure TSerializationTests.T_SER_06_Json_Options_NotNil;
begin
Assert.IsNotNull(fJson.Options, 'JsonSerializer.Options should not be nil');
end;
// ─── YAML ────────────────────────────────────────────────────────────────────
procedure TSerializationTests.T_SER_07_Yaml_FromObject_NonEmpty;
var
p : TSerPerson;
yaml : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Eve';
p.Age := 22;
yaml := fYaml.FromObject(p);
Assert.IsNotEmpty(yaml, 'YAML output should not be empty');
finally
p.Free;
end;
end;
procedure TSerializationTests.T_SER_08_Yaml_ToObject_Class_Deserializes;
var
p : TSerPerson;
p2 : TSerPerson;
yaml : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Frank';
p.Age := 35;
yaml := fYaml.FromObject(p);
finally
p.Free;
end;
p2 := fYaml.ToObject(TSerPerson, yaml) as TSerPerson;
try
Assert.AreEqual('Frank', p2.Name);
Assert.AreEqual(35, p2.Age);
finally
p2.Free;
end;
end;
procedure TSerializationTests.T_SER_09_Yaml_ToObject_Instance_Fills;
var
p : TSerPerson;
p2 : TSerPerson;
yaml : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Grace';
p.Age := 28;
yaml := fYaml.FromObject(p);
finally
p.Free;
end;
p2 := TSerPerson.Create;
try
fYaml.ToObject(p2, yaml);
Assert.AreEqual('Grace', p2.Name);
Assert.AreEqual(28, p2.Age);
finally
p2.Free;
end;
end;
// ─── TSerializers hub ────────────────────────────────────────────────────────
procedure TSerializationTests.T_SER_10_Serializers_Json_NotNil;
begin
Assert.IsNotNull(fHub.Json, 'TSerializers.Json should return a non-nil serializer');
end;
procedure TSerializationTests.T_SER_11_Serializers_Yaml_NotNil;
begin
Assert.IsNotNull(fHub.Yaml, 'TSerializers.Yaml should return a non-nil serializer');
end;
procedure TSerializationTests.T_SER_12_Serializers_Json_StableAcrossCalls;
begin
Assert.AreSame(fHub.Json, fHub.Json,
'TSerializers.Json should return the same instance across calls');
end;
procedure TSerializationTests.T_SER_13_Json_Output_ContainsFieldValue;
var
p : TSerPerson;
json : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Zara';
p.Age := 77;
json := fJson.FromObject(p);
Assert.IsTrue(json.Contains('Zara'), 'JSON must contain name value "Zara"');
finally
p.Free;
end;
end;
procedure TSerializationTests.T_SER_14_Yaml_Output_ContainsFieldValue;
var
p : TSerPerson;
yaml : string;
begin
p := TSerPerson.Create;
try
p.Name := 'Zara';
p.Age := 77;
yaml := fYaml.FromObject(p);
Assert.IsTrue(yaml.Contains('Zara'), 'YAML must contain name value "Zara"');
finally
p.Free;
end;
end;
initialization
TDUnitX.RegisterTestFixture(TSerializationTests);
end.