tcprecompile.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter 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=TTestCLI_Precompile
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCPrecompile;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils,
  20. fpcunit, testregistry, Pas2jsFileUtils, Pas2JsFiler, Pas2jsCompiler,
  21. TCUnitSearch, TCModules;
  22. type
  23. { TCustomTestCLI_Precompile }
  24. TCustomTestCLI_Precompile = class(TCustomTestCLI)
  25. private
  26. FPCUFormat: TPas2JSPrecompileFormat;
  27. FUnitOutputDir: string;
  28. protected
  29. procedure SetUp; override;
  30. procedure CheckPrecompile(MainFile, UnitPaths: string;
  31. SharedParams: TStringList = nil;
  32. FirstRunParams: TStringList = nil;
  33. SecondRunParams: TStringList = nil; ExpExitCode: integer = 0);
  34. function GetJSFilename(ModuleName: string): string; virtual;
  35. public
  36. constructor Create; override;
  37. property PCUFormat: TPas2JSPrecompileFormat read FPCUFormat write FPCUFormat;
  38. property UnitOutputDir: string read FUnitOutputDir write FUnitOutputDir;
  39. end;
  40. { TTestCLI_Precompile }
  41. TTestCLI_Precompile = class(TCustomTestCLI_Precompile)
  42. published
  43. procedure TestPCU_EmptyUnit;
  44. procedure TestPCU_UTF8BOM;
  45. procedure TestPCU_ParamNS;
  46. procedure TestPCU_Overloads;
  47. procedure TestPCU_Overloads_MDelphi_ModeObjFPC;
  48. procedure TestPCU_UnitCycle;
  49. procedure TestPCU_Class_Forward;
  50. procedure TestPCU_Class_Constructor;
  51. procedure TestPCU_Class_ClassConstructor;
  52. procedure TestPCU_ClassInterface;
  53. procedure TestPCU_Namespace;
  54. procedure TestPCU_CheckVersionMain;
  55. procedure TestPCU_CheckVersionMain2;
  56. procedure TestPCU_CheckVersionSystem;
  57. end;
  58. function LinesToList(const Lines: array of string): TStringList;
  59. implementation
  60. function LinesToList(const Lines: array of string): TStringList;
  61. var
  62. i: Integer;
  63. begin
  64. Result:=TStringList.Create;
  65. for i:=Low(Lines) to High(Lines) do Result.Add(Lines[i]);
  66. end;
  67. { TCustomTestCLI_Precompile }
  68. procedure TCustomTestCLI_Precompile.SetUp;
  69. begin
  70. inherited SetUp;
  71. UnitOutputDir:='units';
  72. end;
  73. procedure TCustomTestCLI_Precompile.CheckPrecompile(MainFile,
  74. UnitPaths: string; SharedParams: TStringList; FirstRunParams: TStringList;
  75. SecondRunParams: TStringList; ExpExitCode: integer);
  76. var
  77. JSFilename, OrigSrc, NewSrc, s: String;
  78. JSFile: TCLIFile;
  79. begin
  80. try
  81. AddDir(UnitOutputDir);
  82. // compile, create .pcu files
  83. {$IFDEF VerbosePCUFiler}
  84. writeln('TTestCLI_Precompile.CheckPrecompile create pcu files=========================');
  85. {$ENDIF}
  86. Params.Clear;
  87. Params.Add('-Jminclude');
  88. Params.Add('-Jc');
  89. if SharedParams<>nil then
  90. Params.AddStrings(SharedParams);
  91. if FirstRunParams<>nil then
  92. Params.AddStrings(FirstRunParams);
  93. Compile([MainFile,'-Fu'+UnitPaths,'-JU'+PCUFormat.Ext,'-FU'+UnitOutputDir]);
  94. AssertFileExists(UnitOutputDir+'/system.'+PCUFormat.Ext);
  95. JSFilename:=GetJSFilename(MainFile);
  96. AssertFileExists(JSFilename);
  97. JSFile:=FindFile(JSFilename);
  98. OrigSrc:=JSFile.Source;
  99. // compile, using .pcu files
  100. //for i:=0 to FileCount-1 do
  101. // writeln('TCustomTestCLI_Precompile.CheckPrecompile ',i,' ',Files[i].Filename);
  102. {$IFDEF VerbosePCUFiler}
  103. writeln('TTestCLI_Precompile.CheckPrecompile compile using pcu files==================');
  104. {$ENDIF}
  105. JSFile.Source:='';
  106. Compiler.Reset;
  107. Params.Clear;
  108. Params.Add('-Jminclude');
  109. Params.Add('-Jc');
  110. if SharedParams<>nil then
  111. Params.AddStrings(SharedParams);
  112. if SecondRunParams<>nil then
  113. Params.AddStrings(SecondRunParams);
  114. Compile([MainFile,'-FU'+UnitOutputDir],ExpExitCode);
  115. if ExpExitCode=0 then
  116. begin
  117. NewSrc:=JSFile.Source;
  118. if not CheckSrcDiff(OrigSrc,NewSrc,s) then
  119. begin
  120. WriteSources;
  121. Fail('test1.js: '+s);
  122. end;
  123. end;
  124. finally
  125. SharedParams.Free;
  126. FirstRunParams.Free;
  127. SecondRunParams.Free;
  128. end;
  129. end;
  130. function TCustomTestCLI_Precompile.GetJSFilename(ModuleName: string): string;
  131. begin
  132. Result:=UnitOutputDir+PathDelim+ExtractFilenameOnly(ModuleName)+'.js';
  133. end;
  134. constructor TCustomTestCLI_Precompile.Create;
  135. begin
  136. inherited Create;
  137. FPCUFormat:=PrecompileFormats[0];
  138. end;
  139. { TTestCLI_Precompile }
  140. procedure TTestCLI_Precompile.TestPCU_EmptyUnit;
  141. begin
  142. AddUnit('src/system.pp',[''],['']);
  143. AddFile('test1.pas',[
  144. 'begin',
  145. 'end.']);
  146. CheckPrecompile('test1.pas','src');
  147. end;
  148. procedure TTestCLI_Precompile.TestPCU_UTF8BOM;
  149. var
  150. aFile: TCLIFile;
  151. begin
  152. aFile:=AddUnit('src/system.pp',
  153. ['var',
  154. ' s: string = ''aaaäö'';',
  155. ' s2: string = ''😊'';', // 1F60A
  156. ''],
  157. ['']);
  158. aFile.Source:=UTF8BOM+aFile.Source;
  159. aFile:=AddFile('test1.pas',[
  160. 'begin',
  161. ' s:=''ö😊'';',
  162. 'end.']);
  163. aFile.Source:=UTF8BOM+aFile.Source;
  164. CheckPrecompile('test1.pas','src');
  165. end;
  166. procedure TTestCLI_Precompile.TestPCU_ParamNS;
  167. begin
  168. AddUnit('src/system.pp',[''],['']);
  169. AddUnit('src/foo.unit1.pp',['var i: longint;'],['']);
  170. AddFile('test1.pas',[
  171. 'uses unit1;',
  172. 'begin',
  173. ' i:=3;',
  174. 'end.']);
  175. CheckPrecompile('test1.pas','src',LinesToList(['-FNfoo']));
  176. end;
  177. procedure TTestCLI_Precompile.TestPCU_Overloads;
  178. begin
  179. AddUnit('src/system.pp',['type integer = longint;'],['']);
  180. AddUnit('src/unit1.pp',
  181. ['var i: integer;',
  182. 'procedure DoIt(j: integer); overload;',
  183. 'procedure DoIt(b: boolean);'],
  184. ['procedure DoIt(j: integer);',
  185. 'begin',
  186. ' i:=j;',
  187. 'end;',
  188. 'procedure DoIt(b: boolean);',
  189. 'begin',
  190. ' i:=3;',
  191. 'end;']);
  192. AddUnit('src/unit2.pp',
  193. ['uses unit1;',
  194. 'procedure DoIt(s: string); overload;'],
  195. ['procedure DoIt(s: string);',
  196. 'begin',
  197. ' unit1.i:=length(s);',
  198. 'end;']);
  199. AddFile('test1.pas',[
  200. 'uses unit1, unit2;',
  201. 'procedure DoIt(d: double); overload;',
  202. 'begin',
  203. ' unit1.i:=4;',
  204. 'end;',
  205. 'begin',
  206. ' DoIt(3);',
  207. ' DoIt(''abc'');',
  208. ' DoIt(true);',
  209. ' DoIt(3.3);',
  210. 'end.']);
  211. CheckPrecompile('test1.pas','src');
  212. end;
  213. procedure TTestCLI_Precompile.TestPCU_Overloads_MDelphi_ModeObjFPC;
  214. var
  215. SharedParams: TStringList;
  216. begin
  217. AddUnit('src/system.pp',[
  218. 'type',
  219. ' integer = longint;',
  220. ' TDateTime = type double;'],
  221. ['']);
  222. AddFile('src/unit1.pp',
  223. LinesToStr([
  224. 'unit unit1;',
  225. '{$mode objfpc}',
  226. 'interface',
  227. 'function DoIt(i: integer): TDateTime;', // no overload needed in ObjFPC
  228. 'function DoIt(i, j: integer): TDateTime;',
  229. 'implementation',
  230. 'function DoIt(i: integer): TDateTime;',
  231. 'begin',
  232. ' Result:=i;',
  233. 'end;',
  234. 'function DoIt(i, j: integer): TDateTime;',
  235. 'begin',
  236. ' Result:=i+j;',
  237. 'end;',
  238. 'end.']));
  239. AddFile('test1.pas',[
  240. 'uses unit1;',
  241. 'var d: TDateTime;',
  242. 'begin',
  243. ' d:=DoIt(3);',
  244. ' d:=DoIt(4,5);',
  245. 'end.']);
  246. SharedParams:=TStringList.Create;
  247. SharedParams.Add('-MDelphi');
  248. CheckPrecompile('test1.pas','src',SharedParams);
  249. end;
  250. procedure TTestCLI_Precompile.TestPCU_UnitCycle;
  251. begin
  252. AddUnit('src/system.pp',['type integer = longint;'],['']);
  253. AddUnit('src/unit1.pp',
  254. ['var i: integer;',
  255. 'procedure Do1(j: integer);'],
  256. ['uses unit2;',
  257. 'procedure Do1(j: integer);',
  258. 'begin',
  259. ' Do2(j);',
  260. 'end;']);
  261. AddUnit('src/unit2.pp',
  262. ['uses unit1;',
  263. 'procedure Do2(j: integer);'],
  264. ['procedure Do2(j: integer);',
  265. 'begin',
  266. ' unit1.i:=j;',
  267. 'end;']);
  268. AddFile('test1.pas',[
  269. 'uses unit1;',
  270. 'begin',
  271. ' Do1(3);',
  272. 'end.']);
  273. CheckPrecompile('test1.pas','src');
  274. end;
  275. procedure TTestCLI_Precompile.TestPCU_Class_Forward;
  276. begin
  277. AddUnit('src/system.pp',[
  278. 'type integer = longint;',
  279. 'procedure Writeln; varargs;'],
  280. ['procedure Writeln; begin end;']);
  281. AddUnit('src/unit1.pp',
  282. ['type',
  283. ' TClass = class of TObject;',
  284. ' TBirdClass = class of TBird;',
  285. ' TObject = class',
  286. ' FBirdClass: TBirdClass;',
  287. ' constructor Create;',
  288. ' constructor Create(Id: integer);',
  289. ' property BirdClass: TBirdClass read FBirdClass;',
  290. ' end;',
  291. ' TBird = class',
  292. ' constructor Create(d: double); overload;',
  293. ' end;',
  294. ''],
  295. ['constructor TObject.Create; begin end;',
  296. 'constructor TObject.Create(Id: integer); begin end;',
  297. 'constructor TBird.Create(d: double); begin end;']);
  298. AddFile('test1.pas',[
  299. 'uses unit1;',
  300. 'var',
  301. ' b: TBird;',
  302. ' c: TClass;',
  303. 'begin',
  304. ' c:=TObject;',
  305. ' c:=TBird;',
  306. ' c:=b.BirdClass;',
  307. ' b:=TBird.Create;',
  308. ' b:=TBird.Create(1);',
  309. ' b:=TBird.Create(3.3);',
  310. 'end.']);
  311. CheckPrecompile('test1.pas','src');
  312. end;
  313. procedure TTestCLI_Precompile.TestPCU_Class_Constructor;
  314. begin
  315. AddUnit('src/system.pp',[
  316. 'type integer = longint;',
  317. 'procedure Writeln; varargs;'],
  318. ['procedure Writeln; begin end;']);
  319. AddUnit('src/unit1.pp',[
  320. 'type',
  321. ' TObject = class',
  322. ' constructor Create;',
  323. ' end;',
  324. ' TBird = class',
  325. ' constructor Create; reintroduce;',
  326. ' end;',
  327. ' TCow = class',
  328. ' constructor Create; reintroduce;',
  329. ' end;',
  330. ''],[
  331. 'constructor TObject.Create; begin end;',
  332. 'constructor TBird.Create; begin end;',
  333. 'constructor TCow.Create; begin end;',
  334. '']);
  335. AddUnit('src/unit2.pp',[
  336. 'uses unit1;',
  337. 'procedure DoIt;',
  338. ''],[
  339. 'procedure DoIt;',
  340. 'begin',
  341. ' TBird.Create;',
  342. ' TCow.Create;',
  343. 'end;',
  344. '']);
  345. AddFile('test1.pas',[
  346. 'uses unit2;',
  347. 'begin',
  348. ' DoIt;',
  349. 'end.']);
  350. CheckPrecompile('test1.pas','src');
  351. end;
  352. procedure TTestCLI_Precompile.TestPCU_Class_ClassConstructor;
  353. begin
  354. AddUnit('src/system.pp',[
  355. 'type integer = longint;',
  356. 'procedure Writeln; varargs;'],
  357. ['procedure Writeln; begin end;']);
  358. AddUnit('src/unit1.pp',[
  359. 'type',
  360. ' TObject = class',
  361. ' constructor Create;',
  362. ' end;',
  363. ' TBird = class',
  364. ' class constructor Init;',
  365. ' end;',
  366. ''],[
  367. 'constructor TObject.Create; begin end;',
  368. 'class constructor TBird.Init; begin end;',
  369. '']);
  370. AddUnit('src/unit2.pp',[
  371. 'uses unit1;',
  372. 'procedure DoIt;',
  373. ''],[
  374. 'procedure DoIt;',
  375. 'begin',
  376. ' TBird.Create;',
  377. 'end;',
  378. '']);
  379. AddFile('test1.pas',[
  380. 'uses unit2;',
  381. 'begin',
  382. ' DoIt;',
  383. 'end.']);
  384. CheckPrecompile('test1.pas','src');
  385. end;
  386. procedure TTestCLI_Precompile.TestPCU_ClassInterface;
  387. begin
  388. AddUnit('src/system.pp',[
  389. '{$interfaces corba}',
  390. 'type',
  391. ' integer = longint;',
  392. ' IUnknown = interface',
  393. ' end;',
  394. 'procedure Writeln; varargs;'],
  395. ['procedure Writeln; begin end;']);
  396. AddUnit('src/unit1.pp',[
  397. 'type',
  398. ' IIntf = interface',
  399. ' function GetItems(Index: longint): longint;',
  400. ' procedure SetItems(Index: longint; Value: longint);',
  401. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  402. ' end;',
  403. ''],[
  404. '']);
  405. AddUnit('src/unit2.pp',[
  406. 'uses unit1;',
  407. 'type',
  408. ' IAlias = IIntf;',
  409. ' TObject = class end;',
  410. ' TBird = class(IIntf)',
  411. ' strict private',
  412. ' function IIntf.GetItems = FetchItems;',
  413. ' function FetchItems(Index: longint): longint; virtual; abstract;',
  414. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  415. ' end;',
  416. ''],[
  417. '']);
  418. AddUnit('src/unit3.pp',[
  419. 'uses unit2;',
  420. 'type',
  421. ' TEagle = class(TBird)',
  422. ' function FetchItems(Index: longint): longint; override;',
  423. ' procedure SetItems(Index: longint; Value: longint); override;',
  424. ' end;',
  425. ' TFlying = class(IAlias)',
  426. ' strict private',
  427. ' FEagle: TEagle;',
  428. ' property Eagle: TEagle read FEagle implements IAlias;',
  429. ' public',
  430. ' constructor Create;',
  431. ' end;',
  432. ''],[
  433. 'function TEagle.FetchItems(Index: longint): longint; begin end;',
  434. 'procedure TEagle.SetItems(Index: longint; Value: longint); begin end;',
  435. 'constructor TFlying.Create;',
  436. 'begin',
  437. ' FEagle:=nil;',
  438. 'end;',
  439. '']);
  440. AddFile('test1.pas',[
  441. 'uses unit2, unit3;',
  442. 'type IAlias2 = IAlias;',
  443. 'var',
  444. ' f: TFlying;',
  445. ' i: IAlias2;',
  446. 'begin',
  447. ' f:=TFlying.Create;',
  448. ' i:=f;',
  449. ' i[2]:=i[3];',
  450. 'end.']);
  451. CheckPrecompile('test1.pas','src');
  452. end;
  453. procedure TTestCLI_Precompile.TestPCU_Namespace;
  454. begin
  455. AddUnit('src/system.pp',[
  456. 'type integer = longint;',
  457. 'procedure Writeln; varargs;'],
  458. ['procedure Writeln; begin end;']);
  459. AddUnit('src/Web.Unit1.pp',[
  460. 'var i: integer;',
  461. ''],[
  462. '']);
  463. AddUnit('src/Unit2.pp',[
  464. 'uses WEB.uNit1;',
  465. 'procedure DoIt;',
  466. ''],[
  467. 'procedure DoIt;',
  468. 'begin',
  469. ' writeln(i);',
  470. 'end;',
  471. '']);
  472. AddFile('test1.pas',[
  473. 'uses unIT2;',
  474. 'begin',
  475. ' DoIt;',
  476. 'end.']);
  477. CheckPrecompile('test1.pas','src');
  478. AssertFileExists(UnitOutputDir+'/Unit2.'+PCUFormat.Ext);
  479. AssertFileExists(UnitOutputDir+'/Web.Unit1.'+PCUFormat.Ext);
  480. end;
  481. procedure TTestCLI_Precompile.TestPCU_CheckVersionMain;
  482. var
  483. aFile: TCLIFile;
  484. s, JSFilename, ExpectedSrc: string;
  485. begin
  486. AddUnit('src/system.pp',[
  487. 'type integer = longint;'],
  488. ['']);
  489. AddFile('test1.pas',[
  490. 'begin',
  491. 'end.']);
  492. CheckPrecompile('test1.pas','src',LinesToList(['-JoCheckVersion=Main','-Jm-','-Jc-']));
  493. JSFilename:=GetJSFilename('test1.js');
  494. aFile:=FindFile(JSFilename);
  495. AssertNotNull('File not found '+JSFilename,aFile);
  496. ExpectedSrc:=LinesToStr([
  497. UTF8BOM+'rtl.module("program",["system"],function () {',
  498. ' "use strict";',
  499. ' var $mod = this;',
  500. ' $mod.$main = function () {',
  501. ' rtl.checkVersion('+IntToStr((VersionMajor*100+VersionMinor)*100+VersionRelease)+');',
  502. ' };',
  503. '});']);
  504. if not CheckSrcDiff(ExpectedSrc,aFile.Source,s) then
  505. Fail('TTestCLI_Precompile.TestPCU_CheckVersionMain src diff: '+s);
  506. end;
  507. procedure TTestCLI_Precompile.TestPCU_CheckVersionMain2;
  508. var
  509. aFile: TCLIFile;
  510. s, JSFilename, ExpectedSrc: string;
  511. begin
  512. AddUnit('src/system.pp',[
  513. 'type integer = longint;',
  514. 'procedure Writeln; varargs;'],
  515. ['procedure Writeln; begin end;']);
  516. AddFile('test1.pas',[
  517. 'begin',
  518. ' Writeln;',
  519. 'end.']);
  520. CheckPrecompile('test1.pas','src',LinesToList(['-JoCheckVersion=Main','-Jm-','-Jc-']));
  521. JSFilename:=GetJSFilename('test1.js');
  522. aFile:=FindFile(JSFilename);
  523. AssertNotNull('File not found '+JSFilename,aFile);
  524. ExpectedSrc:=LinesToStr([
  525. UTF8BOM+'rtl.module("program",["system"],function () {',
  526. ' "use strict";',
  527. ' var $mod = this;',
  528. ' $mod.$main = function () {',
  529. ' rtl.checkVersion('+IntToStr((VersionMajor*100+VersionMinor)*100+VersionRelease)+');',
  530. ' pas.system.Writeln();',
  531. ' };',
  532. '});']);
  533. if not CheckSrcDiff(ExpectedSrc,aFile.Source,s) then
  534. Fail('TTestCLI_Precompile.TestPCU_CheckVersionMain src diff: '+s);
  535. end;
  536. procedure TTestCLI_Precompile.TestPCU_CheckVersionSystem;
  537. var
  538. aFile: TCLIFile;
  539. s, JSFilename, ExpectedSrc, VerStr: string;
  540. begin
  541. AddUnit('src/system.pp',[
  542. 'type integer = longint;'],
  543. ['']);
  544. AddFile('test1.pas',[
  545. 'begin',
  546. 'end.']);
  547. CheckPrecompile('test1.pas','src',LinesToList(['-JoCheckVersion=system','-Jm-','-Jc-']));
  548. JSFilename:=GetJSFilename('system.js');
  549. aFile:=FindFile(JSFilename);
  550. AssertNotNull('File not found '+JSFilename,aFile);
  551. writeln('TTestCLI_Precompile.TestPCU_CheckVersionMain ',aFile.Source);
  552. VerStr:=IntToStr((VersionMajor*100+VersionMinor)*100+VersionRelease);
  553. ExpectedSrc:=LinesToStr([
  554. UTF8BOM+'rtl.module("system",[],function () {',
  555. ' "use strict";',
  556. ' rtl.checkVersion('+VerStr+');',
  557. ' var $mod = this;',
  558. '});']);
  559. if not CheckSrcDiff(ExpectedSrc,aFile.Source,s) then
  560. Fail('TTestCLI_Precompile.TestPCU_CheckVersionMain src diff: '+s);
  561. end;
  562. Initialization
  563. RegisterTests([TTestCLI_Precompile]);
  564. end.