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
|
{
Test with:
./testcodetools --suite=TTestFindDeclaration
./testcodetools --suite=TestFindDeclaration_Basic
./testcodetools --suite=TestFindDeclaration_ClassOf
./testcodetools --suite=TestFindDeclaration_With
./testcodetools --suite=TestFindDeclaration_NestedClasses
./testcodetools --suite=TestFindDeclaration_ClassHelper
./testcodetools --suite=TestFindDeclaration_TypeHelper
./testcodetools --suite=TestFindDeclaration_ObjCClass
./testcodetools --suite=TestFindDeclaration_ObjCCategory
./testcodetools --suite=TestFindDeclaration_Generics
./testcodetools --suite=TestFindDeclaration_FileAtCursor
FPC tests:
./testcodetools --suite=TestFindDeclaration_FPCTests
./testcodetools --suite=TestFindDeclaration_FPCTests --filemask=t*.pp
./testcodetools --suite=TestFindDeclaration_FPCTests --filemask=tchlp41.pp
Laz tests:
./testcodetools --suite=TestFindDeclaration_LazTests
./testcodetools --suite=TestFindDeclaration_LazTests --filemask=t*.pp
./testcodetools --suite=TestFindDeclaration_LazTests --filemask=tdefaultproperty1.pp
}
unit TestIdentCompletion;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testregistry, FileProcs, LazFileUtils, LazLogger,
CodeToolManager, ExprEval, CustomCodeTool, FindDeclarationTool,
KeywordFuncLists, CodeCache, IdentCompletionTool, CodeTree,
TestFindDeclaration;
type
{ TTestIdentCompletion }
TTestIdentCompletion = class(TCustomTestFindDeclaration)
private
procedure CheckCodeContext(Context: TCodeContextInfoItem; MarkerName: string
);
published
procedure Test_GetValuesOfCaseVariable_Enum;
procedure Test_FindCodeContext_ProcParams;
procedure Test_FindCodeContext_ProcParams_NoClosingBracket;
procedure Test_FindCodeContext_ProcTypeParams;
procedure Test_FindCodeContext_AttributeParams;
end;
implementation
{ TTestIdentCompletion }
procedure TTestIdentCompletion.CheckCodeContext(Context: TCodeContextInfoItem; MarkerName: string);
var
Marker: TFDMarker;
begin
AssertNotNull('CheckCodeContext: missing context for #'+MarkerName,Context);
AssertEquals('CheckCodeContext: Context.Expr.Desc for #'+MarkerName,
ExpressionTypeDescNames[xtContext],ExpressionTypeDescNames[Context.Expr.Desc]);
if MainTool<>Context.Expr.Context.Tool then
Fail('CheckCodeContext: Context.Expr.Context.Tool for #'+MarkerName+' expected "'+MainTool.MainFilename+'", but found "'+Context.Expr.Context.Tool.MainFilename+'"');
Marker:=FindMarker(MarkerName,'#');
AssertNotNull('CheckCodeContext: missing marker #'+MarkerName,Marker);
AssertEquals('CheckCodeContext: Context.Expr.Context.Node.StartPos for #'+MarkerName,
Marker.CleanPos,Context.Expr.Context.Node.StartPos);
end;
procedure TTestIdentCompletion.Test_GetValuesOfCaseVariable_Enum;
var
List: TStrings;
begin
List:=TStringList.Create;
try
Code.Source:=
'type TEnum = (red,green);'+LineEnding
+'var e: TEnum;'+LineEnding
+'begin'+LineEnding
+' case e of'+LineEnding
+'end.';
List.Clear;
if not CodeToolBoss.GetValuesOfCaseVariable(Code,8,4,List) then begin
Fail('GetValuesOfCaseVariable failed on case enum');
end;
//writeln('TTestIdentCompletion.Test_GetValuesOfCaseVariable_Enum ',List.Text);
AssertEquals('case enum count',2,List.Count);
AssertEquals('case enum[0]','red',List[0]);
AssertEquals('case enum[1]','green',List[1]);
finally
List.Free;
end;
end;
procedure TTestIdentCompletion.Test_FindCodeContext_ProcParams;
var
SrcMark: TFDMarker;
CursorPos: TCodeXYPosition;
CodeContexts: TCodeContextInfo;
begin
StartProgram;
Add([
'{#a}procedure DoIt(i, j: longint);',
'begin',
'end;',
'{#b}procedure DoIt(s, h: string);',
'begin',
'end;',
'begin',
' DoIt(3,{#c}4);',
'end.']);
ParseSimpleMarkers(Code);
SrcMark:=FindMarker('c','#');
AssertNotNull('missing src marker #c',SrcMark);
MainTool.CleanPosToCaret(SrcMark.CleanPos,CursorPos);
CodeContexts:=nil;
try
if not CodeToolBoss.FindCodeContext(Code,CursorPos.X,CursorPos.Y,CodeContexts)
then begin
WriteSource(CursorPos);
Fail('CodeToolBoss.FindCodeContext');
end;
AssertEquals('CodeContexts.Count',2,CodeContexts.Count);
//for i:=0 to CodeContexts.Count-1 do
// debugln(['TTestIdentCompletion.Test_FindCodeContext_ProcParams ',i,' ',CodeContexts[i].AsDebugString(true)]);
CheckCodeContext(CodeContexts[0],'b');
CheckCodeContext(CodeContexts[1],'a');
finally
CodeContexts.Free;
end;
end;
procedure TTestIdentCompletion.Test_FindCodeContext_ProcParams_NoClosingBracket;
var
SrcMark: TFDMarker;
CursorPos: TCodeXYPosition;
CodeContexts: TCodeContextInfo;
begin
StartProgram;
Add([
'{#a}procedure DoIt(i, j: longint);',
'begin',
'end;',
'begin',
' DoIt({#c}',
'end.']);
ParseSimpleMarkers(Code);
SrcMark:=FindMarker('c','#');
AssertNotNull('missing src marker #c',SrcMark);
MainTool.CleanPosToCaret(SrcMark.CleanPos,CursorPos);
CodeContexts:=nil;
try
if not CodeToolBoss.FindCodeContext(Code,CursorPos.X,CursorPos.Y,CodeContexts)
then begin
WriteSource(CursorPos);
Fail('CodeToolBoss.FindCodeContext');
end;
AssertEquals('CodeContexts.Count',1,CodeContexts.Count);
//for i:=0 to CodeContexts.Count-1 do
// debugln(['TTestIdentCompletion.Test_FindCodeContext_ProcParams ',i,' ',CodeContexts[i].AsDebugString(true)]);
CheckCodeContext(CodeContexts[0],'a');
finally
CodeContexts.Free;
end;
end;
procedure TTestIdentCompletion.Test_FindCodeContext_ProcTypeParams;
var
SrcMark: TFDMarker;
CursorPos: TCodeXYPosition;
CodeContexts: TCodeContextInfo;
begin
StartProgram;
Add([
'type',
' TProc = procedure(i,j: longint);',
'var {#p}p: TProc;',
'begin',
' p(3,{#c}4);',
'end.']);
ParseSimpleMarkers(Code);
SrcMark:=FindMarker('c','#');
AssertNotNull('missing src marker #c',SrcMark);
MainTool.CleanPosToCaret(SrcMark.CleanPos,CursorPos);
CodeContexts:=nil;
try
if not CodeToolBoss.FindCodeContext(Code,CursorPos.X,CursorPos.Y,CodeContexts)
then begin
WriteSource(CursorPos);
Fail('CodeToolBoss.FindCodeContext');
end;
AssertEquals('CodeContexts.Count',1,CodeContexts.Count);
//for i:=0 to CodeContexts.Count-1 do
// debugln(['TTestIdentCompletion.Test_FindCodeContext_ProcParams ',i,' ',CodeContexts[i].AsDebugString(true)]);
CheckCodeContext(CodeContexts[0],'p');
finally
CodeContexts.Free;
end;
end;
procedure TTestIdentCompletion.Test_FindCodeContext_AttributeParams;
var
SrcMark: TFDMarker;
CursorPos: TCodeXYPosition;
CodeContexts: TCodeContextInfo;
begin
StartProgram;
Add([
'{$modeswitch prefixedattributes}',
'type',
' BirdAttribute = class',
' {#a}constructor Create; overload;',
' {#b}constructor Create(i,j: longint); overload;',
' end;',
' [Bird({#c})]',
' TColor = 1..3;',
'begin',
'end.']);
ParseSimpleMarkers(Code);
SrcMark:=FindMarker('c','#');
AssertNotNull('missing src marker #c',SrcMark);
MainTool.CleanPosToCaret(SrcMark.CleanPos,CursorPos);
CodeContexts:=nil;
try
if not CodeToolBoss.FindCodeContext(Code,CursorPos.X,CursorPos.Y,CodeContexts)
then begin
WriteSource(CursorPos);
Fail('CodeToolBoss.FindCodeContext');
end;
//for i:=0 to CodeContexts.Count-1 do
// debugln(['TTestIdentCompletion.Test_FindCodeContext_ProcParams ',i,' ',CodeContexts[i].AsDebugString(true)]);
AssertEquals('CodeContexts.Count',3,CodeContexts.Count);
CheckCodeContext(CodeContexts[0],'b');
CheckCodeContext(CodeContexts[1],'a');
// last entry is the default TObject.Create in unit objpas
finally
CodeContexts.Free;
end;
end;
initialization
RegisterTests([TTestIdentCompletion]);
end.
|