tcprecompile.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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,
  21. tcunitsearch, tcmodules;
  22. type
  23. { TCustomTestCLI_Precompile }
  24. TCustomTestCLI_Precompile = class(TCustomTestCLI)
  25. private
  26. FFormat: TPas2JSPrecompileFormat;
  27. protected
  28. procedure CheckPrecompile(MainFile, UnitPaths: string;
  29. SharedParams: TStringList = nil;
  30. FirstRunParams: TStringList = nil;
  31. SecondRunParams: TStringList = nil);
  32. public
  33. constructor Create; override;
  34. property Format: TPas2JSPrecompileFormat read FFormat write FFormat;
  35. end;
  36. { TTestCLI_Precompile }
  37. TTestCLI_Precompile = class(TCustomTestCLI_Precompile)
  38. published
  39. procedure TestPCU_EmptyUnit;
  40. procedure TestPCU_UTF8BOM;
  41. procedure TestPCU_ParamNS;
  42. procedure TestPCU_Overloads;
  43. procedure TestPCU_UnitCycle;
  44. end;
  45. function LinesToList(const Lines: array of string): TStringList;
  46. implementation
  47. function LinesToList(const Lines: array of string): TStringList;
  48. var
  49. i: Integer;
  50. begin
  51. Result:=TStringList.Create;
  52. for i:=Low(Lines) to High(Lines) do Result.Add(Lines[i]);
  53. end;
  54. { TCustomTestCLI_Precompile }
  55. procedure TCustomTestCLI_Precompile.CheckPrecompile(MainFile, UnitPaths: string;
  56. SharedParams: TStringList; FirstRunParams: TStringList;
  57. SecondRunParams: TStringList);
  58. var
  59. UnitOutputDir, JSFilename, OrigSrc, NewSrc, s: String;
  60. JSFile: TCLIFile;
  61. begin
  62. try
  63. UnitOutputDir:='units';
  64. AddDir(UnitOutputDir);
  65. // compile, create .pcu files
  66. {$IFDEF VerbosePCUFiler}
  67. writeln('TTestCLI_Precompile.CheckPrecompile create pcu files=========================');
  68. {$ENDIF}
  69. Params.Clear;
  70. if SharedParams<>nil then
  71. Params.Assign(SharedParams);
  72. if FirstRunParams<>nil then
  73. Params.AddStrings(FirstRunParams);
  74. Compile([MainFile,'-Jc','-Fu'+UnitPaths,'-JU'+Format.Ext,'-FU'+UnitOutputDir]);
  75. AssertFileExists('units/system.'+Format.Ext);
  76. JSFilename:=UnitOutputDir+PathDelim+ExtractFilenameOnly(MainFile)+'.js';
  77. AssertFileExists(JSFilename);
  78. JSFile:=FindFile(JSFilename);
  79. OrigSrc:=JSFile.Source;
  80. // compile, using .pcu files
  81. {$IFDEF VerbosePCUFiler}
  82. writeln('TTestCLI_Precompile.CheckPrecompile compile using pcu files==================');
  83. {$ENDIF}
  84. JSFile.Source:='';
  85. Compiler.Reset;
  86. Params.Clear;
  87. if SharedParams<>nil then
  88. Params.Assign(SharedParams);
  89. if SecondRunParams<>nil then
  90. Params.AddStrings(SecondRunParams);
  91. Compile([MainFile,'-Jc','-FU'+UnitOutputDir]);
  92. NewSrc:=JSFile.Source;
  93. if not CheckSrcDiff(OrigSrc,NewSrc,s) then
  94. begin
  95. WriteSources;
  96. Fail('test1.js: '+s);
  97. end;
  98. finally
  99. SharedParams.Free;
  100. FirstRunParams.Free;
  101. SecondRunParams.Free;
  102. end;
  103. end;
  104. constructor TCustomTestCLI_Precompile.Create;
  105. begin
  106. inherited Create;
  107. FFormat:=PrecompileFormats.FindExt('pcu');
  108. end;
  109. { TTestCLI_Precompile }
  110. procedure TTestCLI_Precompile.TestPCU_EmptyUnit;
  111. begin
  112. AddUnit('src/system.pp',[''],['']);
  113. AddFile('test1.pas',[
  114. 'begin',
  115. 'end.']);
  116. CheckPrecompile('test1.pas','src');
  117. end;
  118. procedure TTestCLI_Precompile.TestPCU_UTF8BOM;
  119. var
  120. aFile: TCLIFile;
  121. begin
  122. aFile:=AddUnit('src/system.pp',
  123. ['var',
  124. ' s: string = ''aaaäö'';',
  125. ' s2: string = ''😊'';', // 1F60A
  126. ''],
  127. ['']);
  128. aFile.Source:=UTF8BOM+aFile.Source;
  129. aFile:=AddFile('test1.pas',[
  130. 'begin',
  131. ' s:=''ö😊'';',
  132. 'end.']);
  133. aFile.Source:=UTF8BOM+aFile.Source;
  134. CheckPrecompile('test1.pas','src');
  135. end;
  136. procedure TTestCLI_Precompile.TestPCU_ParamNS;
  137. begin
  138. AddUnit('src/system.pp',[''],['']);
  139. AddUnit('src/foo.unit1.pp',['var i: longint;'],['']);
  140. AddFile('test1.pas',[
  141. 'uses unit1;',
  142. 'begin',
  143. ' i:=3;',
  144. 'end.']);
  145. CheckPrecompile('test1.pas','src',LinesToList(['-NSfoo']));
  146. end;
  147. procedure TTestCLI_Precompile.TestPCU_Overloads;
  148. begin
  149. AddUnit('src/system.pp',['type integer = longint;'],['']);
  150. AddUnit('src/unit1.pp',
  151. ['var i: integer;',
  152. 'procedure DoIt(j: integer); overload;',
  153. 'procedure DoIt(b: boolean);'],
  154. ['procedure DoIt(j: integer);',
  155. 'begin',
  156. ' i:=j;',
  157. 'end;',
  158. 'procedure DoIt(b: boolean);',
  159. 'begin',
  160. ' i:=3;',
  161. 'end;']);
  162. AddUnit('src/unit2.pp',
  163. ['uses unit1;',
  164. 'procedure DoIt(s: string); overload;'],
  165. ['procedure DoIt(s: string);',
  166. 'begin',
  167. ' unit1.i:=length(s);',
  168. 'end;']);
  169. AddFile('test1.pas',[
  170. 'uses unit1, unit2;',
  171. 'procedure DoIt(d: double); overload;',
  172. 'begin',
  173. ' unit1.i:=4;',
  174. 'end;',
  175. 'begin',
  176. ' DoIt(3);',
  177. ' DoIt(''abc'');',
  178. ' DoIt(true);',
  179. ' DoIt(3.3);',
  180. 'end.']);
  181. CheckPrecompile('test1.pas','src');
  182. end;
  183. procedure TTestCLI_Precompile.TestPCU_UnitCycle;
  184. begin
  185. AddUnit('src/system.pp',['type integer = longint;'],['']);
  186. AddUnit('src/unit1.pp',
  187. ['var i: integer;',
  188. 'procedure Do1(j: integer);'],
  189. ['uses unit2;',
  190. 'procedure Do1(j: integer);',
  191. 'begin',
  192. ' Do2(j);',
  193. 'end;']);
  194. AddUnit('src/unit2.pp',
  195. ['uses unit1;',
  196. 'procedure Do2(j: integer);'],
  197. ['procedure Do2(j: integer);',
  198. 'begin',
  199. ' unit1.i:=j;',
  200. 'end;']);
  201. AddFile('test1.pas',[
  202. 'uses unit1;',
  203. 'begin',
  204. ' Do1(3);',
  205. 'end.']);
  206. CheckPrecompile('test1.pas','src');
  207. end;
  208. Initialization
  209. RegisterTests([TTestCLI_Precompile]);
  210. end.