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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
unit TestDesignerFormTools;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, CodeToolManager, CodeCache, DefineTemplates,
LazLogger, LazFileUtils, AvgLvlTree, AVL_Tree, fpcunit, testregistry,
TestFinddeclaration, TestStdCodetools,
Dsgn_AmbigBearBtn, Dsgn_BearControls, Dsgn_BearButtons;
type
TBearForm1 = class(TBearForm)
end;
{ TTestDesignerFormTools }
TTestDesignerFormTools = class(TCustomTestCTStdCodetools)
private
procedure TestCompleteComponent(const Title: string; CheckUnits: boolean; const Src, Expected: array of string);
procedure TestGatherPublishedVarType(const Title, aClassName: string; const Src, ExpectedVars: array of string);
protected
procedure SetUp; override;
procedure TearDown; override;
public
BearForm1: TBearForm1;
published
// add published variables
procedure TestAddPublishedVariables_Empty;
procedure TestAddPublishedVariables_Button1;
procedure TestAddPublishedVariables_AmbiguousButtons1;
procedure TestAddPublishedVariables_AmbiguousButtons2;
// gather published variable types
procedure TestGatherPublishedVarTypes_Empty;
procedure TestGatherPublishedVarTypes_Button1;
procedure TestGatherPublishedVarTypes_Button2;
end;
implementation
{ TTestDesignerFormTools }
procedure TTestDesignerFormTools.TestCompleteComponent(const Title: string;
CheckUnits: boolean; const Src, Expected: array of string);
var
i, NewX, NewY: Integer;
s, Dir: String;
NewCode, Code: TCodeBuffer;
DefTemp: TDefineTemplate;
begin
Code:=CodeToolBoss.CreateFile('test1.pas');
s:='';
for i:=Low(Src) to High(Src) do
s+=Src[i]+LineEnding;
Code.Source:=s;
Dir:=AppendPathDelim(GetCurrentDir)+'moduletests';
DefTemp:=TDefineTemplate.Create('unitpath','add moduletests',UnitPathMacroName,Dir,da_Define);
try
CodeToolBoss.DefineTree.Add(DefTemp);
if not CodeToolBoss.CompleteComponent(Code,BearForm1,nil,CheckUnits)
and (CodeToolBoss.ErrorDbgMsg<>'') then
begin
NewCode:=Code;
NewY:=1;
NewX:=1;
if (CodeToolBoss.ErrorCode<>nil) and (CodeToolBoss.ErrorLine>0) then begin
NewY:=CodeToolBoss.ErrorLine;
NewX:=CodeToolBoss.ErrorColumn;
NewCode:=CodeToolBoss.ErrorCode;
end;
WriteSource(NewCode.Filename,NewY,NewX);
Fail(Title+': call CompleteComponent failed: "'+CodeToolBoss.ErrorDbgMsg+'"');
end;
s:='';
for i:=Low(Expected) to High(Expected) do
s+=Expected[i]+LineEnding;
CheckDiff(Title,s,Code.Source);
finally
CodeToolBoss.DefineTree.RemoveDefineTemplate(DefTemp);
end;
end;
procedure TTestDesignerFormTools.TestGatherPublishedVarType(const Title,
aClassName: string; const Src, ExpectedVars: array of string);
var
Code, NewCode: TCodeBuffer;
VarNameToToType: TStringToStringTree;
Expected, Actual, Dir: String;
i, NewY, NewX: Integer;
DefTemp: TDefineTemplate;
begin
Code:=CodeToolBoss.CreateFile('test1.pas');
Actual:='';
for i:=Low(Src) to High(Src) do
Actual+=Src[i]+LineEnding;
Code.Source:=Actual;
Dir:=AppendPathDelim(GetCurrentDir)+'moduletests';
DefTemp:=TDefineTemplate.Create('unitpath','add moduletests',UnitPathMacroName,Dir,da_Define);
VarNameToToType:=nil;
try
CodeToolBoss.DefineTree.Add(DefTemp);
if not CodeToolBoss.GatherPublishedVarTypes(Code,aClassName,VarNameToToType)
and (CodeToolBoss.ErrorDbgMsg<>'') then
begin
NewCode:=Code;
NewY:=1;
NewX:=1;
if (CodeToolBoss.ErrorCode<>nil) and (CodeToolBoss.ErrorLine>0) then begin
NewY:=CodeToolBoss.ErrorLine;
NewX:=CodeToolBoss.ErrorColumn;
NewCode:=CodeToolBoss.ErrorCode;
end;
WriteSource(NewCode.Filename,NewY,NewX);
Fail(Title+': call GatherPublishedVarTypes failed: "'+CodeToolBoss.ErrorDbgMsg+'"');
end;
Actual:='';
if VarNameToToType<>nil then begin
Actual:=VarNameToToType.AsText;
Actual:=StringReplace(Actual,'=',':',[rfReplaceAll]);
end;
Expected:='';
for i:=Low(ExpectedVars) to High(ExpectedVars) do
Expected+=ExpectedVars[i]+LineEnding;
if Actual<>Expected then begin
debugln(['TTestDesignerFormTools.TestGatherPublishedVarType Expected:']);
debugln(Expected);
debugln(['TTestDesignerFormTools.TestGatherPublishedVarType Actual:']);
debugln(Actual);
Fail('VarNameToToType differ');
end;
finally
VarNameToToType.Free;
CodeToolBoss.DefineTree.RemoveDefineTemplate(DefTemp);
end;
end;
procedure TTestDesignerFormTools.SetUp;
begin
inherited SetUp;
BearForm1:=TBearForm1.Create(nil);
end;
procedure TTestDesignerFormTools.TearDown;
begin
FreeAndNil(BearForm1);
inherited TearDown;
end;
procedure TTestDesignerFormTools.TestAddPublishedVariables_Empty;
begin
TestCompleteComponent('TestAddPublishedVariables_Empty',true,
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls;'
,'type'
,' TBearForm1 = class(TBearForm)'
,' end;'
,'implementation'
,'end.'],
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls;'
,'type'
,' { TBearForm1 }'
,' TBearForm1 = class(TBearForm)'
,' end;'
,'implementation'
,'end.']);
end;
procedure TTestDesignerFormTools.TestAddPublishedVariables_Button1;
var
Btn: Dsgn_BearButtons.TBearButton;
begin
Btn:=Dsgn_BearButtons.TBearButton.Create(BearForm1);
Btn.Name:='Button1';
TestCompleteComponent('TestAddPublishedVariables_Button1',true,
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons;'
,'type'
,' TBearForm1 = class(TBearForm)'
,' end;'
,'implementation'
,'end.'],
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons;'
,'type'
,' { TBearForm1 }'
,' TBearForm1 = class(TBearForm)'
,' Button1: TBearButton;'
,' end;'
,'implementation'
,'end.']);
end;
procedure TTestDesignerFormTools.TestAddPublishedVariables_AmbiguousButtons1;
var
Btn1: Dsgn_BearButtons.TBearButton;
begin
Btn1:=Dsgn_BearButtons.TBearButton.Create(BearForm1);
Btn1.Name:='Button1';
TestCompleteComponent('TestAddPublishedVariables_AmbiguousButtons1',true,
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons, Dsgn_AmbigBearBtn;'
,'type'
,' TBearForm1 = class(TBearForm)'
,' end;'
,'implementation'
,'end.'],
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons, Dsgn_AmbigBearBtn;'
,'type'
,' { TBearForm1 }'
,' TBearForm1 = class(TBearForm)'
,' Button1: Dsgn_BearButtons.TBearButton;'
,' end;'
,'implementation'
,'end.']);
end;
procedure TTestDesignerFormTools.TestAddPublishedVariables_AmbiguousButtons2;
var
Btn1: Dsgn_BearButtons.TBearButton;
Btn2: Dsgn_AmbigBearBtn.TBearButton;
Label1: TBearLabel;
begin
Btn1:=Dsgn_BearButtons.TBearButton.Create(BearForm1);
Btn1.Name:='Button1';
Btn2:=Dsgn_AmbigBearBtn.TBearButton.Create(BearForm1);
Btn2.Name:='Button2';
Label1:=TBearLabel.Create(BearForm1);
Label1.Name:='Label1';
TestCompleteComponent('TestAddPublishedVariables_AmbiguousButtons2',true,
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons, Dsgn_AmbigBearBtn;'
,'type'
,' TBearForm1 = class(TBearForm)'
,' end;'
,'implementation'
,'end.'],
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons, Dsgn_AmbigBearBtn;'
,'type'
,' { TBearForm1 }'
,' TBearForm1 = class(TBearForm)'
,' Button1: Dsgn_BearButtons.TBearButton;'
,' Button2: Dsgn_AmbigBearBtn.TBearButton;'
,' Label1: TBearLabel;'
,' end;'
,'implementation'
,'end.']);
end;
procedure TTestDesignerFormTools.TestGatherPublishedVarTypes_Empty;
begin
TestGatherPublishedVarType('TestGatherPublishedVarTypes_Empty',
'TBearForm1',
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons, Dsgn_AmbigBearBtn;'
,'type'
,' TBearForm1 = class(TBearForm)'
,' end;'
,'implementation'
,'end.'],
[]
);
end;
procedure TTestDesignerFormTools.TestGatherPublishedVarTypes_Button1;
begin
TestGatherPublishedVarType('TestGatherPublishedVarTypes_Empty',
'TBearForm1',
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons, Dsgn_AmbigBearBtn;'
,'type'
,' TBearForm1 = class(TBearForm)'
,' Button1: TBearButton;'
,' end;'
,'implementation'
,'end.'],
['Button1:Dsgn_AmbigBearBtn/TBearButton']
);
end;
procedure TTestDesignerFormTools.TestGatherPublishedVarTypes_Button2;
begin
TestGatherPublishedVarType('TestGatherPublishedVarTypes_Empty',
'TBearForm1',
['unit test1;'
,'{$mode objfpc}{$H+}'
,'interface'
,'uses Dsgn_BearControls, Dsgn_BearButtons, Dsgn_AmbigBearBtn;'
,'type'
,' TBearForm1 = class(TBearForm)'
,' Button1: TBearButton;'
,' Button2: Dsgn_BearButtons.TBearButton;'
,' Label1: TBearLabel;'
,' end;'
,'implementation'
,'end.'],
['Button1:Dsgn_AmbigBearBtn/TBearButton',
'Button2:Dsgn_BearButtons/TBearButton',
'Label1:Dsgn_BearControls/TBearLabel']
);
end;
initialization
RegisterTests([TTestDesignerFormTools]);
end.
|