Codetools: Testcase, add a few options for upcoming tests

This commit is contained in:
Martin 2021-12-04 14:20:45 +01:00
parent de1209eefb
commit 47177f39ca

View File

@ -21,6 +21,31 @@
./testcodetools --suite=TestFindDeclaration_LazTests --filemask=t*.pp ./testcodetools --suite=TestFindDeclaration_LazTests --filemask=t*.pp
./testcodetools --suite=TestFindDeclaration_LazTests --filemask=tdefaultproperty1.pp ./testcodetools --suite=TestFindDeclaration_LazTests --filemask=tdefaultproperty1.pp
} }
(* Expectation in test-files
{SELECT:TESTS=TEST(|TEST)*}
Each "{" comment starting with a..z is a test instruction, or a list of test instructions separated by |
SELECT can be one of the following tests:
{completion:TESTS}
TEST=([+-]POS=)?ENTRY(;ENTRY)*
Tests: CodeToolBoss.GatherIdentifiers
Each TEST can start with an optional POS (integer positive/negative)
The POS specifies the relative source-pos from the start of the identifier before the comment.
Each ENTRY can start with a ! to test for a non-present completion
{declaration:
Tests: CodeToolBoss.FindDeclaration
Also runs {completion:*}
{guesstype:
Tests: CodeToolBoss.GuessTypeOfIdentifier
*)
unit TestFindDeclaration; unit TestFindDeclaration;
{$i runtestscodetools.inc} {$i runtestscodetools.inc}
@ -35,7 +60,7 @@ uses
FileProcs, LazFileUtils, LazLogger, FileProcs, LazFileUtils, LazLogger,
CodeToolManager, ExprEval, CodeCache, BasicCodeTools, CodeToolManager, ExprEval, CodeCache, BasicCodeTools,
CustomCodeTool, CodeTree, FindDeclarationTool, KeywordFuncLists, CustomCodeTool, CodeTree, FindDeclarationTool, KeywordFuncLists,
IdentCompletionTool, DefineTemplates, TestPascalParser; IdentCompletionTool, DefineTemplates, StrUtils, TestPascalParser;
const const
MarkDecl = '#'; // a declaration, must be unique MarkDecl = '#'; // a declaration, must be unique
@ -240,10 +265,11 @@ var
FoundPath: String; FoundPath: String;
Src: String; Src: String;
NameStartPos, i, l, IdentifierStartPos, IdentifierEndPos, NameStartPos, i, l, IdentifierStartPos, IdentifierEndPos,
BlockTopLine, BlockBottomLine: Integer; BlockTopLine, BlockBottomLine, CommentEnd, StartOffs: Integer;
Marker, ExpectedType, NewType: String; Marker, ExpectedType, NewType, ExpexctedCompletion, ExpexctedTerm,
ExpexctedCompletionPart, ExpexctedTermPart: String;
IdentItem: TIdentifierListItem; IdentItem: TIdentifierListItem;
ItsAKeyword, IsSubIdentifier: boolean; ItsAKeyword, IsSubIdentifier, ExpInvert: boolean;
ExistingDefinition: TFindContext; ExistingDefinition: TFindContext;
ListOfPFindContext: TFPList; ListOfPFindContext: TFPList;
NewExprType: TExpressionType; NewExprType: TExpressionType;
@ -260,8 +286,11 @@ begin
if Src[p]<>'{' then continue; if Src[p]<>'{' then continue;
if Src[p+1] in ['$','%',' ',#0..#31] then continue; if Src[p+1] in ['$','%',' ',#0..#31] then continue;
IdentifierStartPos:=p; // allow spaces before the comment
IdentifierEndPos:=p; IdentifierEndPos:=p;
while (IdentifierEndPos>1) and (IsSpaceChar[Src[IdentifierEndPos-1]]) do
dec(IdentifierEndPos);
IdentifierStartPos:=IdentifierEndPos;
while (IdentifierStartPos>1) and (IsIdentChar[Src[IdentifierStartPos-1]]) do while (IdentifierStartPos>1) and (IsIdentChar[Src[IdentifierStartPos-1]]) do
dec(IdentifierStartPos); dec(IdentifierStartPos);
if IdentifierStartPos=p then begin if IdentifierStartPos=p then begin
@ -284,6 +313,15 @@ begin
continue; continue;
end; end;
CommentEnd := CommentP;
CommentP := p-1;
repeat
NameStartPos:=CommentP+1;
p := NameStartPos;
CommentP := PosEx('|', Src, NameStartPos);
if (CommentP < 1) or (CommentP > CommentEnd) then
CommentP := CommentEnd;
// check for specials: // check for specials:
{declaration:path} {declaration:path}
{guesstype:type} {guesstype:type}
@ -299,11 +337,13 @@ begin
PathPos:=p; PathPos:=p;
//debugln(['TTestFindDeclaration.FindDeclarations Marker="',Marker,'" params: ',dbgstr(MainTool.Src,p,CommentP-p)]); //debugln(['TTestFindDeclaration.FindDeclarations Marker="',Marker,'" params: ',dbgstr(MainTool.Src,p,CommentP-p)]);
if (Marker='declaration') then begin if (Marker='declaration') or (Marker='completion') then begin
ExpectedPath:=copy(Src,PathPos,CommentP-1-PathPos); ExpectedPath:=copy(Src,PathPos,CommentP-1-PathPos);
{$IFDEF VerboseFindDeclarationTests} {$IFDEF VerboseFindDeclarationTests}
debugln(['TTestFindDeclaration.FindDeclarations searching "',Marker,'" at ',MainTool.CleanPosToStr(NameStartPos-1),' ExpectedPath=',ExpectedPath]); debugln(['TTestFindDeclaration.FindDeclarations searching "',Marker,'" at ',MainTool.CleanPosToStr(NameStartPos-1),' ExpectedPath=',ExpectedPath]);
{$ENDIF} {$ENDIF}
if (Marker='declaration') then begin
MainTool.CleanPosToCaret(IdentifierStartPos,CursorPos); MainTool.CleanPosToCaret(IdentifierStartPos,CursorPos);
// test FindDeclaration // test FindDeclaration
@ -341,33 +381,61 @@ begin
AssertEquals('find declaration wrong at '+MainTool.CleanPosToStr(IdentifierStartPos,true),LowerCase(ExpectedPath),LowerCase(FoundPath)); AssertEquals('find declaration wrong at '+MainTool.CleanPosToStr(IdentifierStartPos,true),LowerCase(ExpectedPath),LowerCase(FoundPath));
end; end;
end; end;
end;
// test identifier completion // test identifier completion
if (ExpectedPath<>'') then begin if (ExpectedPath<>'') then begin
for ExpexctedCompletionPart in ExpectedPath.Split(';') do begin
ExpexctedCompletion := ExpexctedCompletionPart;
StartOffs := 0;
if (ExpexctedCompletion <> '') and (ExpexctedCompletion[1] in ['+','-']) then begin
i := Pos('=', ExpexctedCompletion);
if i > 1 then begin
StartOffs := StrToIntDef(copy(ExpexctedCompletion, 1, i-1), 0);
Delete(ExpexctedCompletion, 1, i);
end
else
StartOffs := 0;
end;
StartOffs := StartOffs + IdentifierStartPos;
MainTool.CleanPosToCaret(StartOffs,CursorPos);
if not CodeToolBoss.GatherIdentifiers(CursorPos.Code,CursorPos.X,CursorPos.Y) if not CodeToolBoss.GatherIdentifiers(CursorPos.Code,CursorPos.X,CursorPos.Y)
then begin then begin
if ExpectedPath<>'' then begin if ExpexctedCompletion<>'' then begin
WriteSource(IdentifierStartPos,MainTool); WriteSource(StartOffs,MainTool);
AssertEquals('GatherIdentifiers failed at '+MainTool.CleanPosToStr(IdentifierStartPos,true)+': '+CodeToolBoss.ErrorMessage,false,true); AssertEquals('GatherIdentifiers failed at '+MainTool.CleanPosToStr(StartOffs,true)+': '+CodeToolBoss.ErrorMessage,false,true);
end; end;
continue; continue;
end else begin end else begin
for ExpexctedTermPart in ExpexctedCompletion.Split(',') do begin
ExpexctedTerm := ExpexctedTermPart;
ExpInvert := (ExpexctedTerm <> '') and (ExpexctedTerm[1] = '!');
if ExpInvert then
Delete(ExpexctedTerm, 1, 1);
i:=CodeToolBoss.IdentifierList.GetFilteredCount-1; i:=CodeToolBoss.IdentifierList.GetFilteredCount-1;
while i>=0 do begin while i>=0 do begin
IdentItem:=CodeToolBoss.IdentifierList.FilteredItems[i]; IdentItem:=CodeToolBoss.IdentifierList.FilteredItems[i];
//debugln(['TTestFindDeclaration.FindDeclarations ',IdentItem.Identifier]); //debugln(['TTestFindDeclaration.FindDeclarations ',IdentItem.Identifier]);
l:=length(IdentItem.Identifier); l:=length(IdentItem.Identifier);
if ((l=length(ExpectedPath)) or (ExpectedPath[length(ExpectedPath)-l]='.')) if ((l=length(ExpexctedTerm)) or (ExpexctedTerm[length(ExpexctedTerm)-l]='.'))
and (CompareText(IdentItem.Identifier,RightStr(ExpectedPath,l))=0) and (CompareText(IdentItem.Identifier,RightStr(ExpexctedTerm,l))=0)
then break; then break;
dec(i); dec(i);
end; end;
if i<0 then begin if (i<0) and not ExpInvert then begin
WriteSource(IdentifierStartPos,MainTool); WriteSource(StartOffs,MainTool);
AssertEquals('GatherIdentifiers misses "'+ExpectedPath+'" at '+MainTool.CleanPosToStr(IdentifierStartPos,true),true,i>=0); AssertEquals('GatherIdentifiers misses "'+ExpexctedTerm+'" at '+MainTool.CleanPosToStr(StartOffs,true),true,i>=0);
end
else
if ExpInvert and (i>=0) then begin
WriteSource(StartOffs,MainTool);
AssertEquals('GatherIdentifiers should not have "'+ExpexctedTerm+'" at '+MainTool.CleanPosToStr(StartOffs,true),true,i>=0);
end; end;
end; end;
end; end;
end;
end
end else if Marker='guesstype' then begin end else if Marker='guesstype' then begin
ExpectedType:=copy(Src,PathPos,CommentP-1-PathPos); ExpectedType:=copy(Src,PathPos,CommentP-1-PathPos);
{$IFDEF VerboseFindDeclarationTests} {$IFDEF VerboseFindDeclarationTests}
@ -401,6 +469,7 @@ begin
AssertEquals('Unknown marker at '+MainTool.CleanPosToStr(IdentifierStartPos,true),'declaration',Marker); AssertEquals('Unknown marker at '+MainTool.CleanPosToStr(IdentifierStartPos,true),'declaration',Marker);
continue; continue;
end; end;
until CommentP >= CommentEnd;
end; end;
CheckReferenceMarkers; CheckReferenceMarkers;
end; end;