diff --git a/.gitattributes b/.gitattributes index 3c7cf7ec7a..7d2c7faf61 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1031,6 +1031,8 @@ components/codetools/tests/parsertest.lpi svneol=native#text/plain components/codetools/tests/parsertest.lpr svneol=native#text/plain components/codetools/tests/pt_thlp1.pas svneol=native#text/plain components/codetools/tests/pt_thlp2.pas svneol=native#text/plain +components/codetools/tests/refactoringtests.pas svneol=native#text/plain +components/codetools/tests/rt_explodewith.pas svneol=native#text/plain components/codetools/tests/testcodetools.lpi svneol=native#text/plain components/codetools/tests/testcodetools.lpr svneol=native#text/plain components/codetools/unitdictionary.pas svneol=native#text/plain diff --git a/components/codetools/tests/finddeclarationtests.pas b/components/codetools/tests/finddeclarationtests.pas index 7e9d018de5..fc68dbfaf9 100644 --- a/components/codetools/tests/finddeclarationtests.pas +++ b/components/codetools/tests/finddeclarationtests.pas @@ -18,7 +18,7 @@ ./testcodetools --format=plain --suite=TestFindDeclaration_LazTests --filemask=t*.pp ./testcodetools --format=plain --suite=TestFindDeclaration_LazTests --filemask=tdefaultproperty1.pp } -unit finddeclarationtests; +unit FindDeclarationTests; {$mode objfpc}{$H+} @@ -56,21 +56,8 @@ var BugsTestSuite: TTestSuite; FindDeclarationTestSuite: TTestSuite; -procedure AddToBugsTestSuite(ATest: TTest); -procedure AddToFindDeclarationTestSuite(ATestClass: TClass); - implementation -procedure AddToBugsTestSuite(ATest: TTest); -begin - BugsTestSuite.AddTest(ATest); -end; - -procedure AddToFindDeclarationTestSuite(ATestClass: TClass); -begin - FindDeclarationTestSuite.AddTestSuiteFromClass(ATestClass); -end; - { TTestFindDeclaration } procedure TTestFindDeclaration.FindDeclarations(Filename: string); @@ -313,9 +300,9 @@ initialization GetTestRegistry.TestName := 'All tests'; BugsTestSuite := TTestSuite.Create('Bugs'); GetTestRegistry.AddTest(BugsTestSuite); - FindDeclarationTestSuite := TTestSuite.Create('Parser'); + FindDeclarationTestSuite := TTestSuite.Create('FindDeclaration'); GetTestRegistry.AddTest(FindDeclarationTestSuite); - AddToFindDeclarationTestSuite(TTestFindDeclaration); + FindDeclarationTestSuite.AddTestSuiteFromClass(TTestFindDeclaration); end. diff --git a/components/codetools/tests/refactoringtests.pas b/components/codetools/tests/refactoringtests.pas new file mode 100644 index 0000000000..ec699a76a4 --- /dev/null +++ b/components/codetools/tests/refactoringtests.pas @@ -0,0 +1,86 @@ +{ + Test with: + ./testcodetools --format=plain --suite=TTestRefactoring + ./testcodetools --format=plain --suite=TestExplodeWith +} +unit RefactoringTests; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils, CodeToolManager, CodeCache, CodeTree, LazLogger, + LazFileUtils, fpcunit, testregistry, FindDeclarationTests; + +type + + { TTestRefactoring } + + TTestRefactoring = class(TTestCase) + private + published + procedure TestExplodeWith; + end; + +var + RefactoringTestSuite: TTestSuite; + +implementation + +{ TTestRefactoring } + +procedure TTestRefactoring.TestExplodeWith; +var + Code: TCodeBuffer; + Tool: TCodeTool; + Node: TCodeTreeNode; + CodeXYPos: TCodeXYPosition; + ListOfPCodeXYPosition: TFPList; + i: Integer; + Filename, OldSource: String; +begin + Filename:=ExpandFileNameUTF8('rt_explodewith.pas'); + Code:=CodeToolBoss.LoadFile(Filename,true,false); + AssertEquals('Load file error: '+Filename,true,Code<>nil); + if not CodeToolBoss.Explore(Code,Tool,true) then + AssertEquals('Parse error: ','',CodeToolBoss.ErrorMessage); + ListOfPCodeXYPosition:=nil; + try + // collect all With-Blocks + Node:=Tool.Tree.Root; + while Node<>nil do begin + if Node.Desc=ctnWithVariable then begin + Tool.CleanPosToCaret(Node.StartPos,CodeXYPos); + AddCodePosition(ListOfPCodeXYPosition,CodeXYPos); + end; + Node:=Node.Next; + end; + + for i:=0 to ListOfPCodeXYPosition.Count-1 do begin + CodeXYPos:=PCodeXYPosition(ListOfPCodeXYPosition[i])^; + debugln(['TTestRefactoring.TestExplodeWith ',dbgs(CodeXYPos)]); + OldSource:=Code.Source; + try + if CodeToolBoss.RemoveWithBlock(Code,CodeXYPos.X,CodeXYPos.Y) then begin + // check changes + + end else begin + AssertEquals('CodeToolBoss.RemoveWithBlock failed at '+dbgs(CodeXYPos),'',CodeToolBoss.ErrorMessage); + end; + finally + Code.Source:=OldSource; + end; + end; + finally + FreeListOfPCodeXYPosition(ListOfPCodeXYPosition); + end; +end; + +initialization + RefactoringTestSuite := TTestSuite.Create('Refactoring'); + GetTestRegistry.AddTest(RefactoringTestSuite); + + RefactoringTestSuite.AddTestSuiteFromClass(TTestRefactoring); +end. + diff --git a/components/codetools/tests/rt_explodewith.pas b/components/codetools/tests/rt_explodewith.pas new file mode 100644 index 0000000000..0b813c8ff1 --- /dev/null +++ b/components/codetools/tests/rt_explodewith.pas @@ -0,0 +1,33 @@ +unit rt_explodewith; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils; + +type + + { TMyClass } + + TMyClass = class + public + procedure DoSomething; + end; + +implementation + +{ TMyClass } + +procedure TMyClass.DoSomething; +var + R: TRect; +begin + R:=Rect(1,2,3,4); + with R do + {explodewith:R}Left:=4; +end; + +end. + diff --git a/components/codetools/tests/testcodetools.lpi b/components/codetools/tests/testcodetools.lpi index 0b2d2e9a6e..376310512d 100644 --- a/components/codetools/tests/testcodetools.lpi +++ b/components/codetools/tests/testcodetools.lpi @@ -40,7 +40,7 @@ - + @@ -48,6 +48,7 @@ + @@ -77,6 +78,15 @@ + + + + + + + + + diff --git a/components/codetools/tests/testcodetools.lpr b/components/codetools/tests/testcodetools.lpr index d9c55ba32b..1b70047de4 100644 --- a/components/codetools/tests/testcodetools.lpr +++ b/components/codetools/tests/testcodetools.lpr @@ -21,7 +21,8 @@ program testcodetools; uses Classes, sysutils, consoletestrunner, dom, fpcunit, CodeToolManager, - CodeToolsConfig, LazLogger, finddeclarationtests, fdt_classhelper, + CodeToolsConfig, LazLogger, finddeclarationtests, RefactoringTests, + fdt_classhelper, {$IF FPC_FULLVERSION >= 30101} fdt_typehelper, {$ENDIF} @@ -29,7 +30,7 @@ uses {$IFDEF Darwin} fdt_objccategory, fdt_objcclass, {$ENDIF} - fdt_basic, fdt_with; + fdt_basic, fdt_with, rt_explodewith; const ConfigFilename = 'codetools.config';