2
0

tcstringhelp.pp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. unit tcstringhelp;
  2. {$mode objfpc}{$H+}
  3. //{$modeswitch advancedrecords}
  4. //{$modeswitch typehelpers}
  5. interface
  6. uses
  7. SysUtils, fpcunit, testregistry;
  8. Const
  9. TBI = 'To be implemented';
  10. Type
  11. TTestStringHelper = Class(TTestCase)
  12. Public
  13. Function TestArray(Msg : string; Aexpected : Array of string; AActual : TStringArray) : boolean;
  14. Published
  15. Procedure TestCompare;
  16. Procedure TestCompareOrdinal;
  17. Procedure TestCompareText;
  18. Procedure TestCopy;
  19. Procedure TestCreate;
  20. Procedure TestEndsText;
  21. Procedure TestEquals;
  22. Procedure TestFormat;
  23. Procedure TestIsNullOrEmpty;
  24. Procedure TestIsNullOrWhiteSpace;
  25. Procedure TestJoin;
  26. Procedure TestLowerCase;
  27. Procedure TestParse;
  28. Procedure TestToBoolean;
  29. Procedure TestToDouble;
  30. Procedure TestToExtended;
  31. Procedure TestToInt64;
  32. Procedure TestToInteger;
  33. Procedure TestUppercase;
  34. Procedure TestCompareTo;
  35. Procedure TestContains;
  36. Procedure TestCountChar;
  37. Procedure TestDeQuotedString;
  38. Procedure TestEndsWith;
  39. Procedure TestGetHashCode;
  40. Procedure TestIndexOf;
  41. Procedure TestIndexOfAny;
  42. Procedure TestIndexOfAnyString;
  43. Procedure TestIndexOfUnquoted;
  44. Procedure TestIndexOfAnyUnquoted;
  45. Procedure TestInsert;
  46. Procedure TestIsDelimiter;
  47. Procedure TestIsEmpty;
  48. Procedure TestLastDelimiter;
  49. Procedure TestLastIndexOf;
  50. Procedure TestLastIndexOfString;
  51. Procedure TestLastIndexOfAny;
  52. Procedure TestPadLeft;
  53. Procedure TestPadRight;
  54. Procedure TestQuotedString;
  55. Procedure TestRemove;
  56. Procedure TestReplace;
  57. Procedure TestSplit;
  58. Procedure TestSplitString;
  59. Procedure TestStartsWith;
  60. Procedure TestSubstring;
  61. Procedure TestToCharArray;
  62. Procedure TestToLower;
  63. Procedure TestToLowerInvariant;
  64. Procedure TestToUpper;
  65. Procedure TestToUpperInvariant;
  66. Procedure TestTrim;
  67. Procedure TestTrimLeft;
  68. Procedure TestTrimRight;
  69. Procedure TestTrimEnd;
  70. Procedure TestTrimStart;
  71. Procedure TestChars;
  72. Procedure TestLength;
  73. end;
  74. implementation
  75. Procedure TTestStringHelper.TestCompare;
  76. Var
  77. r : Integer;
  78. begin
  79. // Simple cases
  80. R:=String.Compare('A','B');
  81. AssertTrue('1. Simple Compare strings (A,B) :'+IntToStr(R)+'<0',R<0);
  82. R:=String.Compare('B','A');
  83. AssertTrue('2. Simple Compare strings (B,A) :'+IntToStr(R)+'>0',R>0);
  84. R:=String.Compare('A','A');
  85. AssertTrue('3. Simple Compare strings (A,A) :'+IntToStr(R)+'=0',R=0);
  86. R:=String.Compare('A','a', true);
  87. AssertTrue('4. Simple ignore case Compare strings (A,a) : '+IntToStr(R)+'=0',R=0);
  88. R:=String.Compare('b','a',True);
  89. AssertTrue('5. Simple ignore case Compare strings (b,a) : '+IntToStr(R)+'>0',R>0);
  90. R:=String.Compare('A','a',[coIgnoreCase]);
  91. AssertTrue('6. [coIgnoreCase] Compare strings (A,a) : '+IntToStr(R)+'=0',R=0);
  92. R:=String.Compare('b','a',[coIgnoreCase]);
  93. AssertTrue('7. [coIgnoreCase] Compare strings (b,a) : '+IntToStr(R)+'>0',R>0);
  94. // Check whether index is observed.
  95. R:=String.Compare('AA',1,'AB',1,1);
  96. AssertTrue('8. Compare(''AA'',1,''AB'',1,1) :'+IntToStr(R)+'<0',R<0);
  97. R:=String.Compare('AB',1,'AA',1,1);
  98. AssertTrue('9. Compare(''AB'',1,''AA'',1,1) :'+IntToStr(R)+'>0',R>0);
  99. R:=String.Compare('AA',1,'AA',1,1);
  100. AssertTrue('10. Compare(''AA'',1,''AA'',1,1) :'+IntToStr(R)+'=0',R=0);
  101. // Make sure only len chars are used.
  102. R:=String.Compare('AAC',1,'ABD',1,1);
  103. AssertTrue('11. Compare(''AAC'',1,''ABD'',1,1) :'+IntToStr(R)+'<0',R<0);
  104. R:=String.Compare('ABC',1,'AAD',1,1);
  105. AssertTrue('12 Compare(''ABC'',1,''AAD'',1,1) :'+IntToStr(R)+'>0',R>0);
  106. R:=String.Compare('AAC',1,'AAD',1,1);
  107. AssertTrue('13. Compare(''AAC'',1,''AAD'',1,1) :'+IntToStr(R)+'=0',R=0);
  108. // Index, case insensitive
  109. R:=String.Compare('AA',1,'Aa',1,1,true);
  110. AssertTrue('14. Compare(''AA'',1,''Aa'',1,1,true) : '+IntToStr(R)+'=0',R=0);
  111. R:=String.Compare('Ab',1,'Aa',1,1,True);
  112. AssertTrue('15. Compare(''Ab'',1,''Aa'',1,1,True) : '+IntToStr(R)+'>0',R>0);
  113. R:=String.Compare('A',1,'a',1,1,[coIgnoreCase]);
  114. AssertTrue('16. Compare(''A'',1,''a'',1,1,[coIgnoreCase]) : '+IntToStr(R)+'=0',R=0);
  115. // Index, maxlen, case insensitive
  116. R:=String.Compare('AAC',1,'AaD',1,1,true);
  117. AssertTrue('17. Compare(''AAC'',1,''AaD'',1,1,true) : '+IntToStr(R)+'=0',R=0);
  118. R:=String.Compare('AbC',1,'AaD',1,1,True);
  119. AssertTrue('18. Compare(''AbC'',1,''AaD'',1,1,True) : '+IntToStr(R)+'>0',R>0);
  120. R:=String.Compare('AAC',1,'AaD',1,1,[coIgnoreCase]);
  121. AssertTrue('19. Compare(''AAC'',1,''AaD'',1,1,[coIgnoreCase]) : '+IntToStr(R)+'=0',R=0);
  122. end;
  123. Procedure TTestStringHelper.TestCompareOrdinal;
  124. Var
  125. r : Integer;
  126. begin
  127. // Simple
  128. R:=String.CompareOrdinal('A','B');
  129. AssertTrue('1. Simple Compare strings (A,B) :'+IntToStr(R)+'<0',R<0);
  130. R:=String.CompareOrdinal('B','A');
  131. AssertTrue('2. Simple Compare strings (B,A) :'+IntToStr(R)+'>0',R>0);
  132. R:=String.CompareOrdinal('A','A');
  133. AssertTrue('3. Simple Compare strings (A,A) :'+IntToStr(R)+'=0',R=0);
  134. // Index
  135. R:=String.CompareOrdinal('AA',1,'AB',1,1);
  136. AssertTrue('4. Simple Compare strings (AA,1,AB,1,1) :'+IntToStr(R)+'<0',R<0);
  137. R:=String.CompareOrdinal('AB',1,'AA',1,1);
  138. AssertTrue('5. Simple Compare strings (AB,1,AA,1,1) :'+IntToStr(R)+'>0',R>0);
  139. R:=String.CompareOrdinal('AA',1,'AA',1,1);
  140. AssertTrue('6. Simple Compare strings (AA,1,AA,1,1) :'+IntToStr(R)+'=0',R=0);
  141. // Index, maxlen
  142. R:=String.CompareOrdinal('AAC',1,'ABD',1,1);
  143. AssertTrue('7. Simple Compare strings (AAC,1,ABD,1,1) :'+IntToStr(R)+'<0',R<0);
  144. R:=String.CompareOrdinal('ABC',1,'AAD',1,1);
  145. AssertTrue('8. Simple Compare strings (ABC,1,AAD,1,1) :'+IntToStr(R)+'>0',R>0);
  146. R:=String.CompareOrdinal('AAD',1,'AAD',1,1);
  147. AssertTrue('9. Simple Compare strings (AAC,1,AAD,1,1) :'+IntToStr(R)+'=0',R=0);
  148. end;
  149. Procedure TTestStringHelper.TestCompareText;
  150. Var
  151. r : Integer;
  152. begin
  153. R:=String.CompareText('A','B');
  154. AssertTrue('1. Simple Compare strings (A,B) :'+IntToStr(R)+'<0',R<0);
  155. R:=String.CompareText('B','A');
  156. AssertTrue('Simple Compare strings (B,A) :'+IntToStr(R)+'>0',R>0);
  157. R:=String.CompareText('A','A');
  158. AssertTrue('Simple Compare strings (A,A) :'+IntToStr(R)+'=0',R=0);
  159. //
  160. R:=String.CompareText('A','b');
  161. AssertTrue('Simple Compare strings (A,b) :'+IntToStr(R)+'<0',R<0);
  162. R:=String.CompareText('B','a');
  163. AssertTrue('Simple Compare strings (B,a) :'+IntToStr(R)+'>0',R>0);
  164. R:=String.CompareText('A','a');
  165. AssertTrue('Simple Compare strings (A,a) :'+IntToStr(R)+'=0',R=0);
  166. end;
  167. Procedure TTestStringHelper.TestCopy;
  168. var
  169. A,S : String;
  170. begin
  171. A:=TBI;
  172. S:=String.Copy(A);
  173. AssertEquals('Copy creates equal copy',A,S);
  174. end;
  175. Procedure TTestStringHelper.TestCreate;
  176. Var
  177. A : String;
  178. begin
  179. A:=String.Create('*',5);
  180. AssertEquals('Create with char and length','*****',A);
  181. A:=String.Create(['a','b','c','d','e']);
  182. AssertEquals('Create with array of char','abcde',A);
  183. A:=String.Create(['a','b','c','d','e'],1,3);
  184. AssertEquals('Create with array of char and index, len','bcd',A);
  185. end;
  186. Procedure TTestStringHelper.TestEndsText;
  187. begin
  188. AssertTrue('1. EndsText, correct',String.EndsText('be','to be or not to be'));
  189. AssertTrue('2. EndsText, correct, case insensitive',String.EndsText('BE','to be or not to be'));
  190. AssertFalse('3. EndsText, not correct',String.EndsText('de','to be or not to be'));
  191. AssertFalse('4. EndsText, empty',String.EndsText('','to be or not to be'));
  192. end;
  193. Procedure TTestStringHelper.TestEquals;
  194. Var
  195. A,B : String;
  196. begin
  197. A:='Yes';
  198. B:='No';
  199. AssertFalse('1. Equals(A,B)',String.Equals(A,B));
  200. B:='Yes';
  201. AssertTrue('2. Equals(A,B)',String.Equals(A,B));
  202. B:='No';
  203. AssertFalse('3. A.Equals(B)',A.Equals(B));
  204. B:='Yes';
  205. AssertTrue('4. A.Equals(B)',A.Equals(B));
  206. end;
  207. Procedure TTestStringHelper.TestFormat;
  208. begin
  209. AssertEquals('1. Format as class function','A1 BC', String.Format('A%d B%s',[1,'C']));
  210. AssertEquals('2. Format function','A1 BC', 'A%d B%s'.Format([1,'C']));
  211. end;
  212. Procedure TTestStringHelper.TestIsNullOrEmpty;
  213. begin
  214. AssertTrue('1. Empty string returns true',String.IsNullOrEmpty(''));
  215. end;
  216. Procedure TTestStringHelper.TestIsNullOrWhiteSpace;
  217. Var
  218. C : Char;
  219. begin
  220. AssertTrue('2. Empty string returns true',String.IsNullOrWhitespace(''));
  221. For C:=#1 to #32 do
  222. AssertTrue('Char '+IntToStr(Ord(C))+' string is whitespace',String.IsNullOrWhiteSpace(C));
  223. end;
  224. Procedure TTestStringHelper.TestJoin;
  225. begin
  226. AssertEquals('1 element','ABC',String.Join(' ',['ABC']));
  227. AssertEquals('2 elements','ABC DEF',String.Join(' ',['ABC','DEF']));
  228. AssertEquals('3 elements','ABC DEF GHI',String.Join(' ',['ABC','DEF','GHI']));
  229. AssertEquals('5 elements, index','ABC DEF GHI',String.Join(' ',['NONO','ABC','DEF','GHI','nono'],1,3));
  230. end;
  231. Procedure TTestStringHelper.TestLowerCase;
  232. begin
  233. AssertEquals('1. Simple Lowercase','abc',String.Lowercase('ABC'));
  234. end;
  235. Procedure TTestStringHelper.TestParse;
  236. Var
  237. E : Extended;
  238. begin
  239. E:=12.3;
  240. AssertEquals('Boolean','-1',String.Parse(True));
  241. AssertEquals('Integer','12',String.Parse(Integer(12)));
  242. AssertEquals('Int64','45',String.Parse(NativeInt(45)));
  243. AssertEquals('Extended',FloatToStr(E),String.Parse(E));
  244. end;
  245. Procedure TTestStringHelper.TestToBoolean;
  246. begin
  247. AssertTrue('Class function, true',String.ToBoolean('True'));
  248. AssertTrue('function 1',String.ToBoolean('1'));
  249. AssertFalse('Class function false',String.ToBoolean('False'));
  250. AssertFalse('function 0',String.ToBoolean('0'));
  251. end;
  252. Procedure TTestStringHelper.TestToDouble;
  253. begin
  254. AssertEquals('Class function, 0',0.0,String.ToDouble('0.0'),0.1);
  255. AssertEquals('Class function, 1.2',1.2,String.ToDouble('1.2'),0.1);
  256. AssertEquals('function, 0',0.0,'0.0'.ToDouble,0.1);
  257. AssertEquals('function, 1.2',1.2,'1.2'.ToDouble,0.1);
  258. end;
  259. Procedure TTestStringHelper.TestToExtended;
  260. begin
  261. AssertEquals('Class function, 0',0.0,String.ToExtended('0.0'),0.1);
  262. AssertEquals('Class function, 1.2',1.2,String.ToExtended('1.2'),0.1);
  263. AssertEquals('function, 0',0.0,'0.0'.ToExtended,0.1);
  264. AssertEquals('function, 1.2',1.2,'1.2'.ToExtended,0.1);
  265. end;
  266. Procedure TTestStringHelper.TestToInt64;
  267. begin
  268. AssertEquals('Class function, 0',0,String.ToNativeInt('0'));
  269. AssertEquals('Class function, 12',12,String.ToNativeInt('12'));
  270. AssertEquals('Class function, 1234567890123',1234567890123,String.ToNativeInt('1234567890123'));
  271. // 2 characters because it does not work on length 1, compiler assumes Char as in Delphi
  272. AssertEquals('Class function, 0',0,'00'.ToNativeInt);
  273. AssertEquals('Class function, 12',12,'12'.ToNativeInt);
  274. AssertEquals('Class function, 1234567890123',1234567890123,'1234567890123'.ToNativeInt);
  275. end;
  276. Procedure TTestStringHelper.TestToInteger;
  277. begin
  278. AssertEquals('Class function, 0',0,String.ToInteger('0'));
  279. AssertEquals('Class function, 12',12,String.ToInteger('12'));
  280. AssertEquals('Class function, 123456789',123456789,String.ToInteger('123456789'));
  281. // 2 characters because it does not work on length 1, compiler assumes Char as in Delphi
  282. AssertEquals('Class function, 0',0,'00'.ToInteger);
  283. AssertEquals('Class function, 12',12,'12'.ToInteger);
  284. AssertEquals('Class function, 123456789',123456789,'123456789'.ToInteger);
  285. end;
  286. Procedure TTestStringHelper.TestUppercase;
  287. begin
  288. AssertEquals('1. Simple Lowercase','ABC',String.UpperCase('abc'));
  289. end;
  290. Procedure TTestStringHelper.TestCompareTo;
  291. begin
  292. // 2 characters because it does not work on length 1, compiler assumes Char as in Delphi
  293. AssertTrue('1. A<B','AA'.CompareTo('AB')<0);
  294. AssertTrue('1. A=A','AA'.CompareTo('AA')=0);
  295. AssertTrue('1. B>A','AB'.CompareTo('AA')>0);
  296. end;
  297. Procedure TTestStringHelper.TestContains;
  298. begin
  299. AssertTrue('ABC contains AB','ABC'.Contains('AB'));
  300. AssertTrue('ABC contains BC','ABC'.Contains('BC'));
  301. AssertTrue('ABC contains B','ABC'.Contains('B'));
  302. AssertFalse('ABC does not contain empty','ABC'.Contains(''));
  303. AssertFalse('ABC does not contain DEF','ABC'.Contains('DEF'));
  304. AssertFalse('ABC does not contain a','ABC'.Contains('a'));
  305. end;
  306. (*
  307. // No PChar support
  308. Procedure TTestStringHelper.TestCopyTo;
  309. Type
  310. TCharArray = Array Of Char;
  311. Const
  312. Res1 : Array[0..4] of Char = ('T','o',' ','b','e');
  313. Var
  314. S : String;
  315. A : TCharArray;
  316. I : Integer;
  317. begin
  318. A:=Default(TCharArray);
  319. S:=TBI;
  320. SetLength(A,5);
  321. S.CopyTo(0,A,0,5);
  322. For I:=0 to 4 do
  323. AssertEquals('Zero indexes, Char '+IntToStr(i),Res1[I],A[I]);
  324. S:='AB'+S;
  325. S.CopyTo(2,A,0,5);
  326. For I:=0 to 4 do
  327. AssertEquals('Source index, zero dest index, Char '+IntToStr(i),Res1[I],A[I]);
  328. SetLength(A,8);
  329. S.CopyTo(2,A,3,5);
  330. For I:=0 to 4 do
  331. AssertEquals('Source index, dest index, Char '+IntToStr(i),Res1[I],A[I+3]);
  332. end;
  333. *)
  334. Procedure TTestStringHelper.TestCountChar;
  335. begin
  336. AssertEquals('Empty string',0,''.CountChar(' '));
  337. AssertEquals('Start and end ',2,' ** '.CountChar(' '));
  338. AssertEquals('Middle',2,'* *'.CountChar(' '));
  339. end;
  340. Procedure TTestStringHelper.TestDeQuotedString;
  341. Const
  342. C = TBI;
  343. C2 = 'To be ''implemented';
  344. C3 = 'To be "implemented';
  345. Var
  346. S : String;
  347. begin
  348. S:=''''+C+'''';
  349. AssertEquals('Simple case of '+S,C,S.DequotedString);
  350. S:=''''+StringReplace(C2,'''','''''',[rfReplaceAll])+'''';
  351. AssertEquals('Quoted case of '+S,C2,S.DequotedString);
  352. S:='"'+C+'"';
  353. AssertEquals('Simple case of '+S,C,S.DequotedString('"'));
  354. S:='"'+StringReplace(C3,'"','""',[rfReplaceAll])+'"';
  355. AssertEquals('Quoted case of '+S,C3,S.DequotedString('"'));
  356. end;
  357. Procedure TTestStringHelper.TestEndsWith;
  358. Var
  359. S : String;
  360. begin
  361. S:=TBI;
  362. AssertTrue('length 0', S.EndsWith(''));
  363. AssertTrue('length 1', S.EndsWith('d'));
  364. AssertTrue('length 2', S.EndsWith('ed'));
  365. AssertTrue('equal length (same)', S.EndsWith(S));
  366. AssertFalse('length+2', S.EndsWith(S+'ed'));
  367. AssertFalse('Random string', S.EndsWith('erd'));
  368. AssertTrue('match case ', S.EndsWith('ed',False));
  369. AssertFalse('match case, no match ', S.EndsWith('eD',False));
  370. AssertTrue('no match case, match ', S.EndsWith('ED',True));
  371. AssertFalse('no match case, no match ', S.EndsWith('DED',True));
  372. end;
  373. Procedure TTestStringHelper.TestGetHashCode;
  374. {
  375. Function GetHashCode: Integer;
  376. }
  377. Var
  378. S : String;
  379. begin
  380. S:=TBI;
  381. AssertTrue('Nonzero hashcode',S.GetHashCode<>0);
  382. // A more meaningful test would be nice...
  383. end;
  384. Procedure TTestStringHelper.TestIndexOf;
  385. Var
  386. S : String;
  387. begin
  388. S:=TBI+' To perfection';
  389. // Char based.
  390. AssertEquals('Char, Nonexisting returns -1',-1,S.IndexOf('a'));
  391. AssertEquals('Char, Existing, zero based',0,S.IndexOf('T'));
  392. AssertEquals('Char, Case sensitive',-1,S.IndexOf('I'));
  393. AssertEquals('Char, using start index',10,S.IndexOf('e',5));
  394. AssertEquals('Char, using start index and count, not found',-1,S.IndexOf('e',5,5));
  395. AssertEquals('Char, using start index and count,found',10,S.IndexOf('e',5,6));
  396. // String based.
  397. AssertEquals('String, Nonexisting returns -1',-1,S.IndexOf('a'));
  398. AssertEquals('String, zero based',0,S.IndexOf('T'));
  399. AssertEquals('String, case sensitive',-1,S.IndexOf('I'));
  400. AssertEquals('String, using start index',18,S.IndexOf('To',2));
  401. AssertEquals('String, using start index and count ',-1,S.IndexOf('To be',2,4));
  402. AssertEquals('String, using start index and count (partial overlap)',-1,S.IndexOf('To be',16,4));
  403. end;
  404. Procedure TTestStringHelper.TestIndexOfAny;
  405. Var
  406. S : String;
  407. ES : Array of Char;
  408. begin
  409. S:=TBI;
  410. es:=[];
  411. // Just a set
  412. SetLength(ES,0);
  413. AssertEquals('Empty set',-1,S.IndexOfAny(ES));
  414. AssertEquals('Single char in set, no match',-1,S.IndexOfAny(['a']));
  415. AssertEquals('Single char in set, no match (wrong case)',-1,S.IndexOfAny(['O']));
  416. AssertEquals('2 chars in set, no match',-1,S.IndexOfAny(['a','z']));
  417. AssertEquals('Single char in set, match',4,S.IndexOfAny(['e']));
  418. AssertEquals('2 chars in set, 2nd matches',3,S.IndexOfAny(['a','b']));
  419. // Start index
  420. AssertEquals('StartIndex, Empty set',-1,S.IndexOfAny(ES,2));
  421. AssertEquals('StartIndex, Single char in set, no match',-1,S.IndexOfAny(['a'],2));
  422. AssertEquals('StartIndex, Single char in set, no match (wrong case)',-1,S.IndexOfAny(['O'],1));
  423. AssertEquals('StartIndex, Single char in set, no match (index too big)',-1,S.IndexOfAny(['o'],2));
  424. AssertEquals('StartIndex, 2 chars in set, no match',-1,S.IndexOfAny(['a','z'],4));
  425. AssertEquals('StartIndex, Single char in set, match',4,S.IndexOfAny(['e'],3));
  426. AssertEquals('StartIndex, 2 chars in set, 2nd matches',3,S.IndexOfAny(['a','b'],2));
  427. // Start index, count
  428. AssertEquals('StartIndex, count, Empty set',-1,S.IndexOfAny(ES,2,3));
  429. AssertEquals('StartIndex, count, Single char in set, no match',-1,S.IndexOfAny(['a'],2));
  430. AssertEquals('StartIndex, count, Single char in set, no match (wrong case)',-1,S.IndexOfAny(['O'],1));
  431. AssertEquals('StartIndex, count, Single char in set, no match (index too big)',-1,S.IndexOfAny(['o'],2,4));
  432. AssertEquals('StartIndex, count, Single char in set, no match (index too big, count too small)',-1,S.IndexOfAny(['o'],5,5));
  433. AssertEquals('StartIndex, count, 2 chars in set, no match',-1,S.IndexOfAny(['a','z'],4,3));
  434. AssertEquals('StartIndex, count, Single char in set, match',4,S.IndexOfAny(['e'],3,4));
  435. AssertEquals('StartIndex, count, Single char in set, match in range',10,S.IndexOfAny(['e'],5,6));
  436. AssertEquals('StartIndex, count, 2 chars in set, 2nd matches',3,S.IndexOfAny(['a','b'],2,3));
  437. end;
  438. Procedure TTestStringHelper.TestIndexOfAnyString;
  439. Var
  440. S : String;
  441. ES : Array of String;
  442. begin
  443. S:=TBI;
  444. ES:=[];
  445. // Just a set
  446. SetLength(ES,0);
  447. AssertEquals('Empty set',-1,S.IndexOfAny(ES));
  448. AssertEquals('Single string in set, no match',-1,S.IndexOfAny(['ab']));
  449. AssertEquals('Single string in set, no match (wrong case)',-1,S.IndexOfAny(['TO']));
  450. AssertEquals('2 strings in set, no match',-1,S.IndexOfAny(['ab','yz']));
  451. AssertEquals('Single string in set, match',4,S.IndexOfAny(['e ']));
  452. AssertEquals('2 strings in set, 2nd matches',3,S.IndexOfAny(['ee','be']));
  453. // Start index
  454. AssertEquals('StartIndex, Empty set',-1,S.IndexOfAny(ES,2));
  455. AssertEquals('StartIndex, Single string in set, no match',-1,S.IndexOfAny(['aa'],2));
  456. AssertEquals('StartIndex, Single string in set, no match (wrong case)',-1,S.IndexOfAny(['TO'],1));
  457. AssertEquals('StartIndex, Single string in set, no match (index too big)',-1,S.IndexOfAny(['To'],2));
  458. AssertEquals('StartIndex, 2 strings in set, no match',-1,S.IndexOfAny(['aa','zz'],4));
  459. AssertEquals('StartIndex, Single string in set, match',4,S.IndexOfAny(['e '],3));
  460. AssertEquals('StartIndex, 2 strings in set, 2nd matches',3,S.IndexOfAny(['aa','be'],2));
  461. // Start index, count
  462. AssertEquals('StartIndex, count, Empty set',-1,S.IndexOfAny(ES,2,3));
  463. AssertEquals('StartIndex, count, Single string in set, no match',-1,S.IndexOfAny(['aa'],2));
  464. AssertEquals('StartIndex, count, Single string in set, no match (wrong case)',-1,S.IndexOfAny(['tO'],1));
  465. AssertEquals('StartIndex, count, Single string in set, no match (index too big)',-1,S.IndexOfAny(['To'],2,4));
  466. AssertEquals('StartIndex, count, Single string in set, no match (index too big, count too small)',-1,S.IndexOfAny(['To'],5,5));
  467. AssertEquals('StartIndex, count, 2 strings in set, no match',-1,S.IndexOfAny(['aa','zz'],4,3));
  468. AssertEquals('StartIndex, count, Single string in set, match',4,S.IndexOfAny(['e '],3,4));
  469. AssertEquals('StartIndex, count, Single string in set, match in range',10,S.IndexOfAny(['em'],5,7));
  470. AssertEquals('StartIndex, count, 2 strings in set, 2nd matches',3,S.IndexOfAny(['aa','be'],2,3));
  471. end;
  472. Procedure TTestStringHelper.TestIndexOfUnquoted;
  473. Var
  474. S : String;
  475. begin
  476. // Tests created from special cases in Embarcadero docs.
  477. S:='"Thias" ias iat';
  478. AssertEquals('Simple case, match',8,S.IndexOfUnquoted('ia','"','"'));
  479. S:='"This is" it';
  480. AssertEquals('Simple case, match',10,S.IndexOfUnquoted(' ','"','"'));
  481. S:='"Thias ias iat';
  482. AssertEquals('Opening but not closed',-1,S.IndexOfAnyUnquoted(['i','a'],'"','"'));
  483. S:='"Thias" "ias" "iat"';
  484. AssertEquals('Only spaces unquoted',-1,S.IndexOfAnyUnquoted(['i','a'],'"','"'));
  485. S:='<Thias <ias>> iat';
  486. AssertEquals('Different start/end quotes',14,S.IndexOfAnyUnquoted(['i','a'],'<','>'));
  487. S:='"Thias" ias iat';
  488. AssertEquals('Start index',3,S.IndexOfAnyUnquoted(['i','a'],'"','"',1));
  489. S:='Thias" "ias" "iat';
  490. AssertEquals('Start index',-1,S.IndexOfAnyUnquoted(['i','a'],'"','"',6));
  491. end;
  492. Procedure TTestStringHelper.TestIndexOfAnyUnquoted;
  493. Var
  494. S : String;
  495. begin
  496. // Tests created from special cases in Embarcadero docs.
  497. S:='"This" is it';
  498. AssertEquals('Simple case, match',7,S.IndexOfAnyUnquoted(['i'],'"','"'));
  499. AssertEquals('Simple case 2, match',7,S.IndexOfAnyUnquoted(['a','i'],'"','"'));
  500. S:='"This is it';
  501. AssertEquals('Opening but not closed',-1,S.IndexOfAnyUnquoted(['i'],'"','"'));
  502. S:='"This" "is" "it"';
  503. AssertEquals('Only spaces unquoted',-1,S.IndexOfAnyUnquoted(['i'],'"','"'));
  504. S:='<This <is>> it';
  505. AssertEquals('Different start/end quotes',12,S.IndexOfAnyUnquoted(['i'],'<','>'));
  506. S:='"This" is it';
  507. // The documentation is WRONG on this one. Delphi prints 3, not 2 as in the docs.
  508. AssertEquals('Start index',3,S.IndexOfAnyUnquoted(['i'],'"','"',1));
  509. S:='This" "is" "it';
  510. AssertEquals('Start index',-1,S.IndexOfAnyUnquoted(['i'],'"','"',5));
  511. end;
  512. Procedure TTestStringHelper.TestInsert;
  513. Var
  514. S : String;
  515. begin
  516. S:=TBI;
  517. AssertEquals('0 based (1) (result)','All To be implemented',S.Insert(0,'All '));
  518. AssertEquals('0 based (1) (self)','All To be implemented',S);
  519. S:=TBI;
  520. AssertEquals('0 based (2)','To be completely implemented',S.Insert(6,'completely '));
  521. S:=TBI;
  522. AssertEquals('Negative index','completely '+TBI,S.Insert(-3,'completely '));
  523. S:=TBI;
  524. AssertEquals('Too big index',TBI+'completely ',S.Insert(Length(S)+1,'completely '));
  525. end;
  526. Procedure TTestStringHelper.TestIsDelimiter;
  527. Var
  528. S : String;
  529. begin
  530. S:=TBI;
  531. AssertTrue('Simple case, true',S.IsDelimiter('be',3));
  532. AssertFalse('Simple case, false',S.IsDelimiter('ba',4));
  533. end;
  534. Procedure TTestStringHelper.TestIsEmpty;
  535. Var
  536. S : String;
  537. begin
  538. S:='';
  539. AssertTrue('Simple case, true',S.IsEmpty);
  540. S:='abc';
  541. AssertFalse('Simple case, false',S.IsEmpty);
  542. end;
  543. Procedure TTestStringHelper.TestLastDelimiter;
  544. Var
  545. S : String;
  546. begin
  547. S:=TBI;
  548. AssertEquals('Simple case, match, zero based ',0,S.LastDelimiter('T'));
  549. AssertEquals('Simple case, no match ',-1,S.LastDelimiter('a'));
  550. AssertEquals('Simple case',3,S.LastDelimiter('b'));
  551. AssertEquals('Simple, check last match ',Length(TBI)-2,S.LastDelimiter('e'));
  552. AssertEquals('Multi, no match ',-1,S.LastDelimiter('qy'));
  553. AssertEquals('Multi, last match 1',Length(TBI)-1,S.LastDelimiter('ed'));
  554. AssertEquals('Multi, last match 2',Length(TBI)-2,S.LastDelimiter('eb'));
  555. end;
  556. Procedure TTestStringHelper.TestLastIndexOf;
  557. Var
  558. S : String;
  559. begin
  560. S:=TBI;
  561. AssertEquals('Simple case, no match',-1,S.LastIndexOf('a'));
  562. AssertEquals('Simple case, zero based',0,S.LastIndexOf('T'));
  563. AssertEquals('Simple case last',Length(TBI)-2,S.LastIndexOf('e'));
  564. AssertEquals('Simple case, startindex too low',-1,S.LastIndexOf('e',3));
  565. AssertEquals('Simple case, startindex OK ',4,S.LastIndexOf('e',7));
  566. AssertEquals('Simple case, startindex OK, count too small ',-1,S.LastIndexOf('e',7,3));
  567. AssertEquals('Simple case, startindex OK, count border',4,S.LastIndexOf('e',7,4));
  568. end;
  569. Procedure TTestStringHelper.TestLastIndexOfString;
  570. Var
  571. S : String;
  572. begin
  573. S:=TBI;
  574. AssertEquals('Simple case, no match',-1,S.LastIndexOf('aa'));
  575. AssertEquals('Simple case, zero based',0,S.LastIndexOf('To'));
  576. AssertEquals('Simple case last',Length(TBI)-2,S.LastIndexOf('ed'));
  577. AssertEquals('Simple case, startindex too low',-1,S.LastIndexOf('ed',3));
  578. AssertEquals('Simple case, startindex OK ',3,S.LastIndexOf('be',7));
  579. AssertEquals('Simple case, startindex OK, count too small ',-1,S.LastIndexOf('be',7,3));
  580. AssertEquals('Simple case, startindex OK, count border',3,S.LastIndexOf('be',7,4));
  581. end;
  582. Procedure TTestStringHelper.TestLastIndexOfAny;
  583. Var
  584. S : String;
  585. begin
  586. S:=TBI;
  587. AssertEquals('Simple case, no match',-1,S.LastIndexOfAny(['x']));
  588. AssertEquals('Double case, no match',-1,S.LastIndexOfAny(['a','x']));
  589. AssertEquals('Simple case, zero based',0,S.LastIndexOfAny(['T']));
  590. AssertEquals('Double case, zero based',0,S.LastIndexOfAny(['T','q']));
  591. AssertEquals('Simple case last',Length(TBI)-2,S.LastIndexOf('e'));
  592. AssertEquals('Simple case, startindex too low',-1,S.LastIndexOf('e',3));
  593. AssertEquals('Simple case, startindex OK ',4,S.LastIndexOf('e',7));
  594. AssertEquals('Simple case, startindex OK, count too small ',-1,S.LastIndexOf('e',7,3));
  595. AssertEquals('Simple case, startindex OK, count border',4,S.LastIndexOf('e',7,4));
  596. end;
  597. Procedure TTestStringHelper.TestPadLeft;
  598. Var
  599. S : String;
  600. begin
  601. S:='TBI';
  602. AssertEquals('Default char',' TBI',S.PadLeft(5));
  603. AssertEquals('Length reached','TBI',S.PadLeft(3));
  604. AssertEquals('Length over','TBI',S.PadLeft(2));
  605. AssertEquals('Alternate char','**TBI',S.PadLeft(5,'*'));
  606. end;
  607. Procedure TTestStringHelper.TestPadRight;
  608. Var
  609. S : String;
  610. begin
  611. S:='TBI';
  612. AssertEquals('Default char','TBI ',S.PadRight(5));
  613. AssertEquals('Original remains untouched','TBI',S);
  614. AssertEquals('Length reached','TBI',S.PadRight(3));
  615. AssertEquals('Original remains untouched','TBI',S);
  616. AssertEquals('Length over','TBI',S.PadRight(2));
  617. AssertEquals('Original remains untouched','TBI',S);
  618. AssertEquals('Alternate char','TBI**',S.PadRight(5,'*'));
  619. AssertEquals('Original remains untouched','TBI',S);
  620. end;
  621. Procedure TTestStringHelper.TestQuotedString;
  622. Const
  623. TII = '''This'' is it';
  624. TII2 = '"This" is it';
  625. Var
  626. S : String;
  627. begin
  628. S:=TBI;
  629. AssertEquals('Default case',''''+TBI+'''',S.QuotedString);
  630. AssertEquals('Original remains untouched',TBI,S);
  631. S:=TII;
  632. AssertEquals('Quotes present, doubled','''''''This'''' is it''',S.QuotedString);
  633. AssertEquals('Original remains untouched',TII,S);
  634. // Other quote char
  635. S:=TBI;
  636. AssertEquals('Quote ", Default case','"'+TBI+'"',S.QuotedString('"'));
  637. AssertEquals('Quote ", Original remains untouched',TBI,S);
  638. S:=TII2;
  639. AssertEquals('Quote ", Quotes present, doubled','"""This"" is it"',S.QuotedString('"'));
  640. AssertEquals('Quote ", Original remains untouched',TII2,S);
  641. end;
  642. Procedure TTestStringHelper.TestRemove;
  643. Var
  644. S : String;
  645. begin
  646. S:=TBI;
  647. AssertEquals('Delete all','',S.Remove(0));
  648. AssertEquals('Delete all, original unchanged',TBI,S);
  649. AssertEquals('Delete from index','To',S.Remove(2));
  650. AssertEquals('Delete all, original unchanged',TBI,S);
  651. AssertEquals('Delete from negative index',TBI,S.Remove(-1));
  652. AssertEquals('Delete from negative, original unchanged',TBI,S);
  653. // Count
  654. AssertEquals('Delete N from start','be implemented',S.Remove(0,3));
  655. AssertEquals('Delete all, original unchanged',TBI,S);
  656. AssertEquals('Delete from start index, count','To implemented',S.Remove(2,3));
  657. AssertEquals('Delete from start index, count, original unchanged',TBI,S);
  658. AssertEquals('Delete from negative index, count',TBI,S.Remove(-1,4));
  659. AssertEquals('Delete from negative index, count, original unchanged',TBI,S);
  660. end;
  661. Procedure TTestStringHelper.TestReplace;
  662. {
  663. Function Replace(OldChar: Char; NewChar: Char): string; overload;
  664. Function Replace(OldChar: Char; NewChar: Char; ReplaceFlags: TReplaceFlags): string; overload;
  665. Function Replace(const OldValue: string; const NewValue: string): string; overload;
  666. Function Replace(const OldValue: string; const NewValue: string; ReplaceFlags: TReplaceFlags): string; overload;
  667. }
  668. Var
  669. S : String;
  670. begin
  671. S:=TBI;
  672. // Char
  673. AssertEquals('Simple char','to be implemented',S.Replace('T','t'));
  674. AssertEquals('Simple char, original unchanged',TBI,S);
  675. AssertEquals('Simple char is case sensitive',TBI,S.Replace('t','t'));
  676. AssertEquals('Simple char is replace all','To ba implamantad',S.Replace('e','a'));
  677. AssertEquals('Simple char, case insensitive','to be implemented',S.Replace('t','t',[rfIgnoreCase]));
  678. AssertEquals('Simple char, only first','To ba implemented',S.Replace('e','a',[]));
  679. AssertEquals('Simple char, replace all','To ba implamantad',S.Replace('e','a',[rfReplaceAll]));
  680. // String
  681. AssertEquals('Simple string','ta be implemented',S.Replace('To','ta'));
  682. AssertEquals('Simple string, case sensitive',TBI,S.Replace('to','ta'));
  683. S:='AB AB';
  684. AssertEquals('Simple string is replace all','cd cd',S.Replace('AB','cd'));
  685. S:=TBI;
  686. AssertEquals('Simple string, case insensitive','to be implemented',S.Replace('to','to',[rfIgnoreCase]));
  687. S:='AB AB AB';
  688. AssertEquals('Simple sting, only first','cd AB AB',S.Replace('AB','cd',[]));
  689. S:='AB AB AB';
  690. AssertEquals('Simple string, replace all','cd cd cd',S.Replace('AB','cd',[rfReplaceAll]));
  691. end;
  692. Function TTestStringHelper.TestArray(Msg : string; Aexpected : Array of string; AActual : TStringArray) : boolean;
  693. Var
  694. I : integer;
  695. begin
  696. Result:=False;
  697. AssertEquals(Msg+': Length correct',Length(AExpected),Length(AActual));
  698. for I:=0 to Length(AExpected)-1 do
  699. AssertEquals(Msg+': Element '+IntToStr(i)+' correct',AExpected[i],AActual[i]);
  700. Result:=True;
  701. end;
  702. Procedure TTestStringHelper.TestSplit;
  703. Const
  704. TII = '"This is" it' ;
  705. TII2 = '<This is> it' ;
  706. TII3 = '<This is> it' ;
  707. CA: array[0..7] of string = ('F0;F1;F2', ';F1;F2', ';;F2', 'F0;;F2', ';;', 'F0;F1;', 'F0;;', ';F1;');
  708. Var
  709. S : String;
  710. C: TStringArray;
  711. begin
  712. S:='a b';
  713. C:=S.Split([' ']);
  714. TestArray('One letter',['a','b'],C);
  715. S:=TBI;
  716. C:=S.Split([' ']);
  717. TestArray('Simple case',['To','be','implemented'],C);
  718. C:=S.Split([' '],2);
  719. TestArray('Simple case, count',['To','be'],C);
  720. S:=TII;
  721. C:=S.Split([' ','"']);
  722. TestArray('Quote and space',['','This','is','','it'],C);
  723. C:=S.Split([' ','"'],TStringSplitOptions.ExcludeEmpty);
  724. TestArray('Quote and space, exclude empty',['This','is','it'],C);
  725. C:=S.Split([' '],2);
  726. TestArray('Quote and space, count 2',['"This','is"'],C);
  727. C:=S.Split([' ','"'],2,TStringSplitOptions.ExcludeEmpty);
  728. TestArray('Quote and space, exclude empty,count 2',['This','is'],C);
  729. C:=S.Split([' ','"'],1,TStringSplitOptions.ExcludeEmpty);
  730. TestArray('Quote and space, exclude empty, count 1',['This'],C);
  731. C:=S.Split([' '],'"','"');
  732. TestArray('Quoted, space only',['"This is"','it'],C);
  733. C:=S.Split([' '],'"','"',1);
  734. TestArray('Quoted, space only; count',['"This is"'],C);
  735. S:=TII2;
  736. C:=S.Split([' '],'<','>');
  737. TestArray('Quoted <>, space only',['<This is>','it'],C);
  738. S:=TII3;
  739. C:=S.Split([' '],'<','>');
  740. TestArray('Quoted <>, space only, have space',['<This is>','','it'],C);
  741. S:=TII3;
  742. C:=S.Split([' '],'<','>',TStringSplitOptions.ExcludeEmpty);
  743. TestArray('Quoted <>, space only, have space, exclude empty',['<This is>','it'],C);
  744. for S in CA do
  745. begin
  746. C := S.Split([';']);
  747. AssertEquals('Error : expect 3 elements when splitting string '+S,3,Length(C));
  748. end;
  749. end;
  750. Procedure TTestStringHelper.TestSplitString;
  751. Const
  752. TII = '"This is" it' ;
  753. TII2 = '<This is> it' ;
  754. TII3 = '<This is> it' ;
  755. Var
  756. S : String;
  757. C: TStringArray;
  758. begin
  759. S:=StringReplace(TBI,' ',' ',[rfReplaceAll]);
  760. { C:=S.Split([' ']);
  761. TestArray('Simple case',['To','be','implemented'],C);
  762. C:=S.Split([' '],2);
  763. TestArray('Simple case, count',['To','be'],C);
  764. S:=TII;
  765. C:=S.Split([' ','"']);
  766. TestArray('Quote and space',['','This','is','','it'],C);
  767. C:=S.Split([' ','"'],ExcludeEmpty);
  768. TestArray('Quote and space, exclude empty',['This','is','it'],C);
  769. C:=S.Split([' '],2);
  770. TestArray('Quote and space, count 2',['"This','is"'],C);
  771. C:=S.Split([' ','"'],2,ExcludeEmpty);
  772. TestArray('Quote and space, exclude empty,count 2',['This','is'],C);
  773. C:=S.Split([' ','"'],1,ExcludeEmpty);
  774. TestArray('Quote and space, exclude empty, count 1',['This'],C);
  775. }
  776. S:=TII;
  777. C:=S.Split([' '],'"','"');
  778. TestArray('Quoted, space only',['"This is"','it'],C);
  779. C:=S.Split([' '],'"','"',1);
  780. TestArray('Quoted, space only; count',['"This is"'],C);
  781. S:=TII2;
  782. C:=S.Split([' '],'<','>');
  783. TestArray('Quoted <>, space only',['<This is>','it'],C);
  784. S:=TII3;
  785. C:=S.Split([' '],'<','>');
  786. TestArray('Quoted <>, space only, have space',['<This is>','','it'],C);
  787. S:=TII3;
  788. C:=S.Split([' '],'<','>',TStringSplitOptions.ExcludeEmpty);
  789. TestArray('Quoted <>, space only, have space, exclude empty',['<This is>','it'],C);
  790. end;
  791. Procedure TTestStringHelper.TestStartsWith;
  792. Var
  793. S : String;
  794. begin
  795. S:=TBI;
  796. AssertTrue('Match empty',S.StartsWith(''));
  797. AssertTrue('Match',S.StartsWith('To'));
  798. AssertFalse('Match, case sensitive',S.StartsWith('to'));
  799. AssertFalse('No Match',S.StartsWith('ab'));
  800. AssertFalse('No Match, complete',S.StartsWith('To n'));
  801. AssertFalse('Match, only start',S.StartsWith('be'));
  802. AssertTrue('Match, case insensitive',S.StartsWith('To'));
  803. end;
  804. Procedure TTestStringHelper.TestSubstring;
  805. Var
  806. S : String;
  807. begin
  808. S:=TBI;
  809. // No length
  810. AssertEquals('0 based','ed',S.SubString(Length(S)-2));
  811. AssertEquals('0 based, original untouched',TBI,S);
  812. AssertEquals('Index too big','',S.SubString(Length(S)+2));
  813. AssertEquals('Index negative',TBI,S.SubString(-1));
  814. // Length
  815. AssertEquals('0 based','To',S.SubString(0,2));
  816. AssertEquals('0 based, original untouched',TBI,S);
  817. AssertEquals('Index too big','',S.SubString(Length(S)+2,3));
  818. AssertEquals('Index negative','To',S.SubString(-1,2));
  819. AssertEquals('Sub, index','be',S.SubString(3,2));
  820. end;
  821. Procedure TTestStringHelper.TestToCharArray;
  822. Var
  823. S : String;
  824. C : TCharArray;
  825. I : integer;
  826. begin
  827. S:=TBI;
  828. C:=S.ToCharArray;
  829. AssertEquals('No args, length',Length(S),Length(C));
  830. For I:=1 to Length(S) do
  831. AssertEquals('No args, character (1-based) : '+IntToStr(i),S[i],C[i-1]);
  832. C:=S.ToCharArray(3,Length(S)-3);
  833. AssertEquals('No args, length',Length(S)-3,Length(C));
  834. For I:=4 to Length(S) do
  835. AssertEquals('Args(3,len), character (1-based) : '+IntToStr(i),S[i],C[i-4]);
  836. end;
  837. Procedure TTestStringHelper.TestToLower;
  838. Var
  839. S : String;
  840. begin
  841. S:=TBI;
  842. AssertEquals('Lowercase','to be implemented',S.ToLower);
  843. AssertEquals('Lowercase, original unmodified',TBI,S);
  844. end;
  845. Procedure TTestStringHelper.TestToLowerInvariant;
  846. Var
  847. S : String;
  848. begin
  849. S:=TBI;
  850. AssertEquals('Lowercase','to be implemented',S.ToLowerInvariant);
  851. AssertEquals('Lowercase, original unmodified',TBI,S);
  852. // This probably needs testing of some special cases.
  853. end;
  854. Procedure TTestStringHelper.TestToUpper;
  855. Var
  856. S : String;
  857. begin
  858. S:=TBI;
  859. AssertEquals('Lowercase','TO BE IMPLEMENTED',S.ToUpper);
  860. AssertEquals('Lowercase, original unmodified',TBI,S);
  861. end;
  862. Procedure TTestStringHelper.TestToUpperInvariant;
  863. Var
  864. S : String;
  865. begin
  866. S:=TBI;
  867. AssertEquals('Lowercase','TO BE IMPLEMENTED',S.ToUpperInvariant);
  868. AssertEquals('Lowercase, original unmodified',TBI,S);
  869. // This probably needs testing of some special cases.
  870. end;
  871. Procedure TTestStringHelper.TestTrim;
  872. Var
  873. T,S : String;
  874. C : Char;
  875. begin
  876. S:=TBI;
  877. For C:=#0 to #32 do
  878. S:=C+S+C;
  879. T:=S;
  880. AssertEquals('By default all chars below #32 stripped',TBI,S.Trim);
  881. AssertEquals('Original unmodified',T,S);
  882. S:='lmn'+TBI+'lmn';
  883. T:=S;
  884. AssertEquals('Strip all indicated chars',TBI,S.Trim(['l','m','n']));
  885. AssertEquals('Strip all indicated chars, Original unmodified',T,S);
  886. end;
  887. Procedure TTestStringHelper.TestTrimLeft;
  888. Var
  889. O,T,S : String;
  890. C : Char;
  891. begin
  892. S:=TBI;
  893. T:=TBI;
  894. For C:=#0 to #32 do
  895. begin
  896. S:=C+S+C;
  897. T:=T+C;
  898. end;
  899. O:=S;
  900. AssertEquals('By default all chars below #32 stripped',T,S.TrimLeft);
  901. AssertEquals('Original unmodified',O,S);
  902. S:='lmn'+TBI+'lmn';
  903. T:=TBI+'lmn';
  904. O:=S;
  905. AssertEquals('Strip all indicated chars',T,S.TrimLeft(['l','m','n']));
  906. AssertEquals('Strip all indicated chars, Original unmodified',O,S);
  907. end;
  908. Procedure TTestStringHelper.TestTrimRight;
  909. Var
  910. O,T,S : String;
  911. C : Char;
  912. begin
  913. S:=TBI;
  914. T:=TBI;
  915. For C:=#0 to #32 do
  916. begin
  917. S:=C+S+C;
  918. T:=C+T;
  919. end;
  920. O:=S;
  921. AssertEquals('By default all chars below #32 stripped',T,S.TrimRight);
  922. AssertEquals('Original unmodified',O,S);
  923. S:='lmn'+TBI+'lmn';
  924. T:='lmn'+TBI;
  925. O:=S;
  926. AssertEquals('Strip all indicated chars',T,S.TrimRight(['l','m','n']));
  927. AssertEquals('Strip all indicated chars, Original unmodified',O,S);
  928. end;
  929. Procedure TTestStringHelper.TestTrimEnd;
  930. Var
  931. O,T,S : String;
  932. begin
  933. S:='lmn'+TBI+'lmn';
  934. T:='lmn'+TBI;
  935. O:=S;
  936. AssertEquals('Strip all indicated chars',T,S.TrimRight(['l','m','n']));
  937. AssertEquals('Strip all indicated chars, Original unmodified',O,S);
  938. end;
  939. Procedure TTestStringHelper.TestTrimStart;
  940. Var
  941. O,T,S : String;
  942. begin
  943. S:='lmn'+TBI+'lmn';
  944. T:=TBI+'lmn';
  945. O:=S;
  946. AssertEquals('Strip all indicated chars',T,S.TrimLeft(['l','m','n']));
  947. AssertEquals('Strip all indicated chars, Original unmodified',O,S);
  948. end;
  949. Procedure TTestStringHelper.TestChars;
  950. Var
  951. S : String;
  952. I : Integer;
  953. begin
  954. S:=TBI;
  955. For I:=1 to Length(S) do
  956. AssertEquals('Character (1-based)'+IntToStr(i),S[i],S.Chars[i-1]);
  957. end;
  958. Procedure TTestStringHelper.TestLength;
  959. Var
  960. S : String;
  961. begin
  962. S:=TBI;
  963. AssertEquals('Correct length',Length(TBI),S.Length);
  964. end;
  965. (* // Template code;
  966. Procedure TTestStringHelper.Test;
  967. begin
  968. Result:='To be implemented';
  969. end;
  970. *)
  971. Procedure RegisterStringHelperTests;
  972. begin
  973. RegisterTest(TTestStringHelper);
  974. end;
  975. initialization
  976. RegisterStringHelperTests;
  977. end.