tcfiler.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2014 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript precompile class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestPrecompile.TestPC_EmptyUnit
  13. }
  14. unit tcfiler;
  15. {$mode objfpc}{$H+}
  16. interface
  17. uses
  18. Classes, SysUtils, fpcunit, testregistry,
  19. PasTree, PScanner, PasResolver, PasResolveEval, PParser,
  20. FPPas2Js, Pas2JsFiler,
  21. tcmodules;
  22. type
  23. { TCustomTestPrecompile }
  24. TCustomTestPrecompile = Class(TCustomTestModule)
  25. private
  26. FInitialFlags: TPJUInitialFlags;
  27. FPJUReader: TPJUReader;
  28. FPJUWriter: TPJUWriter;
  29. procedure OnFilerGetSrc(Sender: TObject; aFilename: string; out p: PChar;
  30. out Count: integer);
  31. protected
  32. procedure SetUp; override;
  33. procedure TearDown; override;
  34. procedure WriteReadUnit; virtual;
  35. procedure StartParsing; override;
  36. procedure CheckRestoredResolver(Original, Restored: TPas2JSResolver); virtual;
  37. procedure CheckRestoredDeclarations(const Path: string; Orig, Rest: TPasDeclarations); virtual;
  38. procedure CheckRestoredSection(const Path: string; Orig, Rest: TPasSection); virtual;
  39. procedure CheckRestoredModule(const Path: string; Orig, Rest: TPasModule); virtual;
  40. procedure CheckRestoredModuleScope(const Path: string; Orig, Rest: TPasModuleScope); virtual;
  41. procedure CheckRestoredIdentifierScope(const Path: string; Orig, Rest: TPasIdentifierScope); virtual;
  42. procedure CheckRestoredSectionScope(const Path: string; Orig, Rest: TPasSectionScope); virtual;
  43. procedure CheckRestoredCustomData(const Path: string; El: TPasElement; Orig, Rest: TObject); virtual;
  44. procedure CheckRestoredElement(const Path: string; Orig, Rest: TPasElement); virtual;
  45. procedure CheckRestoredElementList(const Path: string; Orig, Rest: TFPList); virtual;
  46. procedure CheckRestoredPasExpr(const Path: string; Orig, Rest: TPasExpr); virtual;
  47. procedure CheckRestoredUnaryExpr(const Path: string; Orig, Rest: TUnaryExpr); virtual;
  48. procedure CheckRestoredBinaryExpr(const Path: string; Orig, Rest: TBinaryExpr); virtual;
  49. procedure CheckRestoredPrimitiveExpr(const Path: string; Orig, Rest: TPrimitiveExpr); virtual;
  50. procedure CheckRestoredBoolConstExpr(const Path: string; Orig, Rest: TBoolConstExpr); virtual;
  51. procedure CheckRestoredParamsExpr(const Path: string; Orig, Rest: TParamsExpr); virtual;
  52. procedure CheckRestoredRecordValues(const Path: string; Orig, Rest: TRecordValues); virtual;
  53. procedure CheckRestoredPasExprArray(const Path: string; Orig, Rest: TPasExprArray); virtual;
  54. procedure CheckRestoredArrayValues(const Path: string; Orig, Rest: TArrayValues); virtual;
  55. procedure CheckRestoredResString(const Path: string; Orig, Rest: TPasResString); virtual;
  56. procedure CheckRestoredAliasType(const Path: string; Orig, Rest: TPasAliasType); virtual;
  57. procedure CheckRestoredPointerType(const Path: string; Orig, Rest: TPasPointerType); virtual;
  58. procedure CheckRestoredSpecializedType(const Path: string; Orig, Rest: TPasSpecializeType); virtual;
  59. procedure CheckRestoredInlineSpecializedExpr(const Path: string; Orig, Rest: TInlineSpecializeExpr); virtual;
  60. procedure CheckRestoredRangeType(const Path: string; Orig, Rest: TPasRangeType); virtual;
  61. procedure CheckRestoredArrayType(const Path: string; Orig, Rest: TPasArrayType); virtual;
  62. procedure CheckRestoredFileType(const Path: string; Orig, Rest: TPasFileType); virtual;
  63. procedure CheckRestoredEnumValue(const Path: string; Orig, Rest: TPasEnumValue); virtual;
  64. procedure CheckRestoredEnumType(const Path: string; Orig, Rest: TPasEnumType); virtual;
  65. procedure CheckRestoredSetType(const Path: string; Orig, Rest: TPasSetType); virtual;
  66. procedure CheckRestoredVariant(const Path: string; Orig, Rest: TPasVariant); virtual;
  67. procedure CheckRestoredRecordType(const Path: string; Orig, Rest: TPasRecordType); virtual;
  68. procedure CheckRestoredClassType(const Path: string; Orig, Rest: TPasClassType); virtual;
  69. procedure CheckRestoredArgument(const Path: string; Orig, Rest: TPasArgument); virtual;
  70. procedure CheckRestoredProcedureType(const Path: string; Orig, Rest: TPasProcedureType); virtual;
  71. procedure CheckRestoredResultElement(const Path: string; Orig, Rest: TPasResultElement); virtual;
  72. procedure CheckRestoredFunctionType(const Path: string; Orig, Rest: TPasFunctionType); virtual;
  73. procedure CheckRestoredStringType(const Path: string; Orig, Rest: TPasStringType); virtual;
  74. procedure CheckRestoredVariable(const Path: string; Orig, Rest: TPasVariable); virtual;
  75. procedure CheckRestoredExportSymbol(const Path: string; Orig, Rest: TPasExportSymbol); virtual;
  76. procedure CheckRestoredConst(const Path: string; Orig, Rest: TPasConst); virtual;
  77. procedure CheckRestoredProperty(const Path: string; Orig, Rest: TPasProperty); virtual;
  78. procedure CheckRestoredProcedure(const Path: string; Orig, Rest: TPasProcedure); virtual;
  79. procedure CheckRestoredOperator(const Path: string; Orig, Rest: TPasOperator); virtual;
  80. procedure CheckRestoredReference(const Path: string; Orig, Rest: TPasElement); virtual;
  81. public
  82. property PJUWriter: TPJUWriter read FPJUWriter write FPJUWriter;
  83. property PJUReader: TPJUReader read FPJUReader write FPJUReader;
  84. property InitialFlags: TPJUInitialFlags read FInitialFlags;
  85. end;
  86. { TTestPrecompile }
  87. TTestPrecompile = class(TCustomTestPrecompile)
  88. published
  89. procedure Test_Base256VLQ;
  90. procedure TestPC_EmptyUnit;
  91. procedure TestPC_Const;
  92. end;
  93. implementation
  94. { TCustomTestPrecompile }
  95. procedure TCustomTestPrecompile.OnFilerGetSrc(Sender: TObject;
  96. aFilename: string; out p: PChar; out Count: integer);
  97. var
  98. i: Integer;
  99. aModule: TTestEnginePasResolver;
  100. Src: String;
  101. begin
  102. for i:=0 to ResolverCount-1 do
  103. begin
  104. aModule:=Resolvers[i];
  105. if aModule.Filename<>aFilename then continue;
  106. Src:=aModule.Source;
  107. p:=PChar(Src);
  108. Count:=length(Src);
  109. end;
  110. end;
  111. procedure TCustomTestPrecompile.SetUp;
  112. begin
  113. inherited SetUp;
  114. FInitialFlags:=TPJUInitialFlags.Create;
  115. end;
  116. procedure TCustomTestPrecompile.TearDown;
  117. begin
  118. FreeAndNil(FPJUWriter);
  119. FreeAndNil(FPJUReader);
  120. FreeAndNil(FInitialFlags);
  121. inherited TearDown;
  122. end;
  123. procedure TCustomTestPrecompile.WriteReadUnit;
  124. var
  125. ms: TMemoryStream;
  126. PJU: string;
  127. ReadResolver: TTestEnginePasResolver;
  128. ReadFileResolver: TFileResolver;
  129. ReadScanner: TPascalScanner;
  130. ReadParser: TPasParser;
  131. begin
  132. ConvertUnit;
  133. FPJUWriter:=TPJUWriter.Create;
  134. FPJUReader:=TPJUReader.Create;
  135. ms:=TMemoryStream.Create;
  136. ReadParser:=nil;
  137. ReadScanner:=nil;
  138. ReadResolver:=nil;
  139. ReadFileResolver:=nil;
  140. try
  141. try
  142. PJUWriter.OnGetSrc:=@OnFilerGetSrc;
  143. PJUWriter.WritePJU(Engine,InitialFlags,ms);
  144. except
  145. on E: Exception do
  146. begin
  147. {$IFDEF VerbosePas2JS}
  148. writeln('TCustomTestPrecompile.WriteReadUnit WRITE failed');
  149. {$ENDIF}
  150. Fail('Write failed('+E.ClassName+'): '+E.Message);
  151. end;
  152. end;
  153. try
  154. SetLength(PJU,ms.Size);
  155. System.Move(ms.Memory^,PJU[1],length(PJU));
  156. writeln('TCustomTestPrecompile.WriteReadUnit PJU START-----');
  157. writeln(PJU);
  158. writeln('TCustomTestPrecompile.WriteReadUnit PJU END-------');
  159. ReadFileResolver:=TFileResolver.Create;
  160. ReadScanner:=TPascalScanner.Create(ReadFileResolver);
  161. InitScanner(ReadScanner);
  162. ReadResolver:=TTestEnginePasResolver.Create;
  163. ReadResolver.Filename:=Engine.Filename;
  164. ReadResolver.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  165. //ReadResolver.OnFindUnit:=@OnPasResolverFindUnit;
  166. ReadParser:=TPasParser.Create(ReadScanner,ReadFileResolver,ReadResolver);
  167. ReadParser.Options:=po_tcmodules;
  168. ReadResolver.CurrentParser:=ReadParser;
  169. ms.Position:=0;
  170. PJUReader.ReadPJU(ReadResolver,ms);
  171. except
  172. on E: Exception do
  173. begin
  174. {$IFDEF VerbosePas2JS}
  175. writeln('TCustomTestPrecompile.WriteReadUnit READ failed');
  176. {$ENDIF}
  177. Fail('Read failed('+E.ClassName+'): '+E.Message);
  178. end;
  179. end;
  180. CheckRestoredResolver(Engine,ReadResolver);
  181. finally
  182. ReadParser.Free;
  183. ReadScanner.Free;
  184. ReadResolver.Free; // free parser before resolver
  185. ReadFileResolver.Free;
  186. ms.Free;
  187. end;
  188. end;
  189. procedure TCustomTestPrecompile.StartParsing;
  190. begin
  191. inherited StartParsing;
  192. FInitialFlags.ParserOptions:=Parser.Options;
  193. FInitialFlags.ModeSwitches:=Scanner.CurrentModeSwitches;
  194. FInitialFlags.BoolSwitches:=Scanner.CurrentBoolSwitches;
  195. FInitialFlags.ConverterOptions:=Converter.Options;
  196. FInitialFlags.TargetPlatform:=Converter.TargetPlatform;
  197. FInitialFlags.TargetProcessor:=Converter.TargetProcessor;
  198. // ToDo: defines
  199. end;
  200. procedure TCustomTestPrecompile.CheckRestoredResolver(Original,
  201. Restored: TPas2JSResolver);
  202. begin
  203. AssertNotNull('CheckRestoredResolver Original',Original);
  204. AssertNotNull('CheckRestoredResolver Restored',Restored);
  205. if Original.ClassType<>Restored.ClassType then
  206. Fail('CheckRestoredResolver Original='+Original.ClassName+' Restored='+Restored.ClassName);
  207. CheckRestoredElement('RootElement',Original.RootElement,Restored.RootElement);
  208. end;
  209. procedure TCustomTestPrecompile.CheckRestoredDeclarations(const Path: string;
  210. Orig, Rest: TPasDeclarations);
  211. var
  212. i: Integer;
  213. OrigDecl, RestDecl: TPasElement;
  214. SubPath: String;
  215. begin
  216. for i:=0 to Orig.Declarations.Count-1 do
  217. begin
  218. OrigDecl:=TPasElement(Orig.Declarations[i]);
  219. if i>=Rest.Declarations.Count then
  220. AssertEquals(Path+': Declarations.Count',Orig.Declarations.Count,Rest.Declarations.Count);
  221. RestDecl:=TPasElement(Rest.Declarations[i]);
  222. SubPath:=Path+'['+IntToStr(i)+']';
  223. if OrigDecl.Name<>'' then
  224. SubPath:=SubPath+'"'+OrigDecl.Name+'"'
  225. else
  226. SubPath:=SubPath+'?noname?';
  227. CheckRestoredElement(SubPath,OrigDecl,RestDecl);
  228. end;
  229. AssertEquals(Path+': Declarations.Count',Orig.Declarations.Count,Rest.Declarations.Count);
  230. end;
  231. procedure TCustomTestPrecompile.CheckRestoredSection(const Path: string; Orig,
  232. Rest: TPasSection);
  233. begin
  234. if length(Orig.UsesClause)>0 then
  235. ; // ToDo
  236. CheckRestoredDeclarations(Path,Rest,Orig);
  237. end;
  238. procedure TCustomTestPrecompile.CheckRestoredModule(const Path: string; Orig,
  239. Rest: TPasModule);
  240. begin
  241. if not (Orig.CustomData is TPasModuleScope) then
  242. Fail(Path+'.CustomData is not TPasModuleScope'+GetObjName(Orig.CustomData));
  243. CheckRestoredElement(Path+'.InterfaceSection',Orig.InterfaceSection,Rest.InterfaceSection);
  244. CheckRestoredElement(Path+'.ImplementationSection',Orig.ImplementationSection,Rest.ImplementationSection);
  245. if Orig is TPasProgram then
  246. CheckRestoredElement(Path+'.ProgramSection',TPasProgram(Orig).ProgramSection,TPasProgram(Rest).ProgramSection)
  247. else if Orig is TPasLibrary then
  248. CheckRestoredElement(Path+'.LibrarySection',TPasLibrary(Orig).LibrarySection,TPasLibrary(Rest).LibrarySection);
  249. CheckRestoredElement(Path+'.InitializationSection',Orig.InitializationSection,Rest.InitializationSection);
  250. CheckRestoredElement(Path+'.FinalizationSection',Orig.FinalizationSection,Rest.FinalizationSection);
  251. end;
  252. procedure TCustomTestPrecompile.CheckRestoredModuleScope(const Path: string;
  253. Orig, Rest: TPasModuleScope);
  254. begin
  255. AssertEquals(Path+': FirstName',Orig.FirstName,Rest.FirstName);
  256. if Orig.Flags<>Rest.Flags then
  257. Fail(Path+': Flags');
  258. if Orig.BoolSwitches<>Rest.BoolSwitches then
  259. Fail(Path+': BoolSwitches');
  260. CheckRestoredReference(Path+'.AssertClass',Orig.AssertClass,Rest.AssertClass);
  261. CheckRestoredReference(Path+'.AssertDefConstructor',Orig.AssertDefConstructor,Rest.AssertDefConstructor);
  262. CheckRestoredReference(Path+'.AssertMsgConstructor',Orig.AssertMsgConstructor,Rest.AssertMsgConstructor);
  263. CheckRestoredReference(Path+'.RangeErrorClass',Orig.RangeErrorClass,Rest.RangeErrorClass);
  264. CheckRestoredReference(Path+'.RangeErrorConstructor',Orig.RangeErrorConstructor,Rest.RangeErrorConstructor);
  265. end;
  266. procedure TCustomTestPrecompile.CheckRestoredIdentifierScope(
  267. const Path: string; Orig, Rest: TPasIdentifierScope);
  268. var
  269. OrigList: TFPList;
  270. i: Integer;
  271. OrigIdentifier, RestIdentifier: TPasIdentifier;
  272. begin
  273. OrigList:=nil;
  274. try
  275. OrigList:=Orig.GetLocalIdentifiers;
  276. for i:=0 to OrigList.Count-1 do
  277. begin
  278. OrigIdentifier:=TPasIdentifier(OrigList[i]);
  279. RestIdentifier:=Rest.FindLocalIdentifier(OrigIdentifier.Identifier);
  280. if RestIdentifier=nil then
  281. Fail(Path+'.Local['+OrigIdentifier.Identifier+'] Missing RestIdentifier Orig='+OrigIdentifier.Identifier);
  282. repeat
  283. AssertEquals(Path+'.Local.Identifier',OrigIdentifier.Identifier,RestIdentifier.Identifier);
  284. CheckRestoredReference(Path+'.Local',OrigIdentifier.Element,RestIdentifier.Element);
  285. if OrigIdentifier.Kind<>RestIdentifier.Kind then
  286. Fail(Path+'.Local['+OrigIdentifier.Identifier+'] Orig='+PJUIdentifierKindNames[OrigIdentifier.Kind]+' Rest='+PJUIdentifierKindNames[RestIdentifier.Kind]);
  287. if OrigIdentifier.NextSameIdentifier=nil then
  288. begin
  289. if RestIdentifier.NextSameIdentifier<>nil then
  290. Fail(Path+'.Local['+OrigIdentifier.Identifier+'] Too many RestIdentifier.NextSameIdentifier='+GetObjName(RestIdentifier.Element));
  291. break;
  292. end
  293. else begin
  294. if RestIdentifier.NextSameIdentifier=nil then
  295. Fail(Path+'.Local['+OrigIdentifier.Identifier+'] Missing RestIdentifier.NextSameIdentifier Orig='+GetObjName(OrigIdentifier.NextSameIdentifier.Element));
  296. end;
  297. if CompareText(OrigIdentifier.Identifier,OrigIdentifier.NextSameIdentifier.Identifier)<>0 then
  298. Fail(Path+'.Local['+OrigIdentifier.Identifier+'] Cur.Identifier<>Next.Identifier '+OrigIdentifier.Identifier+'<>'+OrigIdentifier.NextSameIdentifier.Identifier);
  299. OrigIdentifier:=OrigIdentifier.NextSameIdentifier;
  300. RestIdentifier:=RestIdentifier.NextSameIdentifier;
  301. until false;
  302. end;
  303. finally
  304. OrigList.Free;
  305. end;
  306. end;
  307. procedure TCustomTestPrecompile.CheckRestoredSectionScope(const Path: string;
  308. Orig, Rest: TPasSectionScope);
  309. var
  310. i: Integer;
  311. OrigUses, RestUses: TPasSectionScope;
  312. begin
  313. AssertEquals(Path+' UsesScopes.Count',Orig.UsesScopes.Count,Rest.UsesScopes.Count);
  314. for i:=0 to Orig.UsesScopes.Count-1 do
  315. begin
  316. OrigUses:=TPasSectionScope(Orig.UsesScopes[i]);
  317. if not (TObject(Rest.UsesScopes[i]) is TPasSectionScope) then
  318. Fail(Path+': Uses['+IntToStr(i)+'] Rest='+GetObjName(TObject(Rest.UsesScopes[i])));
  319. RestUses:=TPasSectionScope(Rest.UsesScopes[i]);
  320. if OrigUses.ClassType<>RestUses.ClassType then
  321. Fail(Path+': Uses['+IntToStr(i)+'] Orig='+GetObjName(OrigUses)+' Rest='+GetObjName(RestUses));
  322. CheckRestoredReference(Path+': Uses['+IntToStr(i)+']',OrigUses.Element,RestUses.Element);
  323. end;
  324. AssertEquals(Path+': Finished',Orig.Finished,Rest.Finished);
  325. CheckRestoredIdentifierScope(Path,Orig,Rest);
  326. end;
  327. procedure TCustomTestPrecompile.CheckRestoredCustomData(const Path: string;
  328. El: TPasElement; Orig, Rest: TObject);
  329. var
  330. C: TClass;
  331. begin
  332. if Orig=nil then
  333. begin
  334. if Rest<>nil then
  335. Fail(Path+': Orig=nil Rest='+GetObjName(Rest));
  336. exit;
  337. end
  338. else if Rest=nil then
  339. Fail(Path+': Orig='+GetObjName(Orig)+' Rest=nil');
  340. if Orig.ClassType<>Rest.ClassType then
  341. Fail(Path+': Orig='+GetObjName(Orig)+' Rest='+GetObjName(Rest));
  342. C:=Orig.ClassType;
  343. if C=TPasModuleScope then
  344. CheckRestoredModuleScope(Path+'[TPasModuleScope]',TPasModuleScope(Orig),TPasModuleScope(Rest))
  345. else if C=TPasSectionScope then
  346. CheckRestoredSectionScope(Path+'[TPasSectionScope]',TPasSectionScope(Orig),TPasSectionScope(Rest))
  347. else
  348. Fail(Path+': unknown CustomData "'+GetObjName(Orig)+'" El='+GetObjName(El));
  349. end;
  350. procedure TCustomTestPrecompile.CheckRestoredElement(const Path: string; Orig,
  351. Rest: TPasElement);
  352. var
  353. C: TClass;
  354. begin
  355. if Orig=nil then
  356. begin
  357. if Rest<>nil then
  358. Fail(Path+': Orig=nil Rest='+GetObjName(Rest));
  359. exit;
  360. end
  361. else if Rest=nil then
  362. Fail(Path+': Orig='+GetObjName(Orig)+' Rest=nil');
  363. if Orig.ClassType<>Rest.ClassType then
  364. Fail(Path+': Orig='+GetObjName(Orig)+' Rest='+GetObjName(Rest));
  365. AssertEquals(Path+': Name',Orig.Name,Rest.Name);
  366. AssertEquals(Path+': SourceFilename',Orig.SourceFilename,Rest.SourceFilename);
  367. AssertEquals(Path+': SourceLinenumber',Orig.SourceLinenumber,Rest.SourceLinenumber);
  368. //AssertEquals(Path+': SourceEndLinenumber',Orig.SourceEndLinenumber,Rest.SourceEndLinenumber);
  369. if Orig.Visibility<>Rest.Visibility then
  370. Fail(Path+': Visibility '+PJUMemberVisibilityNames[Orig.Visibility]+' '+PJUMemberVisibilityNames[Rest.Visibility]);
  371. if Orig.Hints<>Rest.Hints then
  372. Fail(Path+': Hints');
  373. AssertEquals(Path+': HintMessage',Orig.HintMessage,Rest.HintMessage);
  374. if Orig.Parent=nil then
  375. begin
  376. if Rest.Parent<>nil then
  377. Fail(Path+': Orig.Parent=nil Rest.Parent='+GetObjName(Rest.Parent));
  378. end
  379. else if Rest.Parent=nil then
  380. Fail(Path+': Orig.Parent='+GetObjName(Orig.Parent)+' Rest.Parent=nil')
  381. else if Orig.Parent.ClassType<>Rest.Parent.ClassType then
  382. Fail(Path+': Orig.Parent='+GetObjName(Orig.Parent)+' Rest.Parent='+GetObjName(Rest.Parent));
  383. CheckRestoredCustomData(Path+'.CustomData',Rest,Orig.CustomData,Rest.CustomData);
  384. C:=Orig.ClassType;
  385. if C=TUnaryExpr then
  386. CheckRestoredUnaryExpr(Path,TUnaryExpr(Orig),TUnaryExpr(Rest))
  387. else if C=TBinaryExpr then
  388. CheckRestoredBinaryExpr(Path,TBinaryExpr(Orig),TBinaryExpr(Rest))
  389. else if C=TPrimitiveExpr then
  390. CheckRestoredPrimitiveExpr(Path,TPrimitiveExpr(Orig),TPrimitiveExpr(Rest))
  391. else if C=TBoolConstExpr then
  392. CheckRestoredBoolConstExpr(Path,TBoolConstExpr(Orig),TBoolConstExpr(Rest))
  393. else if (C=TNilExpr)
  394. or (C=TInheritedExpr)
  395. or (C=TSelfExpr) then
  396. CheckRestoredPasExpr(Path,TPasExpr(Orig),TPasExpr(Rest))
  397. else if C=TParamsExpr then
  398. CheckRestoredParamsExpr(Path,TParamsExpr(Orig),TParamsExpr(Rest))
  399. else if C=TRecordValues then
  400. CheckRestoredRecordValues(Path,TRecordValues(Orig),TRecordValues(Rest))
  401. else if C=TArrayValues then
  402. CheckRestoredArrayValues(Path,TArrayValues(Orig),TArrayValues(Rest))
  403. // TPasDeclarations is a base class
  404. // TPasUsesUnit is checked in usesclause
  405. // TPasSection is a base class
  406. else if C=TPasResString then
  407. CheckRestoredResString(Path,TPasResString(Orig),TPasResString(Rest))
  408. // TPasType is a base clas
  409. else if (C=TPasAliasType)
  410. or (C=TPasTypeAliasType)
  411. or (C=TPasClassOfType) then
  412. CheckRestoredAliasType(Path,TPasAliasType(Orig),TPasAliasType(Rest))
  413. else if C=TPasPointerType then
  414. CheckRestoredPointerType(Path,TPasPointerType(Orig),TPasPointerType(Rest))
  415. else if C=TPasSpecializeType then
  416. CheckRestoredSpecializedType(Path,TPasSpecializeType(Orig),TPasSpecializeType(Rest))
  417. else if C=TInlineSpecializeExpr then
  418. CheckRestoredInlineSpecializedExpr(Path,TInlineSpecializeExpr(Orig),TInlineSpecializeExpr(Rest))
  419. else if C=TPasRangeType then
  420. CheckRestoredRangeType(Path,TPasRangeType(Orig),TPasRangeType(Rest))
  421. else if C=TPasArrayType then
  422. CheckRestoredArrayType(Path,TPasArrayType(Orig),TPasArrayType(Rest))
  423. else if C=TPasFileType then
  424. CheckRestoredFileType(Path,TPasFileType(Orig),TPasFileType(Rest))
  425. else if C=TPasEnumValue then
  426. CheckRestoredEnumValue(Path,TPasEnumValue(Orig),TPasEnumValue(Rest))
  427. else if C=TPasEnumType then
  428. CheckRestoredEnumType(Path,TPasEnumType(Orig),TPasEnumType(Rest))
  429. else if C=TPasSetType then
  430. CheckRestoredSetType(Path,TPasSetType(Orig),TPasSetType(Rest))
  431. else if C=TPasVariant then
  432. CheckRestoredVariant(Path,TPasVariant(Orig),TPasVariant(Rest))
  433. else if C=TPasRecordType then
  434. CheckRestoredRecordType(Path,TPasRecordType(Orig),TPasRecordType(Rest))
  435. else if C=TPasClassType then
  436. CheckRestoredClassType(Path,TPasClassType(Orig),TPasClassType(Rest))
  437. else if C=TPasArgument then
  438. CheckRestoredArgument(Path,TPasArgument(Orig),TPasArgument(Rest))
  439. else if C=TPasProcedureType then
  440. CheckRestoredProcedureType(Path,TPasProcedureType(Orig),TPasProcedureType(Rest))
  441. else if C=TPasResultElement then
  442. CheckRestoredResultElement(Path,TPasResultElement(Orig),TPasResultElement(Rest))
  443. else if C=TPasFunctionType then
  444. CheckRestoredFunctionType(Path,TPasFunctionType(Orig),TPasFunctionType(Rest))
  445. else if C=TPasStringType then
  446. CheckRestoredStringType(Path,TPasStringType(Orig),TPasStringType(Rest))
  447. else if C=TPasVariable then
  448. CheckRestoredVariable(Path,TPasVariable(Orig),TPasVariable(Rest))
  449. else if C=TPasExportSymbol then
  450. CheckRestoredExportSymbol(Path,TPasExportSymbol(Orig),TPasExportSymbol(Rest))
  451. else if C=TPasConst then
  452. CheckRestoredConst(Path,TPasConst(Orig),TPasConst(Rest))
  453. else if C=TPasProperty then
  454. CheckRestoredProperty(Path,TPasProperty(Orig),TPasProperty(Rest))
  455. else if (C=TPasProcedure)
  456. or (C=TPasFunction)
  457. or (C=TPasConstructor)
  458. or (C=TPasClassConstructor)
  459. or (C=TPasDestructor)
  460. or (C=TPasClassDestructor)
  461. or (C=TPasClassProcedure)
  462. or (C=TPasClassFunction)
  463. then
  464. CheckRestoredProcedure(Path,TPasProcedure(Orig),TPasProcedure(Rest))
  465. else if (C=TPasOperator)
  466. or (C=TPasClassOperator) then
  467. CheckRestoredOperator(Path,TPasOperator(Orig),TPasOperator(Rest))
  468. else if (C=TPasModule)
  469. or (C=TPasProgram)
  470. or (C=TPasLibrary) then
  471. CheckRestoredModule(Path,TPasModule(Orig),TPasModule(Rest))
  472. else if C.InheritsFrom(TPasSection) then
  473. CheckRestoredSection(Path,TPasSection(Orig),TPasSection(Rest))
  474. else
  475. Fail(Path+': unknown class '+C.ClassName);
  476. end;
  477. procedure TCustomTestPrecompile.CheckRestoredElementList(const Path: string;
  478. Orig, Rest: TFPList);
  479. var
  480. OrigItem, RestItem: TObject;
  481. i: Integer;
  482. SubPath: String;
  483. begin
  484. if Orig=nil then
  485. begin
  486. if Rest=nil then
  487. exit;
  488. Fail(Path+' Orig=nil Rest='+GetObjName(Rest));
  489. end
  490. else if Rest=nil then
  491. Fail(Path+' Orig='+GetObjName(Orig)+' Rest=nil')
  492. else if Orig.ClassType<>Rest.ClassType then
  493. Fail(Path+' Orig='+GetObjName(Orig)+' Rest='+GetObjName(Rest));
  494. AssertEquals(Path+'.Count',Orig.Count,Rest.Count);
  495. for i:=0 to Orig.Count-1 do
  496. begin
  497. SubPath:=Path+'['+IntToStr(i)+']';
  498. OrigItem:=TObject(Orig[i]);
  499. if not (OrigItem is TPasElement) then
  500. Fail(SubPath+' Orig='+GetObjName(OrigItem));
  501. RestItem:=TObject(Rest[i]);
  502. if not (RestItem is TPasElement) then
  503. Fail(SubPath+' Rest='+GetObjName(RestItem));
  504. CheckRestoredElement(SubPath,TPasElement(OrigItem),TPasElement(RestItem));
  505. end;
  506. end;
  507. procedure TCustomTestPrecompile.CheckRestoredPasExpr(const Path: string; Orig,
  508. Rest: TPasExpr);
  509. begin
  510. if Orig.Kind<>Rest.Kind then
  511. Fail(Path+'.Kind');
  512. if Orig.OpCode<>Rest.OpCode then
  513. Fail(Path+'.OpCode');
  514. CheckRestoredElement(Path+'.Format1',Orig.format1,Rest.format1);
  515. CheckRestoredElement(Path+'.Format2',Orig.format2,Rest.format2);
  516. end;
  517. procedure TCustomTestPrecompile.CheckRestoredUnaryExpr(const Path: string;
  518. Orig, Rest: TUnaryExpr);
  519. begin
  520. CheckRestoredElement(Path+'.Operand',Orig.Operand,Rest.Operand);
  521. CheckRestoredPasExpr(Path,Orig,Rest);
  522. end;
  523. procedure TCustomTestPrecompile.CheckRestoredBinaryExpr(const Path: string;
  524. Orig, Rest: TBinaryExpr);
  525. begin
  526. CheckRestoredElement(Path+'.left',Orig.left,Rest.left);
  527. CheckRestoredElement(Path+'.right',Orig.right,Rest.right);
  528. CheckRestoredPasExpr(Path,Orig,Rest);
  529. end;
  530. procedure TCustomTestPrecompile.CheckRestoredPrimitiveExpr(const Path: string;
  531. Orig, Rest: TPrimitiveExpr);
  532. begin
  533. AssertEquals(Path+'.Value',Orig.Value,Rest.Value);
  534. CheckRestoredPasExpr(Path,Orig,Rest);
  535. end;
  536. procedure TCustomTestPrecompile.CheckRestoredBoolConstExpr(const Path: string;
  537. Orig, Rest: TBoolConstExpr);
  538. begin
  539. AssertEquals(Path+'.Value',Orig.Value,Rest.Value);
  540. CheckRestoredPasExpr(Path,Orig,Rest);
  541. end;
  542. procedure TCustomTestPrecompile.CheckRestoredParamsExpr(const Path: string;
  543. Orig, Rest: TParamsExpr);
  544. begin
  545. CheckRestoredElement(Path+'.Value',Orig.Value,Rest.Value);
  546. CheckRestoredPasExprArray(Path+'.Params',Orig.Params,Rest.Params);
  547. CheckRestoredPasExpr(Path,Orig,Rest);
  548. end;
  549. procedure TCustomTestPrecompile.CheckRestoredRecordValues(const Path: string;
  550. Orig, Rest: TRecordValues);
  551. var
  552. i: Integer;
  553. begin
  554. AssertEquals(Path+'.Fields.length',length(Orig.Fields),length(Rest.Fields));
  555. for i:=0 to length(Orig.Fields)-1 do
  556. begin
  557. AssertEquals(Path+'.Field['+IntToStr(i)+'].Name',Orig.Fields[i].Name,Rest.Fields[i].Name);
  558. CheckRestoredElement(Path+'.Field['+IntToStr(i)+'].ValueExp',Orig.Fields[i].ValueExp,Rest.Fields[i].ValueExp);
  559. end;
  560. CheckRestoredPasExpr(Path,Orig,Rest);
  561. end;
  562. procedure TCustomTestPrecompile.CheckRestoredPasExprArray(const Path: string;
  563. Orig, Rest: TPasExprArray);
  564. var
  565. i: Integer;
  566. begin
  567. AssertEquals(Path+'.length',length(Orig),length(Rest));
  568. for i:=0 to length(Orig)-1 do
  569. CheckRestoredElement(Path+'['+IntToStr(i)+']',Orig[i],Rest[i]);
  570. end;
  571. procedure TCustomTestPrecompile.CheckRestoredArrayValues(const Path: string;
  572. Orig, Rest: TArrayValues);
  573. begin
  574. CheckRestoredPasExprArray(Path+'.Values',Orig.Values,Rest.Values);
  575. CheckRestoredPasExpr(Path,Orig,Rest);
  576. end;
  577. procedure TCustomTestPrecompile.CheckRestoredResString(const Path: string;
  578. Orig, Rest: TPasResString);
  579. begin
  580. CheckRestoredElement(Path+'.Expr',Orig.Expr,Rest.Expr);
  581. end;
  582. procedure TCustomTestPrecompile.CheckRestoredAliasType(const Path: string;
  583. Orig, Rest: TPasAliasType);
  584. begin
  585. CheckRestoredElement(Path+'.DestType',Orig.DestType,Rest.DestType);
  586. CheckRestoredElement(Path+'.Expr',Orig.Expr,Rest.Expr);
  587. end;
  588. procedure TCustomTestPrecompile.CheckRestoredPointerType(const Path: string;
  589. Orig, Rest: TPasPointerType);
  590. begin
  591. CheckRestoredElement(Path+'.DestType',Orig.DestType,Rest.DestType);
  592. end;
  593. procedure TCustomTestPrecompile.CheckRestoredSpecializedType(
  594. const Path: string; Orig, Rest: TPasSpecializeType);
  595. begin
  596. CheckRestoredElementList(Path+'.Params',Orig.Params,Rest.Params);
  597. CheckRestoredElement(Path+'.DestType',Orig.DestType,Rest.DestType);
  598. end;
  599. procedure TCustomTestPrecompile.CheckRestoredInlineSpecializedExpr(
  600. const Path: string; Orig, Rest: TInlineSpecializeExpr);
  601. begin
  602. CheckRestoredElement(Path+'.DestType',Orig.DestType,Rest.DestType);
  603. end;
  604. procedure TCustomTestPrecompile.CheckRestoredRangeType(const Path: string;
  605. Orig, Rest: TPasRangeType);
  606. begin
  607. CheckRestoredElement(Path+'.RangeExpr',Orig.RangeExpr,Rest.RangeExpr);
  608. end;
  609. procedure TCustomTestPrecompile.CheckRestoredArrayType(const Path: string;
  610. Orig, Rest: TPasArrayType);
  611. begin
  612. CheckRestoredPasExprArray(Path+'.Ranges',Orig.Ranges,Rest.Ranges);
  613. if Orig.PackMode<>Rest.PackMode then
  614. Fail(Path+'.PackMode Orig='+PJUPackModeNames[Orig.PackMode]+' Rest='+PJUPackModeNames[Rest.PackMode]);
  615. CheckRestoredElement(Path+'.ElType',Orig.ElType,Rest.ElType);
  616. end;
  617. procedure TCustomTestPrecompile.CheckRestoredFileType(const Path: string; Orig,
  618. Rest: TPasFileType);
  619. begin
  620. CheckRestoredElement(Path+'.ElType',Orig.ElType,Rest.ElType);
  621. end;
  622. procedure TCustomTestPrecompile.CheckRestoredEnumValue(const Path: string;
  623. Orig, Rest: TPasEnumValue);
  624. begin
  625. CheckRestoredElement(Path+'.Value',Orig.Value,Rest.Value);
  626. end;
  627. procedure TCustomTestPrecompile.CheckRestoredEnumType(const Path: string; Orig,
  628. Rest: TPasEnumType);
  629. begin
  630. CheckRestoredElementList(Path+'.Values',Orig.Values,Rest.Values);
  631. end;
  632. procedure TCustomTestPrecompile.CheckRestoredSetType(const Path: string; Orig,
  633. Rest: TPasSetType);
  634. begin
  635. CheckRestoredElement(Path+'.EnumType',Orig.EnumType,Rest.EnumType);
  636. AssertEquals(Path+'.IsPacked',Orig.IsPacked,Rest.IsPacked);
  637. end;
  638. procedure TCustomTestPrecompile.CheckRestoredVariant(const Path: string; Orig,
  639. Rest: TPasVariant);
  640. begin
  641. CheckRestoredElementList(Path+'.Values',Orig.Values,Rest.Values);
  642. CheckRestoredElement(Path+'.Members',Orig.Members,Rest.Members);
  643. end;
  644. procedure TCustomTestPrecompile.CheckRestoredRecordType(const Path: string;
  645. Orig, Rest: TPasRecordType);
  646. begin
  647. if Orig.PackMode<>Rest.PackMode then
  648. Fail(Path+'.PackMode Orig='+PJUPackModeNames[Orig.PackMode]+' Rest='+PJUPackModeNames[Rest.PackMode]);
  649. CheckRestoredElementList(Path+'.Members',Orig.Members,Rest.Members);
  650. CheckRestoredElement(Path+'.VariantEl',Orig.VariantEl,Rest.VariantEl);
  651. CheckRestoredElementList(Path+'.Variants',Orig.Variants,Rest.Variants);
  652. CheckRestoredElementList(Path+'.GenericTemplateTypes',Orig.GenericTemplateTypes,Rest.GenericTemplateTypes);
  653. end;
  654. procedure TCustomTestPrecompile.CheckRestoredClassType(const Path: string;
  655. Orig, Rest: TPasClassType);
  656. begin
  657. if Orig.PackMode<>Rest.PackMode then
  658. Fail(Path+'.PackMode Orig='+PJUPackModeNames[Orig.PackMode]+' Rest='+PJUPackModeNames[Rest.PackMode]);
  659. if Orig.ObjKind<>Rest.ObjKind then
  660. Fail(Path+'.ObjKind Orig='+PJUObjKindNames[Orig.ObjKind]+' Rest='+PJUObjKindNames[Rest.ObjKind]);
  661. CheckRestoredElement(Path+'.AncestorType',Orig.AncestorType,Rest.AncestorType);
  662. CheckRestoredElement(Path+'.HelperForType',Orig.HelperForType,Rest.HelperForType);
  663. AssertEquals(Path+'.IsForward',Orig.IsForward,Rest.IsForward);
  664. AssertEquals(Path+'.IsExternal',Orig.IsExternal,Rest.IsExternal);
  665. // irrelevant: IsShortDefinition
  666. CheckRestoredElement(Path+'.GUIDExpr',Orig.GUIDExpr,Rest.GUIDExpr);
  667. CheckRestoredElementList(Path+'.Members',Orig.Members,Rest.Members);
  668. AssertEquals(Path+'.Modifiers',Orig.Modifiers.Text,Rest.Modifiers.Text);
  669. CheckRestoredElementList(Path+'.Interfaces',Orig.Interfaces,Rest.Interfaces);
  670. CheckRestoredElementList(Path+'.GenericTemplateTypes',Orig.GenericTemplateTypes,Rest.GenericTemplateTypes);
  671. AssertEquals(Path+'.ExternalNameSpace',Orig.ExternalNameSpace,Rest.ExternalNameSpace);
  672. AssertEquals(Path+'.ExternalName',Orig.ExternalName,Rest.ExternalName);
  673. end;
  674. procedure TCustomTestPrecompile.CheckRestoredArgument(const Path: string; Orig,
  675. Rest: TPasArgument);
  676. begin
  677. if Orig.Access<>Rest.Access then
  678. Fail(Path+'.Access Orig='+PJUArgumentAccessNames[Orig.Access]+' Rest='+PJUArgumentAccessNames[Rest.Access]);
  679. CheckRestoredElement(Path+'.ArgType',Orig.ArgType,Rest.ArgType);
  680. CheckRestoredElement(Path+'.ValueExpr',Orig.ValueExpr,Rest.ValueExpr);
  681. end;
  682. procedure TCustomTestPrecompile.CheckRestoredProcedureType(const Path: string;
  683. Orig, Rest: TPasProcedureType);
  684. begin
  685. CheckRestoredElementList(Path+'.Args',Orig.Args,Rest.Args);
  686. if Orig.CallingConvention<>Rest.CallingConvention then
  687. Fail(Path+'.CallingConvention Orig='+PJUCallingConventionNames[Orig.CallingConvention]+' Rest='+PJUCallingConventionNames[Rest.CallingConvention]);
  688. if Orig.Modifiers<>Rest.Modifiers then
  689. Fail(Path+'.Modifiers');
  690. end;
  691. procedure TCustomTestPrecompile.CheckRestoredResultElement(const Path: string;
  692. Orig, Rest: TPasResultElement);
  693. begin
  694. CheckRestoredElement(Path+'.ResultType',Orig.ResultType,Rest.ResultType);
  695. end;
  696. procedure TCustomTestPrecompile.CheckRestoredFunctionType(const Path: string;
  697. Orig, Rest: TPasFunctionType);
  698. begin
  699. CheckRestoredElement(Path+'.ResultEl',Orig.ResultEl,Rest.ResultEl);
  700. CheckRestoredProcedureType(Path,Orig,Rest);
  701. end;
  702. procedure TCustomTestPrecompile.CheckRestoredStringType(const Path: string;
  703. Orig, Rest: TPasStringType);
  704. begin
  705. AssertEquals(Path+'.LengthExpr',Orig.LengthExpr,Rest.LengthExpr);
  706. end;
  707. procedure TCustomTestPrecompile.CheckRestoredVariable(const Path: string; Orig,
  708. Rest: TPasVariable);
  709. begin
  710. CheckRestoredElement(Path+'.VarType',Orig.VarType,Rest.VarType);
  711. if Orig.VarModifiers<>Rest.VarModifiers then
  712. Fail(Path+'.VarModifiers');
  713. CheckRestoredElement(Path+'.LibraryName',Orig.LibraryName,Rest.LibraryName);
  714. CheckRestoredElement(Path+'.ExportName',Orig.ExportName,Rest.ExportName);
  715. CheckRestoredElement(Path+'.AbsoluteExpr',Orig.AbsoluteExpr,Rest.AbsoluteExpr);
  716. CheckRestoredElement(Path+'.Expr',Orig.Expr,Rest.Expr);
  717. end;
  718. procedure TCustomTestPrecompile.CheckRestoredExportSymbol(const Path: string;
  719. Orig, Rest: TPasExportSymbol);
  720. begin
  721. CheckRestoredElement(Path+'.ExportName',Orig.ExportName,Rest.ExportName);
  722. CheckRestoredElement(Path+'.ExportIndex',Orig.ExportIndex,Rest.ExportIndex);
  723. end;
  724. procedure TCustomTestPrecompile.CheckRestoredConst(const Path: string; Orig,
  725. Rest: TPasConst);
  726. begin
  727. AssertEquals(Path+': IsConst',Orig.IsConst,Rest.IsConst);
  728. CheckRestoredVariable(Path,Orig,Rest);
  729. end;
  730. procedure TCustomTestPrecompile.CheckRestoredProperty(const Path: string; Orig,
  731. Rest: TPasProperty);
  732. begin
  733. CheckRestoredElement(Path+'.IndexExpr',Orig.IndexExpr,Rest.IndexExpr);
  734. CheckRestoredElement(Path+'.ReadAccessor',Orig.ReadAccessor,Rest.ReadAccessor);
  735. CheckRestoredElement(Path+'.WriteAccessor',Orig.WriteAccessor,Rest.WriteAccessor);
  736. CheckRestoredElement(Path+'.ImplementsFunc',Orig.ImplementsFunc,Rest.ImplementsFunc);
  737. CheckRestoredElement(Path+'.DispIDExpr',Orig.DispIDExpr,Rest.DispIDExpr);
  738. CheckRestoredElement(Path+'.StoredAccessor',Orig.StoredAccessor,Rest.StoredAccessor);
  739. CheckRestoredElement(Path+'.DefaultExpr',Orig.DefaultExpr,Rest.DefaultExpr);
  740. CheckRestoredElementList(Path+'.Args',Orig.Args,Rest.Args);
  741. // not needed: ReadAccessorName, WriteAccessorName, ImplementsName, StoredAccessorName
  742. AssertEquals(Path+'.DispIDReadOnly',Orig.DispIDReadOnly,Rest.DispIDReadOnly);
  743. AssertEquals(Path+'.IsDefault',Orig.IsDefault,Rest.IsDefault);
  744. AssertEquals(Path+'.IsNodefault',Orig.IsNodefault,Rest.IsNodefault);
  745. CheckRestoredVariable(Path,Orig,Rest);
  746. end;
  747. procedure TCustomTestPrecompile.CheckRestoredProcedure(const Path: string;
  748. Orig, Rest: TPasProcedure);
  749. begin
  750. CheckRestoredElement(Path+'.ProcType',Orig.ProcType,Rest.ProcType);
  751. CheckRestoredElement(Path+'.PublicName',Orig.PublicName,Rest.PublicName);
  752. CheckRestoredElement(Path+'.LibrarySymbolName',Orig.LibrarySymbolName,Rest.LibrarySymbolName);
  753. CheckRestoredElement(Path+'.LibraryExpr',Orig.LibraryExpr,Rest.LibraryExpr);
  754. CheckRestoredElement(Path+'.DispIDExpr',Orig.DispIDExpr,Rest.DispIDExpr);
  755. AssertEquals(Path+'.AliasName',Orig.AliasName,Rest.AliasName);
  756. if Orig.Modifiers<>Rest.Modifiers then
  757. Fail(Path+'.Modifiers');
  758. AssertEquals(Path+'.MessageName',Orig.MessageName,Rest.MessageName);
  759. if Orig.MessageType<>Rest.MessageType then
  760. Fail(Path+'.MessageType Orig='+PJUProcedureMessageTypeNames[Orig.MessageType]+' Rest='+PJUProcedureMessageTypeNames[Rest.MessageType]);
  761. // ToDo: Body
  762. end;
  763. procedure TCustomTestPrecompile.CheckRestoredOperator(const Path: string; Orig,
  764. Rest: TPasOperator);
  765. begin
  766. if Orig.OperatorType<>Rest.OperatorType then
  767. Fail(Path+'.OperatorType Orig='+PJUOperatorTypeNames[Orig.OperatorType]+' Rest='+PJUOperatorTypeNames[Rest.OperatorType]);
  768. AssertEquals(Path+'.TokenBased',Orig.TokenBased,Rest.TokenBased);
  769. CheckRestoredProcedure(Path,Orig,Rest);
  770. end;
  771. procedure TCustomTestPrecompile.CheckRestoredReference(const Path: string;
  772. Orig, Rest: TPasElement);
  773. begin
  774. if Orig=nil then
  775. begin
  776. if Rest<>nil then
  777. Fail(Path+': Orig=nil Rest='+GetObjName(Rest));
  778. exit;
  779. end
  780. else if Rest=nil then
  781. Fail(Path+': Orig='+GetObjName(Orig)+' Rest=nil');
  782. if Orig.ClassType<>Rest.ClassType then
  783. Fail(Path+': Orig='+GetObjName(Orig)+' Rest='+GetObjName(Rest));
  784. AssertEquals(Path+': Name',Orig.Name,Rest.Name);
  785. if Orig is TPasUnresolvedSymbolRef then
  786. exit; // compiler types and procs are the same in every unit -> skip checking unit
  787. CheckRestoredReference(Path+'.Parent',Orig.Parent,Rest.Parent);
  788. end;
  789. { TTestPrecompile }
  790. procedure TTestPrecompile.Test_Base256VLQ;
  791. procedure Test(i: MaxPrecInt);
  792. var
  793. s: String;
  794. p: PByte;
  795. j: NativeInt;
  796. begin
  797. s:=EncodeVLQ(i);
  798. p:=PByte(s);
  799. j:=DecodeVLQ(p);
  800. if i<>j then
  801. Fail('Encode/DecodeVLQ OrigIndex='+IntToStr(i)+' Code="'+s+'" NewIndex='+IntToStr(j));
  802. end;
  803. procedure TestStr(i: MaxPrecInt; Expected: string);
  804. var
  805. Actual: String;
  806. begin
  807. Actual:=EncodeVLQ(i);
  808. AssertEquals('EncodeVLQ('+IntToStr(i)+')',Expected,Actual);
  809. end;
  810. var
  811. i: Integer;
  812. begin
  813. TestStr(0,#0);
  814. TestStr(1,#2);
  815. TestStr(-1,#3);
  816. for i:=-8200 to 8200 do
  817. Test(i);
  818. Test(High(MaxPrecInt));
  819. Test(High(MaxPrecInt)-1);
  820. Test(Low(MaxPrecInt)+2);
  821. Test(Low(MaxPrecInt)+1);
  822. //Test(Low(MaxPrecInt)); such a high number is not needed by pastojs
  823. end;
  824. procedure TTestPrecompile.TestPC_EmptyUnit;
  825. begin
  826. StartUnit(false);
  827. Add([
  828. 'interface',
  829. 'implementation']);
  830. WriteReadUnit;
  831. end;
  832. procedure TTestPrecompile.TestPC_Const;
  833. begin
  834. StartUnit(false);
  835. Add([
  836. 'interface',
  837. 'const c = 3;',
  838. 'implementation']);
  839. WriteReadUnit;
  840. end;
  841. Initialization
  842. RegisterTests([TTestPrecompile]);
  843. end.