tcreportgenerator.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2022 by Michael van Canneyt and other members of the
  4. Free Pascal development team
  5. test report generator
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit tcreportgenerator;
  13. {$mode objfpc}{$H+}
  14. interface
  15. uses
  16. Classes,
  17. SysUtils,
  18. fpcunit,
  19. testregistry,
  20. fpreport,
  21. udapp,
  22. fpTTF,
  23. fpjson,
  24. {demos}
  25. rptsimplelist,
  26. rptexpressions,
  27. rptgrouping,
  28. rptgrouping2,
  29. rptframes,
  30. rptimages,
  31. rptttf,
  32. rptshapes,
  33. rptdataset,
  34. rptcolumns,
  35. rptmasterdetail,
  36. rptjson,
  37. rptcontnr,
  38. rptnestedgroups,
  39. rptBarcode,
  40. rptQRcode;
  41. type
  42. { TTestDemos }
  43. TTestDemos = class(TTestCase)
  44. private
  45. FFilePath: String;
  46. procedure SaveJSON(pFileName: String; pJSON: TJSONData);
  47. protected
  48. procedure SetUp; override;
  49. procedure TearDown; override;
  50. procedure TestDemo(pName: String; pDemoAppClass: TReportDemoAppClass);
  51. published
  52. procedure SimpleList;
  53. procedure ExpressionDemo;
  54. procedure GroupingDemo;
  55. procedure Grouping2Demo;
  56. procedure FramesDemo;
  57. procedure ImagesDemo;
  58. procedure TTFDemo;
  59. procedure ShapesDemo;
  60. procedure DatasetDemo;
  61. procedure ColumnsDemo;
  62. procedure MasterDetailDemo;
  63. procedure JSONDemo;
  64. procedure CollectionDemo;
  65. procedure ObjectListDemo;
  66. procedure TestNestedGroupDemo;
  67. procedure BarcodeDemo;
  68. procedure QRCodeDemo;
  69. end;
  70. implementation
  71. uses
  72. fpjsonreport,
  73. jsonscanner,
  74. jsonparser;
  75. { TTestDemos }
  76. procedure TTestDemos.SaveJSON(pFileName: String; pJSON: TJSONData);
  77. var
  78. S: TFileStream;
  79. J: TJSONStringType;
  80. begin
  81. S:=TFileStream.Create(pFileName,fmCreate);
  82. try
  83. J:=pJSON.FormatJSON;
  84. S.WriteBuffer(J[1],Length(J));
  85. finally
  86. S.Free;
  87. end;
  88. end;
  89. procedure TTestDemos.SetUp;
  90. begin
  91. inherited SetUp;
  92. FFilePath:=ExtractFilePath(ParamStr(0));
  93. if not ForceDirectories(FFilePath+'rendered') then
  94. Fail('Could not create directory for rendered JSON');
  95. gTTFontCache.Clear;
  96. gTTFontCache.SearchPath.Clear;
  97. gTTFontCache.SearchPath.Add(FFilePath+'fonts/');
  98. gTTFontCache.SearchPath.Add(FFilePath+'../demos/fonts/');
  99. {$IFDEF UNIX}
  100. gTTFontCache.SearchPath.Add(GetUserDir + '.fonts/');
  101. gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu-font-family/');
  102. gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu/');
  103. gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/dejavu/');
  104. {$ENDIF}
  105. // ask to generate the font cache
  106. gTTFontCache.BuildFontCache;
  107. end;
  108. procedure TTestDemos.TearDown;
  109. begin
  110. inherited TearDown;
  111. end;
  112. procedure TTestDemos.TestDemo(pName: String; pDemoAppClass: TReportDemoAppClass);
  113. var
  114. lApp: TReportDemoApp;
  115. lSetJSON: TJSONData;
  116. lActualJSON: TJSONObject;
  117. S: TFileStream;
  118. P: TJSONParser;
  119. J: TJSONStringType;
  120. lEqual: Boolean;
  121. lSetFile, lActualFile: String;
  122. begin
  123. lSetFile:=FFilePath+'rendered'+PathDelim+pName+'.set.json';
  124. lActualFile:=FFilePath+'rendered'+PathDelim+pName+'.actual.json';
  125. lApp:=pDemoAppClass.Create(Nil);
  126. lActualJSON := TJSONObject.Create;
  127. try
  128. // delete old actual
  129. DeleteFile(lActualFile);
  130. // create Report
  131. lApp.TestInit;
  132. // run first time
  133. lApp.rpt.RunReport;
  134. lApp.rpt.SaveRenderToJSON(lActualJSON);
  135. // delete DateCreated
  136. lActualJSON.GetPath('Report.DateCreated').AsString := '';
  137. //SaveJSON(lSetFile, lActualJSON); // uncomment for regeneration after changes
  138. if Not FileExists(lSetFile) then
  139. begin
  140. SaveJSON(lSetFile, lActualJSON);
  141. Ignore('No previous test result available, saved result for reference');
  142. end;
  143. // load set report
  144. S:=TFileStream.Create(lSetFile,fmOpenRead);
  145. try
  146. P:=TJSONParser.Create(S, []);
  147. try
  148. lSetJSON:=TJSONObject(P.Parse);
  149. // compare reports
  150. lEqual := lSetJSON.AsJSON = lActualJSON.AsJSON;
  151. if not lEqual then
  152. SaveJSON(lActualFile, lActualJSON);
  153. AssertTrue('equal renders', lEqual);
  154. // run a second time
  155. lApp.rpt.RunReport;
  156. lActualJSON.Clear;
  157. lApp.rpt.SaveRenderToJSON(lActualJSON);
  158. // delete DateCreated
  159. lActualJSON.GetPath('Report.DateCreated').AsString := '';
  160. // compare reports
  161. lEqual := lSetJSON.AsJSON = lActualJSON.AsJSON;
  162. if not lEqual then
  163. SaveJSON(lActualFile, lActualJSON);
  164. AssertTrue('equal second renders', lEqual);
  165. finally
  166. lSetJSON.Free;
  167. P.Free;
  168. end;
  169. finally
  170. S.Free;
  171. end;
  172. finally
  173. lActualJSON.Free;
  174. lApp.Free;
  175. end;
  176. end;
  177. procedure TTestDemos.SimpleList;
  178. begin
  179. TestDemo('simplelist', TSimpleListDemo);
  180. end;
  181. procedure TTestDemos.ExpressionDemo;
  182. begin
  183. TestDemo('expression', TExpressionsDemo);
  184. end;
  185. procedure TTestDemos.GroupingDemo;
  186. begin
  187. TestDemo('grouping', TGroupingDemo);
  188. end;
  189. procedure TTestDemos.Grouping2Demo;
  190. begin
  191. TestDemo('grouping2', TGrouping2Demo);
  192. end;
  193. procedure TTestDemos.FramesDemo;
  194. begin
  195. TestDemo('frames', TFramesDemo);
  196. end;
  197. procedure TTestDemos.ImagesDemo;
  198. var
  199. cd: String;
  200. begin
  201. cd := GetCurrentDir;
  202. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  203. try
  204. TestDemo('images', TImagesDemo);
  205. finally
  206. SetCurrentDir(cd);
  207. end;
  208. end;
  209. procedure TTestDemos.TTFDemo;
  210. var
  211. cd: String;
  212. begin
  213. cd := GetCurrentDir;
  214. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  215. try
  216. TestDemo('ttf', TTTFDemo);
  217. finally
  218. SetCurrentDir(cd);
  219. end;
  220. end;
  221. procedure TTestDemos.ShapesDemo;
  222. begin
  223. TestDemo('shapes', TShapesDemo);
  224. end;
  225. procedure TTestDemos.DatasetDemo;
  226. var
  227. cd: String;
  228. begin
  229. cd := GetCurrentDir;
  230. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  231. try
  232. TestDemo('dataset', TDatasetDemo);
  233. finally
  234. SetCurrentDir(cd);
  235. end;
  236. end;
  237. procedure TTestDemos.ColumnsDemo;
  238. begin
  239. TestDemo('columns', TColumnsDemo)
  240. end;
  241. procedure TTestDemos.MasterDetailDemo;
  242. begin
  243. TestDemo('masterdetail', TMasterDetailDemo);
  244. end;
  245. procedure TTestDemos.JSONDemo;
  246. var
  247. cd: String;
  248. begin
  249. cd := GetCurrentDir;
  250. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  251. try
  252. TestDemo('json', TJSONDemo);
  253. finally
  254. SetCurrentDir(cd);
  255. end;
  256. end;
  257. procedure TTestDemos.CollectionDemo;
  258. begin
  259. TestDemo('collection', TCollectionDemo);
  260. end;
  261. procedure TTestDemos.ObjectListDemo;
  262. begin
  263. TestDemo('objectlist', TObjectListDemo);
  264. end;
  265. procedure TTestDemos.BarcodeDemo;
  266. begin
  267. TestDemo('barcode', TBarcodeDemo);
  268. end;
  269. procedure TTestDemos.QRCodeDemo;
  270. begin
  271. TestDemo('qrcode', TQRCodeDemo);
  272. end;
  273. procedure TTestDemos.TestNestedGroupDemo;
  274. begin
  275. TestDemo('nestedgroups', TNestedGroupsDemo);
  276. end;
  277. initialization
  278. RegisterTests(
  279. [TTestDemos
  280. ]);
  281. end.