| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleStringUtils" -*- |
| 2 |
{ |
| 3 |
Copyright 2004-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
unit TestCastleStringUtils; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, CastleTester; |
| 23 |
|
| 24 |
type |
| 25 |
TTestCastleStringUtils = class(TCastleTestCase) |
| 26 |
published |
| 27 |
procedure TestIntToStrBase; |
| 28 |
procedure TestDeFormat; |
| 29 |
procedure TestSReplacePercent; |
| 30 |
procedure TestIntToStr2; |
| 31 |
procedure TestCompressWhiteSpace; |
| 32 |
procedure TestFormatNameCounter; |
| 33 |
procedure TestIntToStr64; |
| 34 |
procedure TestCastleStringList; |
| 35 |
procedure TestCastleStringListNewlinesInside; |
| 36 |
procedure TestSReplacePatterns; |
| 37 |
procedure TestSReplacePatternsMenu; |
| 38 |
procedure TestSplitString; |
| 39 |
procedure TestTrimEndingNewline; |
| 40 |
procedure TestAddMultiLine; |
| 41 |
procedure TestRegexpMatches; |
| 42 |
procedure TestIsWild; |
| 43 |
procedure TestSAppendPart; |
| 44 |
procedure TestStringListStrict; |
| 45 |
end; |
| 46 |
|
| 47 |
implementation |
| 48 |
|
| 49 |
uses StrUtils, |
| 50 |
CastleUtils, CastleStringUtils, |
| 51 |
CastleTestUtils; |
| 52 |
|
| 53 |
procedure TTestCastleStringUtils.TestIntToStrBase; |
| 54 |
var i: Integer; |
| 55 |
l: Integer; |
| 56 |
s1, s2: string; |
| 57 |
begin |
| 58 |
for i := 1 to 100 do |
| 59 |
begin |
| 60 |
{ Format('%x', [l]) from FPC fails to work correctly for negative numbers. |
| 61 |
That's an advantage of my IntToStr16. } |
| 62 |
l := Random(High(Integer)){ - High(Integer) div 2}; |
| 63 |
s1 := IntToStr16(l); |
| 64 |
s2 := Format('%x', [l]); |
| 65 |
AssertTrue(s1 = s2); |
| 66 |
end; |
| 67 |
AssertTrue(IntToStr16(-17) = '-11'); |
| 68 |
end; |
| 69 |
|
| 70 |
procedure TTestCastleStringUtils.TestDeFormat; |
| 71 |
var |
| 72 |
s, S2: string; |
| 73 |
i: integer; |
| 74 |
f: float; |
| 75 |
begin |
| 76 |
DeFormat('123FOO98.2e1 '#9'123ioioio-x /'+nl, '%dfoo%f %s /', |
| 77 |
[@i, @f, @s], true); |
| 78 |
AssertTrue(i = 123); |
| 79 |
AssertTrue(f = 98.2e1); |
| 80 |
AssertTrue(s = '123ioioio-x'); |
| 81 |
|
| 82 |
{ %% test } |
| 83 |
DeFormat('%d%%456foobar %', '%%d%%%%%d%s %%', |
| 84 |
[@i, @s], true); |
| 85 |
AssertTrue(i = 456); |
| 86 |
AssertTrue(s = 'foobar'); |
| 87 |
|
| 88 |
{ Test RelatedWhitespaceChecking } |
| 89 |
try |
| 90 |
DeFormat('123 foo', '%d %s %s', [@i, @S, @S2], true, true); |
| 91 |
raise Exception.Create('"DeFormat(123 foo)" with relaxed should fail'); |
| 92 |
except |
| 93 |
on EDeformatError do ; |
| 94 |
end; |
| 95 |
DeFormat('123 foo', '%d %s %s', [@i, @S, @S2], true, false); |
| 96 |
AssertTrue(I = 123); |
| 97 |
AssertTrue(S = ''); |
| 98 |
AssertTrue(S2 = 'foo'); |
| 99 |
|
| 100 |
{ Test %s at the end of data can be '' } |
| 101 |
DeFormat('123 ', '%d %s', [@i, @s], true, true); |
| 102 |
AssertTrue(I = 123); |
| 103 |
AssertTrue(S = ''); |
| 104 |
|
| 105 |
{ Similar as above, but last 2 args different. |
| 106 |
Result should be the same. } |
| 107 |
DeFormat('123 ', '%d %s', [@i, @s], false, false); |
| 108 |
AssertTrue(I = 123); |
| 109 |
AssertTrue(S = ''); |
| 110 |
end; |
| 111 |
|
| 112 |
procedure TTestCastleStringUtils.TestSReplacePercent; |
| 113 |
const |
| 114 |
Replaces: array[0..1]of TPercentReplace = |
| 115 |
((c:'k'; s:'kot'), (c:'p'; s:'pies')); |
| 116 |
begin |
| 117 |
{$warnings off} // testing deprecated routine |
| 118 |
AssertTrue( SPercentReplace('bla%kkk%jk%pies', Replaces, false, '%', false) |
| 119 |
= 'blakotkk%jkpiesies'); |
| 120 |
try |
| 121 |
SPercentReplace('bla%kkk%jk%pies', Replaces, true, '%', false); |
| 122 |
raise Exception.Create('Last SPercentReplace SHOULD raise exception'); |
| 123 |
except on E: EUnknownPercentFormat do AssertTrue(e.Message = 'Unknown format pattern in format "bla%kkk%jk%pies", wrong sequence is : "%j"'); end; |
| 124 |
|
| 125 |
AssertEquals('blakotkkkotkpiesies', SPercentReplace('bla%kkk%Kk%pies', Replaces, true, '%', true)); |
| 126 |
try |
| 127 |
SPercentReplace('bla%kkk%Kk%pies', Replaces, true, '%', false); |
| 128 |
raise Exception.Create('Last SPercentReplace SHOULD raise exception'); |
| 129 |
except on E: EUnknownPercentFormat do AssertTrue(e.Message = 'Unknown format pattern in format "bla%kkk%Kk%pies", wrong sequence is : "%K"'); end; |
| 130 |
|
| 131 |
AssertTrue( SPercentReplace('bla%k%%', Replaces, false, '%', false) = 'blakot%'); |
| 132 |
AssertTrue( SPercentReplace('bla%k%%', Replaces, true, '%', false) = 'blakot%'); |
| 133 |
|
| 134 |
AssertTrue( SPercentReplace('foo%', Replaces, false, '%', false) = 'foo%'); |
| 135 |
try |
| 136 |
AssertTrue( SPercentReplace('foo%', Replaces, true, '%', false) = 'foo%'); |
| 137 |
raise Exception.Create('Last SPercentReplace SHOULD raise exception'); |
| 138 |
except on E: EUnknownPercentFormat do AssertTrue(e.Message = 'Unknown format pattern in format "foo%", wrong sequence is : % at the end of the format string'); end; |
| 139 |
{$warnings on} |
| 140 |
end; |
| 141 |
|
| 142 |
procedure TTestCastleStringUtils.TestIntToStr2; |
| 143 |
var i, Value, MinLength: Integer; |
| 144 |
begin |
| 145 |
AssertTrue(IntToStr2(2) = '10'); |
| 146 |
AssertTrue(IntToStr2(0) = '0'); |
| 147 |
AssertTrue(IntToStr2(2, 4) = '0010'); |
| 148 |
AssertTrue(IntToStr2(-2, 4) = '-0010'); |
| 149 |
AssertTrue(IntToStr2(0, 4) = '0000'); |
| 150 |
|
| 151 |
AssertTrue(IntToStr2(2, 4, '_', 'M', '+') = '__M_'); |
| 152 |
AssertTrue(IntToStr2(-2, 4, '_', 'M', '+') = '+__M_'); |
| 153 |
AssertTrue(IntToStr2(0, 4, '_', 'M', '+') = '____'); |
| 154 |
|
| 155 |
for i := 1 to 100 do |
| 156 |
begin |
| 157 |
Value := Integer(Random(10000)) - 10000 div 2; |
| 158 |
MinLength := Random(5); |
| 159 |
AssertTrue(IntToStrBase(Value, 2, MinLength) = IntToStr2(Value, MinLength)); |
| 160 |
end; |
| 161 |
end; |
| 162 |
|
| 163 |
procedure TTestCastleStringUtils.TestCompressWhiteSpace; |
| 164 |
begin |
| 165 |
AssertTrue(SCompressWhiteSpace('') = ''); |
| 166 |
AssertTrue(SCompressWhiteSpace('a') = 'a'); |
| 167 |
AssertTrue(SCompressWhiteSpace(' ') = ' '); |
| 168 |
AssertTrue(SCompressWhiteSpace(' ') = ' '); |
| 169 |
AssertTrue(SCompressWhiteSpace(' blah blah ') = ' blah blah '); |
| 170 |
AssertTrue(SCompressWhiteSpace(' blah ' + CharTab + 'blah ' + NL) = ' blah blah '); |
| 171 |
end; |
| 172 |
|
| 173 |
procedure TTestCastleStringUtils.TestFormatNameCounter; |
| 174 |
var |
| 175 |
ReplacementsDone: Cardinal; |
| 176 |
AllowOldPercentSyntax: boolean; |
| 177 |
begin |
| 178 |
AssertTrue(FormatNameCounter('', 0, true, ReplacementsDone) = ''); |
| 179 |
AssertTrue(FormatNameCounter('a', 0, true, ReplacementsDone) = 'a'); |
| 180 |
AssertTrue(FormatNameCounter('a%', 0, true, ReplacementsDone) = 'a%'); |
| 181 |
AssertTrue(FormatNameCounter('%a%', 66, true, ReplacementsDone) = '%a%'); |
| 182 |
AssertTrue(FormatNameCounter('%d%', 66, true, ReplacementsDone) = '66%'); |
| 183 |
AssertTrue(FormatNameCounter('%%%', 66, true, ReplacementsDone) = '%%'); |
| 184 |
AssertTrue(FormatNameCounter('%%number%d%d.again%d', 66, true, ReplacementsDone) = '%number6666.again66'); |
| 185 |
AssertTrue(FormatNameCounter('%%number%0d%2d.again%4d', 66, true, ReplacementsDone) = '%number6666.again0066'); |
| 186 |
|
| 187 |
AssertTrue(FormatNameCounter('', 0, false, ReplacementsDone) = ''); |
| 188 |
AssertTrue(FormatNameCounter('a', 0, false, ReplacementsDone) = 'a'); |
| 189 |
AssertTrue(FormatNameCounter('a%', 0, false, ReplacementsDone) = 'a%'); |
| 190 |
AssertTrue(FormatNameCounter('%a%', 66, false, ReplacementsDone) = '%a%'); |
| 191 |
AssertTrue(FormatNameCounter('%d%', 66, false, ReplacementsDone) = '%d%'); |
| 192 |
AssertTrue(FormatNameCounter('%%%', 66, false, ReplacementsDone) = '%%%'); |
| 193 |
AssertTrue(FormatNameCounter('%%number%d%d.again%d', 66, false, ReplacementsDone) = '%%number%d%d.again%d'); |
| 194 |
AssertTrue(FormatNameCounter('%%number%0d%2d.again%4d', 66, false, ReplacementsDone) = '%%number%0d%2d.again%4d'); |
| 195 |
|
| 196 |
{ assertions below should work for both AllowOldPercentSyntax values } |
| 197 |
for AllowOldPercentSyntax := false to true do |
| 198 |
begin |
| 199 |
AssertEquals('', FormatNameCounter('', 0, AllowOldPercentSyntax, ReplacementsDone)); |
| 200 |
AssertEquals('a', FormatNameCounter('a', 0, AllowOldPercentSyntax, ReplacementsDone)); |
| 201 |
AssertEquals('%again66', FormatNameCounter('%again@counter(1)', 66, AllowOldPercentSyntax, ReplacementsDone)); |
| 202 |
AssertEquals('%%again66', FormatNameCounter('%%again@counter(1)', 66, AllowOldPercentSyntax, ReplacementsDone)); |
| 203 |
AssertEquals('%%again0066', FormatNameCounter('%%again@counter(4)', 66, AllowOldPercentSyntax, ReplacementsDone)); |
| 204 |
end; |
| 205 |
end; |
| 206 |
|
| 207 |
procedure TTestCastleStringUtils.TestIntToStr64; |
| 208 |
const |
| 209 |
A1: QWord = $ABCDEF123; |
| 210 |
A2: Int64 = $ABCDEF123; |
| 211 |
A3: Int64 = -$ABCDEF123; |
| 212 |
A4: QWord = $0123456789ABCDEF; |
| 213 |
var |
| 214 |
A5, A6: QWord; |
| 215 |
begin |
| 216 |
AssertTrue(IntToStr16(A1) = 'ABCDEF123'); |
| 217 |
AssertTrue(IntToStr16(A2) = 'ABCDEF123'); |
| 218 |
AssertTrue(IntToStr16(A3) = '-ABCDEF123'); |
| 219 |
|
| 220 |
AssertTrue(IntToStr16(A4) = '123456789ABCDEF'); |
| 221 |
|
| 222 |
A5 := QWord($EFCDAB8967452301); |
| 223 |
AssertTrue(IntToStr16(A5) = 'EFCDAB8967452301'); |
| 224 |
|
| 225 |
A6 := QWord($FFEE000000000000); |
| 226 |
AssertTrue(IntToStr16(A6) = 'FFEE000000000000'); |
| 227 |
end; |
| 228 |
|
| 229 |
procedure TTestCastleStringUtils.TestCastleStringList; |
| 230 |
|
| 231 |
{ Useful to debug state in the middle: |
| 232 |
procedure WritelnList(const S: TCastleStringList); |
| 233 |
var |
| 234 |
I: Integer; |
| 235 |
begin |
| 236 |
Writeln(S.Count); |
| 237 |
for I := 0 to S.Count - 1 do |
| 238 |
Writeln(Format('%4d: %s', [I, S[I]])); |
| 239 |
end; |
| 240 |
} |
| 241 |
|
| 242 |
var sarr, sarr2: TCastleStringList; |
| 243 |
i, j: integer; |
| 244 |
const twoStrings: array[0..1]of string = ('raz','dwa'); |
| 245 |
begin |
| 246 |
for i := 1 to 100 do |
| 247 |
begin |
| 248 |
sarr := TCastleStringList.Create; |
| 249 |
try |
| 250 |
sarr.Count := 4; |
| 251 |
AssertTrue(sarr.Count = 4); |
| 252 |
sarr[0] := 'FOO'; |
| 253 |
sarr[1] := 'foo bar xyz'; |
| 254 |
sarr.Delete(0); |
| 255 |
sarr.AddRange(twoStrings); |
| 256 |
sarr.Add('trzy?'); |
| 257 |
|
| 258 |
AssertTrue(not sarr.Equals(['foo bar xyz', '', ''])); |
| 259 |
AssertTrue(sarr.Equals(['foo bar xyz', '', '', 'raz', 'dwa', 'trzy?'])); |
| 260 |
|
| 261 |
sarr.Reverse; |
| 262 |
AssertTrue(sarr.Equals(['trzy?', 'dwa', 'raz', '', '', 'foo bar xyz'])); |
| 263 |
|
| 264 |
sarr2 := TCastleStringList.Create; |
| 265 |
try |
| 266 |
sarr2.Add('blah'); |
| 267 |
AssertTrue(sarr2.Equals(['blah'])); |
| 268 |
sarr2.Assign(sarr); |
| 269 |
AssertTrue(sarr2.Equals(['trzy?', 'dwa', 'raz', '', '', 'foo bar xyz'])); |
| 270 |
|
| 271 |
{sortuj ustalone 6 stringow} |
| 272 |
sarr.Sort; |
| 273 |
AssertTrue(sarr.Equals(['', '', 'dwa', 'foo bar xyz', 'raz', 'trzy?'])); |
| 274 |
|
| 275 |
{ sprawdz ze kolejnosc na sarr2 pozostala niezmieniona } |
| 276 |
AssertTrue(sarr2.Equals(['trzy?', 'dwa', 'raz', '', '', 'foo bar xyz'])); |
| 277 |
finally sarr2.Free end; |
| 278 |
|
| 279 |
{dodaj losowe stringi, sortuj, sprawdz} |
| 280 |
for j := 0 to 20 do |
| 281 |
sarr.Add( Chr(Random(256)) + Chr(Random(256)) + Chr(Random(256)) ); |
| 282 |
sarr.Sort; |
| 283 |
for j := 0 to sarr.Count-2 do AssertTrue( |
| 284 |
{ sarr[j] <= sarr[j+1] } |
| 285 |
{ Sort may use AnsiCompareStr that takes into account locale } |
| 286 |
AnsiCompareStr(sarr[j], sarr[j+1]) <= 0); |
| 287 |
|
| 288 |
finally sarr.Free end; |
| 289 |
end; |
| 290 |
|
| 291 |
sarr := TCastleStringList.Create; |
| 292 |
try |
| 293 |
{ na tablicy o 0 liczbie elementow tez wszystko powinno isc ok } |
| 294 |
AssertTrue(sarr.Count = 0); |
| 295 |
sarr.Reverse; |
| 296 |
AssertTrue(sarr.Count = 0); |
| 297 |
finally sarr.Free end; |
| 298 |
end; |
| 299 |
|
| 300 |
procedure TTestCastleStringUtils.TestCastleStringListNewlinesInside; |
| 301 |
var |
| 302 |
SL: TCastleStringList; |
| 303 |
begin |
| 304 |
SL := TCastleStringList.Create; |
| 305 |
try |
| 306 |
SL.Add(''); |
| 307 |
SL.Add(NL + 'foo' + NL + 'bar' + NL); |
| 308 |
SL.Add(''); |
| 309 |
SL.Add(''); |
| 310 |
AssertTrue(SL.Count = 4); |
| 311 |
AssertTrue(SL.IndexOf('') = 0); |
| 312 |
|
| 313 |
SL.Delete(0); |
| 314 |
AssertTrue(SL.Count = 3); |
| 315 |
AssertTrue(SL[0] = NL + 'foo' + NL + 'bar' + NL); |
| 316 |
AssertTrue(SL[1] = ''); |
| 317 |
AssertTrue(SL[2] = ''); |
| 318 |
AssertTrue(SL.IndexOf('') = 1); |
| 319 |
|
| 320 |
SL.Delete(1); |
| 321 |
SL.Delete(1); |
| 322 |
AssertTrue(SL.Count = 1); |
| 323 |
AssertTrue(SL[0] = NL + 'foo' + NL + 'bar' + NL); |
| 324 |
AssertTrue(SL.IndexOf(NL + 'foo' + NL + 'bar' + NL) = 0); |
| 325 |
finally FreeAndNil(SL) end; |
| 326 |
end; |
| 327 |
|
| 328 |
procedure TTestCastleStringUtils.TestSReplacePatterns; |
| 329 |
var |
| 330 |
S1, S2: TCastleStringList; |
| 331 |
SMap: TStringStringMap; |
| 332 |
begin |
| 333 |
AssertEquals('bladogbla dog dog', SReplacePatterns('blacatbla dog cat', ['cat'], ['dog'], false)); |
| 334 |
{ test case matching works } |
| 335 |
AssertEquals('bladogbla dog cAt', SReplacePatterns('blacatbla dog cAt', ['cat'], ['dog'], false)); |
| 336 |
AssertEquals('blacatbla dog dog', SReplacePatterns('blacatbla dog cAt', ['cAt'], ['dog'], false)); |
| 337 |
|
| 338 |
AssertEquals('bladogbla dog dog', SReplacePatterns('blacatbla dog cat', ['cat'], ['dog'], true)); |
| 339 |
{ test case ignoring works } |
| 340 |
AssertEquals('bladogbla dog dog', SReplacePatterns('blacatbla dog cAt', ['cat'], ['dog'], true)); |
| 341 |
AssertEquals('bladogbla dog dog', SReplacePatterns('blacatbla dog cAt', ['cAt'], ['dog'], true)); |
| 342 |
AssertEquals('blaDogbla dog Dog', SReplacePatterns('blacatbla dog cAt', ['cat'], ['Dog'], true)); |
| 343 |
AssertEquals('blaDogbla dog Dog', SReplacePatterns('blacatbla dog cAt', ['cAt'], ['Dog'], true)); |
| 344 |
|
| 345 |
{ test pattern inside pattern } |
| 346 |
AssertEquals('12 is 1', SReplacePatterns('foobar is foo', ['foo', 'bar', 'foobar'], ['1', '2', '3'], false)); |
| 347 |
AssertEquals('foobar is foo', SReplacePatterns('foobar is foo', ['foo', 'bar', 'foobar'], ['foo', 'bar', '3'], false)); |
| 348 |
AssertEquals('3 is 1', SReplacePatterns('foobar is foo', ['foobar', 'foo', 'bar'], ['3', '1', '2'], false)); |
| 349 |
AssertEquals('3 is foo', SReplacePatterns('foobar is foo', ['foobar', 'foo', 'bar'], ['3', 'foo', 'bar'], false)); |
| 350 |
AssertEquals('3 is f1', SReplacePatterns('foobar is foo', ['foobar', 'oo'], ['3', '1'], false)); |
| 351 |
AssertEquals('3oo is f1oo', SReplacePatterns('foobar is foo', ['foobar', 'oo'], ['3oo', '1oo'], false)); |
| 352 |
|
| 353 |
{ test overloaded version on TCastleStringList } |
| 354 |
S1 := TCastleStringList.Create; |
| 355 |
S2 := TCastleStringList.Create; |
| 356 |
try |
| 357 |
S1.Append('cat'); |
| 358 |
S2.Append('dog'); |
| 359 |
AssertEquals('bladogbla dog dog', SReplacePatterns('blacatbla dog cat', S1, S2, false)); |
| 360 |
finally |
| 361 |
FreeAndNil(S1); |
| 362 |
FreeAndNil(S2); |
| 363 |
end; |
| 364 |
|
| 365 |
{ test overloaded version on TStringStringMap } |
| 366 |
SMap := TStringStringMap.Create; |
| 367 |
try |
| 368 |
SMap['cat'] := 'dog'; |
| 369 |
AssertEquals('bladogbla dog dog', SReplacePatterns('blacatbla dog cat', SMap, false)); |
| 370 |
finally FreeAndNil(SMap) end; |
| 371 |
end; |
| 372 |
|
| 373 |
procedure TTestCastleStringUtils.TestSReplacePatternsMenu; |
| 374 |
begin |
| 375 |
{ Test replacements done by TCastleWindow.MenuUpdateCaption } |
| 376 |
AssertEquals('&Color', SReplacePatterns('_Color', ['__', '_', '&'], ['_', '&', '&&'], false)); |
| 377 |
AssertEquals('Gr&ay', SReplacePatterns('Gr_ay', ['__', '_', '&'], ['_', '&', '&&'], false)); |
| 378 |
AssertEquals('Somet_hing && ampersand', SReplacePatterns('Somet__hing & ampersand', ['__', '_', '&'], ['_', '&', '&&'], false)); |
| 379 |
AssertEquals('&foo with underscore : _', SReplacePatterns('_foo with underscore : __', ['__', '_', '&'], ['_', '&', '&&'], false)); |
| 380 |
end; |
| 381 |
|
| 382 |
procedure TTestCastleStringUtils.TestSplitString; |
| 383 |
|
| 384 |
procedure AssertStringListEquals(const A: array of string; const List: TCastleStringList); |
| 385 |
var |
| 386 |
I: Integer; |
| 387 |
begin |
| 388 |
AssertEquals(High(A) + 1, List.Count); |
| 389 |
for I := 0 to List.Count - 1 do |
| 390 |
AssertEquals(A[I], List[I]); |
| 391 |
end; |
| 392 |
|
| 393 |
procedure TestSplitAndGlue(const CorrectParts: array of string; |
| 394 |
const S: string; const Delimiter: char); |
| 395 |
var |
| 396 |
List: TCastleStringList; |
| 397 |
begin |
| 398 |
List := SplitString(S, Delimiter); |
| 399 |
try |
| 400 |
AssertStringListEquals(CorrectParts, List); // check SplitString correctness |
| 401 |
AssertEquals(S, GlueStrings(List, Delimiter)); // check GlueStrings is reverse |
| 402 |
finally FreeAndNil(List) end; |
| 403 |
end; |
| 404 |
|
| 405 |
begin |
| 406 |
TestSplitAndGlue(['foo', 'bar'], 'foo|bar', '|'); |
| 407 |
TestSplitAndGlue(['foo'], 'foo', '|'); |
| 408 |
TestSplitAndGlue([''], '', '|'); |
| 409 |
TestSplitAndGlue(['foo', '', 'bar'], 'foo||bar', '|'); |
| 410 |
TestSplitAndGlue(['foo', '', '', 'bar'], 'foo|||bar', '|'); |
| 411 |
TestSplitAndGlue(['foo', '', 'bar', ''], 'foo||bar|', '|'); |
| 412 |
end; |
| 413 |
|
| 414 |
procedure TTestCastleStringUtils.TestTrimEndingNewline; |
| 415 |
begin |
| 416 |
AssertEquals('aa ', TrimEndingNewline('aa ')); |
| 417 |
AssertEquals('aa ', TrimEndingNewline('aa '#10)); |
| 418 |
AssertEquals('aa ', TrimEndingNewline('aa '#13#10)); |
| 419 |
AssertEquals(#13'a'#10'a'#10, TrimEndingNewline(#13'a'#10'a'#10#10)); |
| 420 |
AssertEquals(#13'a'#10'a'#10, TrimEndingNewline(#13'a'#10'a'#10#13#10)); |
| 421 |
end; |
| 422 |
|
| 423 |
procedure TTestCastleStringUtils.TestAddMultiLine; |
| 424 |
var |
| 425 |
SList: TStringList; |
| 426 |
begin |
| 427 |
SList := TStringList.Create; |
| 428 |
try |
| 429 |
AssertEquals(0, SList.Count); |
| 430 |
SList.Add(''); |
| 431 |
AssertEquals(1, SList.Count); |
| 432 |
SList.Add(''); |
| 433 |
AssertEquals(2, SList.Count); |
| 434 |
SList.Add(''); |
| 435 |
AssertEquals(3, SList.Count); |
| 436 |
SList.Add('blahblah'#13'another line'#13#10'yet another line'); |
| 437 |
AssertEquals(4, SList.Count); |
| 438 |
SList.AddMultiLine('blahblah'#13'another line'#13#10'yet another line'); |
| 439 |
AssertEquals(7, SList.Count); |
| 440 |
SList.AddMultiLine('simple'); |
| 441 |
AssertEquals(8, SList.Count); |
| 442 |
SList.AddMultiLine(''); |
| 443 |
AssertEquals(9, SList.Count); |
| 444 |
|
| 445 |
AssertEquals('', SList[0]); |
| 446 |
AssertEquals('', SList[1]); |
| 447 |
AssertEquals('', SList[2]); |
| 448 |
AssertEquals('blahblah'#13'another line'#13#10'yet another line', SList[3]); |
| 449 |
AssertEquals('blahblah', SList[4]); |
| 450 |
AssertEquals('another line', SList[5]); |
| 451 |
AssertEquals('yet another line', SList[6]); |
| 452 |
AssertEquals('simple', SList[7]); |
| 453 |
AssertEquals('', SList[8]); |
| 454 |
finally FreeAndNil(SList) end; |
| 455 |
end; |
| 456 |
|
| 457 |
procedure TTestCastleStringUtils.TestRegexpMatches; |
| 458 |
begin |
| 459 |
AssertTrue(StringMatchesRegexp('blah', 'blah')); |
| 460 |
AssertTrue(StringMatchesRegexp('notblah', 'blah')); |
| 461 |
AssertFalse(StringMatchesRegexp('blah', 'notblah')); |
| 462 |
|
| 463 |
// test range [0-9] |
| 464 |
AssertTrue(StringMatchesRegexp('blah12', 'blah[0-9][0-9]')); |
| 465 |
AssertTrue(StringMatchesRegexp('blah123', 'blah[0-9][0-9]')); |
| 466 |
AssertFalse(StringMatchesRegexp('blah123', '^blah[0-9][0-9]$')); |
| 467 |
AssertFalse(StringMatchesRegexp('blah1', 'blah[0-9][0-9]')); |
| 468 |
AssertFalse(StringMatchesRegexp('blah[0-9][0-9]', 'blah[0-9][0-9]')); |
| 469 |
|
| 470 |
// test range [\d] |
| 471 |
AssertTrue(StringMatchesRegexp('blah12', 'blah[\d][\d]')); |
| 472 |
AssertTrue(StringMatchesRegexp('blah123', 'blah[\d][\d]')); |
| 473 |
AssertFalse(StringMatchesRegexp('blah123', '^blah[\d][\d]$')); |
| 474 |
AssertFalse(StringMatchesRegexp('blah1', 'blah[\d][\d]')); |
| 475 |
AssertFalse(StringMatchesRegexp('blah[\d][\d]', 'blah[\d][\d]')); |
| 476 |
|
| 477 |
// test + |
| 478 |
{ First test unfortunately fails with FPC 3.2.0, fixed only in 3.2.2. |
| 479 |
|
| 480 |
It's a rather important bug (the regexp tested is very simple), |
| 481 |
but luckily CGE doesn't really use regexp much now (and probably never will), |
| 482 |
so it's not a big issue in CGE case. } |
| 483 |
{$ifndef VER3_2_0} |
| 484 |
AssertTrue(StringMatchesRegexp('blah111foo', 'blah1+foo')); |
| 485 |
{$endif} |
| 486 |
AssertTrue(StringMatchesRegexp('blah1foo', 'blah1+foo')); |
| 487 |
AssertFalse(StringMatchesRegexp('blahfoo', 'blah1+foo')); |
| 488 |
|
| 489 |
// test * |
| 490 |
{$ifndef VER3_2_0} // fails with FPC 3.2.0, fixed only in 3.2.2 |
| 491 |
AssertTrue(StringMatchesRegexp('blah111foo', 'blah1*foo')); |
| 492 |
{$endif} |
| 493 |
AssertTrue(StringMatchesRegexp('blah1foo', 'blah1*foo')); |
| 494 |
AssertTrue(StringMatchesRegexp('blahfoo', 'blah1*foo')); |
| 495 |
end; |
| 496 |
|
| 497 |
procedure TTestCastleStringUtils.TestIsWild; |
| 498 |
begin |
| 499 |
AssertFalse(IsWild('TTestCompiler.TestIs', '*testeventloop*', true)); |
| 500 |
AssertFalse(IsWild('TTestCompiler.TestSinglePrecision', '*testeventloop*', true)); |
| 501 |
AssertFalse(IsWild('TTestCompiler.TestCTypesSizes', '*testeventloop*', true)); |
| 502 |
AssertFalse(IsWild('TTestCompiler.TestSizes', '*testeventloop*', true)); |
| 503 |
AssertFalse(IsWild('TTestCompiler.TestPackedOpenArray', '*testeventloop*', true)); |
| 504 |
AssertFalse(IsWild('TTestSysUtils.TestDirectoryFileExists', '*testeventloop*', true)); |
| 505 |
AssertFalse(IsWild('TTestGenericsCollections.Test1', '*testeventloop*', true)); |
| 506 |
|
| 507 |
AssertTrue(IsWild('TTestEventLoop.Test1', '*testeventloop*', true)); |
| 508 |
AssertTrue(IsWild('TTestCastleWindow.TestEventLoop', '*testeventloop*', true)); |
| 509 |
end; |
| 510 |
|
| 511 |
procedure TTestCastleStringUtils.TestSAppendPart; |
| 512 |
begin |
| 513 |
AssertEquals('', SAppendPart('', '', '')); |
| 514 |
AssertEquals('foo', SAppendPart('', ';', 'foo')); |
| 515 |
AssertEquals('foo;foo', SAppendPart('foo', ';', 'foo')); |
| 516 |
AssertEquals('foo;foo;foo', SAppendPart('foo;foo', ';', 'foo')); |
| 517 |
end; |
| 518 |
|
| 519 |
procedure TTestCastleStringUtils.TestStringListStrict; |
| 520 |
|
| 521 |
{ This code is similar to what CastleInternalDelphiDesign does in |
| 522 |
TCastleDelphiIdeIntegration.AddPaths , |
| 523 |
TCastleDelphiIdeIntegration.RemovePaths . } |
| 524 |
|
| 525 |
const |
| 526 |
InputStr = '$(BDSLIB)\$(Platform)\release;$(BDSUSERDIR)\Imports;$(BDSUSERDIR)\Imports\$(Platform);$(BDS)\Imports;$(BDSCOMMONDIR)\Dcp;$(BDS)\include;c:\path with spaces'; |
| 527 |
NewPath1 = 'C:\cygwin64\home\michalis\sources\castle-engine\castle-engine\src\base'; |
| 528 |
NewPath2 = 'C:\cygwin64\home\michalis\sources\castle-engine\castle-engine\src\common_includes'; |
| 529 |
OutputStr = InputStr + ';' + NewPath1 + ';' + NewPath2; |
| 530 |
var |
| 531 |
List: TStringList; |
| 532 |
I: Integer; |
| 533 |
begin |
| 534 |
List := TStringList.Create; |
| 535 |
try |
| 536 |
List.Delimiter := ';'; |
| 537 |
{ This is critically important to not break "c:\path with spaces" in InputStr. |
| 538 |
See also https://github.com/castle-engine/castle-engine/issues/626 . |
| 539 |
Another solution, aside from StrictDelimiter, would be to use |
| 540 |
CGE SplitString + GlueString. } |
| 541 |
List.StrictDelimiter := true; |
| 542 |
List.DelimitedText := InputStr; |
| 543 |
List.Add(NewPath1); |
| 544 |
List.Add(NewPath2); |
| 545 |
AssertEquals(OutputStr, List.DelimitedText); |
| 546 |
|
| 547 |
I := List.IndexOf(NewPath1); |
| 548 |
AssertFalse(I = -1); |
| 549 |
List.Delete(I); |
| 550 |
|
| 551 |
I := List.IndexOf(NewPath2); |
| 552 |
AssertFalse(I = -1); |
| 553 |
List.Delete(I); |
| 554 |
|
| 555 |
AssertEquals(InputStr, List.DelimitedText); |
| 556 |
finally FreeAndNil(List) end; |
| 557 |
end; |
| 558 |
|
| 559 |
initialization |
| 560 |
RegisterTest(TTestCastleStringUtils); |
| 561 |
end. |