-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStringKit.Test.pas
More file actions
1305 lines (1058 loc) · 46.4 KB
/
Copy pathStringKit.Test.pas
File metadata and controls
1305 lines (1058 loc) · 46.4 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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit StringKit.Test;
{$mode objfpc}{$H+}{$J-}
interface
uses
Classes, SysUtils, Types, DateUtils, fpcunit, testregistry,
StringKit;
type
TStringArray = array of string;
type
// TStringTests
TStringTests = class(TTestCase)
protected
procedure SetUp; override;
procedure TearDown; override;
published
// Basic transformations
procedure Test01_From;
procedure Test02_ToString;
procedure Test03_Trim;
procedure Test04_TrimLeft;
procedure Test05_TrimRight;
procedure Test06_ToUpper;
procedure Test07_ToLower;
procedure Test08_Capitalize;
// Advanced transformations
procedure Test09_Reverse;
procedure Test10_Duplicate;
procedure Test11_PadLeft;
procedure Test12_PadRight;
procedure Test13_PadCenter;
procedure Test14_RemoveWhitespace;
procedure Test15_CollapseWhitespace;
// Pattern matching and replacement
procedure Test16_Replace;
procedure Test17_ReplaceRegEx;
procedure Test18_Extract;
procedure Test19_ExtractAll;
procedure Test20_Matches;
// Substrings and parts
procedure Test21_SubString;
procedure Test22_Left;
procedure Test23_Right;
procedure Test24_Words;
// Tests and information
procedure Test25_Contains;
procedure Test26_StartsWith;
procedure Test27_EndsWith;
procedure Test28_IsEmpty;
procedure Test29_Length;
procedure Test30_CountSubString;
// String similarity functions
procedure Test31_LevenshteinDistance;
procedure Test32_LevenshteinSimilarity;
procedure Test33_HammingDistance;
procedure Test34_JaroSimilarity;
procedure Test35_JaroWinklerSimilarity;
procedure Test36_LongestCommonSubsequence;
procedure Test37_LCSSimilarity;
procedure Test38_IsFuzzyMatch;
// Case conversion variants
procedure Test39_ToTitleCase;
procedure Test40_ToCamelCase;
procedure Test41_ToPascalCase;
procedure Test42_ToSnakeCase;
procedure Test43_ToKebabCase;
// String validation
procedure Test44_IsValidEmail;
procedure Test45_IsValidURL;
procedure Test46_IsValidIP;
procedure Test47_IsValidIPv4;
procedure Test48_IsValidIPv6;
procedure Test49_IsValidDate;
// String transformation and formatting
procedure Test50_Truncate;
procedure Test51_FormatFileSize;
procedure Test52_FormatNumber;
procedure Test53_FormatFloat;
// String splitting and joining
procedure Test54_Join;
procedure Test55_Split;
// Phonetic algorithms
procedure Test56_Soundex;
procedure Test57_Metaphone;
// Text analysis
procedure Test58_CountWords;
procedure Test59_FleschKincaidReadability;
procedure Test60_GenerateNGrams;
// Encoding/decoding
procedure Test61_HTMLEncode;
procedure Test62_HTMLDecode;
procedure Test63_URLEncode;
procedure Test64_URLDecode;
// Number Conversions
procedure Test65_ToRoman;
procedure Test66_FromRoman;
procedure Test67_ToOrdinal;
procedure Test68_NumberToWords;
// More Encoding/Decoding Functions
procedure Test71_HexEncode;
procedure Test72_HexDecode;
procedure Test73_Base64Encode;
procedure Test74_Base64Decode;
end;
implementation
// TStringTests
procedure TStringTests.SetUp;
begin
// No setup needed for static functions
end;
procedure TStringTests.TearDown;
begin
// No teardown needed for static functions
end;
procedure TStringTests.Test01_From;
const
TestStr = 'Hello, World!';
begin
AssertEquals('String value should remain unchanged',
TestStr, TestStr);
end;
procedure TStringTests.Test02_ToString;
const
TestStr = 'Hello, World!';
begin
AssertEquals('String value should remain unchanged',
TestStr, TestStr);
end;
procedure TStringTests.Test03_Trim;
const
TestStr = ' Hello, World! ';
begin
AssertEquals('Trim should remove surrounding whitespace',
'Hello, World!', TStringKit.Trim(TestStr));
end;
procedure TStringTests.Test04_TrimLeft;
const
TestStr = ' Hello, World! ';
begin
AssertEquals('TrimLeft should remove leading whitespace',
'Hello, World! ', TStringKit.TrimLeft(TestStr));
end;
procedure TStringTests.Test05_TrimRight;
const
TestStr = ' Hello, World! ';
begin
AssertEquals('TrimRight should remove trailing whitespace',
' Hello, World!', TStringKit.TrimRight(TestStr));
end;
procedure TStringTests.Test06_ToUpper;
const
TestStr = 'Hello, World!';
begin
AssertEquals('ToUpper should work correctly',
'HELLO, WORLD!', TStringKit.ToUpper(TestStr));
end;
procedure TStringTests.Test07_ToLower;
const
TestStr = 'Hello, World!';
begin
AssertEquals('ToLower should work correctly',
'hello, world!', TStringKit.ToLower(TestStr));
end;
procedure TStringTests.Test08_Capitalize;
const
TestStr = 'hello, world!';
begin
AssertEquals('CapitalizeText should work correctly',
'Hello, World!', TStringKit.CapitalizeText(TestStr));
end;
procedure TStringTests.Test09_Reverse;
const
TestStr = 'Hello, World!';
begin
AssertEquals('ReverseText should work correctly',
'!dlroW ,olleH', TStringKit.ReverseText(TestStr));
end;
procedure TStringTests.Test10_Duplicate;
const
TestStr = 'Hello';
begin
AssertEquals('DuplicateText should work correctly',
'HelloHello', TStringKit.DuplicateText(TestStr, 2));
end;
procedure TStringTests.Test11_PadLeft;
const
TestStr = 'test';
begin
AssertEquals('PadLeft should work correctly',
'****test', TStringKit.PadLeft(TestStr, 8, '*'));
end;
procedure TStringTests.Test12_PadRight;
const
TestStr = 'test';
begin
AssertEquals('PadRight should work correctly',
'test****', TStringKit.PadRight(TestStr, 8, '*'));
end;
procedure TStringTests.Test13_PadCenter;
const
TestStr = 'test';
begin
AssertEquals('PadCenter should work correctly',
'**test**', TStringKit.PadCenter(TestStr, 8, '*'));
end;
procedure TStringTests.Test14_RemoveWhitespace;
const
TestStr = ' too many spaces ';
begin
AssertEquals('RemoveWhitespace should work correctly',
'toomanyspaces', TStringKit.RemoveWhitespace(TestStr));
end;
procedure TStringTests.Test15_CollapseWhitespace;
const
TestStr = ' too many spaces ';
begin
AssertEquals('CollapseWhitespace should work correctly',
'too many spaces', TStringKit.Trim(TStringKit.CollapseWhitespace(TestStr)));
end;
procedure TStringTests.Test16_Replace;
const
TestStr = 'Hello, World!';
begin
AssertEquals('ReplaceText should work correctly',
'Hi, World!', TStringKit.ReplaceText(TestStr, 'Hello', 'Hi'));
end;
procedure TStringTests.Test17_ReplaceRegEx;
const
TestStr = 'The year is 2024';
begin
AssertEquals('ReplaceRegEx should work correctly',
'The year is 2024', TStringKit.ReplaceRegEx(TestStr, '\d+', '2024'));
end;
procedure TStringTests.Test18_Extract;
const
TestStr = 'The year is 2024';
var
Matches: TMatchesResults;
begin
Matches := TStringKit.ExtractMatches(TestStr, '\d+');
AssertEquals('ExtractMatches should work correctly',
'2024', Matches[0].Text);
end;
procedure TStringTests.Test19_ExtractAll;
const
TestStr = 'The year is 2024';
var
Results: TStringArray;
begin
Results := TStringKit.ExtractAllMatches(TestStr, '\d+');
AssertEquals('ExtractAllMatches should work correctly',
'2024', Results[0]);
end;
procedure TStringTests.Test20_Matches;
const
TestStr = 'The year is 2024';
begin
AssertTrue('MatchesPattern should work correctly',
TStringKit.MatchesPattern(TestStr, '\d+'));
end;
procedure TStringTests.Test21_SubString;
const
TestStr = 'Hello, World!';
begin
AssertEquals('SubString should work correctly',
'Hello', TStringKit.SubString(TestStr, 1, 5));
end;
procedure TStringTests.Test22_Left;
const
TestStr = 'Hello, World!';
begin
AssertEquals('LeftStr should work correctly',
'Hello', TStringKit.LeftStr(TestStr, 5));
end;
procedure TStringTests.Test23_Right;
const
TestStr = 'Hello, World!';
begin
AssertEquals('RightStr should work correctly',
'World!', TStringKit.RightStr(TestStr, 6));
end;
procedure TStringTests.Test24_Words;
const
TestStr = 'Hello, World!';
var
WordArray: TStringArray;
begin
WordArray := TStringKit.GetWords(TestStr);
AssertEquals('GetWords should work correctly',
'Hello', WordArray[0]);
AssertEquals('GetWords should work correctly',
'World', WordArray[1]);
end;
procedure TStringTests.Test25_Contains;
const
TestStr = 'Hello, World!';
begin
AssertTrue('Contains should work correctly',
TStringKit.Contains(TestStr, 'World'));
end;
procedure TStringTests.Test26_StartsWith;
const
TestStr = 'Hello, World!';
begin
AssertTrue('StartsWith should work correctly',
TStringKit.StartsWith(TestStr, 'Hello'));
end;
procedure TStringTests.Test27_EndsWith;
const
TestStr = 'Hello, World!';
begin
AssertTrue('EndsWith should work correctly',
TStringKit.EndsWith(TestStr, 'World!'));
end;
procedure TStringTests.Test28_IsEmpty;
begin
AssertTrue('IsEmpty should work correctly',
TStringKit.IsEmpty(''));
end;
procedure TStringTests.Test29_Length;
const
TestStr = 'Hello, World!';
begin
AssertEquals('GetLength should work correctly',
13, TStringKit.GetLength(TestStr));
end;
procedure TStringTests.Test30_CountSubString;
const
TestStr = 'Hello, Hello, Hello!';
begin
AssertEquals('CountSubString should work correctly',
3, TStringKit.CountSubString(TestStr, 'Hello'));
end;
procedure TStringTests.Test31_LevenshteinDistance;
begin
// Same strings should have distance 0
AssertEquals('Identical strings should have distance 0',
0, TStringKit.LevenshteinDistance('test', 'test'));
// Single character edit
AssertEquals('Single insertion distance should be 1',
1, TStringKit.LevenshteinDistance('test', 'tests'));
AssertEquals('Single deletion distance should be 1',
1, TStringKit.LevenshteinDistance('tests', 'test'));
AssertEquals('Single substitution distance should be 1',
1, TStringKit.LevenshteinDistance('test', 'tent'));
// Multiple edits
AssertEquals('Multiple edits should be calculated correctly',
3, TStringKit.LevenshteinDistance('kitten', 'sitting'));
end;
procedure TStringTests.Test32_LevenshteinSimilarity;
begin
// Identical strings should have similarity 1.0
AssertEquals('Identical strings should have similarity 1.0',
1.0, TStringKit.LevenshteinSimilarity('test', 'test'));
// Very different strings should have low similarity
AssertTrue('Different strings should have lower similarity',
TStringKit.LevenshteinSimilarity('abc', 'xyz') < 0.5);
// Similar strings should have high similarity
AssertTrue('Similar strings should have higher similarity',
TStringKit.LevenshteinSimilarity('hello', 'hallo') > 0.7);
end;
procedure TStringTests.Test33_HammingDistance;
begin
// Same strings should have distance 0
AssertEquals('Identical strings should have Hamming distance 0',
0, TStringKit.HammingDistance('test', 'test'));
// Different strings of same length
AssertEquals('Strings with 1 different character',
1, TStringKit.HammingDistance('test', 'tent'));
AssertEquals('Strings with 2 different characters',
2, TStringKit.HammingDistance('test', 'tart'));
// Different length strings should return -1
AssertEquals('Different length strings should return -1',
-1, TStringKit.HammingDistance('test', 'tests'));
end;
procedure TStringTests.Test34_JaroSimilarity;
begin
// Identical strings should have similarity 1.0
AssertEquals('Identical strings should have Jaro similarity 1.0',
1.0, TStringKit.JaroSimilarity('test', 'test'));
// Test with strings of different lengths (now supported in standard implementation)
AssertTrue('MARTHA/MARHTA should have high similarity (~0.94)',
Abs(TStringKit.JaroSimilarity('MARTHA', 'MARHTA') - 0.944) < 0.01);
AssertTrue('DWAYNE/DUANE should have good similarity (~0.82)',
Abs(TStringKit.JaroSimilarity('DWAYNE', 'DUANE') - 0.822) < 0.01);
// No matching characters should return 0.0
AssertEquals('No matching characters should return 0.0',
0.0, TStringKit.JaroSimilarity('ABC', 'DEF'));
// Special cases
AssertEquals('Both empty strings should return 1.0',
1.0, TStringKit.JaroSimilarity('', ''));
AssertEquals('One empty string should return 0.0',
0.0, TStringKit.JaroSimilarity('A', ''));
// Test transpositions
AssertTrue('DIXON/DICKSON should return reasonable similarity',
TStringKit.JaroSimilarity('DIXON', 'DICKSON') > 0.6);
end;
procedure TStringTests.Test35_JaroWinklerSimilarity;
begin
// Identical strings should have similarity 1.0
AssertEquals('Identical strings should have Jaro-Winkler similarity 1.0',
1.0, TStringKit.JaroWinklerSimilarity('test', 'test'));
// Strings with matching prefix should have higher similarity in Jaro-Winkler than Jaro
AssertTrue('Strings with matching prefix should have higher similarity in Jaro-Winkler',
TStringKit.JaroWinklerSimilarity('prefix123', 'prefix456') >
TStringKit.JaroSimilarity('prefix123', 'prefix456'));
// Test with strings from examples
AssertTrue('MARTHA/MARHTA should have higher Jaro-Winkler than Jaro similarity',
TStringKit.JaroWinklerSimilarity('MARTHA', 'MARHTA') >
TStringKit.JaroSimilarity('MARTHA', 'MARHTA'));
AssertTrue('DWAYNE/DUANE should have higher Jaro-Winkler than Jaro similarity',
TStringKit.JaroWinklerSimilarity('DWAYNE', 'DUANE') >
TStringKit.JaroSimilarity('DWAYNE', 'DUANE'));
// Test with strings that have common prefix but different lengths
AssertTrue('DIXON/DICKSON should have higher Jaro-Winkler than Jaro similarity',
TStringKit.JaroWinklerSimilarity('DIXON', 'DICKSON') >
TStringKit.JaroSimilarity('DIXON', 'DICKSON'));
// Empty strings
AssertEquals('Both empty strings should return 1.0',
1.0, TStringKit.JaroWinklerSimilarity('', ''));
AssertEquals('One empty string should return 0.0',
0.0, TStringKit.JaroWinklerSimilarity('A', ''));
end;
procedure TStringTests.Test36_LongestCommonSubsequence;
begin
// Identical strings should return the string itself
AssertEquals('Identical strings should return the entire string',
'test', TStringKit.LongestCommonSubsequence('test', 'test'));
// Partial matches
AssertEquals('LCS should find common characters in order',
'abc', TStringKit.LongestCommonSubsequence('abcdef', 'xabcyz'));
AssertEquals('LCS should work with non-consecutive matches',
'acd', TStringKit.LongestCommonSubsequence('abcdef', 'xacdyz'));
// No common subsequence
AssertEquals('No common characters should return empty string',
'', TStringKit.LongestCommonSubsequence('abc', 'xyz'));
end;
procedure TStringTests.Test37_LCSSimilarity;
begin
// Identical strings should have similarity 1.0
AssertEquals('Identical strings should have LCS similarity 1.0',
1.0, TStringKit.LCSSimilarity('test', 'test'));
// No common subsequence should have similarity 0.0
AssertEquals('No common characters should have similarity 0.0',
0.0, TStringKit.LCSSimilarity('abc', 'xyz'));
// Partial matches
AssertTrue('Partial matches should have similarity between 0 and 1',
(TStringKit.LCSSimilarity('abcdef', 'abcxyz') > 0.0) and
(TStringKit.LCSSimilarity('abcdef', 'abcxyz') < 1.0));
end;
procedure TStringTests.Test38_IsFuzzyMatch;
begin
// Identical strings should match with any method and threshold
AssertTrue('Identical strings should match with Levenshtein',
TStringKit.IsFuzzyMatch('test', 'test', 0.8, 0));
AssertTrue('Identical strings should match with Jaro-Winkler',
TStringKit.IsFuzzyMatch('test', 'test', 0.8, 1));
AssertTrue('Identical strings should match with LCS',
TStringKit.IsFuzzyMatch('test', 'test', 0.8, 2));
// Different strings should match or not based on threshold
AssertTrue('Similar strings should match with lower threshold',
TStringKit.IsFuzzyMatch('hello', 'hallo', 0.6, 0));
AssertFalse('Similar strings should not match with higher threshold',
TStringKit.IsFuzzyMatch('hello', 'hallo', 0.9, 0));
// Different methods might give different results for the same strings
AssertTrue('Different methods might give different results for the same strings',
TStringKit.IsFuzzyMatch('prefix123', 'prefix456', 0.7, 1)); // Jaro-Winkler should match with prefix
end;
procedure TStringTests.Test39_ToTitleCase;
begin
AssertEquals('Empty string should remain empty',
'', TStringKit.ToTitleCase(''));
AssertEquals('Title case should capitalize first letter of each word',
'This Is A Test', TStringKit.ToTitleCase('this is a test'));
AssertEquals('Title case should work with mixed case input',
'Mixed Case Input', TStringKit.ToTitleCase('MiXeD cAsE iNpUt'));
AssertEquals('Title case should handle punctuation',
'Hello, World! How Are You?', TStringKit.ToTitleCase('hello, world! how are you?'));
end;
procedure TStringTests.Test40_ToCamelCase;
begin
AssertEquals('Empty string should remain empty',
'', TStringKit.ToCamelCase(''));
AssertEquals('camelCase should start with lowercase and capitalize other words',
'thisIsATest', TStringKit.ToCamelCase('this is a test'));
AssertEquals('camelCase should handle multiple spaces',
'helloWorld', TStringKit.ToCamelCase('hello world'));
AssertEquals('camelCase should normalize existing caps',
'camelCaseText', TStringKit.ToCamelCase('Camel Case TEXT'));
end;
procedure TStringTests.Test41_ToPascalCase;
begin
AssertEquals('Empty string should remain empty',
'', TStringKit.ToPascalCase(''));
AssertEquals('PascalCase should capitalize all words',
'ThisIsATest', TStringKit.ToPascalCase('this is a test'));
AssertEquals('PascalCase should handle multiple spaces',
'HelloWorld', TStringKit.ToPascalCase('hello world'));
AssertEquals('PascalCase should normalize existing caps',
'PascalCaseText', TStringKit.ToPascalCase('Pascal Case TEXT'));
end;
procedure TStringTests.Test42_ToSnakeCase;
begin
AssertEquals('Empty string should remain empty',
'', TStringKit.ToSnakeCase(''));
AssertEquals('snake_case should be all lowercase with underscores',
'this_is_a_test', TStringKit.ToSnakeCase('this is a test'));
AssertEquals('snake_case should handle multiple spaces',
'hello_world', TStringKit.ToSnakeCase('hello world'));
AssertEquals('snake_case should normalize existing caps',
'snake_case_text', TStringKit.ToSnakeCase('Snake Case TEXT'));
end;
procedure TStringTests.Test43_ToKebabCase;
begin
AssertEquals('Empty string should remain empty',
'', TStringKit.ToKebabCase(''));
AssertEquals('kebab-case should be all lowercase with hyphens',
'this-is-a-test', TStringKit.ToKebabCase('this is a test'));
AssertEquals('kebab-case should handle multiple spaces',
'hello-world', TStringKit.ToKebabCase('hello world'));
AssertEquals('kebab-case should normalize existing caps',
'kebab-case-text', TStringKit.ToKebabCase('Kebab Case TEXT'));
end;
procedure TStringTests.Test44_IsValidEmail;
begin
AssertTrue('Valid email should pass',
TStringKit.IsValidEmail('user@example.com'));
AssertTrue('Email with subdomain should pass',
TStringKit.IsValidEmail('user@sub.example.com'));
AssertTrue('Email with numbers should pass',
TStringKit.IsValidEmail('user123@example.com'));
AssertFalse('Email without @ should fail',
TStringKit.IsValidEmail('userexample.com'));
AssertFalse('Email without domain should fail',
TStringKit.IsValidEmail('user@'));
AssertFalse('Email with invalid characters should fail',
TStringKit.IsValidEmail('user@exam@ple.com'));
end;
procedure TStringTests.Test45_IsValidURL;
begin
AssertTrue('Valid HTTP URL should pass',
TStringKit.IsValidURL('http://example.com'));
AssertTrue('Valid HTTPS URL should pass',
TStringKit.IsValidURL('https://example.com'));
AssertTrue('URL with path should pass',
TStringKit.IsValidURL('https://example.com/path'));
AssertTrue('URL with query parameters should pass',
TStringKit.IsValidURL('https://example.com/path?query=1'));
AssertFalse('URL without protocol should fail',
TStringKit.IsValidURL('example.com'));
AssertFalse('URL with invalid protocol should fail',
TStringKit.IsValidURL('invalid://example.com'));
end;
procedure TStringTests.Test46_IsValidIP;
begin
AssertTrue('Valid IPv4 should pass',
TStringKit.IsValidIP('192.168.1.1'));
AssertTrue('Valid IPv6 should pass',
TStringKit.IsValidIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
AssertTrue('Compressed IPv6 should pass',
TStringKit.IsValidIP('::1'));
AssertFalse('Invalid IP should fail',
TStringKit.IsValidIP('256.256.256.256'));
AssertFalse('Not an IP should fail',
TStringKit.IsValidIP('not an ip'));
end;
procedure TStringTests.Test47_IsValidIPv4;
begin
AssertTrue('Valid IPv4 should pass',
TStringKit.IsValidIPv4('192.168.1.1'));
AssertTrue('IP with zeros should pass',
TStringKit.IsValidIPv4('0.0.0.0'));
AssertTrue('Max values should pass',
TStringKit.IsValidIPv4('255.255.255.255'));
AssertFalse('IPv6 should fail',
TStringKit.IsValidIPv4('::1'));
AssertFalse('IP with out-of-range values should fail',
TStringKit.IsValidIPv4('256.256.256.256'));
AssertFalse('IP with wrong format should fail',
TStringKit.IsValidIPv4('192.168.1'));
end;
procedure TStringTests.Test48_IsValidIPv6;
begin
AssertTrue('Valid IPv6 should pass',
TStringKit.IsValidIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
AssertTrue('Compressed IPv6 should pass',
TStringKit.IsValidIPv6('::1'));
AssertTrue('Partially compressed IPv6 should pass',
TStringKit.IsValidIPv6('2001:db8::1'));
AssertFalse('IPv4 should fail',
TStringKit.IsValidIPv6('192.168.1.1'));
AssertFalse('Invalid format should fail',
TStringKit.IsValidIPv6('2001:db8:::1'));
AssertFalse('Too many segments should fail',
TStringKit.IsValidIPv6('1:2:3:4:5:6:7:8:9'));
end;
procedure TStringTests.Test49_IsValidDate;
begin
AssertTrue('Valid date with standard format should pass',
TStringKit.IsValidDate('2023-01-15', 'yyyy-mm-dd'));
AssertTrue('Valid date with custom format should pass',
TStringKit.IsValidDate('15/01/2023', 'dd/mm/yyyy'));
AssertFalse('Invalid date should fail',
TStringKit.IsValidDate('2023-02-31', 'yyyy-mm-dd'));
AssertFalse('Non-date should fail',
TStringKit.IsValidDate('not a date', 'yyyy-mm-dd'));
AssertFalse('Wrong format should fail',
TStringKit.IsValidDate('01-15-2023', 'dd-mm-yyyy'));
end;
procedure TStringTests.Test50_Truncate;
begin
AssertEquals('Short strings should remain unchanged',
'Short text', TStringKit.Truncate('Short text', 15, '...'));
// For the text "Exactly ten" with exactly 10 characters, and a limit of 10,
// the truncate function will add ellipsis, resulting in "Exactly..."
AssertEquals('Text exactly at limit should be truncated properly',
'Exactly...', TStringKit.Truncate('Exactly ten', 10, '...'));
AssertEquals('Longer text should be truncated with ellipsis',
'Very lo...', TStringKit.Truncate('Very long text', 10, '...'));
AssertEquals('Custom ellipsis should work',
'Very lo---', TStringKit.Truncate('Very long text', 10, '---'));
AssertEquals('Empty text should remain empty',
'', TStringKit.Truncate('', 10, '...'));
end;
procedure TStringTests.Test51_FormatFileSize;
begin
AssertEquals('Bytes should be formatted correctly',
'500 B', TStringKit.FormatFileSize(500));
AssertEquals('Kilobytes should be formatted correctly',
'1.50 KB', TStringKit.FormatFileSize(1536));
AssertEquals('Megabytes should be formatted correctly',
'1.50 MB', TStringKit.FormatFileSize(1572864));
AssertEquals('Gigabytes should be formatted correctly',
'1.50 GB', TStringKit.FormatFileSize(1610612736));
end;
procedure TStringTests.Test52_FormatNumber;
begin
AssertEquals('Small number should be formatted without separators',
'123', TStringKit.FormatNumber(123));
AssertEquals('Thousands should be formatted with separators',
'1,234', TStringKit.FormatNumber(1234));
AssertEquals('Millions should be formatted with multiple separators',
'1,234,567', TStringKit.FormatNumber(1234567));
AssertEquals('Custom separator should work',
'1.234.567', TStringKit.FormatNumber(1234567, '.'));
AssertEquals('Zero should be formatted correctly',
'0', TStringKit.FormatNumber(0));
AssertEquals('Negative numbers should be preserved',
'-1234', TStringKit.FormatNumber(-1234));
end;
procedure TStringTests.Test53_FormatFloat;
begin
AssertEquals('Integers should be formatted with decimal places',
'123.00', TStringKit.FormatFloat(123));
AssertEquals('Floating point numbers should be rounded correctly',
'123.46', TStringKit.FormatFloat(123.456));
AssertEquals('Custom decimal places should work',
'123.456', TStringKit.FormatFloat(123.456, 3));
AssertEquals('Custom separators should work',
'1,234.57', TStringKit.FormatFloat(1234.567, 2, '.', ','));
AssertEquals('Custom separators (swapped) should work',
'1.234,57', TStringKit.FormatFloat(1234.567, 2, ',', '.'));
AssertEquals('Zero decimal places should work',
'123', TStringKit.FormatFloat(123.456, 0));
AssertEquals('Negative numbers should be preserved',
'-123.46', TStringKit.FormatFloat(-123.456));
end;
procedure TStringTests.Test54_Join;
var
Arr1, Arr2, Arr3, EmptyArr: TStringDynArray;
begin
SetLength(Arr1, 3);
Arr1[0] := 'one';
Arr1[1] := 'two';
Arr1[2] := 'three';
SetLength(Arr2, 2);
Arr2[0] := 'hello';
Arr2[1] := 'world';
SetLength(Arr3, 1);
Arr3[0] := 'single';
SetLength(EmptyArr, 0);
// Basic joining
AssertEquals('Join should concatenate with delimiter',
'one,two,three', TStringKit.Join(Arr1, ','));
// Different delimiter
AssertEquals('Join should work with different delimiters',
'hello world', TStringKit.Join(Arr2, ' '));
// Single element
AssertEquals('Join with single element should return that element',
'single', TStringKit.Join(Arr3, ','));
// Empty array
AssertEquals('Join with empty array should return empty string',
'', TStringKit.Join(EmptyArr, ','));
end;
procedure TStringTests.Test55_Split;
var
Result: TStringDynArray;
begin
// Basic splitting
Result := TStringKit.Split('one,two,three', ',');
AssertEquals('Split should return correct number of elements', 3, Length(Result));
AssertEquals('First element should match', 'one', Result[0]);
AssertEquals('Second element should match', 'two', Result[1]);
AssertEquals('Third element should match', 'three', Result[2]);
// Splitting with empty entries
Result := TStringKit.Split('one,,three', ',');
AssertEquals('Split with empty entries should include them', 3, Length(Result));
AssertEquals('Empty entry should be preserved', '', Result[1]);
// Removing empty entries
Result := TStringKit.Split('one,,three', ',', 0, True);
AssertEquals('Split with RemoveEmptyEntries should exclude them', 2, Length(Result));
AssertEquals('First element should match', 'one', Result[0]);
AssertEquals('Second element should match', 'three', Result[1]);
// MaxSplit parameter
Result := TStringKit.Split('one,two,three,four', ',', 2);
AssertEquals('MaxSplit should limit number of splits', 3, Length(Result));
AssertEquals('Last element should contain remaining text', 'three,four', Result[2]);
// Empty string
Result := TStringKit.Split('', ',');
AssertEquals('Empty string should return array with one empty element', 1, Length(Result));
AssertEquals('Element should be empty', '', Result[0]);
// No delimiter in string
Result := TStringKit.Split('text', ',');
AssertEquals('No delimiter should return array with original string', 1, Length(Result));
AssertEquals('Element should be original string', 'text', Result[0]);
end;
procedure TStringTests.Test56_Soundex;
begin
AssertEquals('Soundex for Smith should be S530',
'S530', TStringKit.Soundex('Smith'));
AssertEquals('Soundex should be case insensitive',
'S530', TStringKit.Soundex('smith'));
AssertEquals('Soundex for Smyth should match Smith',
TStringKit.Soundex('Smith'), TStringKit.Soundex('Smyth'));
AssertEquals('Soundex for Robert should be R163',
'R163', TStringKit.Soundex('Robert'));
AssertEquals('Soundex for Rupert should match Robert',
TStringKit.Soundex('Robert'), TStringKit.Soundex('Rupert'));
AssertEquals('Soundex for empty string should be empty',
'', TStringKit.Soundex(''));
end;
procedure TStringTests.Test57_Metaphone;
begin
// Note: Some assertions may need adjustment depending on the actual Metaphone implementation
// Instead of AssertNotEquals, use AssertTrue with condition
AssertTrue('Metaphone code should not be empty for valid input',
TStringKit.Metaphone('Smith') <> '');
// Our implementation normalizes plural forms, so both should have the same code
AssertEquals('Metaphone codes for similar sounding words should match with plural normalization',
TStringKit.Metaphone('function'), TStringKit.Metaphone('functions'));
AssertEquals('Metaphone for empty string should be empty',
'', TStringKit.Metaphone(''));
end;
procedure TStringTests.Test58_CountWords;
begin
AssertEquals('Simple sentence should have correct word count',
4, TStringKit.CountWords('This is a test'));
AssertEquals('Sentence with punctuation should count only words',
5, TStringKit.CountWords('Hello, world! How are you?'));
AssertEquals('Empty string should have zero words',
0, TStringKit.CountWords(''));
AssertEquals('String with only non-alphanumeric chars should have zero words',
0, TStringKit.CountWords('!@#$%^&*()'));
end;
procedure TStringTests.Test59_FleschKincaidReadability;
begin
// Since readability is calculated with a complex formula, we test for reasonableness
// Simple text should have high readability (closer to 100)
AssertTrue('Simple text should have high readability score',
TStringKit.FleschKincaidReadability('The dog ran. The cat jumped. I am here.') > 80);
// Complex text should have lower readability
AssertTrue('Complex text should have lower readability score',
TStringKit.FleschKincaidReadability('The mitochondrion is a double membrane-bound organelle found in most eukaryotic organisms.') < 50);
// Empty text should be handled
AssertEquals('Empty text should return zero readability',
0, TStringKit.FleschKincaidReadability(''));
end;
procedure TStringTests.Test60_GenerateNGrams;
var
Result: TStringDynArray;
begin
// Bigrams
Result := TStringKit.GenerateNGrams('This is a test', 2);
AssertEquals('Bigrams should have correct count', 3, Length(Result));
AssertEquals('First bigram should match', 'This is', Result[0]);
AssertEquals('Second bigram should match', 'is a', Result[1]);
AssertEquals('Third bigram should match', 'a test', Result[2]);
// Trigrams
Result := TStringKit.GenerateNGrams('This is a test', 3);
AssertEquals('Trigrams should have correct count', 2, Length(Result));
AssertEquals('First trigram should match', 'This is a', Result[0]);
AssertEquals('Second trigram should match', 'is a test', Result[1]);
// Not enough words
Result := TStringKit.GenerateNGrams('Single', 2);
AssertEquals('Not enough words for n-gram should return empty array', 0, Length(Result));
Result := TStringKit.GenerateNGrams('', 2);
AssertEquals('Empty text should return empty array', 0, Length(Result));
Result := TStringKit.GenerateNGrams('This is a test', 0);
AssertEquals('N=0 should return empty array', 0, Length(Result));
end;
procedure TStringTests.Test61_HTMLEncode;
begin
AssertEquals('Basic HTML encoding should work',
'<div>', TStringKit.HTMLEncode('<div>'));
AssertEquals('Multiple entities should be encoded',
'<a href="test">Link</a>',
TStringKit.HTMLEncode('<a href="test">Link</a>'));
AssertEquals('Ampersand should be encoded',
'This & that', TStringKit.HTMLEncode('This & that'));
AssertEquals('Single quote should be encoded',
'It's mine', TStringKit.HTMLEncode('It''s mine'));