codetools: added TestCompareIdentifiers

This commit is contained in:
mattias 2024-07-27 22:08:58 +02:00
parent e1482c3582
commit fdef402c5a

View File

@ -30,7 +30,7 @@ uses
// LazUtils
LazStringUtils, LazConfigStorage,
// CodeTools
FileProcs, BasicCodeTools, DefineTemplates;
FileProcs, BasicCodeTools, DefineTemplates, KeywordFuncLists;
type
{ TTestBasicCodeTools }
@ -50,6 +50,7 @@ type
procedure TestSimpleFormat;
procedure TestStringToPascalConst;
procedure TestReadNextPascalAtom;
procedure TestCompareIdentifiers;
// FileProcs
procedure TestDateToCfgStr;
procedure TestFilenameIsMatching;
@ -352,6 +353,62 @@ begin
t(Apos3+Apos2+#10+'First'+Apos3+Apos2+';',Apos3+Apos2+#10+'First'+Apos3+Apos2,1); // '''#10First''';
end;
procedure TTestBasicCodeTools.TestCompareIdentifiers;
function GetStr(A: PChar): string;
begin
if A=nil then
Result:='nil'
else if A^=#0 then
Result:='#0'
else
Result:='"'+A+'"';
end;
procedure Test(A, B: PChar; Expected: integer);
var
Actual: Integer;
//AmpA: string;
begin
Actual:=CompareIdentifiers(A,B);
if Actual<>Expected then
Fail('A='+GetStr(A)+' B='+GetStr(B)+', expected '+dbgs(Expected)+', but got '+dbgs(Actual));
//if (A<>nil) and (IsIdentStartChar[A^]) then begin
// AmpA:='&'+A;
// Test(PChar(AmpA),B,Expected);
//end;
end;
procedure t(A, B: PChar; Expected: integer);
begin
Test(A,B,Expected);
if A<>B then
Test(B,A,-Expected)
end;
begin
// Note: CompareIdentifiers expects identifiers or empty.
// Feeding non identifiers like numbers 1 or octal &1 is not defined.
t(nil,nil,0);
t(nil,#0,1);
t(#0,#0,0);
t(#0,#1,0);
t(#1,#2,0);
t('a',nil,-1);
t('a',#0,-1);
t('a','a',0);
t('aa','aa',0);
t('aa','a',-1);
t('ab','a',-1);
t('ab','a;',-1);
t('ab','aa',-1);
t('ab','aaa',-1);
t('ab;','ab',0);
t('ab;','ab,',0);
t('aAa;','aaA',0);
end;
procedure TTestBasicCodeTools.TestDateToCfgStr;
procedure t(const Date: TDateTime; const aFormat, Expected: string);