tcprecompile.pas 15 KB

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