File: testmethodjumptool.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (301 lines) | stat: -rw-r--r-- 8,492 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
 Test with:
     ./runtests --format=plain --suite=TTestMethodJumpTool
     ./runtests --format=plain --suite=TestFindJumpPointIncFilewithIntfAndImpl
}
unit TestMethodJumpTool;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpcunit, testregistry, LazLoggerBase,
  CodeToolManager, StdCodeTools, CodeCache, LinkScanner, TestPascalParser;

type

  { TBaseTestMethodJumpTool }

  TBaseTestMethodJumpTool = class(TCustomTestPascalParser)
  protected
    procedure GetCTMarker(aCode: TCodeBuffer; Comment: string; out Position: TPoint;
      LeftOfComment: boolean = true);
    function GetInfo(aCode: TCodeBuffer; XY: TPoint): string;
    procedure TestJumpToMethod(FromMarker: string; LeftFrom: boolean;
      ToMarker: string; LeftTo: boolean; ToColOffset: integer = 0);
  end;

  { TTestMethodJumpTool }

  TTestMethodJumpTool = class(TBaseTestMethodJumpTool)
  published
    procedure TestFindJumpPointIncFilewithIntfAndImpl;
    procedure TestMethodJump_IntfToImplSingleProc;
    procedure TestMethodJump_IntfToImplSingleProcWrongName;
    procedure TestMethodJump_IntfToImplSingleProcWrongParam;
    procedure TestMethodJump_SingleMethod;
    procedure TestMethodJump_MultiMethodWrongName;
    procedure TestMethodJump_DelphiGenericClass;
    procedure TestMethodJump_DelphiGenericMethod;
  end;

implementation

{ TBaseTestMethodJumpTool }

procedure TBaseTestMethodJumpTool.GetCTMarker(aCode: TCodeBuffer; Comment: string;
  out Position: TPoint; LeftOfComment: boolean);
var
  p: SizeInt;
begin
  Position:=Point(0,0);
  if Comment[1]<>'{' then
    Comment:='{'+Comment+'}';
  p:=System.Pos(Comment,aCode.Source);
  if p<1 then begin
    WriteSource(CodeXYPosition(1,1,Code));
    Fail('TTestMethodJumpTool.GetCTMarker, missing marker "'+Comment+'" in "'+Code.Filename+'"');
  end;
  if not LeftOfComment then
    inc(p,length(Comment));
  aCode.AbsoluteToLineCol(p,Position.Y,Position.X);
  if Position.Y<1 then begin
    WriteSource(CodeXYPosition(Position.X,Position.Y,Code));
    Fail('TTestMethodJumpTool.GetCTMarker, Code.AbsoluteToLineCol: "'+Comment+'" Pos='+dbgs(Position)+' in "'+Code.Filename+'"');
  end;
end;

function TBaseTestMethodJumpTool.GetInfo(aCode: TCodeBuffer; XY: TPoint): string;
var
  Line: String;
begin
  Line:=aCode.GetLine(XY.Y-1);
  Result:=dbgs(XY)+': '+copy(Line,1,XY.X-1)+'|'+copy(Line,XY.X,length(Line));
end;

procedure TBaseTestMethodJumpTool.TestJumpToMethod(FromMarker: string;
  LeftFrom: boolean; ToMarker: string; LeftTo: boolean; ToColOffset: integer);
var
  FromPos, ToPos: TPoint;
  NewCode: TCodeBuffer;
  NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer;
  RevertableJump: boolean;
begin
  GetCTMarker(Code,FromMarker,FromPos,LeftFrom);
  GetCTMarker(Code,ToMarker,ToPos,LeftTo);
  inc(ToPos.X,ToColOffset);
  if not CodeToolBoss.JumpToMethod(Code,FromPos.X,FromPos.Y,NewCode, NewX, NewY,
    NewTopLine, BlockTopLine, BlockBottomLine, RevertableJump) then begin
    WriteSource(CodeXYPosition(FromPos.X,FromPos.Y,Code));
    Fail('CodeToolBoss.JumpToMethod failed, From {'+FromMarker+'} line='+IntToStr(FromPos.Y)+' col='+IntToStr(FromPos.X));
  end;
  if NewCode<>Code then begin
    WriteSource(CodeXYPosition(FromPos.X,FromPos.Y,Code));
    AssertEquals('JumpToMethod jumped to wrong file, From {'+FromMarker+'} line='+IntToStr(FromPos.Y)+' col='+IntToStr(FromPos.X),
      Code.Filename,NewCode.Filename);
  end;
  if (NewY<>ToPos.Y) or (NewX<>ToPos.X) then begin
    WriteSource(CodeXYPosition(ToPos.X,ToPos.Y,Code));
    Fail('JumpToMethod jumped to wrong line,col. From {'+FromMarker+'} '+dbgs(FromPos)
      +', expected {'+ToMarker+'} '+dbgs(ToPos)+', actual '+dbgs(Point(NewX,NewY)));
  end;
end;

{ TTestMethodJumpTool }

procedure TTestMethodJumpTool.TestFindJumpPointIncFilewithIntfAndImpl;

  procedure Test(aTitle: string; Code: TCodeBuffer;
    StartMarker: string; LeftOfStart: boolean;
    EndMarker: string; LeftOfEnd: boolean);
  var
    BlockStart: TPoint;
    BlockEnd: TPoint;
    NewCode: TCodeBuffer;
    NewX: integer;
    NewY: integer;
    NewTopline, BlockTopLine, BlockBottomLine: integer;
    RevertableJump: boolean;
  begin
    GetCTMarker(Code,StartMarker,BlockStart,LeftOfStart);
    GetCTMarker(Code,EndMarker,BlockEnd,LeftOfEnd);
    //debugln(['TTestCTStdCodetools.TestCTStdFindBlockStart BlockStart=',GetInfo(BlockStart),' BlockEnd=',GetInfo(BlockEnd)]);
    if not CodeToolBoss.JumpToMethod(Code,BlockStart.X,BlockStart.Y,
      NewCode,NewX,NewY,NewTopline,BlockTopLine,BlockBottomLine,RevertableJump)
    then
      AssertEquals(aTitle+': '+CodeToolBoss.ErrorMessage,true,false)
    else
      AssertEquals(aTitle,GetInfo(Code,BlockEnd),GetInfo(NewCode,Point(NewX,NewY)))
  end;

var
  IncCode: TCodeBuffer;
begin
  Code.Source:=''
    +'unit Test1;'+LineEnding
    +'interface'+LineEnding
    +'{$DEFINE UseInterface}'
    +'{$I TestMethodJumpTool2.inc}'+LineEnding
    +'{$UNDEF UseInterface}'+LineEnding
    +'implementation'+LineEnding
    +'{$DEFINE UseImplementation}'
    +'{$I TestMethodJumpTool2.inc}'+LineEnding
    +'end.'+LineEnding;
  IncCode:=CodeToolBoss.CreateFile('TestMethodJumpTool2.inc');
  IncCode.Source:=''
    +'{%MainUnit test1.pas}'+LineEnding
    +'{$IFDEF UseInterface}'+LineEnding
    +'procedure {ProcHeader}DoSomething;'+LineEnding
    +'{$ENDIF}'+LineEnding
    +'{$IFDEF UseImplementation}'+LineEnding
    +'procedure DoSomething;'+LineEnding
    +'begin'+LineEnding
    +'  {ProcBody}writeln;'+LineEnding
    +'end;'+LineEnding
    +'{$ENDIF}'+LineEnding;

  Test('Method jump from interface to implementation in one include file',
       IncCode,'ProcHeader',false,'ProcBody',true);
  Test('Method jump from implementation to interface in one include file',
       IncCode,'ProcBody',false,'ProcHeader',false);
end;

procedure TTestMethodJumpTool.TestMethodJump_IntfToImplSingleProc;
begin
  Add([
  'unit Test1;',
  '{$mode objfpc}{$H+}',
  'interface',
  'procedure {a}DoIt;',
  'implementation',
  'procedure DoIt;',
  'begin',
  '  {b}',
  'end;',
  'end.']);
  TestJumpToMethod('a',false,'b',true);
end;

procedure TTestMethodJumpTool.TestMethodJump_IntfToImplSingleProcWrongName;
begin
  Add([
  'unit Test1;',
  '{$mode objfpc}{$H+}',
  'interface',
  'procedure {a}DoIt;',
  'implementation',
  'procedure {b}Do2It;',
  'begin',
  'end;',
  'end.']);
  TestJumpToMethod('a',false,'b',false,2);
end;

procedure TTestMethodJumpTool.TestMethodJump_IntfToImplSingleProcWrongParam;
begin
  Add([
  'unit Test1;',
  '{$mode objfpc}{$H+}',
  'interface',
  'procedure {a}DoIt(s: string);',
  'implementation',
  'procedure DoIt(s: {b}ansistring);',
  'begin',
  'end;',
  'end.']);
  TestJumpToMethod('a',false,'b',false);
end;

procedure TTestMethodJumpTool.TestMethodJump_SingleMethod;
begin
  Add([
  'unit Test1;',
  '{$mode objfpc}{$H+}',
  'interface',
  'type',
  '  TBird = class',
  '    procedure {a}DoIt(s: string);',
  '  end;',
  'implementation',
  'procedure TBird.DoIt(s: string);',
  'begin',
  '  {b}',
  'end;',
  'end.']);
  TestJumpToMethod('a',false,'b',true);
end;

procedure TTestMethodJumpTool.TestMethodJump_MultiMethodWrongName;
begin
  Add([
  'unit Test1;',
  '{$mode objfpc}{$H+}',
  'interface',
  'type',
  '  TBird = class',
  '    procedure {a}DoIt(s: string);',
  '    procedure DoIt;',
  '  end;',
  'implementation',
  'procedure TBird.{b}Do2It(s: string);',
  'begin',
  'end;',
  'procedure TBird.DoIt;',
  'begin',
  'end;',
  'end.']);
  TestJumpToMethod('a',false,'b',false,2);
end;

procedure TTestMethodJumpTool.TestMethodJump_DelphiGenericClass;
begin
  Add([
  'unit Test1;',
  '{$mode delphi}{$H+}',
  'interface',
  'type',
  '  TBird<T> = class',
  '    procedure {a}DoIt(s: T);',
  '    procedure {b}DoIt;',
  '  end;',
  'implementation',
  'procedure TBird<T>.{c}Do2It(s: T);',
  'begin',
  'end;',
  'procedure TBird<T>.DoIt;',
  'begin',
  '{d}',
  'end;',
  'end.']);
  TestJumpToMethod('a',false,'c',false,2);
  TestJumpToMethod('b',false,'d',true,0);
end;

procedure TTestMethodJumpTool.TestMethodJump_DelphiGenericMethod;
begin
  Add([
  'unit Test1;',
  '{$mode delphi}{$H+}',
  'interface',
  'type',
  '  TBird = class',
  '    generic class procedure {a}DoIt<T>(s: T);',
  '    procedure DoIt;',
  '  end;',
  'implementation',
  'generic class procedure TBird.{b}Do2It<T>(s: T);',
  'begin',
  'end;',
  'procedure TBird.DoIt;',
  'begin',
  'end;',
  'end.']);
  TestJumpToMethod('a',false,'b',false,2);
end;

initialization
  RegisterTest(TTestMethodJumpTool);

end.