8
fpcunit, Classes, SysUtils;
21
Classes, SysUtils, fpcunit, testglobals, FileProcs, CodeToolManager,
22
CodeCache, CustomCodeTool;
13
TCodeBlocksTest = class(TTestCase)
15
function CompareComplete(const InputDefines, ResultFile: String): Boolean;
26
{ TTestCodetoolsCompleteBlock }
28
TTestCodetoolsCompleteBlock = class(TTestCase)
30
function CreateFullSrc(Src: string; out Cursor: integer): string;
31
procedure TestCompleteBlocks;
33
procedure CompleteBlock(Src, ExpectedSrc: string;
34
OnlyIfCursorBlockIndented: boolean = false);
35
procedure CompleteBlock(Src: string; OnlyIfCursorBlockIndented: boolean = false);
36
procedure CompleteBlockFail(Src: string;
37
OnlyIfCursorBlockIndented: boolean = false);
17
procedure TestCompleteBlocks;
39
procedure TestCompleteBlockClassStart;
40
procedure TestCompleteBlockBegin;
41
procedure TestCompleteBlockRepeat;
42
procedure TestCompleteBlockCase;
43
procedure TestCompleteBlockTry;
44
procedure TestCompleteBlockAsm;
45
procedure TestCompleteBlockIf;
24
function TCodeBlocksTest.CompareComplete(const InputFile, InputDefines, ResultFile: String): Boolean;
29
function StripSpaceChars(const s: string): String;
50
{ TTestCodetoolsCompleteBlock }
52
procedure TTestCodetoolsCompleteBlock.TestCompleteBlocks;
54
procedure CompareComplete(a,b,c: string);
31
// removes all [#10,#13,#9, #32] chars, giving a line: "beginwriteln('helloworld');end."
33
for i:=length(Result) downto 1 do
34
if Result[i] in [#10,#13,#9,' '] then
35
System.Delete(Result,i,1);
56
writeln('CompareComplete ',a,',',b,',',c);
38
// ToDo: fix path to completeblock, InputFile nd ResultFile
39
st := GetProcessOutput('completeblock '+InputFile+' '+inputdefines); // reads all output from blockcompleted file
40
// remove debugging output and take only the new source
41
while (st.Count>0) and (st[0]<>'{%MainUnit unit1.pas}') do st.Delete(0);
42
// reads the correct result file
43
rs.LoadFromFile(resultfile);
45
AssertEquals(StripSpaceChars(st.text), StripSpaceChars(rs.text)); // compares resulting strings
48
procedure TCodeBlocksTest.TestCompleteBlocks;
50
60
CompareComplete('ifbeginelse1.inc','6 28 ifbeginelse fpcunit', 'ifbeginelse1_result.inc');
51
61
CompareComplete('whilebegin1.inc','5 10 whilebegin fpcunit', 'whilebegin1_result.inc');
65
75
CompareComplete('tryif1.inc','4 6 tryif fpcunit', 'tryif1_result.inc');
78
function TTestCodetoolsCompleteBlock.CreateFullSrc(Src: string;
79
out Cursor: integer): string;
82
{Result:='unit testcompleteblock;'+LineEnding
83
+'interface'+LineEnding
85
if not (Result[length(Result)] in [#10,#13]) then
86
Result:=Result+LineEnding;
87
Cursor:=System.Pos('|',Result);
88
System.Delete(Result,Cursor,1);
91
procedure TTestCodetoolsCompleteBlock.CompleteBlock(Src, ExpectedSrc: string;
92
OnlyIfCursorBlockIndented: boolean);
102
ExpectedCode: TCodeBuffer;
107
FullExpectedSrc: String;
108
TrimExpected: String;
111
AssertEquals('Src is empty',Trim(Src)<>'',true);
112
AssertEquals('ExpectedSrc is empty',Trim(ExpectedSrc)<>'',true);
114
ExpectedCode:=TCodeBuffer.Create;
116
// replace cursor | marker in Src
117
Code:=CodeToolBoss.CreateFile('TestCompleteBlock.pas');
118
FullSrc:=CreateFullSrc(Src,p);
120
AssertEquals('missing cursor | in test source: "'+dbgstr(Src)+'"',true,false);
121
Code.Source:=FullSrc;
122
Code.AbsoluteToLineCol(p,Y,X);
124
// replace cursor | marker in ExpectedSrc
125
FullExpectedSrc:=CreateFullSrc(ExpectedSrc,ep);
127
AssertEquals('missing cursor | in expected source: "'+dbgstr(ExpectedSrc)+'"',true,false);
128
ExpectedCode.Source:=FullExpectedSrc;
129
ExpectedCode.AbsoluteToLineCol(ep,eY,eX);
131
if not CodeToolBoss.CompleteBlock(Code,X,Y,OnlyIfCursorBlockIndented,
132
NewCode,NewX,NewY,NewTopLine)
134
AssertEquals('CodeToolBoss.CompleteBlock returned false for src="'+dbgstr(Src)+'"',true,false);
137
TrimExpected:=dbgstr(Trim(FullExpectedSrc));
138
TrimResult:=dbgstr(Trim(Code.Source));
139
if TrimExpected<>TrimResult then begin
140
debugln(['TTestCodetoolsCompleteBlock.CompleteBlock FAILED Expected:']);
141
debugln(FullExpectedSrc);
142
debugln(['TTestCodetoolsCompleteBlock.CompleteBlock FAILED Found:']);
143
debugln(Code.Source);
144
debugln(['TTestCodetoolsCompleteBlock.CompleteBlock FAILED end']);
146
AssertEquals('CompleteBlock did no or the wrong completion: ',TrimExpected,TrimResult);
153
procedure TTestCodetoolsCompleteBlock.CompleteBlock(Src: string;
154
OnlyIfCursorBlockIndented: boolean);
156
CompleteBlock(Src,Src,OnlyIfCursorBlockIndented);
159
procedure TTestCodetoolsCompleteBlock.CompleteBlockFail(Src: string;
160
OnlyIfCursorBlockIndented: boolean);
167
NewCode: TCodeBuffer;
172
AssertEquals('Src is empty',Trim(Src)<>'',true);
174
// replace cursor | marker in Src
175
Code:=CodeToolBoss.CreateFile('TestCompleteBlock.pas');
176
FullSrc:=CreateFullSrc(Src,p);
178
AssertEquals('missing cursor | in test source: "'+dbgstr(Src)+'"',true,false);
179
Code.Source:=FullSrc;
180
Code.AbsoluteToLineCol(p,Y,X);
182
if CodeToolBoss.CompleteBlock(Code,X,Y,OnlyIfCursorBlockIndented,
183
NewCode,NewX,NewY,NewTopLine)
185
debugln(['TTestCodetoolsCompleteBlock.CompleteBlockFail completion: ',dbgstr(Code.Source)]);
186
AssertEquals('CodeToolBoss.CompleteBlock returned true for incompletable src="'+dbgstr(Src)+'"',true,false);
190
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockClassStart;
192
CompleteBlock('type'+LineEnding
193
+' TTestClass = class(TObject)|',
195
+' TTestClass = class(TObject)'+LineEnding
197
CompleteBlock('type'+LineEnding
198
+' TTestClass = class(TObject)|'+LineEnding
201
+' TTestClass = class(TObject)'+LineEnding
205
CompleteBlock('type'+LineEnding
206
+' TTestClass = class(TObject)|'+LineEnding
209
+' TTestClass = class(TObject)'+LineEnding
215
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockBegin;
217
CompleteBlock('begin'+LineEnding
218
+' begin|'+LineEnding
221
+' begin|'+LineEnding
224
CompleteBlock('begin'+LineEnding
225
+' while do begin|'+LineEnding
228
+' while do begin|'+LineEnding
231
CompleteBlock('begin'+LineEnding
232
+' while do'+LineEnding
233
+' begin|'+LineEnding
236
+' while do'+LineEnding
237
+' begin|'+LineEnding
240
CompleteBlock('begin'+LineEnding
241
+' begin|'+LineEnding
242
+' writeln;'+LineEnding
245
+' begin|'+LineEnding
246
+' writeln;'+LineEnding
249
CompleteBlock('begin'+LineEnding
250
+' begin|'+LineEnding
251
+' writeln;'+LineEnding
254
+' begin|'+LineEnding
256
+' writeln;'+LineEnding
258
{ Not implemented yet:
259
CompleteBlock('procedure a;'+LineEnding
263
'procedure a;'+LineEnding
270
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockRepeat;
272
CompleteBlock('begin'+LineEnding
273
+' repeat|'+LineEnding
276
+' repeat|'+LineEnding
277
+' until ;'+LineEnding
281
+' if FindFirstUTF8(Dir+FileMask,faAnyFile,FileInfo)=0 then begin'+LineEnding
282
+' repeat'+LineEnding
283
+' // check if special file'+LineEnding
284
+' if (FileInfo.Name=''.'') or (FileInfo.Name=''..'') or (FileInfo.Name='''')'+LineEnding
286
+' continue;'+LineEnding
288
+' if FilenameIsPascalUnit(FileInfo.Name,false) then begin'+LineEnding
289
+' List.Add(Dir+FileInfo.Name);'+LineEnding
290
+' end else if (FileInfo.Attr and faDirectory)>0 then begin'+LineEnding
291
+' CollectUnits(Dir+);'+LineEnding
293
+' until FindNextUTF8(FileInfo)<>0;'+LineEnding
295
+' FindCloseUTF8(FileInfo);'+LineEnding);
298
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockCase;
300
CompleteBlock('begin'+LineEnding
301
+' case of|'+LineEnding
304
+' case of|'+LineEnding
307
CompleteBlock('begin'+LineEnding
308
+' case of|'+LineEnding
311
+' case of|'+LineEnding
316
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockTry;
318
CompleteBlock('begin'+LineEnding
323
+' finally'+LineEnding
326
CompleteBlock('begin'+LineEnding
328
+' finally|'+LineEnding
332
+' finally|'+LineEnding
335
CompleteBlock('begin'+LineEnding
337
+' except|'+LineEnding
341
+' except|'+LineEnding
346
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockAsm;
348
CompleteBlock('begin'+LineEnding
357
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockIf;
359
CompleteBlock('begin'+LineEnding
360
+' if then begin|'+LineEnding
363
+' if then begin|'+LineEnding
366
CompleteBlock('begin'+LineEnding
367
+' if then begin|'+LineEnding
371
+' if then begin|'+LineEnding
375
CompleteBlockFail('begin'+LineEnding
378
+' finally'+LineEnding
384
AddToCodetoolsTestSuite(TTestCodetoolsCompleteBlock);