codetools: added test for find declaration nested class

git-svn-id: trunk@40199 -
This commit is contained in:
mattias 2013-02-06 18:17:55 +00:00
parent 248c598499
commit a70b6fb7e5
5 changed files with 92 additions and 32 deletions

1
.gitattributes vendored
View File

@ -851,6 +851,7 @@ components/codetools/sourcechanger.pas svneol=native#text/pascal
components/codetools/sourcelog.pas svneol=native#text/pascal components/codetools/sourcelog.pas svneol=native#text/pascal
components/codetools/stdcodetools.pas svneol=native#text/pascal components/codetools/stdcodetools.pas svneol=native#text/pascal
components/codetools/tests/fdt_classhelper.pas svneol=native#text/plain components/codetools/tests/fdt_classhelper.pas svneol=native#text/plain
components/codetools/tests/fdt_nestedclasses.pas svneol=native#text/plain
components/codetools/tests/fdtbase.pas svneol=native#text/plain components/codetools/tests/fdtbase.pas svneol=native#text/plain
components/codetools/tests/finddeclarationtest.lpi svneol=native#text/plain components/codetools/tests/finddeclarationtest.lpi svneol=native#text/plain
components/codetools/tests/finddeclarationtest.lpr svneol=native#text/plain components/codetools/tests/finddeclarationtest.lpr svneol=native#text/plain

View File

@ -0,0 +1,44 @@
unit fdt_nestedclasses;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TBaseClass = class
public type
TBaseSubClass = class
procedure DoSomething; virtual;
end;
end;
TCustomClass = class(TBaseClass)
public type
TCustomSubClass = class(TBaseSubClass{declaration:fdt_nestedclasses.TBaseClass.TBaseSubClass})
procedure DoSomething; override;
end;
end;
implementation
{ TCustomClass.TCustomSubClass }
procedure TCustomClass.TCustomSubClass.DoSomething;
begin
//put the cursor here and hit Ctrl+Space
end;
{ TBaseClass.TBaseSubClass }
procedure TBaseClass.TBaseSubClass.DoSomething;
begin
end;
end.

View File

@ -1,8 +1,9 @@
{ {
Test with: Test with:
./finddeclarationtest --format=plain --suite=TTestFindDeclarationClassHelper ./finddeclarationtest --format=plain --suite=TTestFindDeclarationClassHelper
./finddeclarationtest --format=plain --suite=TestFindDeclaration_base ./finddeclarationtest --format=plain --suite=TestFindDeclaration_Base
./finddeclarationtest --format=plain --suite=TestFindDeclaration_classhelper ./finddeclarationtest --format=plain --suite=TestFindDeclaration_NestedClasses
./finddeclarationtest --format=plain --suite=TestFindDeclaration_ClassHelper
} }
unit fdtbase; unit fdtbase;
@ -17,14 +18,15 @@ uses
type type
{ TTestFindDeclarationClassHelper } { TTestFindDeclaration }
TTestFindDeclarationClassHelper = class(TTestCase) TTestFindDeclaration = class(TTestCase)
private private
procedure FindDeclarations(Filename, Marker: string); procedure FindDeclarations(Filename, Marker: string);
published published
procedure TestFindDeclaration_base; procedure TestFindDeclaration_Base;
procedure TestFindDeclaration_classhelper; procedure TestFindDeclaration_NestedClasses;
procedure TestFindDeclaration_ClassHelper;
end; end;
var var
@ -46,9 +48,9 @@ begin
FindDeclarationTestSuite.AddTestSuiteFromClass(ATestClass); FindDeclarationTestSuite.AddTestSuiteFromClass(ATestClass);
end; end;
{ TTestFindDeclarationClassHelper } { TTestFindDeclaration }
procedure TTestFindDeclarationClassHelper.FindDeclarations(Filename, procedure TTestFindDeclaration.FindDeclarations(Filename,
Marker: string); Marker: string);
procedure PrependPath(Prefix: string; var Path: string); procedure PrependPath(Prefix: string; var Path: string);
@ -64,12 +66,12 @@ var
StartPos: Integer; StartPos: Integer;
ExpectedPath: String; ExpectedPath: String;
PathPos: Integer; PathPos: Integer;
CursorPos, TargetCursorPos: TCodeXYPosition; CursorPos, FoundCursorPos: TCodeXYPosition;
TargetTopLine: integer; FoundTopLine: integer;
TargetTool: TFindDeclarationTool; FoundTool: TFindDeclarationTool;
TargetCleanPos: Integer; FoundCleanPos: Integer;
TargetNode: TCodeTreeNode; FoundNode: TCodeTreeNode;
TargetPath: String; FoundPath: String;
begin begin
Filename:=TrimAndExpandFilename(Filename); Filename:=TrimAndExpandFilename(Filename);
Code:=CodeToolBoss.LoadFile(Filename,true,false); Code:=CodeToolBoss.LoadFile(Filename,true,false);
@ -87,41 +89,48 @@ begin
p:=FindCommentEnd(Tool.Src,p,Tool.Scanner.NestedComments); p:=FindCommentEnd(Tool.Src,p,Tool.Scanner.NestedComments);
if Tool.Src[StartPos]<>'{' then continue; if Tool.Src[StartPos]<>'{' then continue;
PathPos:=StartPos+1; PathPos:=StartPos+1;
//debugln(['TTestFindDeclaration.FindDeclarations Comment: ',dbgstr(Tool.Src,StartPos,p-StartPos)]);
if copy(Tool.Src,PathPos,length(Marker))<>Marker then continue; if copy(Tool.Src,PathPos,length(Marker))<>Marker then continue;
PathPos+=length(Marker); PathPos+=length(Marker);
ExpectedPath:=copy(Tool.Src,PathPos,p-1-PathPos); ExpectedPath:=copy(Tool.Src,PathPos,p-1-PathPos);
//debugln(['TTestFindDeclarationClassHelper.FindDeclarations ',ExpectedPath]); //debugln(['TTestFindDeclaration.FindDeclarations ExpectedPath=',ExpectedPath]);
Tool.CleanPosToCaret(StartPos-1,CursorPos); Tool.CleanPosToCaret(StartPos-1,CursorPos);
if not CodeToolBoss.FindDeclaration(CursorPos.Code,CursorPos.X,CursorPos.Y, if not CodeToolBoss.FindDeclaration(CursorPos.Code,CursorPos.X,CursorPos.Y,
TargetCursorPos.Code,TargetCursorPos.X,TargetCursorPos.Y,TargetTopLine) FoundCursorPos.Code,FoundCursorPos.X,FoundCursorPos.Y,FoundTopLine)
then begin then begin
AssertEquals('find declaration failed at '+Tool.CleanPosToStr(StartPos-1)+': '+CodeToolBoss.ErrorMessage,false,true); AssertEquals('find declaration failed at '+Tool.CleanPosToStr(StartPos-1)+': '+CodeToolBoss.ErrorMessage,false,true);
continue; continue;
end else begin end else begin
TargetTool:=CodeToolBoss.GetCodeToolForSource(TargetCursorPos.Code,true,true) as TFindDeclarationTool; FoundTool:=CodeToolBoss.GetCodeToolForSource(FoundCursorPos.Code,true,true) as TFindDeclarationTool;
TargetTool.CaretToCleanPos(TargetCursorPos,TargetCleanPos); FoundTool.CaretToCleanPos(FoundCursorPos,FoundCleanPos);
TargetNode:=TargetTool.FindDeepestNodeAtPos(TargetCleanPos,true); FoundNode:=FoundTool.FindDeepestNodeAtPos(FoundCleanPos,true);
TargetPath:=''; FoundPath:='';
while TargetNode<>nil do begin while FoundNode<>nil do begin
case TargetNode.Desc of case FoundNode.Desc of
ctnTypeDefinition,ctnVarDefinition,ctnConstDefinition: ctnTypeDefinition,ctnVarDefinition,ctnConstDefinition:
PrependPath(GetIdentifier(@TargetTool.Src[TargetNode.StartPos]),TargetPath); PrependPath(GetIdentifier(@FoundTool.Src[FoundNode.StartPos]),FoundPath);
ctnInterface,ctnUnit: ctnInterface,ctnUnit:
PrependPath(TargetTool.GetSourceName(false),TargetPath); PrependPath(FoundTool.GetSourceName(false),FoundPath);
end; end;
TargetNode:=TargetNode.Parent; FoundNode:=FoundNode.Parent;
end; end;
AssertEquals('find declaration wrong at '+Tool.CleanPosToStr(StartPos-1),LowerCase(ExpectedPath),LowerCase(TargetPath)); //debugln(['TTestFindDeclaration.FindDeclarations FoundPath=',FoundPath]);
AssertEquals('find declaration wrong at '+Tool.CleanPosToStr(StartPos-1),LowerCase(ExpectedPath),LowerCase(FoundPath));
end; end;
end; end;
end; end;
procedure TTestFindDeclarationClassHelper.TestFindDeclaration_base; procedure TTestFindDeclaration.TestFindDeclaration_Base;
begin begin
FindDeclarations('fdt_classhelper.pas','declaration:'); FindDeclarations('fdt_classhelper.pas','declaration:');
end; end;
procedure TTestFindDeclarationClassHelper.TestFindDeclaration_classhelper; procedure TTestFindDeclaration.TestFindDeclaration_NestedClasses;
begin
FindDeclarations('fdt_nestedclasses.pas','declaration:');
end;
procedure TTestFindDeclaration.TestFindDeclaration_ClassHelper;
begin begin
FindDeclarations('fdt_classhelper.pas','declaration-classhelper:'); FindDeclarations('fdt_classhelper.pas','declaration-classhelper:');
end; end;
@ -133,6 +142,6 @@ initialization
FindDeclarationTestSuite := TTestSuite.Create('Parser'); FindDeclarationTestSuite := TTestSuite.Create('Parser');
GetTestRegistry.AddTest(FindDeclarationTestSuite); GetTestRegistry.AddTest(FindDeclarationTestSuite);
AddToFindDeclarationTestSuite(TTestFindDeclarationClassHelper); AddToFindDeclarationTestSuite(TTestFindDeclaration);
end. end.

View File

@ -41,7 +41,7 @@
<PackageName Value="fpcunitconsolerunner"/> <PackageName Value="fpcunitconsolerunner"/>
</Item2> </Item2>
</RequiredPackages> </RequiredPackages>
<Units Count="3"> <Units Count="4">
<Unit0> <Unit0>
<Filename Value="finddeclarationtest.lpr"/> <Filename Value="finddeclarationtest.lpr"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
@ -57,6 +57,11 @@
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="fdt_classhelper"/> <UnitName Value="fdt_classhelper"/>
</Unit2> </Unit2>
<Unit3>
<Filename Value="fdt_nestedclasses.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="fdt_nestedclasses"/>
</Unit3>
</Units> </Units>
</ProjectOptions> </ProjectOptions>
<CompilerOptions> <CompilerOptions>

View File

@ -20,8 +20,8 @@ program finddeclarationtest;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
uses uses
Classes, sysutils, consoletestrunner, dom, fpcunit, Classes, sysutils, consoletestrunner, dom, fpcunit, CodeToolManager,
CodeToolManager, CodeToolsConfig, fdtbase, fdt_classhelper; CodeToolsConfig, LazLogger, fdtbase, fdt_classhelper, fdt_nestedclasses;
const const
ConfigFilename = 'codetools.config'; ConfigFilename = 'codetools.config';
@ -64,6 +64,7 @@ begin
// To not parse the FPC sources every time, the options are saved to a file. // To not parse the FPC sources every time, the options are saved to a file.
Options.LoadFromFile(ConfigFilename); Options.LoadFromFile(ConfigFilename);
end; end;
DebugLn(['EnvironmentVariables: PP, FPCDIR, LAZARUSDIR, FPCTARGET, FPCTARGETCPU']);
Options.InitWithEnvironmentVariables; Options.InitWithEnvironmentVariables;
if HasOption('submitter') then if HasOption('submitter') then