Repository navigation
Expand file tree
/
Copy pathQuick.YAML.Tests.pas
More file actions
757 lines (673 loc) · 19.8 KB
/
Copy pathQuick.YAML.Tests.pas
File metadata and controls
757 lines (673 loc) · 19.8 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
unit Quick.YAML.Tests;
interface
uses
DUnitX.TestFramework,
Quick.YAML,
Quick.Value,
System.SysUtils,
System.Classes;
type
[TestFixture]
TYAMLTest = class
private
fYamlObj: TYamlObject;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
// Scalar value types
[Test] procedure TestScalarValues;
[Test] procedure TestScalarString;
[Test] procedure TestScalarInteger;
[Test] procedure TestScalarFloat;
[Test] procedure TestScalarBoolean;
// Object structure
[Test] procedure TestNestedObjects;
[Test] procedure TestDeepNestedObjects;
[Test] procedure TestAddPairFluent;
[Test] procedure TestGetPairByName;
[Test] procedure TestGetPairByIndex;
[Test] procedure TestRemovePair;
[Test] procedure TestCount;
[Test] procedure TestEnumerator;
// Arrays
[Test] procedure TestArrays;
[Test] procedure TestArrayScalarItems;
[Test] procedure TestArrayMixedItems;
[Test] procedure TestInlineArray;
// Null & comments
[Test] procedure TestNullValues;
[Test] procedure TestComments;
// Parsing
[Test] procedure TestParseYaml;
[Test] procedure TestParseYaml_MultipleScalars;
[Test] procedure TestParseYaml_NestedObject;
[Test] procedure TestParseYaml_ScalarArray;
[Test] procedure TestParseYaml_InlineArray;
[Test] procedure TestParseYaml_IntegerValue;
[Test] procedure TestParseYaml_FloatValue;
[Test] procedure TestParseYaml_BooleanValue;
// Serialization
[Test] procedure TestToYaml;
[Test] procedure TestToYaml_NestedObject;
[Test] procedure TestToYaml_Array;
[Test] procedure TestToYaml_RoundTrip;
// Edge cases
[Test] procedure TestParseYaml_EmptyString;
[Test] procedure TestParseYaml_Comments;
[Test] procedure TestGetValue_NonExisting_ReturnsNil;
[Test] procedure TestParseYamlValue_Object;
[Test] procedure TestParseYamlValue_Array;
// Issue #114: escape sequences in double-quoted scalars
[Test] procedure TestEscape_DoubleQuote;
[Test] procedure TestEscape_Backslash;
[Test] procedure TestEscape_Newline;
[Test] procedure TestEscape_Tab;
[Test] procedure TestEscape_UnicodeU4;
[Test] procedure TestEscape_UnicodeU8;
[Test] procedure TestEscape_HexX2;
[Test] procedure TestEscape_CommonControlChars;
[Test] procedure TestEscape_UnquotedStringUnchanged;
[Test] procedure TestEscape_InArrayItem;
end;
implementation
procedure TYAMLTest.Setup;
begin
fYamlObj := TYamlObject.Create;
end;
procedure TYAMLTest.TearDown;
begin
fYamlObj.Free;
end;
{ ---- Scalar types ---- }
procedure TYAMLTest.TestScalarValues;
begin
fYamlObj.AddPair('string', 'test');
fYamlObj.AddPair('integer', 123);
fYamlObj.AddPair('float', 123.45);
fYamlObj.AddPair('boolean', TYamlBoolean.Create(True));
Assert.AreEqual('test', fYamlObj.Values['string'].AsString);
Assert.AreEqual(123, fYamlObj.Values['integer'].Value.AsInteger);
Assert.AreEqual(Double(123.45), Double(fYamlObj.Values['float'].Value.AsExtended));
Assert.IsTrue(fYamlObj.Values['boolean'].Value.AsBoolean);
end;
procedure TYAMLTest.TestScalarString;
var
v: TYamlString;
begin
v := TYamlString.Create('hello');
Assert.AreEqual('hello', v.AsString);
Assert.IsTrue(v.IsScalar);
Assert.IsFalse(v.Null);
v.Free;
v := TYamlString.Create;
Assert.IsTrue(v.Null, 'Default TYamlString should be null');
v.Free;
end;
procedure TYAMLTest.TestScalarInteger;
var
v: TYamlInteger;
begin
v := TYamlInteger.Create(42);
Assert.AreEqual(42, v.Value.AsInteger);
Assert.IsTrue(v.IsScalar);
Assert.IsFalse(v.Null);
Assert.AreEqual('42', v.AsString);
v.Free;
end;
procedure TYAMLTest.TestScalarFloat;
var
v: TYamlFloat;
begin
v := TYamlFloat.Create(3.14);
Assert.AreEqual(Double(3.14), Double(v.Value.AsExtended));
Assert.IsTrue(v.IsScalar);
Assert.IsFalse(v.Null);
v.Free;
end;
procedure TYAMLTest.TestScalarBoolean;
var
vTrue, vFalse: TYamlBoolean;
begin
vTrue := TYamlBoolean.Create(True);
vFalse := TYamlBoolean.Create(False);
Assert.IsTrue(vTrue.Value.AsBoolean);
Assert.IsFalse(vFalse.Value.AsBoolean);
Assert.IsTrue(vTrue.IsScalar);
vTrue.Free;
vFalse.Free;
end;
{ ---- Object structure ---- }
procedure TYAMLTest.TestNestedObjects;
var
nested: TYamlObject;
begin
nested := TYamlObject.Create;
nested.AddPair('child', 'value');
fYamlObj.AddPair('parent', nested);
Assert.AreEqual('value', TYamlObject(fYamlObj.Values['parent']).Values['child'].AsString);
end;
procedure TYAMLTest.TestDeepNestedObjects;
var
level1, level2: TYamlObject;
begin
level2 := TYamlObject.Create;
level2.AddPair('deep', 'found');
level1 := TYamlObject.Create;
level1.AddPair('level2', level2);
fYamlObj.AddPair('level1', level1);
Assert.AreEqual('found',
TYamlObject(TYamlObject(fYamlObj.Values['level1']).Values['level2']).Values['deep'].AsString,
'Deep nested value should be accessible');
end;
procedure TYAMLTest.TestAddPairFluent;
begin
// AddPair returns Self so calls can be chained
fYamlObj
.AddPair('a', 'alpha')
.AddPair('b', 2)
.AddPair('c', 3.0);
Assert.AreEqual(3, fYamlObj.Count, 'Fluent AddPair should add 3 pairs');
Assert.AreEqual('alpha', fYamlObj.Values['a'].AsString);
Assert.AreEqual(2, fYamlObj.Values['b'].Value.AsInteger);
end;
procedure TYAMLTest.TestGetPairByName;
var
pair: TYamlPair;
begin
fYamlObj.AddPair('key', 'value');
pair := fYamlObj.GetPairByName('key');
Assert.IsNotNull(pair, 'Should find pair by name');
Assert.AreEqual('key', pair.Name);
Assert.AreEqual('value', pair.Value.AsString);
Assert.IsNull(fYamlObj.GetPairByName('nonexistent'), 'Should return nil for missing pair');
end;
procedure TYAMLTest.TestGetPairByIndex;
var
pair: TYamlPair;
begin
fYamlObj.AddPair('first', 'one');
fYamlObj.AddPair('second', 'two');
pair := fYamlObj.GetPair(0);
Assert.AreEqual('first', pair.Name);
pair := fYamlObj.GetPair(1);
Assert.AreEqual('second', pair.Name);
end;
procedure TYAMLTest.TestRemovePair;
var
removed: TYamlPair;
begin
fYamlObj.AddPair('keep', 'yes');
fYamlObj.AddPair('remove', 'no');
Assert.AreEqual(2, fYamlObj.Count);
removed := fYamlObj.RemovePair('remove');
Assert.IsNotNull(removed, 'RemovePair should return the removed pair');
Assert.AreEqual(1, fYamlObj.Count, 'Count should decrease after removal');
Assert.IsNull(fYamlObj.GetPairByName('remove'), 'Removed pair should no longer be found');
removed.Free;
end;
procedure TYAMLTest.TestCount;
begin
Assert.AreEqual(0, fYamlObj.Count, 'Empty object should have count 0');
fYamlObj.AddPair('a', 'x');
Assert.AreEqual(1, fYamlObj.Count);
fYamlObj.AddPair('b', 'y');
Assert.AreEqual(2, fYamlObj.Count);
end;
procedure TYAMLTest.TestEnumerator;
var
pair: TYamlPair;
count: Integer;
begin
fYamlObj.AddPair('x', '1');
fYamlObj.AddPair('y', '2');
fYamlObj.AddPair('z', '3');
count := 0;
for pair in fYamlObj do
Inc(count);
Assert.AreEqual(3, count, 'Enumerator should iterate all pairs');
end;
{ ---- Arrays ---- }
procedure TYAMLTest.TestArrays;
var
arr: TYamlArray;
begin
arr := TYamlArray.Create;
arr.AddElement(TYamlString.Create('item1'));
arr.AddElement(TYamlString.Create('item2'));
arr.AddElement(TYamlInteger.Create(123));
fYamlObj.AddPair('array', arr);
Assert.AreEqual(3, TYamlArray(fYamlObj.Values['array']).Count);
Assert.AreEqual('item1', TYamlArray(fYamlObj.Values['array']).Items[0].AsString);
Assert.AreEqual(123, TYamlArray(fYamlObj.Values['array']).Items[2].Value.AsInteger);
end;
procedure TYAMLTest.TestArrayScalarItems;
var
arr: TYamlArray;
item: TYamlValue;
count: Integer;
begin
arr := TYamlArray.Create;
arr.AddElement(TYamlString.Create('a'));
arr.AddElement(TYamlString.Create('b'));
arr.AddElement(TYamlString.Create('c'));
fYamlObj.AddPair('list', arr);
count := 0;
for item in TYamlArray(fYamlObj.Values['list']) do
Inc(count);
Assert.AreEqual(3, count, 'Array enumerator should visit all items');
end;
procedure TYAMLTest.TestArrayMixedItems;
var
arr: TYamlArray;
begin
arr := TYamlArray.Create;
arr.AddElement(TYamlString.Create('text'));
arr.AddElement(TYamlInteger.Create(7));
arr.AddElement(TYamlFloat.Create(1.5));
arr.AddElement(TYamlBoolean.Create(True));
arr.AddElement(TYamlNull.Create);
fYamlObj.AddPair('mixed', arr);
var a := TYamlArray(fYamlObj.Values['mixed']);
Assert.AreEqual(5, a.Count);
Assert.AreEqual('text', a.Items[0].AsString);
Assert.AreEqual(7, a.Items[1].Value.AsInteger);
Assert.IsTrue(a.Items[3].Value.AsBoolean);
Assert.IsTrue(a.Items[4].Null);
end;
procedure TYAMLTest.TestInlineArray;
var
arr: TYamlArray;
begin
arr := TYamlArray.Create(TYamlString.Create('only'));
Assert.AreEqual(1, arr.Count, 'Single-element constructor should set Count=1');
Assert.AreEqual('only', arr.Items[0].AsString);
arr.Free;
end;
{ ---- Null & comments ---- }
procedure TYAMLTest.TestNullValues;
begin
fYamlObj.AddPair('nullvalue', TYamlNull.Create);
Assert.IsTrue(fYamlObj.Values['nullvalue'].Null);
Assert.AreEqual('null', fYamlObj.Values['nullvalue'].AsString);
end;
procedure TYAMLTest.TestComments;
begin
fYamlObj.AddPair('key', TYamlString.Create('value'));
fYamlObj.AddPair('#comment', TYamlComment.Create('This is a comment'));
Assert.IsTrue(fYamlObj.GetPairByName('#comment').Value is TYamlComment);
Assert.AreEqual('This is a comment', fYamlObj.GetPairByName('#comment').Value.AsString);
end;
{ ---- Parsing ---- }
procedure TYAMLTest.TestParseYaml;
const
TEST_YAML =
'name: John'#13#10 +
'age: 30'#13#10 +
'address:'#13#10 +
' street: Main St'#13#10 +
' number: 123'#13#10 +
'hobbies:'#13#10 +
' - reading'#13#10 +
' - gaming'#13#10;
begin
fYamlObj.ParseYaml(TEST_YAML);
Assert.AreEqual('John', fYamlObj.Values['name'].AsString);
Assert.AreEqual(30, fYamlObj.Values['age'].Value.AsInteger);
Assert.AreEqual('Main St', TYamlObject(fYamlObj.Values['address']).Values['street'].AsString);
Assert.AreEqual(2, TYamlArray(fYamlObj.Values['hobbies']).Count);
Assert.AreEqual('reading', TYamlArray(fYamlObj.Values['hobbies']).Items[0].AsString);
Assert.AreEqual('gaming', TYamlArray(fYamlObj.Values['hobbies']).Items[1].AsString);
end;
procedure TYAMLTest.TestParseYaml_MultipleScalars;
const
YAML =
'first: alpha'#13#10 +
'second: beta'#13#10 +
'third: gamma'#13#10;
begin
fYamlObj.ParseYaml(YAML);
Assert.AreEqual(3, fYamlObj.Count, 'Should parse 3 scalar pairs');
Assert.AreEqual('alpha', fYamlObj.Values['first'].AsString);
Assert.AreEqual('beta', fYamlObj.Values['second'].AsString);
Assert.AreEqual('gamma', fYamlObj.Values['third'].AsString);
end;
procedure TYAMLTest.TestParseYaml_NestedObject;
const
YAML =
'server:'#13#10 +
' host: localhost'#13#10 +
' port: 8080'#13#10;
begin
fYamlObj.ParseYaml(YAML);
var server := TYamlObject(fYamlObj.Values['server']);
Assert.IsNotNull(server, 'server should be a nested object');
Assert.AreEqual('localhost', server.Values['host'].AsString);
Assert.AreEqual(8080, server.Values['port'].Value.AsInteger);
end;
procedure TYAMLTest.TestParseYaml_ScalarArray;
const
YAML =
'colors:'#13#10 +
' - red'#13#10 +
' - green'#13#10 +
' - blue'#13#10;
begin
fYamlObj.ParseYaml(YAML);
var arr := TYamlArray(fYamlObj.Values['colors']);
Assert.IsNotNull(arr, 'colors should be an array');
Assert.AreEqual(3, arr.Count, 'Should have 3 color items');
Assert.AreEqual('red', arr.Items[0].AsString);
Assert.AreEqual('green', arr.Items[1].AsString);
Assert.AreEqual('blue', arr.Items[2].AsString);
end;
procedure TYAMLTest.TestParseYaml_InlineArray;
const
YAML = 'tags: [one, two, three]'#13#10;
begin
fYamlObj.ParseYaml(YAML);
var arr := TYamlArray(fYamlObj.Values['tags']);
Assert.IsNotNull(arr, 'tags should be parsed as inline array');
Assert.AreEqual(3, arr.Count, 'Inline array should have 3 items');
Assert.AreEqual('one', arr.Items[0].AsString);
Assert.AreEqual('two', arr.Items[1].AsString);
Assert.AreEqual('three', arr.Items[2].AsString);
end;
procedure TYAMLTest.TestParseYaml_IntegerValue;
const
YAML = 'count: 42'#13#10;
begin
fYamlObj.ParseYaml(YAML);
Assert.AreEqual(42, fYamlObj.Values['count'].Value.AsInteger);
end;
procedure TYAMLTest.TestParseYaml_FloatValue;
const
YAML = 'ratio: 0.75'#13#10;
var
fs: TFormatSettings;
expected: string;
begin
fYamlObj.ParseYaml(YAML);
// The parser may store the value as a string using the locale decimal separator.
// Compare as string to be locale-independent.
fs := TFormatSettings.Create;
fs.DecimalSeparator := '.';
expected := FloatToStr(0.75, fs);
Assert.AreEqual(expected, fYamlObj.Values['ratio'].AsString, 'Float value should be parsed correctly');
end;
procedure TYAMLTest.TestParseYaml_BooleanValue;
const
YAML =
'enabled: true'#13#10 +
'disabled: false'#13#10;
begin
fYamlObj.ParseYaml(YAML);
// booleans parsed as strings by the scalar path - check AsString
Assert.AreEqual('true', fYamlObj.Values['enabled'].AsString);
Assert.AreEqual('false', fYamlObj.Values['disabled'].AsString);
end;
{ ---- Serialization ---- }
procedure TYAMLTest.TestToYaml;
var
yaml: string;
begin
fYamlObj.AddPair('name', 'John');
fYamlObj.AddPair('age', 30);
yaml := fYamlObj.ToYaml;
Assert.Contains(yaml, 'name: John', 'ToYaml should contain name pair');
Assert.Contains(yaml, 'age: 30', 'ToYaml should contain age pair');
end;
procedure TYAMLTest.TestToYaml_NestedObject;
var
nested: TYamlObject;
yaml: string;
begin
nested := TYamlObject.Create;
nested.AddPair('city', 'Madrid');
fYamlObj.AddPair('address', nested);
yaml := fYamlObj.ToYaml;
Assert.Contains(yaml, 'address:', 'ToYaml should contain nested key');
Assert.Contains(yaml, 'city: Madrid', 'ToYaml should contain nested value');
end;
procedure TYAMLTest.TestToYaml_Array;
var
arr: TYamlArray;
yaml: string;
begin
arr := TYamlArray.Create;
arr.AddElement(TYamlString.Create('alpha'));
arr.AddElement(TYamlString.Create('beta'));
fYamlObj.AddPair('items', arr);
yaml := fYamlObj.ToYaml;
Assert.Contains(yaml, 'items:', 'ToYaml should contain array key');
Assert.Contains(yaml, 'alpha', 'ToYaml should contain first array item');
Assert.Contains(yaml, 'beta', 'ToYaml should contain second array item');
end;
procedure TYAMLTest.TestToYaml_RoundTrip;
const
YAMLTEXT =
'name: Alice'#13#10 +
'score: 99'#13#10;
var
yaml: string;
parsed: TYamlObject;
begin
fYamlObj.ParseYaml(YAMLTEXT);
yaml := fYamlObj.ToYaml;
parsed := TYamlObject.Create;
try
parsed.ParseYaml(yaml);
Assert.AreEqual('Alice', parsed.Values['name'].AsString, 'Round-trip name should match');
Assert.AreEqual(99, parsed.Values['score'].Value.AsInteger, 'Round-trip score should match');
finally
parsed.Free;
end;
end;
{ ---- Edge cases ---- }
procedure TYAMLTest.TestParseYaml_EmptyString;
begin
Assert.WillNotRaise(
procedure begin fYamlObj.ParseYaml(''); end,
nil, 'Parsing empty string should not raise');
Assert.AreEqual(0, fYamlObj.Count, 'Parsing empty string should produce empty object');
end;
procedure TYAMLTest.TestParseYaml_Comments;
const
YAML =
'# this is a top comment'#13#10 +
'key: value'#13#10;
begin
fYamlObj.ParseYaml(YAML);
Assert.AreEqual('value', fYamlObj.Values['key'].AsString, 'Key after comment should be parsed');
end;
procedure TYAMLTest.TestGetValue_NonExisting_ReturnsNil;
begin
Assert.IsNull(fYamlObj.Values['doesnotexist'], 'Values[] for unknown key should return nil');
end;
procedure TYAMLTest.TestParseYamlValue_Object;
const
YAML = 'key: val'#13#10;
var
result: TYamlAncestor;
begin
result := TYamlObject.ParseYamlValue(YAML);
try
Assert.IsNotNull(result, 'ParseYamlValue should return a non-nil result');
Assert.IsTrue(result is TYamlObject, 'ParseYamlValue should return a TYamlObject for key:value input');
finally
result.Free;
end;
end;
procedure TYAMLTest.TestParseYamlValue_Array;
const
YAML =
'- first'#13#10 +
'- second'#13#10;
var
result: TYamlAncestor;
begin
result := TYamlObject.ParseYamlValue(YAML);
try
Assert.IsNotNull(result, 'ParseYamlValue should return a non-nil result');
Assert.IsTrue(result is TYamlArray, 'ParseYamlValue should return a TYamlArray for array input');
Assert.AreEqual(2, TYamlArray(result).Count, 'Array should have 2 items');
finally
result.Free;
end;
end;
// ── Issue #114: escape sequences in double-quoted scalars ────────────────
procedure TYAMLTest.TestEscape_DoubleQuote;
const
YAML = 'value: "\"hello\""'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('"hello"', obj.Values['value'].AsString,
'Escaped double-quote (\" inside double-quoted scalar) must decode to "');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_Backslash;
const
YAML = 'path: "C:\\Users\\test"'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('C:\Users\test', obj.Values['path'].AsString,
'Escaped backslash (\\) must decode to a single backslash');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_Newline;
const
YAML = 'msg: "line1\nline2"'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('line1'#10'line2', obj.Values['msg'].AsString,
'Escape \n must decode to LF');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_Tab;
const
YAML = 'msg: "col1\tcol2"'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('col1'#9'col2', obj.Values['msg'].AsString,
'Escape \t must decode to TAB');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_UnicodeU4;
const
// \u0022 is U+0022 = double-quote character
YAML = 'ch: "\u0022"'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('"', obj.Values['ch'].AsString,
'Escape \u0022 must decode to the double-quote character (issue #114)');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_UnicodeU8;
const
// \U00000041 = 'A'
YAML = 'ch: "\U00000041"'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('A', obj.Values['ch'].AsString,
'Escape \U00000041 must decode to ''A''');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_HexX2;
const
// \x41 = 0x41 = 'A'
YAML = 'ch: "\x41"'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('A', obj.Values['ch'].AsString,
'Escape \x41 must decode to ''A''');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_CommonControlChars;
const
YAML = 'ctrl: "\r\n"'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual(#13#10, obj.Values['ctrl'].AsString,
'Escape \r\n must decode to CR+LF');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_UnquotedStringUnchanged;
const
// An unquoted value must NOT have its backslashes interpreted
YAML = 'raw: C:\Users\test'#13#10;
var
obj : TYamlObject;
begin
obj := TYamlObject.Create(YAML);
try
Assert.AreEqual('C:\Users\test', obj.Values['raw'].AsString,
'Unquoted scalar must be returned verbatim without escape processing');
finally
obj.Free;
end;
end;
procedure TYAMLTest.TestEscape_InArrayItem;
const
YAML =
'items:'#13#10 +
' - "\u0041"'#13#10 +
' - "\u0042"'#13#10;
var
obj : TYamlObject;
arr : TYamlArray;
begin
obj := TYamlObject.Create(YAML);
try
arr := obj.Values['items'] as TYamlArray;
Assert.AreEqual('A', arr.Items[0].AsString,
'Array item \u0041 must decode to ''A''');
Assert.AreEqual('B', arr.Items[1].AsString,
'Array item \u0042 must decode to ''B''');
finally
obj.Free;
end;
end;
initialization
TDUnitX.RegisterTestFixture(TYAMLTest);
end.